Skip to content

Commit ab8a391

Browse files
authored
fix(security): don't admit a server v0.69 held in quarantine on upgrade (RC-UPG-001) (#1485)
# Pull Request ## Description Fixes RC-UPG-001, the release blocker in the v0.70.0-rc.2 test report. On upgrade from v0.69, a server that v0.69 held in quarantine was admitted, and all of its tools were auto-approved. **Root cause** - v0.69's restart path (`lookupServerConfigForRestart`) re-read `mcp_config.json`. A server with no `quarantined` key reads as `false`, and that path wrote the entry to config.db **ungated**. - The baseline security scan shortly after startup triggers such a restart. So does any manual or secret-driven restart. - v0.69 kept the server quarantined in memory, which is why it looked held. config.db now said `quarantined:false`. - The RC already gates the restart path (#1463). On upgrade, though, the config-load admission gate reads that stale record as "known to config.db = already admitted", and auto-baseline then approves every tool. **Fix** (`internal/runtime/config_load_admission_gate.go`) - Being known to config.db is no longer enough by itself. A known, unquarantined server is held again when all of these are true: - its config never states `quarantined`; - its trust mode would hold it; - it has **no approval baseline**. A baseline means at least one tool record that is approved, or that carries an `ApprovedHash`, such as a rug-pulled "changed" record. - Servers with approved tools are unaffected. They still get only the existing pre-fix advisory. - If approval records can't be read, the gate keeps the old rule: unknown is not the same as never admitted. - The re-quarantine is logged ("Quarantining known server with no approved tool baseline"), emitted as activity, and persisted. The next start takes the existing "retain recorded quarantine" branch. **Deliberate trade-off (fail-closed).** Some vetted servers have no tool records, and nothing in storage distinguishes them from the stale v0.69 record. They are held once on upgrade: - zero-tool servers (prompts or resources only); - servers never discovered while live, such as disabled ones; - servers last vetted before v0.21. One approval releases each, and so does an explicit `"quarantined": false` set before upgrading. This is documented in `docs/features/security-quarantine.md` and the changelog. **Tests** - New `config_load_admission_gate_upgrade_test.go` covers: - a known server with only pending records, and one with no records, is held; - an approved record, or a changed-after-approval record, counts as vetted; - explicit `false` and `trust_mode: auto` stay live; - the hold survives a reload. - Three existing tests modeled a "vetted" server without the approval baseline that every real vetted server has. They now seed one. ## Testing - [x] I have tested these changes locally - [x] I have added/updated tests that prove my fix is effective or my feature works - [x] All existing tests pass Upgrade replay with the real binaries, on an isolated instance: v0.69 with a keyless server and a vetted server, then a restart of the keyless one, then an upgrade. | | keyless server after upgrade | vetted server | |---|---|---| | v0.70.0-rc.2 | live, 9 tools auto-approved (bug) | live | | this branch | quarantined, 0 tools | live | The race suite with `-tags server` passes for runtime, config, storage, server and httpapi, and lint is clean. The cross-model review (zcode) found one MEDIUM, the population above. It is kept as a documented trade-off, and round 2 came back clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 0477f1a commit ab8a391

7 files changed

