Commit daaaceb
authored
fix: [AI-656] single-pass env-var resolution for MCP configs (#697)
Follow-up to #666. The two-layer design (parse-time `ConfigPaths.parseText` in
`readJsonSafe` + launch-time `resolveEnvVars` in `mcp/index.ts`) created two
correctness bugs:
1. `$${VAR}` escape broken end-to-end: Layer 1 turned `$${VAR}` into literal
`${VAR}`; Layer 2 then re-resolved it to the live env value. No syntax
remained for passing a literal `${SOMETHING}` to an MCP server.
2. Variable-chain injection: with `EVIL_VAR="${SECRET}"` in the shell and a
config referencing `${EVIL_VAR}`, Layer 1 produced the literal `"${SECRET}"`
which Layer 2 then resolved to the actual secret — exfiltrating a variable
the config never mentioned.
The fix collapses to a single resolution point at config load time, scoped to
the fields that need it.
Changes:
- `config/paths.ts`: export shared `ENV_VAR_PATTERN`, `resolveEnvVarsInString()`,
and `newEnvSubstitutionStats()`. Internal `substitute()` reuses the same
regex grammar — no more duplicate implementation in `mcp/index.ts`.
- `mcp/discover.ts`: revert the `ConfigPaths.parseText` call in `readJsonSafe`
(was substituting across the whole JSON text, not just env blocks). Add
`resolveServerEnvVars()` helper called from `transform()`, scoped to `env`
and `headers` values only. Emits `log.warn` for unresolved vars with server
and source context.
- `mcp/index.ts`: remove `resolveEnvVars()` and `ENV_VAR_PATTERN` entirely. The
stdio launch site now uses `mcp.environment` directly — single resolution
point already ran at config load time.
- `test/mcp/env-var-interpolation.test.ts`: switch to
`ConfigPaths.resolveEnvVarsInString`. Add 5 regression tests: single-pass
no-chain; `$${VAR}` survives end-to-end; chain injection blocked; `${VAR}`
resolved in remote server `headers`; `${VAR}` in command args / URL is NOT
resolved (scope check). Also fixes fragile `process.env = {...}` teardown.
Addresses the PR #666 consensus code review:
- Critical: double interpolation (resolution collapsed to one pass)
- Major: regex duplication (single source in `config/paths.ts`)
- Major: silent empty-string on unresolved vars (now `log.warn` with context)
- Major: whole-JSON-text scope (narrowed to `env` and `headers` only)
- Major: swallowed `catch {}` in `readJsonSafe` (removed with the whole block)
- Minor: `resolveEnvVars` top-level export (removed)
- Minor: `mcp.headers` not resolved (now resolved)
Test results: 36/36 tests pass in `test/mcp/env-var-interpolation.test.ts` +
`test/mcp/discover.test.ts`. Marker guard clean. No new TS errors in touched
files.
Relates to #656 and #666.1 parent 350001d commit daaaceb
File tree
4 files changed
+363
-141
lines changed- packages/opencode
- src
- config
- mcp
- test/mcp
4 files changed
+363
-141
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
87 | 168 | | |
88 | 169 | | |
89 | 170 | | |
90 | 171 | | |
91 | 172 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
121 | 191 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
128 | 202 | | |
129 | | - | |
130 | | - | |
131 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
132 | 208 | | |
133 | 209 | | |
134 | 210 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
10 | 41 | | |
11 | 42 | | |
12 | 43 | | |
| |||
32 | 63 | | |
33 | 64 | | |
34 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
35 | 69 | | |
36 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
37 | 76 | | |
38 | 77 | | |
39 | 78 | | |
40 | 79 | | |
41 | 80 | | |
42 | 81 | | |
43 | 82 | | |
44 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
45 | 89 | | |
46 | 90 | | |
47 | 91 | | |
| |||
63 | 107 | | |
64 | 108 | | |
65 | 109 | | |
66 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
67 | 116 | | |
68 | 117 | | |
69 | 118 | | |
| |||
93 | 142 | | |
94 | 143 | | |
95 | 144 | | |
96 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
97 | 149 | | |
98 | 150 | | |
99 | 151 | | |
| |||
117 | 169 | | |
118 | 170 | | |
119 | 171 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | 172 | | |
133 | 173 | | |
134 | 174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | 28 | | |
52 | 29 | | |
53 | 30 | | |
| |||
532 | 509 | | |
533 | 510 | | |
534 | 511 | | |
535 | | - | |
536 | | - | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
537 | 518 | | |
538 | 519 | | |
539 | 520 | | |
| |||
0 commit comments