Skip to content

Commit 7084db9

Browse files
committed
fix: enforce allowlist-only MCP runtime smoke in closure tests
Close the A6 acceptance gap by probing MCP initialize/tools against a temp package-shaped tree that only contains mcp, hooks/_runtime, and the derived runtime closure. Also collect path.join(__dirname, scripts/lib) deps so dynamic ConfirmBinding loads of cp-digest stay in the deploy allowlist.
1 parent d65c666 commit 7084db9

2 files changed

Lines changed: 71 additions & 20 deletions

File tree

‎scripts/lib/runtime-dependency-closure.js‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ function collectRequireRequests(source) {
5454
return requests
5555
}
5656

57+
/**
58+
* Capture path.join(__dirname, '..', '..', 'scripts', 'lib', 'file.js') style
59+
* runtime deps used when require() is dynamic after existsSync.
60+
* Returns portable package-relative paths under scripts/lib.
61+
*/
62+
function collectPathJoinScriptLibDeps(source) {
63+
const deps = []
64+
const patterns = [
65+
/path\.join\(\s*__dirname\s*,\s*(?:['"]\.\.['"]\s*,\s*){2}['"]scripts['"]\s*,\s*['"]lib['"]\s*,\s*['"]([^'"]+)['"]\s*\)/g,
66+
/path\.join\(\s*__dirname\s*,\s*['"]\.\.\/\.\.\/scripts\/lib\/([^'"]+)['"]\s*\)/g
67+
]
68+
for (const re of patterns) {
69+
let match
70+
while ((match = re.exec(source)) !== null) {
71+
const name = String(match[1] || '').replace(/\\/g, '/')
72+
if (!name) continue
73+
deps.push(portable(path.posix.join('scripts/lib', name)))
74+
}
75+
}
76+
return deps
77+
}
78+
79+
function collectEdgesFromSource(packageRoot, fromRelative, source, fsImpl) {
80+
const edges = []
81+
for (const request of collectRequireRequests(source)) {
82+
const resolved = resolveRelativeModule(packageRoot, fromRelative, request, fsImpl)
83+
if (resolved) edges.push(resolved)
84+
}
85+
for (const dep of collectPathJoinScriptLibDeps(source)) {
86+
if (fsImpl.existsSync(path.join(packageRoot, ...dep.split('/')))) edges.push(dep)
87+
}
88+
return edges
89+
}
90+
5791
function collectRuntimeScriptDeps(packageRoot, options = {}) {
5892
const fsImpl = options.fs || fs
5993
const roots = Array.isArray(options.roots) && options.roots.length
@@ -74,18 +108,16 @@ function collectRuntimeScriptDeps(packageRoot, options = {}) {
74108

75109
for (const entry of entryFiles) {
76110
const source = readPackageText(packageRoot, entry, fsImpl)
77-
for (const request of collectRequireRequests(source)) {
78-
const resolved = resolveRelativeModule(packageRoot, entry, request, fsImpl)
79-
if (resolved) enqueue(resolved)
111+
for (const edge of collectEdgesFromSource(packageRoot, entry, source, fsImpl)) {
112+
enqueue(edge)
80113
}
81114
}
82115

83116
for (let index = 0; index < queue.length; index += 1) {
84117
const current = queue[index]
85118
const source = readPackageText(packageRoot, current, fsImpl)
86-
for (const request of collectRequireRequests(source)) {
87-
const resolved = resolveRelativeModule(packageRoot, current, request, fsImpl)
88-
if (resolved) enqueue(resolved)
119+
for (const edge of collectEdgesFromSource(packageRoot, current, source, fsImpl)) {
120+
enqueue(edge)
89121
}
90122
}
91123

@@ -114,6 +146,8 @@ function assertRuntimeClosureCovered(packageRoot, allowlist, options = {}) {
114146

115147
module.exports = {
116148
DEFAULT_RUNTIME_ROOTS,
149+
collectRequireRequests,
150+
collectPathJoinScriptLibDeps,
117151
collectRuntimeScriptDeps,
118152
runtimeClosureCoverage,
119153
assertRuntimeClosureCovered

‎scripts/test-mcp-runtime-closure.js‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,44 @@ function fakeRuntimeSeed() {
132132
['scripts/lib/fake-runtime-only.js'],
133133
'new runtime-only require must enter closure'
134134
)
135+
136+
// Dynamic path.join(__dirname, '..', '..', 'scripts', 'lib', ...) must also enter closure.
137+
const tmpJoin = fs.mkdtempSync(path.join(os.tmpdir(), 'runtime-closure-join-'))
138+
fs.mkdirSync(path.join(tmpJoin, 'hooks', '_runtime'), { recursive: true })
139+
fs.mkdirSync(path.join(tmpJoin, 'scripts', 'lib'), { recursive: true })
140+
fs.writeFileSync(
141+
path.join(tmpJoin, 'hooks', '_runtime', 'join-fixture.cjs'),
142+
"const p = path.join(__dirname, '..', '..', 'scripts', 'lib', 'join-only-dep.js')\nrequire(p)\n"
143+
)
144+
fs.writeFileSync(path.join(tmpJoin, 'scripts', 'lib', 'join-only-dep.js'), "'use strict'\nmodule.exports = {}\n")
145+
assert.deepStrictEqual(
146+
collectRuntimeScriptDeps(tmpJoin, { roots: ['hooks/_runtime'] }),
147+
['scripts/lib/join-only-dep.js'],
148+
'path.join scripts/lib dep must enter closure'
149+
)
135150
}
136151

137-
function sourcePackageToolSmoke() {
138-
const mem = path.join(ROOT, 'mcp', 'memory-server.js')
139-
const prof = path.join(ROOT, 'mcp', 'profile-server.js')
140-
const memR = mcpToolCallProbe(mem, ROOT, 'memory_status', { agent: 'grok', project: 'devcodex', limit: 2 }, {
152+
function allowlistOnlyMcpSmoke() {
153+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'runtime-closure-mcp-'))
154+
// MCP servers resolve ../hooks/_runtime and ../scripts/lib from package-shaped layout.
155+
copyDir(path.join(ROOT, 'mcp'), path.join(tmp, 'mcp'))
156+
copyDir(path.join(ROOT, 'hooks', '_runtime'), path.join(tmp, 'hooks', '_runtime'))
157+
copyRuntimeDeps(tmp, CLAUDE_MCP_RUNTIME_SCRIPT_DEPS)
158+
159+
const mem = path.join(tmp, 'mcp', 'memory-server.js')
160+
const prof = path.join(tmp, 'mcp', 'profile-server.js')
161+
const initR = mcpInitializeProbe(mem, tmp, { timeoutMs: 5000 })
162+
assert.strictEqual(initR.passed, true, `allowlist-only initialize failed: ${initR.error}`)
163+
const memR = mcpToolCallProbe(mem, tmp, 'memory_status', { agent: 'grok', project: 'devcodex', limit: 2 }, {
141164
timeoutMs: 8000
142165
})
143-
assert.strictEqual(memR.passed, true, `memory smoke failed: ${memR.error} ${memR.textHead}`)
144-
const profR = mcpToolCallProbe(prof, ROOT, 'profile_compose_entry_check', {
166+
assert.strictEqual(memR.passed, true, `allowlist-only memory smoke failed: ${memR.error} ${memR.textHead}`)
167+
const profR = mcpToolCallProbe(prof, tmp, 'profile_compose_entry_check', {
145168
project: 'devcodex',
146169
status: 'PASS',
147170
nextStep: 'test'
148171
}, { timeoutMs: 8000 })
149-
assert.strictEqual(profR.passed, true, `profile smoke failed: ${profR.error} ${profR.textHead}`)
172+
assert.strictEqual(profR.passed, true, `allowlist-only profile smoke failed: ${profR.error} ${profR.textHead}`)
150173
}
151174

152175
function packlistContainsRuntimeClosure() {
@@ -165,15 +188,9 @@ function packlistContainsRuntimeClosure() {
165188
assert.ok(files.has('scripts/lib/runtime-dependency-closure.js'), 'npm package must include runtime closure owner')
166189
}
167190

168-
function initStillWorks() {
169-
const r = mcpInitializeProbe(path.join(ROOT, 'mcp', 'memory-server.js'), ROOT, { timeoutMs: 5000 })
170-
assert.strictEqual(r.passed, true, `initialize failed: ${r.error}`)
171-
}
172-
173191
closureCoverage()
174192
fakeRuntimeSeed()
175193
layoutReplaySmoke()
176-
sourcePackageToolSmoke()
194+
allowlistOnlyMcpSmoke()
177195
packlistContainsRuntimeClosure()
178-
initStillWorks()
179196
console.log('test-mcp-runtime-closure: PASS')

0 commit comments

Comments
 (0)