Skip to content

fix(mcp): keep identifiers split by the intent scan cut out of events - #452

Open
lucasheriques wants to merge 1 commit into
mainfrom
lucas/mcp-intent-cut-leak
Open

lucasheriques wants to merge 1 commit into
mainfrom
lucas/mcp-intent-cut-leak

Conversation

@lucasheriques

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

posthogmcp cut $mcp_intent to twice its limit (4,096 bytes) before redacting, then kept the first 2,048. An identifier that the first cut split in half no longer matches its pattern. Redactions earlier in the text shrink it (an email of 39 bytes becomes [redacted], 10 bytes), so the raw fragment lands inside the kept limit. The unknown tool name (CaptureUnknownTool) had the same shape at 512/256 bytes. This is the leak fixed in PostHog/posthog-ruby#349 and PostHog/posthog-python#1217. It reaches posthogmcpgo and posthogmcpsdk users through the core.

What main captures when a start of about 100 emails precedes the identifier at the cut:

Identifier at the 2x-limit cut $mcp_intent on main ends with This PR
phc_Ab3dEf6hIj9lMn2pQr5tUv8xYz1bCd … phc_Ab3dEf6hIj9... not present
ada.lovelace@example.com … ada.lovelace@exampl... not present
4111 1111 1111 1111 … 4111 1111 1... not present
123-45-6789 … 123-45... not present
(415) 555-0142 … (415) 555... not present

boundedFreeText now follows the sdk-specs bounded scan (mcp-analytics spec, "These passes SHALL cost in proportion to the property limits"). Text up to limit + 1,024 bytes is redacted whole. Longer text is redacted twice, once cut at limit + 1,024 and once cut at the limit, and only the common prefix is kept, followed by .... A match split by the cut at the limit is redacted in the first result and raw in the second. A match split by the margin cut lies past the end of the second result. So neither reaches the event, and the cost still follows the limit, not the input.

The binary gate also runs on the uncut text now, as the spec and Ruby do. On main, a 10 KB base64 intent became 2,045 raw As plus ..., because the 4,096-byte pre-cut kept it under the 10,240-byte gate. It now becomes the binary placeholder. truncateUTF8 was already a linear pass over the whole intent (ToValidUTF8), so this adds no new cost class.

Long benign text keeps its start. The new test pins byte-identical output for long prose and for long CJK text.

💚 How did you test it?

  • TestCaptureFreeTextNeverKeepsAnIdentifierSplitByAScanCut places a PostHog token, email, card, SSN, NANP phone and URL userinfo across the cut at the limit, at limit + margin, and at the old 2x limit, for both $mcp_intent and the unknown tool name. On main, 10 cases fail: every identifier except URL userinfo, at the 2x cut, for both fields. With this PR all 36 pass.
  • TestCaptureToolCallBoundsALongIntent: prose and CJK keep their start, and binary data becomes the placeholder. The binary case fails on main.
  • go test ./... in posthogmcp, posthogmcpgo and posthogmcpsdk, plus go vet and gofmt: all pass.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Claude Code (Opus 5.5). It reproduced the leak with a failing table test first, then ported the spec's two-scan common-prefix algorithm, the same one posthog-python#1217 uses.
  • The kept intent can now be shorter than 2,048 bytes when redactions shrink the start. It only ever holds text from the first 2,048 input bytes, as the spec prescribes. On main, text from bytes 2,048 to 4,096 could fill the gap, which is how the leak happened.
  • Limits stay in bytes, as everywhere else in the Go package. The spec counts characters, which is the same for ASCII.
  • The released posthogmcpgo and posthogmcpsdk modules require core v1.44.1. Users get the fix by upgrading posthog-go once this releases.

🤖 Generated with Claude Code

posthogmcp cut $mcp_intent (and the unknown tool name) to twice its limit
before redacting, then kept the first limit bytes. An identifier the cut
split no longer matched its pattern, and once earlier redactions shrank the
text, its raw fragment (`ada.lovelace@exampl`, `phc_Ab3dEf6hIj9`, a partial
card or phone) landed inside the kept limit. Same leak as posthog-ruby#349
and posthog-python#1217.

