mirror of
https://github.com/Such-Software/smirk-backend-core.git
synced 2026-10-17 15:31:38 +02:00
Add tiered per-IP rate limiting to the router, completing the assembly: - STRICT tier (~2 req/s, burst 20) on the unauthenticated, abuse-prone surface: /api/v1/auth/* and /api/v1/website/*. This closes the website-challenge growth vector (the prior rate-limit LOW): an unauthenticated flood of challenge requests is now throttled per IP. - NORMAL tier (~10 req/s, burst 60) on the authenticated wallet/identity API. - PeerIpKeyExtractor keys on the peer IP; `main` now serves with into_make_service_with_connect_info::<SocketAddr> so the IP is available, and the test harness injects a loopback ConnectInfo (each test builds a fresh router with fresh limiter state). Over-limit returns 429. The limiter sits inside the global layers (CORS/timeout/ body/compression/trace) and in front of the handlers. L1 test proves it throttles: a 40-request burst to a strict-tier route returns 429 (the governor runs before the handler, so even unauthenticated requests count). All 33 integration tests + 114 lib tests pass through the fully-layered router; openapi unchanged.
28 lines
873 B
Rust
28 lines
873 B
Rust
//! L1 integration: the per-IP rate limiter actually throttles a burst.
|
|
//!
|
|
//! The strict tier wraps the unauthenticated auth/website surface. The governor
|
|
//! runs before the handler, so an unauthenticated burst to a strict-tier route
|
|
//! still counts — past the burst size it must return 429. (All requests share the
|
|
//! injected loopback peer, so they hit one bucket.)
|
|
|
|
mod common;
|
|
|
|
use axum::http::StatusCode;
|
|
|
|
#[tokio::test]
|
|
async fn strict_tier_throttles_a_burst() {
|
|
let app = require_app!();
|
|
let mut saw_429 = false;
|
|
for _ in 0..40 {
|
|
let (status, _) = app.request("GET", "/api/v1/auth/me", None, None).await;
|
|
if status == StatusCode::TOO_MANY_REQUESTS {
|
|
saw_429 = true;
|
|
break;
|
|
}
|
|
}
|
|
assert!(
|
|
saw_429,
|
|
"a rapid burst to a strict-tier endpoint should be rate-limited (429)"
|
|
);
|
|
}
|