Repository navigation
Harden MCP rate limits, referral tracking, guidelines topic and embedding queue - #1731
Open
BenjaminMichaelis wants to merge 2 commits into
Open
BenjaminMichaelis wants to merge 2 commits into
BenjaminMichaelis wants to merge 2 commits into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)McpTokenNormalizationMiddlewarenow 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 theGET /mcpshim 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)/health,/alive,/version,/.well-known/*) and non-GET requests no longer count referrals.?rid=still counts, including repeat views by the same visitor (the owner wants page-visit counts, so there is no per-visitor dedupe).GetCSharpGuidelines(BookGuidelinesTool)Embedding queue (
EmbeddingService)GenerateEmbeddingAsync(interactive chat and MCP queries) no longer takes the process-wide pacing lock. The 250 msMinInterRequestDelayMspacing 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/mcpor/mcp/anything, the owner's pages, content andDELETE /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.Tests42/42.EssentialCSharp.Web.Tests359/361; the 2 failures come from local AI user-secrets and fail the same way onmain.🤖 Generated with Claude Code