Repository navigation
feat(gateway): add auth rate-limiting & brute-force protection - #15035
Conversation
| // Create auth rate limiter from config (if configured, or use defaults). | ||
| const rateLimitConfig = cfgAtStart.gateway?.auth?.rateLimit; | ||
| const authRateLimiter: AuthRateLimiter = createAuthRateLimiter(rateLimitConfig ?? undefined); | ||
|
|
There was a problem hiding this comment.
Rate limiter always enabled
startGatewayServer always constructs an AuthRateLimiter using default limits even when gateway.auth.rateLimit is unset, so rate limiting is effectively always on (except loopback exemption). This is a behavioral change and also conflicts with the new audit finding that warns when no rate limit is configured (since the runtime still rate-limits anyway). Consider making the limiter undefined unless explicitly configured, or updating the audit/PR claims to match the always-enabled behavior.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/gateway/server.impl.ts
Line: 273:276
Comment:
**Rate limiter always enabled**
`startGatewayServer` always constructs an `AuthRateLimiter` using default limits even when `gateway.auth.rateLimit` is unset, so rate limiting is effectively *always on* (except loopback exemption). This is a behavioral change and also conflicts with the new audit finding that warns when no rate limit is configured (since the runtime still rate-limits anyway). Consider making the limiter undefined unless explicitly configured, or updating the audit/PR claims to match the always-enabled behavior.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Fixed in 3fe7b87. The limiter is now only created when explicitly configured:
Also addressed the other two findings from the summary:
WS rate-limit keying now uses resolveGatewayClientIp() for proxy-aware IP resolution
All auth-protected HTTP endpoints (/v1/chat/completions, /v1/responses, /tools/invoke) now thread the rate limiter with proper 429 + Retry-After responses
Additional Comments (2)
The WS handshake auth passes Also appears in: src/gateway/server/ws-connection/message-handler.ts:200-201 (resolved client IP computed but not used). Prompt To Fix With AIThis is a comment left during a code review.
Path: src/gateway/server/ws-connection/message-handler.ts
Line: 412:419
Comment:
**WS rate-limit uses proxy IP**
The WS handshake auth passes `clientIp: remoteAddr` into `authorizeGatewayConnect`, so rate limiting is keyed on the TCP peer address rather than the resolved client IP. When the gateway is behind a trusted reverse proxy, `remoteAddr` will be the proxy IP, causing all end users to share a single bucket and potentially lock each other out.
Also appears in: src/gateway/server/ws-connection/message-handler.ts:200-201 (resolved client IP computed but not used).
How can I resolve this? If you propose a fix, please make it concise.
Also appears in: src/gateway/openresponses-http.ts:345-355, src/gateway/tools-invoke-http.ts:117-128. Prompt To Fix With AIThis is a comment left during a code review.
Path: src/gateway/openai-http.ts
Line: 186:195
Comment:
**HTTP endpoints bypass limiter**
`handleOpenAiHttpRequest` calls `authorizeGatewayConnect` without passing the new `rateLimiter`, so brute-force attempts against `/v1/chat/completions` are still unlimited even when rate limiting is configured/enabled elsewhere. If the intent is "gateway authentication endpoints" broadly, these handlers need to thread the limiter through the same way `server-http.ts` does for `/api/channels/*`.
Also appears in: src/gateway/openresponses-http.ts:345-355, src/gateway/tools-invoke-http.ts:117-128.
How can I resolve this? If you propose a fix, please make it concise. |
Add a per-IP sliding-window rate limiter to Gateway authentication endpoints (HTTP, WebSocket upgrade, and WS message-level auth). When gateway.auth.rateLimit is configured, failed auth attempts are tracked per client IP. Once the threshold is exceeded within the sliding window, further attempts are blocked with HTTP 429 + Retry-After until the lockout period expires. Loopback addresses are exempt by default so local CLI sessions are never locked out. The limiter is only created when explicitly configured (undefined otherwise), keeping the feature fully opt-in and backward-compatible.
|
Fixed in 3fe7b87. The limiter is now only created when explicitly configured: Also addressed the other two findings from the summary: WS rate-limit keying now uses resolveGatewayClientIp() for proxy-aware IP resolution |
|
Hi @steipete, Verified by Opus 4.6: |
|
Danke Harald! Yeah the concept for the "debug web UI" was for localhost only, was never intended to be on a shared network. |
…law#15035) * feat(gateway): add auth rate-limiting & brute-force protection Add a per-IP sliding-window rate limiter to Gateway authentication endpoints (HTTP, WebSocket upgrade, and WS message-level auth). When gateway.auth.rateLimit is configured, failed auth attempts are tracked per client IP. Once the threshold is exceeded within the sliding window, further attempts are blocked with HTTP 429 + Retry-After until the lockout period expires. Loopback addresses are exempt by default so local CLI sessions are never locked out. The limiter is only created when explicitly configured (undefined otherwise), keeping the feature fully opt-in and backward-compatible. * fix(gateway): isolate auth rate-limit scopes and normalize 429 responses --------- Co-authored-by: buerbaumer <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
…law#15035) * feat(gateway): add auth rate-limiting & brute-force protection Add a per-IP sliding-window rate limiter to Gateway authentication endpoints (HTTP, WebSocket upgrade, and WS message-level auth). When gateway.auth.rateLimit is configured, failed auth attempts are tracked per client IP. Once the threshold is exceeded within the sliding window, further attempts are blocked with HTTP 429 + Retry-After until the lockout period expires. Loopback addresses are exempt by default so local CLI sessions are never locked out. The limiter is only created when explicitly configured (undefined otherwise), keeping the feature fully opt-in and backward-compatible. * fix(gateway): isolate auth rate-limit scopes and normalize 429 responses --------- Co-authored-by: buerbaumer <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
…law#15035) * feat(gateway): add auth rate-limiting & brute-force protection Add a per-IP sliding-window rate limiter to Gateway authentication endpoints (HTTP, WebSocket upgrade, and WS message-level auth). When gateway.auth.rateLimit is configured, failed auth attempts are tracked per client IP. Once the threshold is exceeded within the sliding window, further attempts are blocked with HTTP 429 + Retry-After until the lockout period expires. Loopback addresses are exempt by default so local CLI sessions are never locked out. The limiter is only created when explicitly configured (undefined otherwise), keeping the feature fully opt-in and backward-compatible. * fix(gateway): isolate auth rate-limit scopes and normalize 429 responses --------- Co-authored-by: buerbaumer <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
…law#15035) * feat(gateway): add auth rate-limiting & brute-force protection Add a per-IP sliding-window rate limiter to Gateway authentication endpoints (HTTP, WebSocket upgrade, and WS message-level auth). When gateway.auth.rateLimit is configured, failed auth attempts are tracked per client IP. Once the threshold is exceeded within the sliding window, further attempts are blocked with HTTP 429 + Retry-After until the lockout period expires. Loopback addresses are exempt by default so local CLI sessions are never locked out. The limiter is only created when explicitly configured (undefined otherwise), keeping the feature fully opt-in and backward-compatible. * fix(gateway): isolate auth rate-limit scopes and normalize 429 responses --------- Co-authored-by: buerbaumer <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
…law#15035) * feat(gateway): add auth rate-limiting & brute-force protection Add a per-IP sliding-window rate limiter to Gateway authentication endpoints (HTTP, WebSocket upgrade, and WS message-level auth). When gateway.auth.rateLimit is configured, failed auth attempts are tracked per client IP. Once the threshold is exceeded within the sliding window, further attempts are blocked with HTTP 429 + Retry-After until the lockout period expires. Loopback addresses are exempt by default so local CLI sessions are never locked out. The limiter is only created when explicitly configured (undefined otherwise), keeping the feature fully opt-in and backward-compatible. * fix(gateway): isolate auth rate-limit scopes and normalize 429 responses --------- Co-authored-by: buerbaumer <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
…law#15035) * feat(gateway): add auth rate-limiting & brute-force protection Add a per-IP sliding-window rate limiter to Gateway authentication endpoints (HTTP, WebSocket upgrade, and WS message-level auth). When gateway.auth.rateLimit is configured, failed auth attempts are tracked per client IP. Once the threshold is exceeded within the sliding window, further attempts are blocked with HTTP 429 + Retry-After until the lockout period expires. Loopback addresses are exempt by default so local CLI sessions are never locked out. The limiter is only created when explicitly configured (undefined otherwise), keeping the feature fully opt-in and backward-compatible. * fix(gateway): isolate auth rate-limit scopes and normalize 429 responses --------- Co-authored-by: buerbaumer <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
Hallo Peter Steinberger, Dear code maintainer,
I just saw you on Lex Fridman (https://www.youtube.com/watch?v=YFjfBk8HI5o&t=1212s) and got inspired to help to IT-security improve the code. => This is the first PR which protects against brute-force credential guessing.
Verified by Opus 4.6:
Summary
Adds a per-IP sliding-window rate limiter to Gateway authentication endpoints
(HTTP, WebSocket upgrade, and WS message-level auth). Protects against brute-force
credential guessing when the Gateway is exposed beyond loopback.
Motivation
When
gateway.bindis set tolan,tailnet, orauto, the auth endpoints are network-reachable. Without rate limiting, an attackercan attempt unlimited password/token guesses. This PR closes that gap.
What's included
src/gateway/auth-rate-limit.tssrc/gateway/auth-rate-limit.test.tsGatewayAuthRateLimitConfigtypesrc/config/types.gateway.tsrateLimited/retryAfterMsfields + check/record/reset in auth flowsrc/gateway/auth.tssendRateLimited()— HTTP 429 +Retry-Afterhelpersrc/gateway/http-common.tssrc/gateway/server-http.ts/v1/chat/completionssrc/gateway/openai-http.ts/v1/responsessrc/gateway/openresponses-http.ts/tools/invokesrc/gateway/tools-invoke-http.tssrc/gateway/server-runtime-state.tssrc/gateway/server-ws-runtime.tssrc/gateway/server/ws-connection.tsclientIpsrc/gateway/server/ws-connection/message-handler.tsauthRateLimitersrc/gateway/server.impl.tsgateway.auth_no_rate_limitsecurity audit findingsrc/security/audit.tsDesign decisions
127.0.0.1/::1skip the check (local dev shouldn't be blocked).undefined-safe; zero behaviour change whenrateLimitis not configured.sharedAuthcall inmessage-handler.tsintentionally omits the limiter to avoid double-counting a single connection attempt.resolveGatewayClientIp()(respectsX-Forwarded-For/X-Real-IPwith trusted-proxy validation) instead of rawremoteAddr, so users behind a reverse proxy are tracked individually./v1/chat/completions,/v1/responses,/tools/invoke,/api/channels/*, and canvas auth all thread the rate limiter and return proper 429 +Retry-Afterresponses.unref()'d and explicitly cleared in the server close handler.Configuration example
Test results
Checklist
.jsimport extensions)npx tsc --noEmitclean on all touched files