fix(api-server): apply rate limiting before auth to prevent API key brute-force - #1
Open
joshfatoye0011-bit wants to merge 1 commit into
Open
fix(api-server): apply rate limiting before auth to prevent API key brute-force#1joshfatoye0011-bit wants to merge 1 commit into
joshfatoye0011-bit wants to merge 1 commit into
Conversation
…-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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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