Strip leading UTF-8 BOM before filtering list responses - #6304
Open
Yanhaoxi wants to merge 2 commits into
Open
Conversation
A client strips a leading BOM per the WHATWG UTF-8 decode algorithm before parsing, but the response filter decoded and sniffed the raw bytes. Go's encoding/json treats EF BB BF as a syntax error, so a BOM-prefixed list response failed every decode and smuggled-result sniff and passed through unfiltered, leaking policy-denied tools/prompts/resources. The SSE processing path already stripped the BOM; the JSON decode path, the JSON/SSE sniffs in FlushAndFilter, and the tool filter's unrecognized-media-type sniff did not. Strip the BOM once in FlushAndFilter before the media-type dispatch (covering the JSON path and both default-branch sniffs) and once at the top of processUnrecognizedMimeType in the tool filter. Add regression tests mirroring the existing SSE BOM test.
Yanhaoxi
requested review from
ChrisJBurns,
JAORMX,
amirejaz,
blkt,
jhrozek,
rdimitrov and
tgrunnagle
as code owners
August 13, 2026 11:51
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6304 +/- ##
==========================================
+ Coverage 72.82% 72.87% +0.05%
==========================================
Files 742 742
Lines 77762 77764 +2
==========================================
+ Hits 56632 56673 +41
+ Misses 17155 17100 -55
- Partials 3975 3991 +16 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
A client strips a leading UTF-8 BOM per the WHATWG UTF-8 decode algorithm before parsing, but the response filters decoded and sniffed the raw bytes. Go's
encoding/jsontreatsEF BB BFas a syntax error (not whitespace), so a BOM-prefixed list response failed every decode and smuggled-result sniff and passed through unfiltered — leaking tools/prompts/resources that the Cedar policy (authz) or tool filter (mcp) was supposed to remove. This is the same #5257-class leak the SSE processing path was already hardened against; the JSON decode path, bothFlushAndFilterdefault-branch sniffs, and the tool filter's unrecognized-media-type sniff were not.WHAT changed:
FlushAndFilterbefore the media-type dispatch (pkg/authz/response_filter.go), covering theapplication/jsondecode path plus the JSON and SSE smuggled-result sniffs in the default branch.processUnrecognizedMimeType(pkg/mcp/tool_filter.go), covering both its JSON sniff andsniffSSEToolsList.Fixes #6301
Type of change
Test plan
task test)task test-e2e)task lint-fix)Run:
go test ./pkg/authz/... ./pkg/mcp/.... New tests:TestResponseFilteringWriter_JSON_LeadingBOMBypass(JSON path + JSON sniff),TestResponseFilteringWriter_Sniff_SSE_LeadingBOMBypass(SSE sniff),TestNewListToolsMappingMiddleware_BOMCannotSmuggleToolsList(tool filter, JSON + SSE). Verified the new tests FAIL without the source fix and PASS with it; fullpkg/authz/...andpkg/mcp/...suites pass.Lint:
task lintreports only 3 pre-existinggciissues in files this PR does not touch (pkg/authz/annotation_cache.go,pkg/authz/authorizers.go,pkg/authz/config.go), confirmed present on cleanmain; none of this PR's 4 files are flagged.go vetclean,gofmtclean.API Compatibility
v1beta1API.Changes
pkg/authz/response_filter.goFlushAndFilterbefore media-type dispatchpkg/authz/response_filter_test.gopkg/mcp/tool_filter.goprocessUnrecognizedMimeTypepkg/mcp/tool_filter_test.goDoes this introduce a user-facing change?
Yes: a misbehaving/untrusted upstream that prepends a UTF-8 BOM to a list response can no longer bypass list filtering; the denied items are filtered as before.
Implementation plan
Approved implementation plan
Four code points share one root cause: no leading-BOM strip before decode/sniff.
processJSONResponsedecode + fail-closed check, JSON sniff (carriesResult), SSE sniff (sseCarriesResult). Fixed with onebytes.TrimPrefixinFlushAndFilterbefore the media-type switch.processUnrecognizedMimeTypeJSON sniff +sniffSSEToolsList. Fixed with onebytes.TrimPrefixat the function entry (covers both sniffs).TestResponseFilteringWriter_SSE_LeadingBOMBypassper route.Special notes for reviewers
processSSEResponse) is retained;bytes.TrimPrefixis idempotent so the redundant strip is harmless.carriesResult/sseCarriesResultwould previously have returnedfalseon the BOM — the fix restores the fail-closed behavior those branches were built for (authz: SSE response filter leaks unfiltered tools/list on undecodable / non-Response data lines (bypasses cedar) #5257).processUnrecognizedMimeType) — the BOM fix does not alter that.