Skip to content

Filter SSE list answers on the event stream - #6949

Merged
aponcedeleonch merged 37 commits into
stacklok:mainfrom
rkb32:fix/6926-sse-tools-list-authz
Nov 26, 2026
Merged

aponcedeleonch merged 37 commits into
stacklok:mainfrom
rkb32:fix/6926-sse-tools-list-authz

Conversation

@rkb32

@rkb32 rkb32 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #6926.
Includes the POST-path hardening first proposed in #7211 (closed unmerged).

Summary

Why: Over SSE, list answers (tools, prompts, resources, resource templates, skills) reached the client on the GET event stream, which was not filtered.

What: Each GET event is now filtered as it arrives, with the same per-entry authorization as the streamable path. The filter is picked from the shape of each result. find_tool answers are filtered too.

Does this introduce a user-facing change?

  • SSE list answers. Before: every entry was visible. After: only authorized entries are sent.
  • find_tool answers on the GET stream. Before: unfiltered. After: filtered by policy.
  • tools/call answers that name tools under a tools key (other than a valid find_tool answer). Before: passed through. After: a JSON-RPC error carrying the request id.
  • All GETs behind authz (including non-MCP GETs on the transparent proxy). Before: Accept-Encoding passed through. After: stripped; a content-coded 2xx stream gets 502 Bad Gateway. A filtered 2xx GET carries Cache-Control: private, no-store. Upgrade requests are refused.
  • Successful (2xx) response with a Content-Type other than text/event-stream. Before: body passed through. After: read as one JSON-RPC message or as SSE. A list answer is filtered and sent; other clean answers are sent unchanged; a body that names a result it cannot read is rejected with an error carrying its id, or dropped if no id can be read. Bodies with no result pass through unchanged. Non-2xx bodies pass through unchanged.
  • Held 2xx body over 16 MiB. Before: no limit. After: the stream ends with one error (none if no id can be read).
  • An unfinished SSE event over 16 MiB. Before: buffered without limit. After: the stream ends with one error carrying its id, or nothing if no id can be read.

Special notes for reviewers

  • A 2xx with an unrecognised Content-Type is read as one JSON-RPC message or as SSE, so a client that ignores Content-Type cannot read unfiltered list answers.
  • Permissions are taken from the request that opens the stream. A change to the caller's role or token applies on reconnect.
  • Known gaps, not verified: prompt/resource deadline behaviour, stdio transport. A tool name under a member with another key (e.g. _meta.related) is not checked on the GET stream.

Test plan

  • go test for pkg/authz/..., pkg/transport/proxy/httpsse, pkg/transport/proxy/transparent (run locally, not via task)
  • E2E tests not run
  • Manual testing: none

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-26T21:59:10.750483Z
We couldn’t complete this review. Comment @codex review to try again.
63ddc0e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@rkb32 rkb32 changed the title Filter SSE tools/list, prompts/list and resources/list answers per caller Filter SSE list answers on the event stream Oct 27, 2026
@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch 2 times, most recently from f4289c8 to 17c0685 Compare November 1, 2026 17:15

@aponcedeleonch aponcedeleonch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this, @rkb32. It follows the direction from #6926 closely: it's modeled on toolFilterWriter, covers all five list types, reuses the shared per-type filters so the semantics match the POST path, and the transparent-proxy test is there. I also appreciate how candid the description is about the limitations.

The main thing to fix is that the stream path re-implements the head of resolveSSEEvent instead of reusing it, and in doing so it drops some of the POST path's fail-closed checks. Two of them can leak an unfiltered list from a hostile backend. Details are inline, and each fix should be small.

Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter_test.go Resolved
Comment thread docs/authz.md Resolved
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.22556% with 52 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.15%. Comparing base (2125a91) to head (5ec2d5b).
⚠️ Report is 94 commits behind head on main.

Files with missing lines Patch % Lines
pkg/authz/sse_stream_filter.go 90.37% 33 Missing ⚠️
pkg/authz/response_filter.go 89.67% 19 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           main    #6949       +/-   ##
=========================================
+ Coverage      0   82.15%   +82.15%     
=========================================
  Files         0      932      +932     
  Lines         0    94889    +94889     
