Skip to content
Open
Show file tree
Hide file tree
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 Nov 19, 2026
34ec327
Remove extra blank line at end of response filter
rkb32 Nov 21, 2026
ee93d90
Share the sniffing decision and SSE event head
rkb32 Nov 21, 2026
159ba62
Test the POST path's lone CR fail-closed answer
rkb32 Nov 21, 2026
64afaf7
Log the SSE filtering of an unlabeled POST body
rkb32 Nov 21, 2026
4409435
Return only the sniff action from the classifier
rkb32 Nov 21, 2026
da2190d
Fix the classifier return after the signature change
rkb32 Nov 21, 2026
b9647fc
Filter SSE list answers on the GET event stream
rkb32 Nov 19, 2026
36dd70d
Share the decision for unlabeled 2xx bodies
rkb32 Nov 19, 2026
40d7894
Fail closed on tools values that name a tool
rkb32 Nov 21, 2026
5765265
Tidy the tools-value comment
rkb32 Nov 21, 2026
40733db
Document dropping id-less rejected frames
rkb32 Nov 21, 2026
1d37e69
Test the GET writer's Unwrap and note streamable GET
rkb32 Nov 21, 2026
93ec9aa
Share the SSE event head between the POST and GET filters
rkb32 Nov 21, 2026
fd6f48d
Answer rejections with the id a client decodes
rkb32 Nov 21, 2026
efdf1c6
Log each rejected 2xx body once
rkb32 Nov 21, 2026
f1102bc
Shorten the streaming filter docs to operator behavior
rkb32 Nov 21, 2026
136eb70
Explain the ambiguous result key rejection
rkb32 Nov 21, 2026
fd0246f
Fix comment wrapping and a stray CR in the stream filter
rkb32 Nov 21, 2026
e0d53c0
Move the POST lone CR test to the POST PR
rkb32 Nov 21, 2026
95d211f
Remove a stray blank line from the authz docs
rkb32 Nov 21, 2026
4e238c4
Pin the list methods in both directions
rkb32 Nov 21, 2026
442ad1c
Drop id-less errors and scan each byte once
rkb32 Nov 21, 2026
c214582
Clean up the stream filter's decide, comments and classifier call
rkb32 Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/authz.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ Then `tools/list` will only show the "weather" tool for that user.

A filtered list depends on the caller's permissions, so `tools/list`, `prompts/list`, and `resources/list` responses carry the HTTP header `Cache-Control: private, no-store`, and a `cacheScope` or `ttlMs` member the backend sent in the result is replaced with `"private"` or `0`. The proxy does not add either member when the backend omitted it, so a list from a backend on an MCP revision that predates these members reaches the client without them. A result that repeats `cacheScope` or `ttlMs`, or spells one with a case-insensitive alias, fails closed with a generic internal JSON-RPC error. `resources/templates/list` responses carry the same header, and their result always includes `cacheScope: "private"` and `ttlMs: 0`.

The SSE transport (`--transport sse`) and the GET stream of streamable HTTP filter list answers the same way, because the middleware wraps every `GET`. Each event is filtered as it arrives, with the filter chosen from the result's shape. A result that is not a clean JSON-RPC response, carries more than one list, or is ambiguous fails closed with a JSON-RPC error that carries the request id. A `find_tool` answer is filtered; other results pass through unchanged. A 2xx response that is not `text/event-stream` is held until the handler returns, then read as one JSON-RPC message or as SSE events. A non-2xx response passes through unchanged. A body larger than 16 MiB ends the stream with one error. A filtered stream with a `Content-Encoding` other than `identity` gets `502 Bad Gateway`. A frame with no readable id is dropped, because an error without an id ends the client's session.

##### Methods covered by a prompt or resource policy

Some MCP methods do not name a capability directly but still act on one. They are
Expand Down
12 changes: 12 additions & 0 deletions pkg/authz/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ func Middleware(a authorizers.Authorizer, next http.Handler, passThroughTools ma
annotationCache := NewAnnotationCache()

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// A GET opens the server-to-client event stream. Its list answers are
// filtered per event as they arrive, by the shape of each result. Every GET
// is wrapped, because the legacy SSE client may not send Accept:
// text/event-stream, so the Accept header cannot mark the stream.
if r.Method == http.MethodGet {

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.

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, get Cache-Control: private, no-store on 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.

stream := newSSEStreamFilterWriter(w, a, r, annotationCache, passThroughTools)
// The filter cannot parse a content-coded body, so ask the backend for an identity body.
next.ServeHTTP(stream, mcp.WithoutAcceptEncoding(r))
stream.finish()
return
}

// Check if we should skip authorization before checking parsed data
if shouldSkipInitialAuthorization(r) {
next.ServeHTTP(w, r)
Expand Down
459 changes: 397 additions & 62 deletions pkg/authz/response_filter.go

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions pkg/authz/response_filter_split_test.go
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")
})
}
}
Loading