Skip to content

fix(mcp): rate-limit MCP routes and harden the default profile - #1781

Merged
realcodesiman merged 1 commit into
mainfrom
fix/mcp-rate-limit-harden-default
Nov 25, 2026
Merged

realcodesiman merged 1 commit into
mainfrom
fix/mcp-rate-limit-harden-default

Conversation

@realcodesiman

Copy link
Copy Markdown
Contributor

Summary

  • Every MCP route (/sse, /messages, /claude, /openai) had no rate limit, so one credential or one attacker cycling bad tokens could flood it.
  • The default profile kept serving while token introspection was down and let any caller reuse another identity's session.

Changes

  • src/server/rate-limit.ts (new): a per-credential limit keyed by a hash of the token (CHATBOTX_MCP_RATE_LIMIT_PER_MINUTE, default 600). Requests without a token are not counted, so shared egress IPs (Claude, ChatGPT) never share one bucket. An optional per-IP limit on rejected tokens (CHATBOTX_MCP_AUTH_FAILURE_LIMIT_PER_MINUTE, off by default) takes the client IP from CHATBOTX_MCP_TRUSTED_PROXY_HOPS. Over the limit returns 429 with Retry-After.
  • src/server/sse-server.ts: when introspection is down, the default profile now fails closed with 503 + Retry-After. Each session is bound to the identity that opened it (OAuth user and client, or the workspace token hash), and any other identity gets 403. Tokens passed in the query string are logged as deprecated and can be turned off with CHATBOTX_MCP_ALLOW_QUERY_TOKEN=false.
  • src/token-introspection.ts / src/jwt-claims.ts: expired JWTs are rejected before introspection and do not count as failures. An unavailable-introspection result is now cached for 2s (was 30s), and the cache sweep is throttled.
  • src/oauth.ts: removed the unused unverifiedJwtAudiences helper.
  • src/env.ts, .env.example, README.md: added and documented the new env vars.

Test plan

  • Pre-commit typecheck (chatbotx-mcp)
  • pnpm lint
  • pnpm --filter chatbotx-mcp test (rate-limit, sse-server, token-introspection, openai-transport, oauth)
  • Manual: exceed the per-token limit and get 429 with Retry-After; stop the builder and get 503 on /sse; reuse a session id with another token and get 403

Rate limits on every MCP route (/sse, /messages, /claude, /openai):
- per credential (hashed), CHATBOTX_MCP_RATE_LIMIT_PER_MINUTE (600);
  requests without a token are not counted, so shared egress IPs
  (Claude, ChatGPT) never share one bucket
- rejected tokens per client IP, CHATBOTX_MCP_AUTH_FAILURE_LIMIT_PER_MINUTE,
  off by default; the client IP comes from CHATBOTX_MCP_TRUSTED_PROXY_HOPS
  (socket address by default) so a forged X-Forwarded-For cannot dodge it
  or lock out a shared egress IP
- 429 with Retry-After; expired JWTs are rejected without introspection
  and do not count as failures

Default profile (/sse, /messages):
- fail closed with 503 + Retry-After while introspection is unavailable;
  that result is now cached for 2s instead of 30s
- bind each session to the identity that opened it (OAuth user and
  client, or the workspace token hash); another identity gets 403
- query-string tokens are logged as deprecated and can be turned off
  with CHATBOTX_MCP_ALLOW_QUERY_TOKEN=false

Also throttles the introspection cache sweep and removes the now-unused
unverifiedJwtAudiences helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working as expected label Nov 25, 2026
@realcodesiman
realcodesiman merged commit 7d377e7 into main Nov 25, 2026
15 checks passed
@realcodesiman
realcodesiman deleted the fix/mcp-rate-limit-harden-default branch November 25, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant