Skip to content

fix(mcp): rate-limit requests before token authentication - #782

Merged
unjica merged 1 commit into
developfrom
fix/mcp-pre-auth-rate-limit
Oct 12, 2026
Merged

unjica merged 1 commit into
developfrom
fix/mcp-pre-auth-rate-limit

Conversation

@unjica

@unjica unjica commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Unauthenticated /mcp requests previously bypassed all Fastify rate-limit scopes, allowing invalid read tokens to trigger unlimited database lookups. Wrap MCP routes in an encapsulated @fastify/rate-limit scope, following the existing app pattern, to address the CodeQL js/missing-rate-limiting finding.

  • Add RATE_LIMIT_MAX_MCP: 300 requests/minute by default, 100,000 in tests unless explicitly overridden.
  • Preserve existing Postgres per-token/per-org limits, Fastify proxy trust, and all other IP buckets.
  • Document that the pre-auth bucket uses the connecting proxy address in production and is a coarse abuse guard. Per-client IP limiting remains a separate follow-up pending proxy-chain verification.
  • Add tests for missing/invalid credentials reaching 429 before further token lookups, scope isolation, and disabled MCP returning 404. Update deployment docs, env example, and Unreleased changelog.

Validation:

  • pnpm lint: passed (zero warnings).
  • pnpm --filter api build: passed.
  • Local disposable Postgres 16, migrations deployed, DATABASE_URL=postgresql://postgres:pg@localhost:55432/ci RUN_DB_INTEGRATION_TESTS=true pnpm --filter api test: 157 files passed, 1 skipped; 1,261 tests passed, 8 skipped. All 35 MCP tests passed, including 3 new tests and 17 real-database integration tests.
  • pnpm --filter api exec tsc --noEmit: 106 existing diagnostics; verified identical against unchanged HEAD.
  • pnpm --filter dashboard exec tsc --noEmit: 6 existing diagnostics; verified identical against unchanged source with incremental caching disabled.
  • git diff --check: passed. CodeQL must be confirmed by PR CI.

No merge, release, deployment, production database access, or proxy-trust changes.

Register MCP routes in an encapsulated Fastify rate-limit scope with
RATE_LIMIT_MAX_MCP (300 requests/minute by default). Preserve token/org
limits and proxy trust, and document the coarse connecting-proxy bucket.

Cover missing and invalid credentials, blocked token lookups, scope
isolation, and disabled MCP behavior.
@unjica
unjica merged commit d0267d7 into develop Oct 12, 2026
7 checks passed
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