@chroxy/server 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/CONFIG.md +411 -14
  2. package/hooks/permission-hook.sh +253 -3
  3. package/package.json +8 -15
  4. package/sidecar/node_modules/.package-lock.json +29 -0
  5. package/sidecar/node_modules/ws/LICENSE +20 -0
  6. package/sidecar/node_modules/ws/README.md +548 -0
  7. package/sidecar/node_modules/ws/browser.js +8 -0
  8. package/sidecar/node_modules/ws/index.js +22 -0
  9. package/sidecar/node_modules/ws/lib/buffer-util.js +131 -0
  10. package/sidecar/node_modules/ws/lib/constants.js +19 -0
  11. package/sidecar/node_modules/ws/lib/event-target.js +292 -0
  12. package/sidecar/node_modules/ws/lib/extension.js +203 -0
  13. package/sidecar/node_modules/ws/lib/limiter.js +55 -0
  14. package/sidecar/node_modules/ws/lib/permessage-deflate.js +528 -0
  15. package/sidecar/node_modules/ws/lib/receiver.js +706 -0
  16. package/sidecar/node_modules/ws/lib/sender.js +602 -0
  17. package/sidecar/node_modules/ws/lib/stream.js +161 -0
  18. package/sidecar/node_modules/ws/lib/subprotocol.js +62 -0
  19. package/sidecar/node_modules/ws/lib/validation.js +152 -0
  20. package/sidecar/node_modules/ws/lib/websocket-server.js +554 -0
  21. package/sidecar/node_modules/ws/lib/websocket.js +1393 -0
  22. package/sidecar/node_modules/ws/package.json +70 -0
  23. package/sidecar/node_modules/ws/wrapper.mjs +21 -0
  24. package/src/base-session.js +151 -6
  25. package/src/billing-budget.js +5 -1
  26. package/src/binary-provenance-trust.js +95 -0
  27. package/src/built-in-tools/file-ops.js +5 -4
  28. package/src/byok-mcp-client.js +1061 -13
  29. package/src/byok-mcp-config.js +1201 -4
  30. package/src/byok-mcp-fleet.js +374 -29
  31. package/src/byok-mcp-oauth-store.js +327 -0
  32. package/src/byok-mcp-oauth.js +635 -0
  33. package/src/byok-mcp-trust.js +254 -37
  34. package/src/byok-session.js +789 -13
  35. package/src/byok-tool-executor.js +20 -13
  36. package/src/checkpoint-manager.js +33 -6
  37. package/src/chroxy-host-metadata.js +122 -0
  38. package/src/claude-stream-parser.js +56 -0
  39. package/src/claude-tui/pty-driver.js +54 -14
  40. package/src/claude-tui-session.js +106 -7
  41. package/src/cli/config-cmd.js +2 -2
  42. package/src/cli/deploy-cmd.js +1 -1
  43. package/src/cli/init-cmd.js +1 -1
  44. package/src/cli/providers-cmd.js +3 -3
  45. package/src/cli/schedule-cmd.js +775 -0
  46. package/src/cli/server-cmd.js +16 -1
  47. package/src/cli/service-cmd.js +39 -19
  48. package/src/cli/shared.js +7 -4
  49. package/src/cli/tokens-cmd.js +213 -0
  50. package/src/cli/tunnel-cmd.js +1 -1
  51. package/src/cli-session.js +66 -10
  52. package/src/cli.js +4 -0
  53. package/src/codex-app-server-session.js +671 -39
  54. package/src/codex-session.js +121 -18
  55. package/src/config.js +453 -27
  56. package/src/control-room/integrations.js +16 -10
  57. package/src/control-room/skills-inventory.js +6 -1
  58. package/src/control-room/survey.js +32 -0
  59. package/src/credential-store.js +256 -10
  60. package/src/dashboard-next/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  61. package/src/diagnostics.js +3 -0
  62. package/src/docker-sdk-session.js +16 -1
  63. package/src/docker-session.js +9 -0
  64. package/src/doctor.js +19 -8
  65. package/src/environments/backends/docker.js +8 -0
  66. package/src/environments/backends/k8s.js +9 -0
  67. package/src/event-ingest.js +19 -1
  68. package/src/event-normalizer.js +354 -19
  69. package/src/external-session-registry.js +0 -0
  70. package/src/gemini-session.js +19 -2
  71. package/src/github-webhook.js +188 -4
  72. package/src/handler-utils.js +42 -8
  73. package/src/handlers/checkpoint-handlers.js +126 -19
  74. package/src/handlers/control-room-handlers.js +48 -18
  75. package/src/handlers/conversation-handlers.js +127 -2
  76. package/src/handlers/credential-handlers.js +43 -2
  77. package/src/handlers/file-handlers.js +47 -3
  78. package/src/handlers/github-webhook-handlers.js +204 -0
  79. package/src/handlers/input-handlers.js +4 -0
  80. package/src/handlers/orchestration-handlers.js +238 -0
  81. package/src/handlers/scheduler-handlers.js +510 -0
  82. package/src/handlers/session-handlers.js +10 -2
  83. package/src/handlers/settings-handlers.js +510 -11
  84. package/src/handlers/token-handlers.js +11 -2
  85. package/src/http-routes.js +159 -0
  86. package/src/jsonl-reader.js +60 -6
  87. package/src/jsonl-subprocess-session.js +35 -6
  88. package/src/keychain.js +159 -12
  89. package/src/logger.js +182 -19
  90. package/src/notification-prefs.js +88 -6
  91. package/src/notifications/discord-webhook-sink.js +30 -1
  92. package/src/orchestration/build-manager.js +56 -0
  93. package/src/orchestration/decision-contract.js +245 -0
  94. package/src/orchestration/git-ops.js +347 -0
  95. package/src/orchestration/orchestration-manager.js +1295 -0
  96. package/src/orchestration/permission-gate.js +109 -0
  97. package/src/orchestration/role-prompts.js +136 -0
  98. package/src/orchestration/run-budget.js +81 -0
  99. package/src/orchestration/run-ledger.js +487 -0
  100. package/src/orchestration/run-model.js +205 -0
  101. package/src/orchestration/run-record.js +336 -0
  102. package/src/orchestration/run-report.js +194 -0
  103. package/src/orchestration/to-wire.js +201 -0
  104. package/src/orchestration/turn-driver.js +398 -0
  105. package/src/pairing.js +146 -1
  106. package/src/permission-audit.js +79 -3
  107. package/src/permission-floor.js +455 -0
  108. package/src/permission-manager.js +492 -40
  109. package/src/permission-resolver.js +55 -7
  110. package/src/permission-rule-store.js +411 -0
  111. package/src/platform.js +367 -23
  112. package/src/providers.js +21 -2
  113. package/src/schedule-parser.js +311 -0
  114. package/src/scheduled-task-store.js +653 -0
  115. package/src/scheduler.js +1029 -0
  116. package/src/sdk-session.js +338 -5
  117. package/src/server-cli/server-orchestrator.js +23 -0
  118. package/src/server-cli/tunnel-lifecycle-handler.js +3 -2
  119. package/src/server-cli.js +238 -26
  120. package/src/server-identity.js +28 -4
  121. package/src/service.js +291 -33
  122. package/src/session-manager.js +417 -25
  123. package/src/session-message-history.js +27 -21
  124. package/src/session-title.js +168 -0
  125. package/src/session-token-store.js +60 -35
  126. package/src/skills-usage.js +20 -2
  127. package/src/statusline.js +584 -0
  128. package/src/subagent-counter.js +9 -1
  129. package/src/supervisor.js +77 -27
  130. package/src/token-manager.js +39 -9
  131. package/src/transcript-tasks.js +41 -6
  132. package/src/tunnel/cloudflare.js +115 -8
  133. package/src/tunnel/index.js +4 -1
  134. package/src/tunnel-check.js +1 -1
  135. package/src/turn-tracker.js +0 -0
  136. package/src/usage-normalize.js +118 -0
  137. package/src/user-shell-session.js +98 -32
  138. package/src/utils/componentwise-resolver.js +205 -0
  139. package/src/utils/composite-key.js +45 -0
  140. package/src/utils/iso-datetime.js +51 -0
  141. package/src/utils/preflight.js +140 -28
  142. package/src/utils/spawn-env.js +8 -2
  143. package/src/utils/verify-binary.js +276 -0
  144. package/src/utils/verify-provenance.js +241 -0
  145. package/src/utils/wire-counters.js +48 -0
  146. package/src/worktree-gc.js +4 -1
  147. package/src/ws-file-ops/browser.js +42 -11
  148. package/src/ws-file-ops/common.js +68 -1
  149. package/src/ws-file-ops/git.js +221 -3
  150. package/src/ws-file-ops/index.js +5 -0
  151. package/src/ws-file-ops/memory.js +358 -0
  152. package/src/ws-file-ops/reader.js +124 -0
  153. package/src/ws-forwarding.js +44 -16
  154. package/src/ws-handler-context.js +19 -0
  155. package/src/ws-history.js +84 -11
  156. package/src/ws-message-handlers.js +6 -0
  157. package/src/ws-outbound-schemas.js +207 -0
  158. package/src/ws-permissions.js +285 -4
  159. package/src/ws-schemas.js +1 -0
  160. package/src/ws-server.js +301 -17