=========================================
+ Hits          0    77957    +77957     
- Misses        0    16927    +16927     
- Partials      0        5        +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch from 17c0685 to 27113bf Compare November 13, 2026 22:12
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter_test.go Resolved
@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch from 5c86827 to 0702a94 Compare November 16, 2026 23:16

@aponcedeleonch aponcedeleonch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the quick turnaround, @rkb32. Almost everything from the last two rounds is in. The split BOM, the SSE-framed mislabeled body and the mid-stream test all look good, and sniffBodyShape is a step in the right direction.

The main thing left is the reuse point. The per-event filtering is shared now, but the framing loop, the head of resolveSSEEvent, the mislabeled-body decision and the list mapping are still written twice, and one of them has already drifted. Details inline.

Two things that don't fit on a line:

  • The PR body is out of date. It still says a mislabeled 2xx is "read as one JSON-RPC message, as the reviewer suggested" and that anything that isn't JSON-RPC is dropped, but a body with no result now passes through. The trade-off that a mislabeled body is held until the handler returns should be in there too, plus the POST path changes (see inline).
  • The non-test change is around 680 lines, over the 400-line guideline. Moving the POST path fixes to their own PR would bring it closer and make both easier to review. Also, the test plan should use task test rather than go test.

Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/middleware.go Resolved
Comment thread pkg/authz/response_filter.go Resolved
Comment thread docs/authz.md Resolved
Comment thread docs/authz.md Resolved
A POST body that carries a result past encoding/json's depth cap was
written unfiltered. Reject it instead, answering with the request id,
as the GET stream does. Also filter a result split across lone CR line
breaks, and share the no-shape decision between the POST path and the
GET stream so the two cannot drift apart.
@rkb32

rkb32 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@aponcedeleonch Thanks for the reviews. Two things before I reply to each thread:

Correction: my earlier reply said the cap is 1 MiB. It's 16 MiB, matching the default in pkg/mcp/tool_filter.go. The docs and PR body say so.
I opened the POST-path fixes as #7211 to shrink this PR. The GET side is still about 740 non-test lines. Do you want it split further, or is one PR OK?

@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch from 0702a94 to 375410e Compare November 21, 2026 21:46
@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch from b719112 to 42163f0 Compare November 21, 2026 22:35
@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch from 6497a3b to 1c6f188 Compare November 21, 2026 23:05
The GET event stream carried list answers unfiltered. Filter each event
as it arrives, choosing the filter from the shape of its result, with
the same per-entry authorization as the streamable path. Answer rejected
frames with a JSON-RPC error that carries the request id, so the client
does not wait for a timeout. Document the limits: a 16 MiB cap on an
open event, a held body sniffed before it is filtered, and a 101 upgrade
through ReverseProxy, which is refused.
The POST path and the GET stream each decided how to handle a 2xx body with an unrecognised media type, in different orders. Move that decision into one classifier. Each caller still writes its own rejection and chooses its own request id, so no response changes.
@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch from 1c6f188 to c214582 Compare November 21, 2026 23:33

@aponcedeleonch aponcedeleonch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for splitting out #7211, @rkb32. This round looks good. The silent drop is fixed, the line framing and event head are shared now, and the logging is cleaner. I'm happy to keep the GET side as one PR. It's one logical change and most of it lives in one file.

A couple of things left:

  • Commit e0d53c025 deletes TestResponseFilteringWriter_SSE_LoneCRLineBreakFailsClosed, which #7211 adds. Once #7211 merges, this PR would remove it from main. I'd drop that commit.
  • The find_tool shape detection needs a decision. Details inline.

Comment thread pkg/authz/sse_stream_filter.go Resolved
Comment thread pkg/authz/sse_stream_filter_test.go Resolved
Comment thread docs/authz.md Resolved
rkb32 and others added 15 commits November 23, 2026 09:53
Decode each GET stream list response once instead of twice, and log
filtering errors on the GET stream at WARN so the POST path keeps its
ERROR level.
A tools/call answer from a non-find_tool tool with a named tools list is
treated as a find_tool answer on the GET stream, because the stream cannot
see the request. Pin that it fails closed, and document it in authz.md with
the point where the caller's identity is read.

The list-member test now loops over the list filter kinds, so a new kind
that no listMembers entry reaches fails the test.
A tool answer whose content names a tools member anywhere, outside a readable
find_tool answer, passed through the GET stream unfiltered. Such answers now
fail closed. Log each filter failure once, and fix comments and docs that the
change made stale.
Add a table test for the three tool answer shapes that named tools outside
a readable find_tool output. Unwrap a JSON string literal when looking for a
tools member, so a doubly encoded list fails closed too. Narrow the docs
sentence to a tools member that names a tool.
A find_tool-shaped answer on the GET stream could carry tool names in _meta,
an extra content member, or a member beside tools, and those reached the
client unfiltered. Empty every tools list and fail closed if a tools name
remains. Check the emptied copy so an authorized tool whose schema has a
tools property still passes. POST is unchanged.
Only another tools member is checked, not a tool name under other keys.
Make the beside-tools test depend on the fix by putting the extra member in
both outputs. Correct the docs and comment to match.
Every GET now drops Accept-Encoding and ends a coded stream with 502, not
500. A role or token change during the stream applies on reconnect.
The 502 replaces the response before any event is sent. Describe every
GET as the event stream and rewrap the paragraph.
Move the JSON-in-string branch into mentionsToolsString so the
function is under the gocyclo limit of 15. Behavior is unchanged.
Define the list kinds once in listKinds and build listMembers from it,
so a new list kind is added in one place. responseFilterForMethod reads
the same table. Behavior is unchanged.
The event-stream writer supplies its shape rules to the shared filter
as selectMethod, and both the event and sniffed-body paths ask the
writer for the method. The find_tool and tools-member rules stay in the
stream file. Behavior is unchanged.
@rkb32

rkb32 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Status by point:

  • Silent drop of 2xx bodies: now answered with an error carrying the request id (writeRejection). Only id-less bodies are dropped, logged at WARN.
  • Parsing: the GET stream filter splits events with the shared parseSSELines and processSSELines.
  • Method map: listMembers is derived from one table (listKinds) that responseFilterForMethod also reads. TestListMembersMapToListFilters pins both directions.
  • Sniffed bodies: writeSniffed now goes through the same shape-selection path as events (responseMethod). It keeps its own entry point, because a sniffed body is read once the handler returns.
  • Shape rules: the find_tool and tools-member rules stay on the GET side (resultMethod), passed to the shared writer as selectMethod. Moving them into the POST-shared filter would apply GET-only rules to POST.
  • Logging: the GET path logs stream errors at WARN.
  • e0d53c025: not in this branch.
  • Find_tool shape: fail closed on tool answers naming tools off the find_tool shape, documented in docs/authz.md.

rkb32 added 2 commits November 25, 2026 16:20
Split the GET-stream paragraph into an intro and a list of observable
behavior, state that permissions are taken at connect, and say why the
duplicate result-key check exists only on the stream path.
Add the observable GET-stream behaviors that the review asked to see:
non-MCP GETs on the transparent proxy, Cache-Control on filtered 2xx
responses, and refused connection upgrades.
@rkb32
rkb32 force-pushed the fix/6926-sse-tools-list-authz branch from c214582 to 683dac0 Compare November 25, 2026 22:26
The media-type, sniff and decide comments still described the earlier
drop-everything behavior. Describe rejection, pass-through, and the
held-body limit as the code does.

@aponcedeleonch aponcedeleonch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for sticking with this, @rkb32. Everything from the last round is in, and the new find_tool checks look good to me.

Two small things before merging, neither blocking:

  • The PR body is a bit behind the code. It still says 1 MiB instead of 16 MiB, says non-JSON-RPC bodies are dropped, and lists find_tool as a gap. It'd also help to mention that it depends on #7211, and the user-facing changes that are only in the docs today (every GET wrapped, Accept-Encoding stripped, the 502, tools/call answers with a tools list failing closed).
  • nit: in docs/authz.md, "A frame with no readable id is dropped" reads like notifications get dropped too. Something like "A frame that fails closed but has no readable id is dropped" would be clearer.

@aponcedeleonch
aponcedeleonch merged commit 46586fa into stacklok:main Nov 26, 2026
43 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.

tools/list is not filtered on the SSE transport

2 participants