boundedFreeText now follows the sdk-specs bounded scan: text over limit +
1,024 bytes is redacted cut at limit + 1,024 and cut at limit, and only the
common prefix is kept. The binary gate runs on the uncut text, so a base64
intent becomes the binary placeholder instead of 2,045 raw characters (the
4,096-byte pre-cut kept it under the 10,240-byte gate).

Tested: go test ./posthogmcp/ (plus posthogmcpgo, posthogmcpsdk) pass.
TestCaptureFreeTextNeverKeepsAnIdentifierSplitByAScanCut fails 10 cases on
main (token, email, card, SSN, phone at the old 2x-limit cut, for intent and
unknown tool name); the binary case of TestCaptureToolCallBoundsALongIntent
fails on main too. Long prose keeps byte-identical output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lucasheriques
lucasheriques requested a review from a team as a code owner November 13, 2026 23:10
@lucasheriques lucasheriques self-assigned this Nov 13, 2026
@veria-ai

veria-ai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

PR overview

The PR changes MCP intent sanitization to keep identifiers split by scan cutoffs out of captured events.

One issue remains open, with none addressed so far: URL userinfo spanning both scan cutoffs can escape redaction and leave credentials in the captured $mcp_intent event. This is a concrete credential-exposure regression, though the reported impact is limited to specially structured intent URLs.

Open issues (1)

Fixed/addressed: 0 · PR risk: 6/10

Reviews

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

posthog-go Compliance Report

Date: 2026-11-13 23:13:53 UTC
Duration: 169140ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

✅ 94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 9ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 7ms
Required Headers.Has Authorization Bearer Header ✅ 6ms
Required Headers.Has Content Type Json ✅ 6ms
Required Headers.Has Posthog Sdk Info Format ✅ 7ms
Required Headers.Has Posthog Attempt Header ✅ 6ms
Required Headers.Has Posthog Request Id ✅ 7ms
Required Headers.Has Posthog Request Timestamp ✅ 5ms
Required Headers.Has User Agent ✅ 7ms
Body Format.Body Has Created At And Batch ✅ 6ms
Body Format.No Api Key In Body ✅ 6ms
Body Format.No Sent At In Body ✅ 6ms
Event Format.Event Has Required Root Fields ✅ 6ms
Event Format.Event Uuid Is Valid ✅ 6ms
Event Format.Event Timestamp Is Rfc3339 ✅ 6ms
Event Format.Distinct Id Is String ✅ 6ms
Event Format.Distinct Id At Root Not Properties ✅ 6ms
Event Format.Custom Properties Preserved ✅ 6ms
Event Format.Set Properties Preserved ✅ 5ms
Event Format.Set Once Properties Preserved ✅ 6ms
Event Format.Groups Properties Preserved ✅ 5ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 6ms
Event Format.Event Has Required Root Fields Batch ✅ 8ms
Event Format.Event Uuid Is Valid Batch ✅ 8ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 9ms
Event Format.Distinct Id Is String Batch ✅ 8ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 8ms
Event Format.Custom Properties Preserved Batch ✅ 8ms
Event Format.Set Properties Preserved Batch ✅ 8ms
Event Format.Set Once Properties Preserved Batch ✅ 9ms
Event Format.Groups Properties Preserved Batch ✅ 19ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 8ms
Batch Behavior.Multiple Events In Single Batch ✅ 31ms
Batch Behavior.Batch Envelope Smoke ✅ 20ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 4ms
Batch Behavior.Flush At Triggers Batch ✅ 1007ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 8ms
Deduplication.Generates Unique Uuids ✅ 15ms
Deduplication.Different Events Same Content Different Uuids ✅ 8ms
Deduplication.Preserves Uuid On Retry ✅ 5134ms
Deduplication.Preserves Timestamp On Retry ✅ 5136ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 5134ms
Deduplication.No Duplicate Events In Batch ✅ 32ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 17ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 10133ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 5134ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 2015ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 5134ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 7ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 29ms
Response Format Validation.Success No Retry After When All Ok ✅ 27ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 129ms
Response Format Validation.Success No Retry After When Drop Only ✅ 28ms
Response Format Validation.Response Echoes Request Id ✅ 6ms
Retry Behavior.Retries On 408 ✅ 5134ms
Retry Behavior.Retries On 500 ✅ 5136ms
Retry Behavior.Retries On 503 ✅ 5136ms
Retry Behavior.Retries On 504 ✅ 5136ms
Retry Behavior.Retryable Errors Have Retry After ✅ 2133ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 8138ms
Retry Behavior.Does Not Retry On 400 ✅ 2133ms
Retry Behavior.Does Not Retry On 401 ✅ 2132ms
Retry Behavior.Does Not Retry On 402 ✅ 2132ms
Retry Behavior.Does Not Retry On 413 ✅ 2132ms
Retry Behavior.Does Not Retry On 415 ✅ 2133ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2133ms
Retry Behavior.Implements Backoff ✅ 15145ms
Retry Behavior.Max Retries Respected ✅ 15144ms
Partial Batch Handling.Handles 200 Full Success ✅ 2011ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3032ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3032ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3030ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 5133ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 5136ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 5136ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 5135ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 5133ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 5135ms
Partial Batch Handling.Respects Retry After On Partial ✅ 5134ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3031ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3033ms
Compression.Sends Gzip Content Encoding ✅ 17ms
Compression.No Content Encoding When Disabled ✅ 6ms
Compression.Compressed Body Is Decompressible ✅ 15ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2131ms
Event Options.Cookieless Mode Override ✅ 8ms
Event Options.Disable Skew Correction Override ✅ 6ms
Event Options.Process Person Profile Override ✅ 6ms
Event Options.Product Tour Id Override ✅ 5ms
Event Options.Unset Options Omitted ✅ 7ms
Event Options.Options Override In Batch ✅ 7ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 6ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 6ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 6ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 16ms
Request Payload.Flags Request Uses V2 Query Param ✅ 15ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 15ms
Request Payload.Flags Request Omits Authorization Header ✅ 17ms
Request Payload.Token In Flags Body Matches Init ✅ 15ms
Request Payload.Groups Round Trip ✅ 16ms
Request Payload.Groups Default To Empty Object ✅ 15ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 16ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 15ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 16ms
Request Lifecycle.No Flags Request On Init Alone ✅ 3ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 6ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 28ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 16ms
Retry Behavior.Retries Flags On 502 ✅ 17ms
Retry Behavior.Retries Flags On 504 ✅ 17ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 16ms