@@ -1,11 +1,58 @@
1
1
  /**
2
2
  * MCP server spawn trust store (#4457).
3
3
  *
4
- * Persists per-tuple trust decisions so a user is asked exactly ONCE per
5
- * (server_name, command, args[0]) tuple — not on every chroxy restart. The
6
- * tuple deliberately excludes args[1..N] so a legitimate version bump
7
- * (e.g. args[1] = "mcp-github@1.4.2" → "mcp-github@1.5.0") doesn't re-prompt,
8
- * but a binary swap (`command: rm -rf` instead of `node`) DOES.
4
+ * Persists per-config trust decisions so a user is asked exactly ONCE per
5
+ * spawn-determining configuration — not on every chroxy restart.
6
+ *
7
+ * ## The key covers EVERYTHING that determines what executes (#7001)
8
+ *
9
+ * Up to v1 the key was the partial tuple `(name, command, args[0])` for stdio
10
+ * and `(name, sanitized-url)` for remote. That was unsound: `args[1..N]` and
11
+ * `env` are exactly as spawn-determining as `command`, so all three of
12
+ *
13
+ * { command: 'npx', args: ['-y', '@modelcontextprotocol/server-github'] }
14
+ * { command: 'npx', args: ['-y', '@attacker/pwn'] }
15
+ * { command: 'npx', args: ['-y', '…'], env: { NODE_OPTIONS: '--require /tmp/evil.js' } }
16
+ *
17
+ * collapsed onto the SAME key `["github","npx","-y"]`. Once any one of them was
18
+ * trusted, `isTrusted()` returned true for the other two, so a remove + re-add
19
+ * of the same name could swap in arbitrary host code with no trust prompt at all
20
+ * — and re-spawn it silently on every later session start. `_buildChildEnv()` is
21
+ * `{ ...process.env, ...config.env }`, so an injected `NODE_OPTIONS` alone was
22
+ * enough; it did not even need to change `command`.
23
+ *
24
+ * v2 therefore keys on a sha256 of a CANONICAL (key-sorted) JSON encoding of the
25
+ * full spawn-determining config — a digest rather than another hand-picked tuple,
26
+ * so a future spawn-determining field cannot be forgotten the way `env` was:
27
+ *
28
+ * - stdio: `{ v, kind: 'stdio', name, command, argsDigest, envDigest }`
29
+ * - remote: `{ v, kind: 'remote', name, url, urlDigest, headersDigest }`
30
+ *
31
+ * `args` / `env` / `headers` and the raw url enter the key as DIGESTS, never in
32
+ * cleartext, for one reason: the components are persisted (see below) and those
33
+ * fields routinely carry credentials (`--api-key=…`, `GITHUB_TOKEN`,
34
+ * `Authorization`, `?token=`). Digesting them keeps the key exhaustive while the
35
+ * store keeps its "no credential material ever reaches disk" property.
36
+ *
37
+ * Two deliberate consequences:
38
+ *
39
+ * 1. **Version bumps now re-prompt.** The pre-v2 rationale for excluding
40
+ * `args[1..N]` was that `mcp-github@1.4.2` → `@1.5.0` shouldn't re-ask. But
41
+ * "the package changed" is indistinguishable from "the package was swapped",
42
+ * and one prompt per genuine config edit is the correct price for closing an
43
+ * RCE path.
44
+ * 2. **A rotated credential inside a remote url re-prompts** — reversing the
45
+ * #6821 note that a changed query param would not. A query param can change
46
+ * what the endpoint does, and the SANITIZED url is published verbatim on the
47
+ * `mcp_servers` broadcast (`redactMcpUrl`), so a sanitized-url-only key was
48
+ * publicly known and freely inheritable by a different effective config.
49
+ *
50
+ * MIGRATION: v1 entries are DROPPED at load, not honoured (`loadTrustStore`
51
+ * requires `v === 2` and recomputes the key from the persisted components). There
52
+ * is deliberately NO compatibility shim — keeping the weak key alive for existing
53
+ * records would keep the collision exploitable. Previously-trusted servers
54
+ * re-prompt exactly once and are rewritten as v2 on the next allow. Re-prompting
55
+ * is the fail-safe direction; silently honouring a weak key is not.
9
56
  *
10
57
  * Storage: ~/.chroxy/mcp-trust.json (mode 0600). Atomic writes via temp +
11
58
  * rename so a crashed `chroxy doctor` mid-prompt can't corrupt the file.
@@ -19,15 +66,31 @@
19
66
  * time (UX) AND the load+write critical section is serialised across all
20
67
  * gates in the same process.
21
68
  *
22
- * v1 has no "session-only" tier — once trusted, trusted forever for that
23
- * tuple. Users who need to revoke can edit the JSON directly. A future
69
+ * There is no "session-only" tier — once trusted, trusted forever for that
70
+ * exact config. Users who need to revoke can edit the JSON directly. A future
24
71
  * iteration can add expiry timestamps without breaking the file format.
25
72
  */
