-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathresponse_filter.go
More file actions
555 lines (486 loc) · 19.2 KB
/
Copy pathresponse_filter.go
File metadata and controls
555 lines (486 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0
// Package authz provides authorization utilities for MCP servers.
package authz
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"github.com/mark3labs/mcp-go/mcp"
"golang.org/x/exp/jsonrpc2"
"github.com/stacklok/toolhive/pkg/authz/authorizers"
"github.com/stacklok/toolhive/pkg/vmcp/optimizer"
"github.com/stacklok/toolhive/pkg/vmcp/session/optimizerdec"
)
var errBug = errors.New("there's a bug")
// ResponseFilteringWriter wraps an http.ResponseWriter to intercept and filter responses
type ResponseFilteringWriter struct {
http.ResponseWriter
authorizer authorizers.Authorizer
request *http.Request
method string
buffer *bytes.Buffer
statusCode int
annotationCache *AnnotationCache
passThroughTools map[string]struct{}
}
// NewResponseFilteringWriter creates a new response filtering writer.
// The annotationCache parameter is optional; pass nil to disable annotation caching.
// The passThroughTools parameter is optional; tools whose names appear in this set
// bypass policy filtering because authorization is enforced elsewhere (e.g., inside
// the optimizer decorator for find_tool/call_tool).
func NewResponseFilteringWriter(
w http.ResponseWriter, authorizer authorizers.Authorizer, r *http.Request, method string,
annotationCache *AnnotationCache, passThroughTools map[string]struct{},
) *ResponseFilteringWriter {
return &ResponseFilteringWriter{
ResponseWriter: w,
authorizer: authorizer,
request: r,
method: method,
buffer: &bytes.Buffer{},
statusCode: http.StatusOK,
annotationCache: annotationCache,
passThroughTools: passThroughTools,
}
}
// Write captures the response body for filtering
func (rfw *ResponseFilteringWriter) Write(data []byte) (int, error) {
return rfw.buffer.Write(data)
}
// WriteHeader captures the status code
func (rfw *ResponseFilteringWriter) WriteHeader(statusCode int) {
rfw.statusCode = statusCode
}
// FlushAndFilter processes the captured response and applies filtering if needed.
// Returns an error if filtering or writing fails.
func (rfw *ResponseFilteringWriter) FlushAndFilter() error {
// If it's not a successful response, just pass it through
if rfw.statusCode != http.StatusOK && rfw.statusCode != http.StatusAccepted {
rfw.ResponseWriter.WriteHeader(rfw.statusCode)
_, err := rfw.ResponseWriter.Write(rfw.buffer.Bytes()) //nolint:gosec // G705 - JSON-RPC response, not rendered as HTML
return err
}
// Check if this response needs filtering
if !requiresResponseFiltering(rfw.method) {
rfw.ResponseWriter.WriteHeader(rfw.statusCode)
_, err := rfw.ResponseWriter.Write(rfw.buffer.Bytes()) //nolint:gosec // G705 - JSON-RPC response, not rendered as HTML
return err
}
rawResponse := rfw.buffer.Bytes()
// Skip filtering for empty responses (common in SSE scenarios where actual data comes via SSE stream)
if len(rawResponse) == 0 {
rfw.ResponseWriter.WriteHeader(rfw.statusCode)
_, err := rfw.ResponseWriter.Write(rawResponse) //nolint:gosec // G705 - JSON-RPC response, not rendered as HTML
return err
}
mimeType := strings.Split(rfw.ResponseWriter.Header().Get("Content-Type"), ";")[0]
switch mimeType {
case "application/json":
// Remove the upstream Content-Length header. The reverse proxy copies it
// from the backend response via Header() (which we don't override), but
// filtering changes the body size. Without this, Go's HTTP server detects
// the mismatch and tears down the connection.
rfw.ResponseWriter.Header().Del("Content-Length")
return rfw.processJSONResponse(rawResponse)
case "text/event-stream":
// Same issue: filtering changes the SSE payload size.
rfw.ResponseWriter.Header().Del("Content-Length")
return rfw.processSSEResponse(rawResponse)
default:
rfw.ResponseWriter.WriteHeader(rfw.statusCode)
_, err := rfw.ResponseWriter.Write(rawResponse)
return err
}
}
// Flush implements http.Flusher if the underlying ResponseWriter supports it.
// This method is required for streaming support (SSE, streamable-http).
//
// We must delete the Content-Length header before flushing because
// httputil.ReverseProxy (with FlushInterval: -1) calls Flush() after copying
// the backend response. The first Flush() on the underlying writer triggers an
// implicit WriteHeader(200), sending headers to the wire. If the stale
// Content-Length is still present at that point, it's too late to remove it in
// FlushAndFilter().
func (rfw *ResponseFilteringWriter) Flush() {
if flusher, ok := rfw.ResponseWriter.(http.Flusher); ok {
rfw.ResponseWriter.Header().Del("Content-Length")
flusher.Flush()
}
}
func (rfw *ResponseFilteringWriter) processJSONResponse(rawResponse []byte) error {
message, err := jsonrpc2.DecodeMessage(rawResponse)
if err != nil {
rfw.ResponseWriter.WriteHeader(rfw.statusCode)
_, err := rfw.ResponseWriter.Write(rawResponse)
return err
}
response, ok := message.(*jsonrpc2.Response)
if !ok {
rfw.ResponseWriter.WriteHeader(rfw.statusCode)
_, err := rfw.ResponseWriter.Write(rawResponse)
return err
}
filteredResponse, err := rfw.filterListResponse(response)
if err != nil {
return rfw.writeErrorResponse(response.ID, err)
}
filteredData, err := jsonrpc2.EncodeMessage(filteredResponse)
if err != nil {
return rfw.writeErrorResponse(response.ID, err)
}
rfw.ResponseWriter.WriteHeader(rfw.statusCode)
_, err = rfw.ResponseWriter.Write(filteredData)
return err
}
//nolint:gocyclo
func (rfw *ResponseFilteringWriter) processSSEResponse(rawResponse []byte) error {
// Note: this routine is adapted from the one in pkg/mcp/tool_filter.go.
// I don't see an obvious way to factor out the commonalities, so I'm
// duplicating it here, but we should refactor response parsing
// respecting mime types to a common routine.
var linesep []byte
if bytes.Contains(rawResponse, []byte("\r\n")) {
linesep = []byte("\r\n")
} else if bytes.Contains(rawResponse, []byte("\n")) {
linesep = []byte("\n")
} else if bytes.Contains(rawResponse, []byte("\r")) {
linesep = []byte("\r")
} else {
return fmt.Errorf("unsupported separator: %s", string(rawResponse))
}
var linesepTotal, linesepCount int
linesepTotal = bytes.Count(rawResponse, linesep)
lines := bytes.Split(rawResponse, linesep)
for _, line := range lines {
if len(line) == 0 {
continue
}
var written bool
if data, ok := bytes.CutPrefix(line, []byte("data:")); ok {
message, err := jsonrpc2.DecodeMessage(data)
switch {
case err != nil:
// Pass this line through unfiltered. Earlier revisions wrote
// rawResponse and returned here, which leaked every subsequent
// data line on the stream past the filter (issue #5257).
if rfw.method == string(mcp.MethodToolsList) {
slog.Warn("SSE data line could not be decoded as JSON-RPC; passing through unfiltered",
"method", rfw.method, "error", err)
}
default:
if response, ok := message.(*jsonrpc2.Response); ok {
filteredResponse, err := rfw.filterListResponse(response)
if err != nil {
return rfw.writeErrorResponse(response.ID, err)
}
filteredData, err := jsonrpc2.EncodeMessage(filteredResponse)
if err != nil {
return rfw.writeErrorResponse(response.ID, err)
}
_, err = rfw.ResponseWriter.Write([]byte("data: " + string(filteredData) + "\n"))
if err != nil {
return fmt.Errorf("%w: %w", errBug, err)
}
written = true
} else if rfw.method == string(mcp.MethodToolsList) {
// Non-Response message (e.g. a notifications/* frame
// interleaved on the stream). Pass through unfiltered for
// this line only; the next data line may still be the real
// tools/list response and must reach the filter.
slog.Warn("SSE data line was not a JSON-RPC Response; passing through unfiltered",
"method", rfw.method)
}
}
}
if !written {
_, err := rfw.ResponseWriter.Write(line)
if err != nil {
return fmt.Errorf("%w: %w", errBug, err)
}
}
_, err := rfw.ResponseWriter.Write(linesep)
if err != nil {
return fmt.Errorf("%w: %w", errBug, err)
}
linesepCount++
}
// This ensures we don't send too few line separators, which might break
// SSE parsing.
if linesepCount < linesepTotal {
_, err := rfw.ResponseWriter.Write(linesep)
if err != nil {
return fmt.Errorf("%w: %w", errBug, err)
}
}
return nil
}
// requiresResponseFiltering reports whether the method needs response filtering.
// This covers the three MCP list operations and the optimizer's find_tool call,
// whose response embeds a filtered tool list inside a CallToolResult.
func requiresResponseFiltering(method string) bool {
return method == string(mcp.MethodToolsList) ||
method == string(mcp.MethodPromptsList) ||
method == string(mcp.MethodResourcesList) ||
method == optimizerdec.FindToolName
}
// filterListResponse filters the list response based on authorization policies
func (rfw *ResponseFilteringWriter) filterListResponse(response *jsonrpc2.Response) (*jsonrpc2.Response, error) {
if response.Error != nil {
// If there's an error in the response, don't filter
return response, nil
}
if response.Result == nil {
// If there's no result, don't filter
return response, nil
}
// Filter based on the method
switch rfw.method {
case string(mcp.MethodToolsList):
return rfw.filterToolsResponse(response)
case string(mcp.MethodPromptsList):
return rfw.filterPromptsResponse(response)
case string(mcp.MethodResourcesList):
return rfw.filterResourcesResponse(response)
case optimizerdec.FindToolName:
return rfw.filterFindToolResponse(response)
default:
// Unknown method, just return as-is
return response, nil
}
}
// filterToolsResponse filters tools based on call_tool authorization
func (rfw *ResponseFilteringWriter) filterToolsResponse(response *jsonrpc2.Response) (*jsonrpc2.Response, error) {
// Parse the result as a ListToolsResult
var listResult mcp.ListToolsResult
if err := json.Unmarshal(response.Result, &listResult); err != nil {
// If we can't parse it as a list response, just return it as-is
return response, nil
}
// Populate annotation cache from tools/list response so that
// subsequent tools/call requests can look up annotations.
rfw.annotationCache.SetFromToolsList(listResult.Tools)
// When the optimizer is enabled, its meta-tools (find_tool, call_tool) appear
// in tools/list instead of real backend tools. These meta-tools won't match
// any operator-written Cedar policy (which references real tool names), so
// default-deny would filter them out — leaving the client with zero tools.
// Authorization for the underlying backend tools is enforced by the authz
// middleware: call_tool requests are intercepted and the inner tool_name
// argument is authorized against Cedar policy before the request is served.
// See: https://github.com/stacklok/toolhive/issues/4373
passThrough := []mcp.Tool{}
regular := []mcp.Tool{}
for _, t := range listResult.Tools {
if _, ok := rfw.passThroughTools[t.Name]; ok {
passThrough = append(passThrough, t)
} else {
regular = append(regular, t)
}
}
// filterToolsByPolicy checks each tool against the caller's Cedar policies
// (injecting annotations into context for when-clause evaluation) and returns
// only tools the caller is authorized to call.
policyFiltered := filterToolsByPolicy(rfw.request.Context(), rfw.authorizer, regular)
filteredTools := make([]mcp.Tool, 0, len(passThrough)+len(policyFiltered))
filteredTools = append(filteredTools, passThrough...)
filteredTools = append(filteredTools, policyFiltered...)
// Create a new result with filtered tools
filteredResult := mcp.ListToolsResult{
PaginatedResult: listResult.PaginatedResult,
Tools: filteredTools,
}
// Marshal the filtered result back
filteredResultData, err := json.Marshal(filteredResult)
if err != nil {
return nil, err
}
// Create a new response with the filtered result
filteredResponse := &jsonrpc2.Response{
ID: response.ID,
Result: json.RawMessage(filteredResultData),
}
return filteredResponse, nil
}
// filterPromptsResponse filters prompts based on get_prompt authorization
func (rfw *ResponseFilteringWriter) filterPromptsResponse(response *jsonrpc2.Response) (*jsonrpc2.Response, error) {
// Parse the result as a ListPromptsResult
var listResult mcp.ListPromptsResult
if err := json.Unmarshal(response.Result, &listResult); err != nil {
// If we can't parse it as a list response, just return it as-is
return response, nil
}
// Note: instantiating the list ensures that no null value is sent over the wire.
// This is basically defensive programming, but for clients.
filteredPrompts := []mcp.Prompt{}
for _, prompt := range listResult.Prompts {
// Check if the user is authorized to get this prompt
authorized, err := rfw.authorizer.AuthorizeWithJWTClaims(
rfw.request.Context(),
authorizers.MCPFeaturePrompt,
authorizers.MCPOperationGet,
prompt.Name,
nil, // No arguments for the authorization check
)
if err != nil {
slog.Warn("Authorization check failed for prompt, skipping",
"prompt", prompt.Name, "error", err)
continue
}
if authorized {
filteredPrompts = append(filteredPrompts, prompt)
} else {
slog.Debug("Prompt denied by authorization policy",
"prompt", prompt.Name)
}
}
if denied := len(listResult.Prompts) - len(filteredPrompts); denied > 0 {
slog.Debug("Authorization policy filtered prompts",
"total", len(listResult.Prompts), "allowed", len(filteredPrompts), "denied", denied)
}
// Create a new result with filtered prompts
filteredResult := mcp.ListPromptsResult{
PaginatedResult: listResult.PaginatedResult,
Prompts: filteredPrompts,
}
// Marshal the filtered result back
filteredResultData, err := json.Marshal(filteredResult)
if err != nil {
return nil, err
}
// Create a new response with the filtered result
filteredResponse := &jsonrpc2.Response{
ID: response.ID,
Result: json.RawMessage(filteredResultData),
}
return filteredResponse, nil
}
// filterResourcesResponse filters resources based on read_resource authorization
func (rfw *ResponseFilteringWriter) filterResourcesResponse(response *jsonrpc2.Response) (*jsonrpc2.Response, error) {
// Parse the result as a ListResourcesResult
var listResult mcp.ListResourcesResult
if err := json.Unmarshal(response.Result, &listResult); err != nil {
// If we can't parse it as a list response, just return it as-is
return response, nil
}
// Note: instantiating the list ensures that no null value is sent over the wire.
// This is basically defensive programming, but for clients.
filteredResources := []mcp.Resource{}
for _, resource := range listResult.Resources {
// Check if the user is authorized to read this resource
authorized, err := rfw.authorizer.AuthorizeWithJWTClaims(
rfw.request.Context(),
authorizers.MCPFeatureResource,
authorizers.MCPOperationRead,
resource.URI,
nil, // No arguments for the authorization check
)
if err != nil {
slog.Warn("Authorization check failed for resource, skipping",
"resource", resource.URI, "error", err)
continue
}
if authorized {
filteredResources = append(filteredResources, resource)
} else {
slog.Debug("Resource denied by authorization policy",
"resource", resource.URI)
}
}
if denied := len(listResult.Resources) - len(filteredResources); denied > 0 {
slog.Debug("Authorization policy filtered resources",
"total", len(listResult.Resources), "allowed", len(filteredResources), "denied", denied)
}
// Create a new result with filtered resources
filteredResult := mcp.ListResourcesResult{
PaginatedResult: listResult.PaginatedResult,
Resources: filteredResources,
}
// Marshal the filtered result back
filteredResultData, err := json.Marshal(filteredResult)
if err != nil {
return nil, err
}
// Create a new response with the filtered result
filteredResponse := &jsonrpc2.Response{
ID: response.ID,
Result: json.RawMessage(filteredResultData),
}
return filteredResponse, nil
}
// writeErrorResponse writes an error response
func (rfw *ResponseFilteringWriter) writeErrorResponse(id jsonrpc2.ID, err error) error {
errorResponse := &jsonrpc2.Response{
ID: id,
Error: jsonrpc2.NewError(500, fmt.Sprintf("Error filtering response: %v", err)),
}
errorData, marshalErr := json.Marshal(errorResponse)
if marshalErr != nil {
// If we can't even marshal the error, write a simple error
rfw.ResponseWriter.WriteHeader(http.StatusInternalServerError)
_, writeErr := rfw.ResponseWriter.Write([]byte(`{"error": "Internal server error"}`))
return writeErr
}
rfw.ResponseWriter.WriteHeader(http.StatusInternalServerError)
_, writeErr := rfw.ResponseWriter.Write(errorData)
return writeErr
}
// filterFindToolResponse filters the tools list embedded in a find_tool tools/call
// response. The response is a CallToolResult whose first text content item contains
// a JSON-encoded optimizer.FindToolOutput. Only tools the caller is authorized to
// call are retained.
//
// mcp.CallToolResult is used directly with its built-in UnmarshalJSON so that the
// Content interface slice is deserialized correctly into concrete types
// (TextContent, ImageContent, etc.) without a bespoke minimal struct.
//
// To identify which content item carries the find_tool output, each TextContent item
// is tentatively unmarshaled as optimizer.FindToolOutput. A successful unmarshal is a
// stronger signal than checking tc.Type == "text" alone — it confirms the item actually
// carries a find_tool result rather than an arbitrary text payload (e.g. an error string).
func (rfw *ResponseFilteringWriter) filterFindToolResponse(response *jsonrpc2.Response) (*jsonrpc2.Response, error) {
// Use mcp.CallToolResult's built-in UnmarshalJSON for correct Content interface dispatch.
var callResult mcp.CallToolResult
if err := json.Unmarshal(response.Result, &callResult); err != nil || callResult.IsError {
return response, nil
}
// Find the first TextContent item that successfully unmarshals as optimizer.FindToolOutput.
textIdx := -1
var output optimizer.FindToolOutput
for i, c := range callResult.Content {
tc, ok := c.(mcp.TextContent)
if !ok {
continue
}
if err := json.Unmarshal([]byte(tc.Text), &output); err == nil {
textIdx = i
break
}
}
if textIdx == -1 {
return response, nil
}
// Populate annotation cache before filtering, mirroring filterToolsResponse.
// Subsequent call_tool requests use these annotations for Cedar when-clause evaluation
// (e.g. resource.readOnlyHint). The cache is populated from the unfiltered list so
// that annotations are available even for tools that Cedar will deny.
rfw.annotationCache.SetFromToolsList(output.Tools)
output.Tools = filterToolsByPolicy(rfw.request.Context(), rfw.authorizer, output.Tools)
filteredText, err := json.Marshal(output)
if err != nil {
return nil, fmt.Errorf("re-encoding find_tool output: %w", err)
}
original := callResult.Content[textIdx].(mcp.TextContent)
callResult.Content[textIdx] = mcp.TextContent{Type: original.Type, Text: string(filteredText)}
filteredResult, err := json.Marshal(callResult)
if err != nil {
return nil, fmt.Errorf("re-encoding call result: %w", err)
}
return &jsonrpc2.Response{
ID: response.ID,
Result: json.RawMessage(filteredResult),
}, nil
}