|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +func newRejectQueryTestServer(t *testing.T, reject bool) *Server { |
| 16 | + t.Helper() |
| 17 | + cfg := config.DefaultConfig() |
| 18 | + cfg.DataDir = t.TempDir() |
| 19 | + cfg.Listen = "127.0.0.1:0" |
| 20 | + cfg.APIKey = "test-api-key-reject-query" |
| 21 | + cfg.MCPRejectQueryAPIKey = reject |
| 22 | + srv, err := NewServer(cfg, zap.NewNop()) |
| 23 | + require.NoError(t, err) |
| 24 | + t.Cleanup(func() { _ = srv.Shutdown() }) |
| 25 | + return srv |
| 26 | +} |
| 27 | + |
| 28 | +// With mcp_reject_query_apikey a URL-borne credential is a 401 on every MCP |
| 29 | +// path, and the downstream handler never runs. It must not degrade to the |
| 30 | +// anonymous administrator that an unrecognised credential gets when |
| 31 | +// require_mcp_auth is off. |
| 32 | +func TestMCPAuthMiddleware_RejectQueryAPIKey(t *testing.T) { |
| 33 | + srv := newRejectQueryTestServer(t, true) |
| 34 | + |
| 35 | + for _, path := range []string{"/mcp", "/mcp/all", "/mcp/code"} { |
| 36 | + t.Run("query credential refused on "+path, func(t *testing.T) { |
| 37 | + for _, q := range []string{"?apikey=test-api-key-reject-query", "?apikey=", "?apikey=mcp_agt_whatever"} { |
| 38 | + seen, code := captureAuthContext(t, srv, httptest.NewRequest(http.MethodPost, path+q, http.NoBody)) |
| 39 | + assert.Equal(t, http.StatusUnauthorized, code, q) |
| 40 | + assert.Nil(t, seen, "handler must not run for %s", q) |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + t.Run("header credentials still work", func(t *testing.T) { |
| 46 | + req := httptest.NewRequest(http.MethodPost, "/mcp", http.NoBody) |
| 47 | + req.Header.Set("X-API-Key", "test-api-key-reject-query") |
| 48 | + seen, code := captureAuthContext(t, srv, req) |
| 49 | + require.Equal(t, http.StatusOK, code) |
| 50 | + require.NotNil(t, seen) |
| 51 | + assert.True(t, seen.IsAdmin()) |
| 52 | + assert.False(t, seen.Anonymous) |
| 53 | + |
| 54 | + req = httptest.NewRequest(http.MethodPost, "/mcp", http.NoBody) |
| 55 | + req.Header.Set("Authorization", "Bearer test-api-key-reject-query") |
| 56 | + seen, code = captureAuthContext(t, srv, req) |
| 57 | + require.Equal(t, http.StatusOK, code) |
| 58 | + require.NotNil(t, seen) |
| 59 | + assert.False(t, seen.Anonymous) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("other query parameters are untouched", func(t *testing.T) { |
| 63 | + seen, code := captureAuthContext(t, srv, httptest.NewRequest(http.MethodPost, "/mcp?profile=work", http.NoBody)) |
| 64 | + require.Equal(t, http.StatusOK, code) |
| 65 | + require.NotNil(t, seen) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +// The default keeps the query form working, so existing clients that can only |
| 70 | +// configure a URL are unaffected. |
| 71 | +func TestMCPAuthMiddleware_QueryAPIKeyAcceptedByDefault(t *testing.T) { |
| 72 | + srv := newRejectQueryTestServer(t, false) |
| 73 | + |
| 74 | + seen, code := captureAuthContext(t, srv, httptest.NewRequest(http.MethodPost, "/mcp?apikey=test-api-key-reject-query", http.NoBody)) |
| 75 | + require.Equal(t, http.StatusOK, code) |
| 76 | + require.NotNil(t, seen) |
| 77 | + assert.True(t, seen.IsAdmin()) |
| 78 | + assert.False(t, seen.Anonymous, "the query credential must still authenticate by default") |
| 79 | +} |
0 commit comments