Skip to content

Harden MCP rate limits, referral tracking, guidelines topic and embedding queue - #1731

Open
BenjaminMichaelis wants to merge 2 commits into
mainfrom
security/mcp-referral-hardening
Open

BenjaminMichaelis wants to merge 2 commits into
mainfrom
security/mcp-referral-hardening

Conversation

@BenjaminMichaelis

@BenjaminMichaelis BenjaminMichaelis commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes the MCP, referral and embedding findings from the recent security review. This PR is independent of the Identity PR.

Changes

MCP bearer tokens and site rate limits (Program.cs)

  • McpTokenNormalizationMiddleware now runs only for the MCP transport (POST /mcp). It used to run for every /mcp* path and method, so a bearer token acted as its user on the GET /mcp shim and on fallback paths like /mcp/anything. Those requests were charged to the owner's site-wide and content rate-limit buckets, which they share with the owner's browser session. Someone holding a leaked token could keep the owner rate-limited, including on the page used to revoke the token. Now those paths see the normal (cookie or anonymous) principal.

Referral tracking (ReferralTrackingMiddleware, ReferralService)

  • Endpoints that opt out of rate limiting (/health, /alive, /version, /.well-known/*) and non-GET requests no longer count referrals.
  • Every other GET page view with ?rid= still counts, including repeat views by the same visitor (the owner wants page-visit counts, so there is no per-visitor dedupe).
  • The self-referral check is now case-insensitive, matching the case-insensitive database lookup in production.

GetCSharpGuidelines (BookGuidelinesTool)

  • Topics over 500 characters are rejected, matching the sibling tools. The topic is matched word by word against every guideline and was echoed back unbounded.

Embedding queue (EmbeddingService)

  • GenerateEmbeddingAsync (interactive chat and MCP queries) no longer takes the process-wide pacing lock. The 250 ms MinInterRequestDelayMs pacing now applies only to batch rebuilds, so one caller's burst can't queue every other user's search behind it. Transient-failure retries still apply.

Tests

  • McpBearerRateLimitIsolationTests: after a bearer burst on /mcp or /mcp/anything, the owner's pages, content and DELETE /api/McpToken/{id} are not rate-limited.
  • ReferralTrackingMiddlewareTests: exempt endpoints don't count; repeat views by one visitor each count; different visitors each count.
  • BookGuidelinesToolTests: the 500-character topic cap.
  • EssentialCSharp.Chat.Tests/EmbeddingServiceTests: concurrent query embeddings run in parallel instead of queueing.

Against unmodified main, every new negative test fails (3 MCP, 4 referral, 1 guidelines, 1 embedding); the positive controls pass on both. Full runs locally: EssentialCSharp.Chat.Tests 42/42. EssentialCSharp.Web.Tests 359/361; the 2 failures come from local AI user-secrets and fail the same way on main.

🤖 Generated with Claude Code

BenjaminMichaelis and others added 2 commits November 26, 2026 13:13
…ue (security audit)

Fixes the non-Identity findings from the 2026-11 security audit:

- Normalize the MCP bearer principal only for the MCP transport
  (POST /mcp). Elsewhere a bearer token no longer acts as its user, so a
  leaked token can't spend the owner's site-wide or content rate-limit
  budgets (and lock them out of revoking it).
- Referral tracking skips endpoints that opt out of rate limiting
  (/health, /alive, /version, /.well-known) and non-GET requests, counts a
  visitor once per referral ID per 24 hours, and compares the self-referral
  check case-insensitively like the database lookup.
- GetCSharpGuidelines rejects topics over 500 characters, like the sibling
  tools.
- Interactive query embeddings no longer go through the process-wide
  pacing lock; the 250 ms pacing now applies only to batch rebuilds.

Adds regression tests derived from the audit's reproductions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The owner wants page-visit metrics, so drop the per-visitor 24-hour dedupe.
GET page views still count; endpoints that skip rate limiting still don't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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