26
73
 
74
+ import { createHash } from 'node:crypto'
27
75
  import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync, chmodSync, unlinkSync } from 'node:fs'
28
76
  import { homedir } from 'node:os'
29
77
  import { join, dirname } from 'node:path'
30
78
 
79
+ /**
80
+ * On-disk trust-record version. Bumped to 2 by #7001 (partial tuple → full
81
+ * spawn-config digest). `loadTrustStore` honours ONLY this version, so bumping it
82
+ * again is the supported way to force a re-prompt if the key composition ever
83
+ * has to change once more.
84
+ */
85
+ export const TRUST_KEY_VERSION = 2
86
+
87
+ /**
88
+ * Sentinel for an absent/empty args list or string map. Deliberately not a valid
89
+ * sha256 hex string (64 hex chars), so "no env" can never be confused with the
90
+ * digest of some env, and the on-disk record stays readable.
91
+ */
92
+ const EMPTY_DIGEST = 'none'
93
+
31
94
  // Per-path tail of an in-process async mutex chain (#4460). Each
32
95
  // withTrustStoreLock call awaits the current tail, then publishes a new
33
96
  // tail (its own settle-promise). When a section finishes, if its
@@ -72,25 +135,168 @@ export function defaultTrustStorePath() {
72
135
  }
