|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "go.uber.org/zap" |
| 9 | + "go.uber.org/zap/zaptest/observer" |
| 10 | + |
| 11 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 12 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/storage" |
| 13 | +) |
| 14 | + |
| 15 | +// RC-UPG-001 (v0.70.0-rc.2 NO-GO): v0.69's restart path re-read the config |
| 16 | +// file and wrote its server entry to config.db ungated, so a keyless server |
| 17 | +// the gate had quarantined ended up recorded as quarantined:false while v0.69 |
| 18 | +// itself still held it in memory. On upgrade the gate read that record as |
| 19 | +// "already through admission", and auto-baseline then approved every tool. |
| 20 | +// |
| 21 | +// Being known to config.db is therefore not proof of admission on its own. |
| 22 | +// A server that really ran under an older release always has an approval |
| 23 | +// baseline (auto-baseline approves a trusted server's tools on first |
| 24 | +// discovery); one that never got past quarantine has none. |
| 25 | + |
| 26 | +// seedServer records name in config.db as unquarantined — the shape both a |
| 27 | +// vetted server and the RC-UPG-001 stale record share. |
| 28 | +func seedServer(t *testing.T, rt *Runtime, name string) { |
| 29 | + t.Helper() |
| 30 | + require.NoError(t, rt.storageManager.SaveUpstreamServer(&config.ServerConfig{ |
| 31 | + Name: name, Command: "./x", Protocol: "stdio", Enabled: true, Quarantined: false, |
| 32 | + })) |
| 33 | +} |
| 34 | + |
| 35 | +// seedApproval records one tool approval for server. |
| 36 | +func seedApproval(t *testing.T, rt *Runtime, server, tool, status, approvedHash string) { |
| 37 | + t.Helper() |
| 38 | + require.NoError(t, rt.storageManager.SaveToolApproval(&storage.ToolApprovalRecord{ |
| 39 | + ServerName: server, ToolName: tool, Status: status, |
| 40 | + ApprovedHash: approvedHash, CurrentHash: "h1", |
| 41 | + })) |
| 42 | +} |
| 43 | + |
| 44 | +func TestConfigLoadAdmissionGate_RequarantinesKnownServerWithoutApprovalBaseline(t *testing.T) { |
| 45 | + core, logs := observer.New(zap.InfoLevel) |
| 46 | + rt, cfg, _ := gateEnvAt(t, []map[string]any{ |
| 47 | + {"name": "legacy-keyless", "command": "./x", "protocol": "stdio", "enabled": true}, |
| 48 | + }, nil, zap.New(core)) |
| 49 | + |
| 50 | + // What v0.69 left behind: known, recorded unquarantined, and only |
| 51 | + // pending tool records from discovery while it was held. |
| 52 | + seedServer(t, rt, "legacy-keyless") |
| 53 | + seedApproval(t, rt, "legacy-keyless", "create_entities", storage.ToolApprovalStatusPending, "") |
| 54 | + |
| 55 | + require.NoError(t, rt.LoadConfiguredServers(cfg)) |
| 56 | + |
| 57 | + assert.True(t, storedServer(t, rt, "legacy-keyless").Quarantined, |
| 58 | + "a server with no approved tool was never admitted; a stale unquarantined record must not admit it") |
| 59 | + assert.True(t, rt.Config().Servers[0].Quarantined) |
| 60 | + assert.Empty(t, logs.FilterMessageSnippet("predate").All(), |
| 61 | + "a server the gate holds is not also reported as a live pre-fix admission") |
| 62 | + assert.NotEmpty(t, logs.FilterMessageSnippet("no approved tool baseline").All()) |
| 63 | +} |
| 64 | + |
| 65 | +func TestConfigLoadAdmissionGate_RequarantinesKnownServerWithNoToolRecords(t *testing.T) { |
| 66 | + rt, cfg := gateEnv(t, []map[string]any{ |
| 67 | + {"name": "legacy-keyless", "command": "./x", "protocol": "stdio", "enabled": true}, |
| 68 | + }, nil) |
| 69 | + seedServer(t, rt, "legacy-keyless") |
| 70 | + |
| 71 | + require.NoError(t, rt.LoadConfiguredServers(cfg)) |
| 72 | + |
| 73 | + assert.True(t, storedServer(t, rt, "legacy-keyless").Quarantined) |
| 74 | +} |
| 75 | + |
| 76 | +func TestConfigLoadAdmissionGate_ApprovalBaselineMeansVetted(t *testing.T) { |
| 77 | + for _, tc := range []struct { |
| 78 | + name, status, approvedHash string |
| 79 | + }{ |
| 80 | + {"approved", storage.ToolApprovalStatusApproved, "h1"}, |
| 81 | + // A rug-pulled tool is "changed" but was approved before. |
| 82 | + {"changed after approval", storage.ToolApprovalStatusChanged, "h0"}, |
| 83 | + } { |
| 84 | + t.Run(tc.name, func(t *testing.T) { |
| 85 | + rt, cfg := gateEnv(t, []map[string]any{ |
| 86 | + {"name": "vetted", "command": "./x", "protocol": "stdio", "enabled": true}, |
| 87 | + }, nil) |
| 88 | + seedServer(t, rt, "vetted") |
| 89 | + seedApproval(t, rt, "vetted", "read_graph", tc.status, tc.approvedHash) |
| 90 | + |
| 91 | + require.NoError(t, rt.LoadConfiguredServers(cfg)) |
| 92 | + |
| 93 | + assert.False(t, storedServer(t, rt, "vetted").Quarantined, |
| 94 | + "a server with an approval baseline was admitted before; upgrade safety leaves it live") |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// The re-quarantine is still only for servers the trust mode would hold. |
| 100 | +func TestConfigLoadAdmissionGate_NoBaselineButNotGatedStaysLive(t *testing.T) { |
| 101 | + rt, cfg := gateEnv(t, []map[string]any{ |
| 102 | + {"name": "explicit", "command": "./x", "protocol": "stdio", "enabled": true, "quarantined": false}, |
| 103 | + {"name": "trusted", "command": "./x", "protocol": "stdio", "enabled": true, "trust_mode": "auto"}, |
| 104 | + }, nil) |
| 105 | + seedServer(t, rt, "explicit") |
| 106 | + seedServer(t, rt, "trusted") |
| 107 | + |
| 108 | + require.NoError(t, rt.LoadConfiguredServers(cfg)) |
| 109 | + |
| 110 | + assert.False(t, storedServer(t, rt, "explicit").Quarantined, "an explicit quarantined:false is obeyed") |
| 111 | + assert.False(t, storedServer(t, rt, "trusted").Quarantined, "trust_mode auto never gates") |
| 112 | +} |
| 113 | + |
| 114 | +// The decision persists: the next load takes the "retain recorded |
| 115 | +// quarantine" branch, so the server stays held until a user approves it. |
| 116 | +func TestConfigLoadAdmissionGate_RequarantineSurvivesRestart(t *testing.T) { |
| 117 | + rt, cfg, path := gateEnvAt(t, []map[string]any{ |
| 118 | + {"name": "legacy-keyless", "command": "./x", "protocol": "stdio", "enabled": true}, |
| 119 | + }, nil, zap.NewNop()) |
| 120 | + seedServer(t, rt, "legacy-keyless") |
| 121 | + require.NoError(t, rt.LoadConfiguredServers(cfg)) |
| 122 | + require.True(t, storedServer(t, rt, "legacy-keyless").Quarantined) |
| 123 | + |
| 124 | + reloaded, err := config.LoadFromFile(path) |
| 125 | + require.NoError(t, err) |
| 126 | + require.NoError(t, rt.LoadConfiguredServers(reloaded)) |
| 127 | + assert.True(t, storedServer(t, rt, "legacy-keyless").Quarantined) |
| 128 | +} |
0 commit comments