Skip to content

fix(api-server): apply rate limiting before auth to prevent API key brute-force - #1

Open
joshfatoye0011-bit wants to merge 1 commit into
mainfrom
fix/rate-limit-before-auth
Open

fix(api-server): apply rate limiting before auth to prevent API key brute-force#1
joshfatoye0011-bit wants to merge 1 commit into
mainfrom
fix/rate-limit-before-auth

Conversation

@joshfatoye0011-bit

Copy link
Copy Markdown
Owner

Problem The

oute_layer order in main.rs placed �uth_middleware as the outermost layer, so it ran before
ate_limit_middleware. Requests with an invalid or missing API key were rejected by auth (401/403) before the rate limiter ever saw them — meaning an attacker could brute-force ROUTER_API_KEY with unlimited attempts per second, completely bypassing the rate limiter. ## Fix Swap the two .route_layer calls so
ate_limit_middleware is added last (outermost) and therefore executes first on every incoming request, regardless of the API key supplied. \
ust // Before (broken) .route_layer(from_fn_with_state(rate_limiter, rate_limit_middleware)) // inner — skipped on bad key .route_layer(from_fn_with_state(auth_config, auth::auth_middleware)); // outer — ran first // After (fixed) .route_layer(from_fn_with_state(auth_config, auth::auth_middleware)) // inner .route_layer(from_fn_with_state(rate_limiter, rate_limit_middleware)); // outer — runs first \\ The
eplay_protection_middleware on /simulate is unaffected — it is correctly innermost. ## Tests added - ** est_rate_limit_applies_to_invalid_api_key_requests** — sends repeated requests with a wrong key and asserts the response becomes 429 (with Retry-After header and error: rate_limit_exceeded body) once the quota is exhausted. - ** est_valid_key_still_works_after_invalid_key_is_rate_limited** — asserts that exhausting one key's rate-limit bucket does not affect a separate key's bucket. ## Acceptance criteria - [x] Rate limiting applies to requests before auth outcome - [x] Repeated invalid API keys eventually receive 429

…-force Previously the route_layer order placed auth_middleware as the outermost layer, meaning it ran before rate_limit_middleware. Unauthenticated/ invalid-key requests were rejected by auth (401/403) before the rate limiter ever saw them, allowing unlimited brute-force attempts against ROUTER_API_KEY. Fix: swap the two route_layer calls so rate_limit_middleware is added last (outermost) and therefore executes first on every request, regardless of the API key supplied. Add two tests: - test_rate_limit_applies_to_invalid_api_key_requests: asserts that repeated requests with a wrong key eventually receive 429 once the quota is exhausted. - test_valid_key_still_works_after_invalid_key_is_rate_limited: asserts that exhausting one key's bucket does not affect a separate key's bucket (rate limiter keys by x-api-key value).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant