Repository navigation
Filter SSE list answers on the event stream #6949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rkb32
wants to merge
24
commits into
stacklok:main
Choose a base branch
from
rkb32:fix/6926-sse-tools-list-authz
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,602
−62
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6c615cd
Harden POST filter against undecodable results
rkb32 34ec327
Remove extra blank line at end of response filter
rkb32 ee93d90
Share the sniffing decision and SSE event head
rkb32 159ba62
Test the POST path's lone CR fail-closed answer
rkb32 64afaf7
Log the SSE filtering of an unlabeled POST body
rkb32 4409435
Return only the sniff action from the classifier
rkb32 da2190d
Fix the classifier return after the signature change
rkb32 b9647fc
Filter SSE list answers on the GET event stream
rkb32 36dd70d
Share the decision for unlabeled 2xx bodies
rkb32 40d7894
Fail closed on tools values that name a tool
rkb32 5765265
Tidy the tools-value comment
rkb32 40733db
Document dropping id-less rejected frames
rkb32 1d37e69
Test the GET writer's Unwrap and note streamable GET
rkb32 93ec9aa
Share the SSE event head between the POST and GET filters
rkb32 fd6f48d
Answer rejections with the id a client decodes
rkb32 efdf1c6
Log each rejected 2xx body once
rkb32 f1102bc
Shorten the streaming filter docs to operator behavior
rkb32 136eb70
Explain the ambiguous result key rejection
rkb32 fd0246f
Fix comment wrapping and a stray CR in the stream filter
rkb32 e0d53c0
Move the POST lone CR test to the POST PR
rkb32 95d211f
Remove a stray blank line from the authz docs
rkb32 4e238c4
Pin the list methods in both directions
rkb32 442ad1c
Drop id-less errors and scan each byte once
rkb32 c214582
Clean up the stream filter's decide, comments and classifier call
rkb32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package authz | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPOSTFiltersSplitResultInMislabeledBody(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| authorizer := &mockAuthorizer{results: map[string]mockResult{ | ||
| "secret": {authorized: false}, | ||
| }} | ||
| body := "data: {\"jsonrpc\":\"2.0\",\"id\":33,\ndata: \"result\":{\"tools\":[{\"name\":\"secret\",\"inputSchema\":{\"type\":\"object\"}}]}}\n\n" | ||
| req := httptest.NewRequest(http.MethodPost, "/mcp", nil) | ||
| rr := httptest.NewRecorder() | ||
| rfw := NewResponseFilteringWriter(rr, authorizer, req, "tools/list", nil, nil) | ||
| rfw.ResponseWriter.Header().Set("Content-Type", "text/plain") | ||
| _, err := rfw.Write([]byte(body)) | ||
| require.NoError(t, err) | ||
| require.NoError(t, rfw.FlushAndFilter()) | ||
|
|
||
| assert.False(t, strings.Contains(rr.Body.String(), "secret"), "a split result in a mislabeled POST body must not leak") | ||
| assert.Contains(t, rr.Body.String(), `"id":33`, "the split result must be filtered and answered, not dropped") | ||
| } | ||
|
|
||
| // TestPOSTFiltersNestedPastDepthCapInUndecodableBody pins the POST path to the GET | ||
| // stream's rule: a result nested past encoding/json's depth cap is failed closed, not | ||
| // written raw, under both a JSON and a mislabeled media type. | ||
| func TestPOSTFiltersNestedPastDepthCapInUndecodableBody(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| deep := strings.Repeat("[", 10001) + strings.Repeat("]", 10001) | ||
| body := `{"jsonrpc":"2.0","id":7,"result":{"tools":[{"name":"secret","inputSchema":{"x":` + deep + `}}]}}` | ||
|
|
||
| for _, contentType := range []string{"application/json", "text/plain"} { | ||
| t.Run(contentType, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| authorizer := &mockAuthorizer{results: map[string]mockResult{ | ||
| "secret": {authorized: false}, | ||
| }} | ||
| req := httptest.NewRequest(http.MethodPost, "/mcp", nil) | ||
| rr := httptest.NewRecorder() | ||
| rfw := NewResponseFilteringWriter(rr, authorizer, req, "tools/list", nil, nil) | ||
| rfw.ResponseWriter.Header().Set("Content-Type", contentType) | ||
| _, err := rfw.Write([]byte(body)) | ||
| require.NoError(t, err) | ||
| require.NoError(t, rfw.FlushAndFilter()) | ||
|
|
||
| assert.False(t, strings.Contains(rr.Body.String(), "secret"), "a deep result must not leak") | ||
| assert.Contains(t, rr.Body.String(), `"id":7`, "the deep result must be answered with its id") | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wraps every GET that reaches the authz middleware, not only event streams. On the transparent proxy that includes any non-MCP GET to the backend. All of them now lose
Accept-Encoding, getCache-Control: private, no-storeon a 2xx and can't upgrade. A non-SSE 2xx body is also held until the handler returns and dropped if it's over 1 MiB.Most of this follows from not trusting Content-Type, so I'm fine with the behavior. It's broader than what the PR body and the docs describe, though, so I'd call it out in the user-facing section as a change for any GET behind authz.