@posthog posthog 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.

PostHog Review · Standard · reviewed 8b4d917: 1 Must fix, 1 Should fix.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

Comment thread posthogmcp/sanitize.go
Comment on lines +503 to +505
withMargin := sanitizeFreeText(prefixUTF8(value, limit+scanMarginBytes))
atLimit := sanitizeFreeText(prefixUTF8(value, limit))
return truncateUTF8(commonPrefix(withMargin, atLimit)+truncationSuffix, limit)

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.

P1 · Prevent URL userinfo spanning both scan cuts from leaking

With a URL starting at intent byte 1900 and a 1,300-byte password before @localhost, both prefixes end before @. sanitizeFreeText leaves both URLs unchanged, so commonPrefix emits https://ada: followed by 133 password bytes. The previous implementation redacts this input. Conservatively discard URL authorities that cross both scan boundaries.

Comment thread posthogmcp/sanitize.go
if len(value) <= limit+scanMarginBytes {
return truncateUTF8(sanitizeFreeText(value), limit)
}
if isBinaryBlob(value) {

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.

P2 · Bound the binary check before scanning large intents

A multi-megabyte base64 intent now sends its entire value through isBase64Like's regexes synchronously; 16 MiB takes about 400 ms in isolation. This defeats the spec's limit-proportional privacy checks, required by AGENTS.md:12. Implement the binary gate using the spec's first/last 1,024-character samples.

Comment thread posthogmcp/sanitize.go
if isBinaryBlob(value) {
return binaryRedactedValue
}
withMargin := sanitizeFreeText(prefixUTF8(value, limit+scanMarginBytes))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Medium: Both scan cuts can hide URL userinfo

For an intent of "https://ada:hunter2" + strings.Repeat("x", 3080) + "@internal/", both prefixes end before @, so neither pass recognizes the userinfo: the captured $mcp_intent starts with https://ada:hunter2xxxx..., whereas the previous implementation returns https://***@internal/. Redact or omit URL authorities that cross the scan boundary rather than treating agreement between two incomplete scans as evidence that their common prefix is safe.

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