73
136
 
74
137
  /**
75
- * Build the canonical tuple key. server.args may be undefined or empty —
76
- * we still produce a key (args[0] is the empty string) so the consent flow
77
- * works for shell-built-in-style configs like { command: 'true' }.
78
- *
79
- * #4461: serialise via JSON.stringify so component values containing the
80
- * separator (spaces, NUL bytes, brackets, quotes) cannot collide. The
81
- * encoded form is also readable on disk under cat ~/.chroxy/mcp-trust.json,
82
- * which makes hand-debugging trust entries practical. The pre-#4461 NUL
83
- * separator (\\0-delimited triplet) is structurally vulnerable to a
84
- * tampered on-disk entry that re-uses a NUL inside name/command — JSON-
85
- * stringified tuples are unambiguous by construction (quoted strings escape
86
- * every embedded quote and control char), so no separator hack is needed.
138
+ * Return a credential-stripped, stable form of a remote MCP server url for
139
+ * use as a trust key (#6821). Strips userinfo (`user:pass@`), query, and
140
+ * fragment — none of those belong on disk, and stripping them means a
141
+ * rotated token or a changed query param does not re-prompt for the same
142
+ * endpoint. An unparseable url maps to a fixed placeholder — NEVER the raw
143
+ * input: recordTrust persists this value to disk, and a malformed url can
144
+ * still embed credential material (`user:pass@`, `?token=`). Collapsing all
145
+ * unparseable urls for a server name onto one key is fail-safe, because an
146
+ * unparseable url can never be connected to anyway (config parsing rejects
147
+ * it and fetch would throw).
87
148
  */