Lines changed: 220 additions & 5 deletions

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ Releases follow [Semantic Versioning](https://semver.org/).
167167

168168
### Bug Fixes
169169

170+
- **Upgrade no longer admits a server an older release held in quarantine (RC-UPG-001).** A v0.69 restart (including the baseline security scan shortly after startup) wrote a keyless server's un-gated config over its recorded quarantine, while v0.69 kept holding it in memory. On upgrade the config-load admission gate read that record as already admitted and auto-baseline approved all of its tools. The gate now also requires an approval baseline: a server recorded as unquarantined whose tools were never approved is quarantined again for review. Servers with approved tools are unaffected; vetted servers with no tool records (zero-tool servers, servers that never connected while live, servers last vetted before v0.21) are also held once — approve them, or set `"quarantined": false` before upgrading.
171+
170172
- **security/quarantine:** a server added by editing `mcp_config.json` (no `quarantined` key,
171173
first seen) stayed held for review only until its first restart. Restarting it (by hand, after a
172174
secret change, or through the automatic baseline scan's connect) wrote the raw file entry over

‎docs/features/security-quarantine.md‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,19 @@ held; a server whose trust mode is `auto` is admitted.
5353

5454
The second condition is what makes upgrading safe: every server you are already
5555
running has a `config.db` record, so **upgrading never re-quarantines a server
56-
you have already vetted**. The boundary is that a server present in a
56+
you have already vetted**. "Vetted" means the record is backed by an approval
57+
baseline — at least one of the server's tools was approved at some point, which
58+
happens automatically when a trusted server first connects. A server recorded
59+
as unquarantined whose tools were never approved was never admitted, and the
60+
gate holds it for review (see the restart note below).
61+
62+
Because the rule cannot tell such a server apart from a vetted one that has no
63+
tool records, a few vetted servers are also held once on upgrade: servers that
64+
expose no tools (prompts or resources only), servers that never connected while
65+
live (for example, disabled ever since they were vetted), and servers last
66+
vetted before v0.21. Approve them from the quarantine review; the decision is
67+
recorded and they are not held again. To skip the review, add
68+
`"quarantined": false` to the server's entry before upgrading. The boundary is that a server present in a
5769
hand-written config but absent from `config.db` — after a wiped data directory,
5870
or on a machine that has never seen that config before — is treated as
5971
first-seen and held for review.
@@ -103,8 +115,11 @@ quarantine: a restart from the REST API, the CLI, the tray or an MCP client,
103115
"restart all", a secret change that restarts the servers using it, and a security
104116
scan (including the baseline scan that runs shortly after startup) all re-read the
105117
server from `mcp_config.json` and wrote the un-gated entry over the recorded
106-
quarantine. Such a server shows up in the same "predate the config-load admission
107-
gate" warning and should be reviewed the same way. From this fix on, a restart
118+
quarantine. Upgrading now catches this case: such a server has no approved tool,
119+
so the gate quarantines it again at startup and logs `Quarantining known server
120+
with no approved tool baseline`; approve it from the quarantine review as usual.
121+
A server that ran unquarantined (and so has approved tools) still only shows up
122+
in the "predate the config-load admission gate" warning. From this fix on, a restart
108123
runs the file entry through the admission gate, and `config.db` refuses to lower a
109124
recorded quarantine unless the operator states `"quarantined": false` or the
110125
server is released from the quarantine review.

‎internal/runtime/config_load_admission_gate.go‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
77
"github.com/smart-mcp-proxy/mcpproxy-go/internal/runtime/configsvc"
8+
"github.com/smart-mcp-proxy/mcpproxy-go/internal/storage"
89
)
910

1011
// Issue #937 — the trust-mode admission gate on the CONFIG-LOAD path.
@@ -37,6 +38,23 @@ import (
3738
// a server the user has already vetted must not suddenly disappear behind a
3839
// quarantine wall on a version bump.
3940
//
41+
// "Known" alone is not enough, though (RC-UPG-001): v0.69's restart path wrote
42+
// the ungated file entry over a gated record, leaving a server that was never
43+
// admitted recorded as quarantined:false. So a known, unstated, trust-gated
44+
// server is admitted only if it also has an approval baseline — at least one
45+
// tool record that was approved at some point. A server that ran and exposed
46+
// tools has one (auto-baseline approves a trusted server's tools on first
47+
// discovery); the stale v0.69 record has none, and nothing else in storage
48+
// tells the two apart, so a server without one is held again.
49+
//
50+
// That deliberately fails closed for vetted servers that have no tool
51+
// records: zero-tool (prompts/resources-only) servers, servers never
52+
// discovered while live (e.g. disabled since they were vetted), and servers
53+
// last vetted before tool approvals were stored (v0.21). Each is held once
54+
// on upgrade; one approval releases it and records the decision. When the
55+
// approval records cannot be read the gate keeps the old rule: unknown is not
56+
// the same as never admitted.
57+
//
4058
// The cost of that choice is that an install ALREADY hit by #937 keeps its
4159
// poisoned server live, because the buggy admission left exactly the config.db
4260
// record this gate reads as "vetted". Nothing distinguishes the two at load
@@ -104,6 +122,9 @@ type admissionDecision struct {
104122
server string
105123
reason string
106124
newly bool
125+
// neverApproved marks a known server re-held because it has no approval
126+
// baseline (RC-UPG-001).
127+
neverApproved bool
107128
}
108129

109130
// applyConfigLoadAdmissionGate resolves the admission decision for every server
@@ -152,6 +173,15 @@ func (r *Runtime) applyConfigLoadAdmissionGate(cfg *config.Config, stored map[st
152173
// the fix looks like. Upgrade safety says leave it alone; honesty
153174
// says say so.
154175
if !sc.QuarantineExplicitlySet() && cfg.QuarantineDefaultForServer(sc) {
176+
if vetted, ok := r.hasApprovalBaseline(sc.Name); ok && !vetted {
177+
decisions[i] = admissionDecision{
178+
server: sc.Name,
179+
reason: "server recorded as unquarantined but with no approved tool baseline was never admitted; held for review by the " +
180+
string(sc.EffectiveTrustMode()) + " trust mode",
181+
neverApproved: true,
182+
}
183+
continue
184+
}
155185
preFix = append(preFix, sc.Name)
156186
}
157187
continue
@@ -184,7 +214,12 @@ func (r *Runtime) applyConfigLoadAdmissionGate(cfg *config.Config, stored map[st
184214
sc.Quarantined = true
185215
gated.Servers[i] = sc
186216

187-
if d.newly {
217+
if d.neverApproved {
218+
r.logger.Warn("Quarantining known server with no approved tool baseline",
219+
zap.String("server", d.server),
220+
zap.String("trust_mode", string(sc.EffectiveTrustMode())),
221+
zap.String("reason", "config.db records it unquarantined, but no tool was ever approved, so it was never admitted (RC-UPG-001: a v0.69 restart overwrote the gated record)"))
222+
} else if d.newly {
188223
r.logger.Warn("Quarantining first-seen server from configuration file",
189224
zap.String("server", d.server),
190225
zap.String("trust_mode", string(sc.EffectiveTrustMode())),
@@ -200,7 +235,7 @@ func (r *Runtime) applyConfigLoadAdmissionGate(cfg *config.Config, stored map[st
200235
// never be able to stall a config update.
201236
pending := make([]admissionDecision, 0, len(decisions))
202237
for _, d := range decisions {
203-
if d.newly {
238+
if d.newly || d.neverApproved {
204239
pending = append(pending, d)
205240
}
206241
}
@@ -439,3 +474,29 @@ func (r *Runtime) publishAdmissionGatedConfig(previous, gated *config.Config) {
439474
}
440475
r.mu.Unlock()
441476
}
477+
478+
// hasApprovalBaseline reports whether serverName has ever had a tool approved:
479+
// a record that is approved now, or one that carries an approved hash (a
480+
// "changed" record after a rug pull was approved before). ok is false when the
481+
// records cannot be read, so the caller can abstain instead of treating an
482+
// unreadable store as "never approved".
483+
func (r *Runtime) hasApprovalBaseline(serverName string) (vetted, ok bool) {
484+
if r.storageManager == nil {
485+
return false, false
486+
}
487+
records, err := r.storageManager.ListToolApprovals(serverName)
488+
if err != nil {
489+
r.logger.Warn("Failed to read tool approvals for the admission gate",
490+
zap.String("server", serverName), zap.Error(err))
491+
return false, false
492+
}
493+
for _, rec := range records {
494+
if rec == nil {
495+
continue
496+
}
497+
if rec.Status == storage.ToolApprovalStatusApproved || rec.ApprovedHash != "" {
498+
return true, true
499+
}
500+
}
501+
return false, true
502+
}

‎internal/runtime/config_load_admission_gate_dedupe_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
1111
"github.com/smart-mcp-proxy/mcpproxy-go/internal/runtime/configsvc"
12+
"github.com/smart-mcp-proxy/mcpproxy-go/internal/storage"
1213
)
1314

1415
// Item 4a: on an upgrade start the pre-fix advisory must be emitted once per
@@ -28,6 +29,7 @@ func TestConfigLoadAdmissionGate_PreFixAdvisoryOncePerProcess(t *testing.T) {
2829
require.NoError(t, rt.storageManager.SaveUpstreamServer(&config.ServerConfig{
2930
Name: name, Command: "./x", Protocol: "stdio", Enabled: true,
3031
}))
32+
seedApproval(t, rt, name, "read_graph", storage.ToolApprovalStatusApproved, "h1")
3133
}
3234

3335
// Production startup order (lifecycle.go): hook + initial gate, then

‎internal/runtime/config_load_admission_gate_review_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.uber.org/zap/zaptest/observer"
1313

1414
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
15+
"github.com/smart-mcp-proxy/mcpproxy-go/internal/storage"
1516
)
1617

1718
// Cross-model review findings on the #937 admission gate.
@@ -157,6 +158,8 @@ func TestConfigLoadAdmissionGate_WarnsAboutServersAdmittedBeforeTheFix(t *testin
157158
require.NoError(t, rt.storageManager.SaveUpstreamServer(&config.ServerConfig{
158159
Name: name, Command: "./x", Protocol: "stdio", Enabled: true, Quarantined: false,
159160
}))
161+
// Live under the old release, so it has an approval baseline.
162+
seedApproval(t, rt, name, "read_graph", storage.ToolApprovalStatusApproved, "h1")
160163
}
161164

162165
require.NoError(t, rt.LoadConfiguredServers(cfg))

‎internal/runtime/config_load_admission_gate_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"go.uber.org/zap"
1212

1313
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
14+
"github.com/smart-mcp-proxy/mcpproxy-go/internal/storage"
1415
)
1516

1617
// Issue #937 — the trust-mode admission gate must apply on the CONFIG-LOAD path,
@@ -149,6 +150,9 @@ func TestConfigLoadAdmissionGate_KnownServerIsNotRequarantined(t *testing.T) {
149150
require.NoError(t, rt.storageManager.SaveUpstreamServer(&config.ServerConfig{
150151
Name: "longstanding", Command: "./ok", Protocol: "stdio", Enabled: true, Quarantined: false,
151152
}))
153+
// A server that really ran has an approval baseline (RC-UPG-001: without
154+
// one, "known" is not proof of admission).
155+
seedApproval(t, rt, "longstanding", "read_graph", storage.ToolApprovalStatusApproved, "h1")
152156

153157
require.NoError(t, rt.LoadConfiguredServers(cfg))
154158

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)