-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlers-up.test.ts
More file actions
352 lines (309 loc) · 12.7 KB
/
Copy pathhandlers-up.test.ts
File metadata and controls
352 lines (309 loc) · 12.7 KB
1
2
3
4
5
6
7
8
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* Tests for handleMcpUp — the MCP (untrusted-caller) surface over `mcpm up`.
*
* These exercise the PR #64 hardening fixes:
* - A: a thrown handleUp failure is surfaced (result.error + result.failed),
* never swallowed into a clean empty result.
* - C: allowProcessEnv:false — a required env var only present in process.env is
* NOT auto-read, so the server fails (and the failure is surfaced).
* - D: allowUrlServers:false — URL servers are recorded as blocked, never installed.
*
* handleMcpUp constructs its own RegistryClient and reads the stack file relative
* to process.cwd(), so we mock the registry module and chdir into a temp dir that
* holds a real mcpm.yaml + mcpm-lock.yaml.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { writeFile, mkdtemp } from "fs/promises";
import path from "path";
import os from "os";
import type { ServerEntry } from "../../registry/types.js";
import type { ClientId } from "../../config/paths.js";
import type { McpServerEntry } from "../../config/adapters/index.js";
import type { ServerDeps } from "../../server/handlers.js";
// Integration-style tests: each uses a real temp dir, and the first handleMcpUp
// call pays a one-time dynamic import("../commands/up.js"). Under CI coverage
// instrumentation on the slower Node 22 runner that first test can exceed the
// 5s default timeout, so raise it for this file (it runs ~1s locally).
vi.setConfig({ testTimeout: 30000 });
// ---------------------------------------------------------------------------
// Mock the registry client that handleMcpUp constructs internally.
// ---------------------------------------------------------------------------
function makeServerEntry(name: string, version: string): ServerEntry {
return {
server: {
name,
version,
packages: [
{
registryType: "npm",
identifier: `@test/${name.split("/").pop()}`,
environmentVariables: [],
},
],
},
};
}
// vi.hoisted: the factory below is hoisted above normal module init, so the mock
// fn must be created in a hoisted block to be referenceable inside it.
const { getServerMock } = vi.hoisted(() => ({
getServerMock: vi.fn((name: string, version?: string) =>
Promise.resolve(makeServerEntry(name, version ?? "1.0.0"))
),
}));
vi.mock("../../registry/client.js", () => ({
RegistryClient: class {
getServer = getServerMock;
getServerVersions = vi.fn().mockResolvedValue([]);
searchServers = vi.fn().mockResolvedValue({ servers: [] });
},
}));
// Imported after vi.mock so the mocked RegistryClient is in effect.
const { handleMcpUp } = await import("../../server/handlers.js");
// ---------------------------------------------------------------------------
// Deps + adapter helpers
// ---------------------------------------------------------------------------
function makeAdapter(servers: Record<string, McpServerEntry> = {}) {
return {
clientId: "claude-desktop" as ClientId,
read: vi.fn().mockResolvedValue({ ...servers }),
addServer: vi.fn().mockResolvedValue(undefined),
removeServer: vi.fn().mockResolvedValue(undefined),
setServerDisabled: vi.fn().mockResolvedValue(undefined),
};
}
function makeDeps(adapter = makeAdapter()): ServerDeps {
return {
registrySearch: vi.fn().mockResolvedValue([]),
registryGetServer: vi.fn((name: string) =>
Promise.resolve(makeServerEntry(name, "1.0.0"))
),
detectClients: vi
.fn<() => Promise<ClientId[]>>()
.mockResolvedValue(["claude-desktop"]),
getAdapter: vi.fn().mockReturnValue(adapter),
getConfigPath: vi.fn().mockReturnValue("/mock/config.json"),
scanTier1: vi.fn().mockReturnValue([]),
computeTrustScore: vi.fn().mockReturnValue({
score: 75,
maxPossible: 80,
level: "safe",
breakdown: { healthCheck: 15, staticScan: 40, externalScan: 0, registryMeta: 10 },
}),
addToStore: vi.fn().mockResolvedValue(undefined),
removeFromStore: vi.fn().mockResolvedValue(undefined),
};
}
const lockFor = (name: string, identifier: string) => `
lockfileVersion: 1
lockedAt: "2026-04-05T10:00:00Z"
servers:
${name}:
version: "1.0.0"
registryType: npm
identifier: "${identifier}"
trust:
score: 75
maxPossible: 80
level: safe
assessedAt: "2026-04-05T10:00:00Z"
`;
// ---------------------------------------------------------------------------
// cwd management — handleMcpUp resolves the stack file relative to cwd.
// ---------------------------------------------------------------------------
let originalCwd: string;
let tmpDir: string;
async function writeStack(stackYaml: string, lockYaml?: string): Promise<void> {
await writeFile(path.join(tmpDir, "mcpm.yaml"), stackYaml, "utf-8");
if (lockYaml !== undefined) {
await writeFile(path.join(tmpDir, "mcpm-lock.yaml"), lockYaml, "utf-8");
}
}
beforeEach(async () => {
originalCwd = process.cwd();
tmpDir = await mkdtemp(path.join(os.tmpdir(), "mcpm-mcpup-test-"));
process.chdir(tmpDir);
getServerMock.mockClear();
});
afterEach(() => {
process.chdir(originalCwd);
});
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("handleMcpUp", () => {
const REGISTRY_STACK = `
version: "1"
servers:
io.github.test/server-a:
version: "^1.0.0"
`;
const REGISTRY_LOCK = lockFor("io.github.test/server-a", "@test/server-a");
// Fix F.2
it("dryRun: true does not install and returns no installed servers", async () => {
await writeStack(REGISTRY_STACK, REGISTRY_LOCK);
const adapter = makeAdapter();
const deps = makeDeps(adapter);
const result = await handleMcpUp({ dryRun: true }, deps);
expect(adapter.addServer).not.toHaveBeenCalled();
expect(result.installed).toEqual([]);
expect(result.error).toBeUndefined();
});
// Fix F.3, corrected per review finding H2: handleMcpUp constructs its own
// handleUp deps with the REAL computeTrustScore (cts), so overriding
// deps.computeTrustScore (as the original test did) has no effect on this path.
// We instead drive the genuine pipeline: the clean mock server scores 55/80 = 69%
// (health-null 15 + static 40 + externalScan 0 + registryMeta 0; no scanner ->
// maxPossible 80), which is below the 90% policy floor (and the 94% lock snapshot
// also blocks it via score-drop), so the real checkTrustPolicy blocks it.
it("blocked-by-policy: a server below the trust floor is blocked via the real pipeline", async () => {
const stackWithPolicy = `
version: "1"
policy:
minTrustScore: 90
servers:
io.github.test/server-a:
version: "^1.0.0"
`;
await writeStack(stackWithPolicy, REGISTRY_LOCK);
const adapter = makeAdapter();
const deps = makeDeps(adapter);
const result = await handleMcpUp({}, deps);
expect(result.blocked).toContain("io.github.test/server-a");
expect(adapter.addServer).not.toHaveBeenCalled();
});
// Fix F.4 (and fix A + fix C)
it("missing-env (allowProcessEnv:false): required env not provided -> failed + surfaced error", async () => {
const stackWithEnv = `
version: "1"
servers:
io.github.test/server-a:
version: "^1.0.0"
env:
API_TOKEN:
required: true
secret: true
`;
await writeStack(stackWithEnv, REGISTRY_LOCK);
const adapter = makeAdapter();
const deps = makeDeps(adapter);
// Even if the secret leaks into process.env, allowProcessEnv:false must NOT
// read it — so the required var stays unresolved and the server fails.
const original = process.env.API_TOKEN;
process.env.API_TOKEN = "ambient-secret-should-not-be-used";
try {
const result = await handleMcpUp({}, deps);
// Fix A: a thrown failure is surfaced, not swallowed into a clean result.
// M1: the failed server lands in `failed` by NAME (not the error message).
expect(result.error).toBeDefined();
expect(result.failed).toContain("io.github.test/server-a");
expect(result.installed).toEqual([]);
// Fix C: the ambient secret was never written into a client config.
expect(adapter.addServer).not.toHaveBeenCalled();
} finally {
if (original === undefined) delete process.env.API_TOKEN;
else process.env.API_TOKEN = original;
}
});
// Fix F.5 (and fix D)
it("URL server (allowUrlServers:false): appears in blocked, addServer not called", async () => {
const urlStack = `
version: "1"
servers:
io.github.test/remote:
url: "https://example.com/mcp"
`;
// URL servers do not require a lock entry; omit the lock file so runLock is
// a no-op-ish path is avoided — provide an (unused) lock to keep it simple.
const urlLock = `
lockfileVersion: 1
lockedAt: "2026-04-05T10:00:00Z"
servers:
io.github.test/remote:
url: "https://example.com/mcp"
`;
await writeStack(urlStack, urlLock);
const adapter = makeAdapter();
const deps = makeDeps(adapter);
const result = await handleMcpUp({}, deps);
expect(result.blocked).toContain("io.github.test/remote");
expect(adapter.addServer).not.toHaveBeenCalled();
expect(result.installed).toEqual([]);
});
// Fix A: no clients detected -> handleUp throws -> surfaced, not clean empty.
it("surfaces a thrown failure when no clients are detected", async () => {
await writeStack(REGISTRY_STACK, REGISTRY_LOCK);
const adapter = makeAdapter();
const deps = makeDeps(adapter);
(deps.detectClients as ReturnType<typeof vi.fn>).mockResolvedValue([]);
const result = await handleMcpUp({}, deps);
// M1: a whole-batch throw (no server ever processed) is signaled via the
// authoritative `error` field — NOT by poisoning `failed`, which holds server
// names only. So `failed` is empty here while `error` carries the message.
expect(result.error).toBeDefined();
expect(result.failed).toEqual([]);
expect(result.installed).toEqual([]);
});
// Fix B: a traversing stackFile is rejected unconditionally (resolved-path
// containment), independent of the Zod default.
it("rejects a stackFile outside the working directory (path traversal)", async () => {
await writeStack(REGISTRY_STACK, REGISTRY_LOCK);
const deps = makeDeps();
await expect(
handleMcpUp({ stackFile: "../escape.yaml" }, deps)
).rejects.toThrow("within the working directory");
});
// H1: a `.env` in the working directory is an ambient secret source. On the MCP
// surface (allowEnvFile:false) it must NOT be harvested into an installed config.
// This fails on the pre-fix code (which read .env ungated) and passes after.
it("does not harvest the working-directory .env into installed configs (H1)", async () => {
const stackWithEnv = `
version: "1"
servers:
io.github.test/server-a:
version: "^1.0.0"
env:
LEAKME:
required: false
secret: false
`;
await writeStack(stackWithEnv, REGISTRY_LOCK);
await writeFile(
path.join(tmpDir, ".env"),
"LEAKME=host-secret-from-dotenv\n",
"utf-8"
);
const adapter = makeAdapter();
const deps = makeDeps(adapter);
const result = await handleMcpUp({}, deps);
expect(result.installed).toContain("io.github.test/server-a");
// Installed, but the .env value must never reach the written config env.
const entry = adapter.addServer.mock.calls[0]?.[2] as
| { env?: Record<string, string> }
| undefined;
expect(entry?.env?.LEAKME).toBeUndefined();
});
// M3: lexical containment passes an in-cwd symlink (its own path is inside cwd),
// but the reader would follow it out of tree. The realpath re-check must reject it.
it("rejects a stackFile that is an in-cwd symlink pointing outside (symlink traversal)", async () => {
const { symlink, writeFile: wf, mkdtemp: mkd } = await import("fs/promises");
const outsideDir = await mkd(path.join(os.tmpdir(), "mcpm-outside-"));
const outsideTarget = path.join(outsideDir, "secret.yaml");
await wf(outsideTarget, REGISTRY_STACK, "utf-8");
// Plant an in-cwd symlink whose real target is outside cwd.
await symlink(outsideTarget, path.join(tmpDir, "link.yaml"));
const deps = makeDeps();
await expect(
handleMcpUp({ stackFile: "link.yaml" }, deps)
).rejects.toThrow("within the working directory");
});
// Happy path: a registry server installs and is categorized as installed.
it("installs a registry server and reports it under installed", async () => {
await writeStack(REGISTRY_STACK, REGISTRY_LOCK);
const adapter = makeAdapter();
const deps = makeDeps(adapter);
const result = await handleMcpUp({}, deps);
expect(result.installed).toContain("io.github.test/server-a");
expect(adapter.addServer).toHaveBeenCalledTimes(1);
expect(result.error).toBeUndefined();
});
});