|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/mark3labs/mcp-go/mcp" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 12 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/security" |
| 13 | +) |
| 14 | + |
| 15 | +// Issue #1683: direct routing mode (/mcp/all, server__tool names) is the fifth |
| 16 | +// upstream dispatch path and must honour output_sanitisation like call_tool_* |
| 17 | +// and code_execution do. Each test drives the handler mcp-go actually |
| 18 | +// registered (directRoutingFixture) against a real in-process upstream that |
| 19 | +// returns a secret. |
| 20 | + |
| 21 | +func directSanitisationFixture(t *testing.T, cfg *config.OutputSanitisationConfig, body string) *directRoutingFixture { |
| 22 | + t.Helper() |
| 23 | + f := newDirectRoutingFixture(t, directServerSpec{server: "github", tools: []toolSpec{readSpec("get_secret")}}) |
| 24 | + f.proxy.config.OutputSanitisation = cfg |
| 25 | + f.proxy.sanitisationDetector = security.NewDetector(config.DefaultSensitiveDataDetectionConfig()) |
| 26 | + f.ups["github"].okBody.Store(&body) |
| 27 | + return f |
| 28 | +} |
| 29 | + |
| 30 | +func directText(t *testing.T, r *mcp.CallToolResult) string { |
| 31 | + t.Helper() |
| 32 | + require.NotEmpty(t, r.Content) |
| 33 | + return r.Content[0].(mcp.TextContent).Text |
| 34 | +} |
| 35 | + |
| 36 | +func TestDirectModeSanitisation_RedactMasksSecret(t *testing.T) { |
| 37 | + f := directSanitisationFixture(t, sanCfg("redact", false, false), "token "+awsKeyFixture+" end") |
| 38 | + res := f.call(t, context.Background(), "github", "get_secret") |
| 39 | + require.False(t, res.IsError, directText(t, res)) |
| 40 | + assert.NotContains(t, directText(t, res), awsKeyFixture, "the agent must not receive the raw secret") |
| 41 | + assert.Contains(t, directText(t, res), "token ", "non-secret text survives") |
| 42 | +} |
| 43 | + |
| 44 | +func TestDirectModeSanitisation_BlockRefusesCriticalResponse(t *testing.T) { |
| 45 | + f := directSanitisationFixture(t, sanCfg("block", false, false), awsKeyFixture) |
| 46 | + res := f.call(t, context.Background(), "github", "get_secret") |
| 47 | + assert.True(t, res.IsError, "a critical detection in block mode must block") |
| 48 | + assert.Contains(t, directText(t, res), "blocked by sanitisation policy") |
| 49 | + assert.NotContains(t, directText(t, res), awsKeyFixture) |
| 50 | +} |
| 51 | + |
| 52 | +func TestDirectModeSanitisation_SpotlightWrapsUntrustedText(t *testing.T) { |
| 53 | + f := directSanitisationFixture(t, sanCfg("spotlight", true, false), "hello") |
| 54 | + res := f.call(t, context.Background(), "github", "get_secret") |
| 55 | + require.False(t, res.IsError, directText(t, res)) |
| 56 | + text := directText(t, res) |
| 57 | + assert.Contains(t, text, "untrusted:github/get_secret") |
| 58 | + assert.Contains(t, text, "hello") |
| 59 | +} |
| 60 | + |
| 61 | +func TestDirectModeSanitisation_StripRemovesControlSequences(t *testing.T) { |
| 62 | + f := directSanitisationFixture(t, sanCfg("spotlight", false, true), "plain\x1b[31mred\x1b[0m") |
| 63 | + res := f.call(t, context.Background(), "github", "get_secret") |
| 64 | + require.False(t, res.IsError, directText(t, res)) |
| 65 | + assert.NotContains(t, directText(t, res), "\x1b[") |
| 66 | +} |
| 67 | + |
| 68 | +func TestDirectModeSanitisation_DefaultOptOutForwardsUnchanged(t *testing.T) { |
| 69 | + f := directSanitisationFixture(t, config.DefaultOutputSanitisationConfig(), "token "+awsKeyFixture) |
| 70 | + res := f.call(t, context.Background(), "github", "get_secret") |
| 71 | + require.False(t, res.IsError, directText(t, res)) |
| 72 | + assert.Equal(t, "token "+awsKeyFixture, directText(t, res), |
| 73 | + "with no output_sanitisation configured the response is forwarded byte for byte") |
| 74 | +} |
0 commit comments