88
- export function trustTupleKey(server) {
149
+ function sanitizeTrustUrl(url) {
150
+ if (typeof url !== 'string' || url.length === 0) return ''
151
+ try {
152
+ const u = new URL(url)
153
+ u.username = ''
154
+ u.password = ''
155
+ u.search = ''
156
+ u.hash = ''
157
+ return u.toString()
158
+ } catch {
159
+ return 'invalid-url'
160
+ }
161
+ }
162
+
163
+ function sha256Hex(input) {
164
+ return createHash('sha256').update(input, 'utf8').digest('hex')
165
+ }
166
+
167
+ /**
168
+ * Deterministic JSON encoding: object keys sorted, array order preserved (arg
169
+ * ORDER is semantic — `['-e', 'code']` is not `['code', '-e']`).
170
+ *
171
+ * #4461's property is preserved for free: every string is JSON-quoted, so a
172
+ * component value containing a separator, quote, NUL or newline cannot be made to
173
+ * straddle a field boundary and collide with a different component split.
174
+ */
175
+ function canonicalJson(value) {
176
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]`
177
+ if (value !== null && typeof value === 'object') {
178
+ return `{${Object.keys(value)
179
+ .sort()
180
+ .map((k) => `${JSON.stringify(k)}:${canonicalJson(value[k])}`)
181
+ .join(',')}}`
182
+ }
183
+ return JSON.stringify(value === undefined ? null : value)
184
+ }
185
+
186
+ /**
187
+ * Digest of a spawn argv list. Order-sensitive, covers EVERY element (the v1 key
188
+ * covered only `args[0]`, which is what made `['-y', '@attacker/pwn']` collide
189
+ * with `['-y', '@modelcontextprotocol/server-github']`).
190
+ */
191
+ export function argsDigest(args) {
192
+ const list = Array.isArray(args) ? args.map((a) => String(a)) : []
193
+ if (list.length === 0) return EMPTY_DIGEST
194
+ return sha256Hex(canonicalJson(list))
195
+ }
196
+
197
+ /**
198
+ * Digest of a string→string map (`env` or `headers`), canonically ordered so key
199
+ * insertion order cannot change the key. Values are hashed, never persisted —
200
+ * `env` and `headers` routinely carry API tokens.
201
+ *
202
+ * A `__proto__` key present as an OWN property (JSON.parse produces one) is
203
+ * covered like any other: `Object.entries` sees it, so it enters the digest and
204
+ * cannot be used to smuggle an uncovered difference past the key.
205
+ */
206
+ export function stringMapDigest(map) {
207
+ if (!map || typeof map !== 'object' || Array.isArray(map)) return EMPTY_DIGEST
208
+ const entries = Object.entries(map)
209
+ .map(([k, v]) => [String(k), String(v)])
210
+ .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0))
211
+ if (entries.length === 0) return EMPTY_DIGEST
212
+ return sha256Hex(canonicalJson(entries))
213
+ }
214
+
215
+ /**
216
+ * The canonical component bag a trust key hashes over — and, verbatim, the record
217
+ * `recordTrust` persists. Keeping "what is hashed" and "what is stored" the SAME
218
+ * object is what lets `loadTrustStore` recompute the key from disk (the #4461
219
+ * tamper check) without ever storing a credential in cleartext.
220
+ *
221
+ * Two shapes (#6821), discriminated by an explicit `kind` so they can never
222
+ * alias:
223
+ * - remote: `{ v, kind: 'remote', name, url, urlDigest, headersDigest }` —
224
+ * `url` is the credential-stripped display form; `urlDigest` covers the FULL
225
+ * raw url (userinfo, query, fragment) so a changed query param is a different
226
+ * endpoint configuration.
227
+ * - stdio: `{ v, kind: 'stdio', name, command, argsDigest, envDigest }` —
228
+ * `command` stays cleartext (it is the field the consent prompt shows, and a
229
+ * binary path is not credential material); the full argv and env are covered
230
+ * by digests.
231
+ */
232
+ export function trustKeyComponents(server) {
89
233
  if (!server || typeof server !== 'object') throw new Error('trustTupleKey: missing server')
90
234
  const name = String(server.name || '')
91
- const command = String(server.command || '')
92
- const arg0 = Array.isArray(server.args) && server.args.length > 0 ? String(server.args[0]) : ''
93
- return JSON.stringify([name, command, arg0])
235
+ const rawUrl = typeof server.url === 'string' ? server.url : ''
236
+ if (rawUrl) {
237
+ return {
238
+ v: TRUST_KEY_VERSION,
239
+ kind: 'remote',
240
+ name,
241
+ url: sanitizeTrustUrl(rawUrl),
242
+ urlDigest: sha256Hex(rawUrl),
243
+ headersDigest: stringMapDigest(server.headers),
244
+ }
245
+ }
246
+ return {
247
+ v: TRUST_KEY_VERSION,
248
+ kind: 'stdio',
249
+ name,
250
+ command: String(server.command || ''),
251
+ argsDigest: argsDigest(server.args),
252
+ envDigest: stringMapDigest(server.env),
253
+ }
254
+ }
255
+
256
+ /**
257
+ * The trust key for a server config: sha256 over the canonical component bag.
258
+ *
259
+ * Named `trustTupleKey` for continuity with #4457/#4461 call sites, but since
260
+ * #7001 it is a DIGEST of the whole spawn-determining config, not a tuple of
261
+ * hand-picked fields. Two configs share a key iff they would spawn/connect
262
+ * identically.
263
+ */
264
+ export function trustTupleKey(server) {
265
+ return sha256Hex(canonicalJson(trustKeyComponents(server)))
266
+ }
267
+
268
+ /**
269
+ * Rebuild the component bag from a persisted record, reading EXACTLY the fields
270
+ * the bag defines (never a spread of the raw entry — an attacker-added extra key
271
+ * must not be able to participate in the recompute). Returns null for anything
272
+ * that is not a current-version record, which includes every v1 entry: those
273
+ * carry `args0` and no `v`, so they are dropped rather than honoured.
274
+ */
275
+ function componentsFromEntry(entry) {
276
+ if (!entry || typeof entry !== 'object') return null
277
+ if (entry.v !== TRUST_KEY_VERSION) return null
278
+ const str = (v) => (typeof v === 'string' ? v : '')
279
+ if (entry.kind === 'remote') {
280
+ return {
281
+ v: TRUST_KEY_VERSION,
282
+ kind: 'remote',
283
+ name: str(entry.name),
284
+ url: str(entry.url),
285
+ urlDigest: str(entry.urlDigest),
286
+ headersDigest: str(entry.headersDigest),
287
+ }
288
+ }
289
+ if (entry.kind === 'stdio') {
290
+ return {
291
+ v: TRUST_KEY_VERSION,
292
+ kind: 'stdio',
293
+ name: str(entry.name),
294
+ command: str(entry.command),
295
+ argsDigest: str(entry.argsDigest),
296
+ envDigest: str(entry.envDigest),
297
+ }
298
+ }
299
+ return null
94
300
  }
95
301
 
96
302
  /**
@@ -104,20 +310,26 @@ export function loadTrustStore(filePath = defaultTrustStorePath(), { log } = {})
104
310
  const arr = Array.isArray(raw?.trustedTuples) ? raw.trustedTuples : []
105
311
  const tuples = new Set()
106
312
  const entries = []
313
+ let legacyDropped = 0
107
314
  for (const t of arr) {
108
315
  if (!t || typeof t.key !== 'string') continue
316
+ // #7001: a pre-v2 record was computed with the weak (name, command,
317
+ // args[0]) / (name, sanitized-url) key, which collided across configs that
318
+ // spawn different code. It is DROPPED, never translated — honouring it
319
+ // would keep that collision exploitable. The user re-prompts once and the
320
+ // record is rewritten as v2 on the next allow.
321
+ const components = componentsFromEntry(t)
322
+ if (!components) {
323
+ legacyDropped += 1
324
+ continue
325
+ }
109
326
  // #4461: recompute the canonical key from the recorded components and
110
327
  // drop any entry whose stored `key` doesn't match. A tampered entry
111
328
  // (e.g. stored key kept but command swapped to something dangerous)
112
329
  // yields a mismatch and is silently dropped — the user re-prompts
113
- // for that tuple on next start. Pre-tamper trust is forfeit; correct
330
+ // for that config on next start. Pre-tamper trust is forfeit; correct
114
331
  // behaviour given the integrity claim is broken.
115
- const args0 = typeof t.args0 === 'string' ? t.args0 : ''
116
- const recomputed = trustTupleKey({
117
- name: typeof t.name === 'string' ? t.name : '',
118
- command: typeof t.command === 'string' ? t.command : '',
119
- args: [args0],
120
- })
332
+ const recomputed = sha256Hex(canonicalJson(components))
121
333
  if (recomputed !== t.key) {
122
334
  log?.warn?.(`MCP trust store ${filePath} entry "${t.name}" key tampered (recomputed mismatch); dropping`)
123
335
  continue
@@ -125,6 +337,13 @@ export function loadTrustStore(filePath = defaultTrustStorePath(), { log } = {})
125
337
  tuples.add(t.key)
126
338
  entries.push(t)
127
339
  }
340
+ if (legacyDropped > 0) {
341
+ log?.warn?.(
342
+ `MCP trust store ${filePath}: dropped ${legacyDropped} pre-v${TRUST_KEY_VERSION} trust record(s) ` +
343
+ '— they were keyed on a partial config (command + args[0] only) and are no longer honoured (#7001); ' +
344
+ 'affected MCP servers will ask for trust once more',
345
+ )
346
+ }
128
347
  return { tuples, path: filePath, entries }
129
348
  } catch (err) {
130
349
  log?.warn?.(`MCP trust store ${filePath} unreadable: ${err?.message || err}`)
@@ -142,13 +361,11 @@ export function recordTrust(server, filePath = defaultTrustStorePath()) {
142
361
  const existing = loadTrustStore(filePath)
143
362
  if (existing.tuples.has(key)) return existing
144
363
  const entries = Array.isArray(existing.entries) ? [...existing.entries] : []
145
- entries.push({
146
- key,
147
- name: server.name,
148
- command: server.command,
149
- args0: Array.isArray(server.args) && server.args.length > 0 ? server.args[0] : '',
150
- trustedAt: new Date().toISOString(),
151
- })
364
+ // #7001: persist the component bag the key was hashed over, verbatim, so
365
+ // loadTrustStore can recompute and verify it. Only digests of args/env/headers
366
+ // and of the raw url are written — no argv element, env value, header value or
367
+ // url credential ever reaches disk.
368
+ entries.push({ ...trustKeyComponents(server), key, trustedAt: new Date().toISOString() })
152
369
  const dir = dirname(filePath)
153
370
  if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: 0o700 })
154
371
  const tmp = `${filePath}.tmp`