Skip to content

feat(gateway): add auth rate-limiting & brute-force protection - #15035

Merged
steipete merged 2 commits into
openclaw:mainfrom
buerbaumer:feat/gateway-auth-rate-limit
Feb 13, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
buerbaumer:feat/gateway-auth-rate-limit

Conversation

@buerbaumer

@buerbaumer buerbaumer commented Feb 12, 2026 •

Copy link
Copy Markdown
Contributor

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.bind is set to lan, tailnet, or auto, the auth endpoints are network-reachable. Without rate limiting, an attacker
can attempt unlimited password/token guesses. This PR closes that gap.

What's included

Description File
Core rate limiter (sliding window, per-IP, configurable lockout) src/gateway/auth-rate-limit.ts
15 unit tests (window, lockout, isolation, loopback exemption, prune, dispose) src/gateway/auth-rate-limit.test.ts
GatewayAuthRateLimitConfig type src/config/types.gateway.ts
rateLimited / retryAfterMs fields + check/record/reset in auth flow src/gateway/auth.ts
sendRateLimited() — HTTP 429 + Retry-After helper src/gateway/http-common.ts
Thread rateLimiter through HTTP server, canvas auth, upgrade handler src/gateway/server-http.ts
Thread rateLimiter + 429 response for /v1/chat/completions src/gateway/openai-http.ts
Thread rateLimiter + 429 response for /v1/responses src/gateway/openresponses-http.ts
Thread rateLimiter + 429 response for /tools/invoke src/gateway/tools-invoke-http.ts
Thread rateLimiter through runtime state src/gateway/server-runtime-state.ts
Thread rateLimiter through WS runtime src/gateway/server-ws-runtime.ts
Thread rateLimiter through WS connection src/gateway/server/ws-connection.ts
Pass to primary auth only (avoid double-count); use proxy-aware clientIp src/gateway/server/ws-connection/message-handler.ts
Create, wire, and dispose authRateLimiter src/gateway/server.impl.ts
gateway.auth_no_rate_limit security audit finding src/security/audit.ts

Design decisions

  • Sliding window — fairer than fixed-window; resets naturally as old attempts expire.
  • Loopback exempt — 127.0.0.1 / ::1 skip the check (local dev shouldn't be blocked).
  • Optional & backward-compatible — everything is undefined-safe; zero behaviour change when rateLimit is not configured.
  • Single count per WS handshake — the secondary sharedAuth call in message-handler.ts intentionally omits the limiter to avoid double-counting a single connection attempt.
  • Proxy-aware IP keying — WS rate-limit uses resolveGatewayClientIp() (respects X-Forwarded-For / X-Real-IP with trusted-proxy validation) instead of raw remoteAddr, so users behind a reverse proxy are tracked individually.
  • All auth-protected HTTP endpoints covered — /v1/chat/completions, /v1/responses, /tools/invoke, /api/channels/*, and canvas auth all thread the rate limiter and return proper 429 + Retry-After responses.
  • Dispose on shutdown — the prune timer is unref()'d and explicitly cleared in the server close handler.

Configuration example

gateway:
  auth:
    rateLimit:
      maxAttempts: 10
      windowMs: 60000
      lockoutMs: 300000

Test results

✓ src/gateway/auth-rate-limit.test.ts (15 tests) — all pass
✓ src/gateway/auth.test.ts (7 tests) — all pass
✓ All 285 gateway tests (46 files) — all pass
✓ TypeScript: zero errors on all 15 changed files

Checklist

  • New code follows existing patterns (pure functions, explicit DI, .js import extensions)
  • All parameters are optional / backward-compatible
  • Unit tests cover happy path, edge cases, and cleanup
  • No new dependencies added
  • Existing tests unaffected (285/285 pass)
  • npx tsc --noEmit clean on all touched files
  • All auth-protected HTTP endpoints thread the rate limiter
  • WS rate-limit keying uses proxy-aware client IP resolution

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L labels Feb 12, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

12 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment thread src/gateway/server.impl.ts Outdated
Comment on lines +273 to +276
// Create auth rate limiter from config (if configured, or use defaults).
const rateLimitConfig = cfgAtStart.gateway?.auth?.rateLimit;
const authRateLimiter: AuthRateLimiter = createAuthRateLimiter(rateLimitConfig ?? undefined);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@greptile-apps

greptile-apps Bot commented Feb 12, 2026

Copy link
Copy Markdown
Contributor
Additional Comments (2)

src/gateway/server/ws-connection/message-handler.ts
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).

Prompt To Fix With AI
This 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.

src/gateway/openai-http.ts
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.

Prompt To Fix With AI
This 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.
@buerbaumer

Copy link
Copy Markdown
Contributor Author

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

@buerbaumer

buerbaumer commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @steipete,
I just saw you on Lex Fridman (https://www.youtube.com/watch?v=YFjfBk8HI5o&t=1212s) and got inspired to help to further "harden" the code. => This PR which protects against brute-force credential guessing.
Grüße aus Wien,
Harald

Verified by Opus 4.6:

@steipete steipete self-assigned this Feb 13, 2026
@steipete

Copy link
Copy Markdown
Contributor

Danke Harald!

Yeah the concept for the "debug web UI" was for localhost only, was never intended to be on a shared network.

@steipete
steipete merged commit 30b6ecc into openclaw:main Feb 13, 2026
3 checks passed
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: XL and removed size: L labels Feb 13, 2026
skyhawk14 pushed a commit to skyhawk14/openclaw that referenced this pull request Feb 13, 2026
…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]>
@buerbaumer
buerbaumer deleted the feat/gateway-auth-rate-limit branch February 13, 2026 18:56
GwonHyeok pushed a commit to learners-superpumped/openclaw that referenced this pull request Feb 15, 2026
…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]>
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…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]>
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…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]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…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]>
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants