Skip to content

Commit a4e3041

Browse files
author
Cad from Arca
committed
test(update): harden global root parsing
1 parent 921154b commit a4e3041

4 files changed

Lines changed: 141 additions & 42 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function parseGlobalRootOutput(manager: "npm" | "pnpm", stdout: string): string | null {
2+
const root = stdout.trim();
3+
if (!root || root.includes("\n") || root.includes("\r")) {
4+
return null;
5+
}
6+
// npm >= 11 redacts UUID-like path segments to literal "***".
7+
return manager === "npm" && root.includes("***") ? null : root;
8+
}

‎src/infra/package-update-steps.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
UPDATE_POST_INSTALL_DOCTOR_ADVISORY_EXIT_CODE,
1414
} from "./update-doctor-result.js";
1515
import {
16+
resolveGlobalInstallTarget,
1617
resolveNpmGlobalPrefixLayoutFromPrefix,
1718
type CommandRunner,
1819
type ResolvedGlobalInstallTarget,
@@ -156,6 +157,60 @@ describe("markPackagePostInstallDoctorAdvisory", () => {
156157
});
157158

158159
describe("runGlobalPackageUpdateSteps", () => {
160+
it("keeps redacted npm roots out of staged paths and install arguments", async () => {
161+
await withTempDir({ prefix: "openclaw-package-update-redacted-root-" }, async (base) => {
162+
const redactedRoot = path.join(
163+
base,
164+
"ci-agent",
165+
"***",
166+
"workspace",
167+
"sbx-***",
168+
"nvm",
169+
"versions",
170+
"node",
171+
"v24.14.0",
172+
"lib",
173+
"node_modules",
174+
);
175+
const runCommand: CommandRunner = async (argv) => {
176+
if (argv.join(" ") === "npm root -g") {
177+
return { stdout: `${redactedRoot}\n`, stderr: "", code: 0 };
178+
}
179+
throw new Error(`unexpected command: ${argv.join(" ")}`);
180+
};
181+
const installTarget = await resolveGlobalInstallTarget({
182+
manager: "npm",
183+
runCommand,
184+
timeoutMs: 1000,
185+
});
186+
const installArgv: string[][] = [];
187+
188+
const result = await runGlobalPackageUpdateSteps({
189+
installTarget,
190+
installSpec: "[email protected]",
191+
packageName: "openclaw",
192+
runCommand,
193+
runStep: async ({ name, argv, cwd }) => {
194+
installArgv.push(argv);
195+
return {
196+
name,
197+
command: argv.join(" "),
198+
cwd: cwd ?? process.cwd(),
199+
durationMs: 1,
200+
exitCode: 1,
201+
};
202+
},
203+
timeoutMs: 1000,
204+
});
205+
206+
expect(result.failedStep).not.toBeNull();
207+
expect(installArgv).toHaveLength(2);
208+
expect(installArgv.flat()).not.toContain("--prefix");
209+
expect(installArgv.flat().some((arg) => arg.includes("***"))).toBe(false);
210+
await expectPathMissing(redactedRoot);
211+
});
212+
});
213+
159214
it("installs npm updates into a clean staged prefix before swapping the global package", async () => {
160215
await withTempDir({ prefix: "openclaw-package-update-staged-" }, async (base) => {
161216
const prefix = path.join(base, "prefix");

‎src/infra/update-global.test.ts‎

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -411,42 +411,84 @@ describe("update global helpers", () => {
411411
});
412412
});
413413

414-
it("falls back to the running package root when npm redacts UUID-like path segments", async () => {
415-
await withMockedPlatform("darwin", async () => {
416-
await withTempDir({ prefix: "openclaw-update-redacted-probe-" }, async (base) => {
417-
const globalRoot = path.join(base, "usr", "local", "lib", "node_modules");
418-
const pkgRoot = path.join(globalRoot, "openclaw");
419-
const redactedRoot = path.join(
420-
base,
421-
"tmp",
422-
"ci-agent",
423-
"***",
424-
"workspace",
425-
"sbx-***",
426-
"nvm",
427-
"versions",
428-
"node",
429-
"v24.14.0",
430-
"lib",
431-
"node_modules",
432-
);
433-
await fs.mkdir(pkgRoot, { recursive: true });
414+
it("rejects redacted npm global roots when no running-package fallback exists", async () => {
415+
await withTempDir({ prefix: "openclaw-update-redacted-probe-" }, async (base) => {
416+
const redactedRoot = path.join(
417+
base,
418+
"ci-agent",
419+
"***",
420+
"workspace",
421+
"sbx-***",
422+
"nvm",
423+
"versions",
424+
"node",
425+
"v24.14.0",
426+
"lib",
427+
"node_modules",
428+
);
429+
const runCommand = createNpmRootRunner({ defaultNpmRoot: redactedRoot });
434430

435-
const runCommand = createNpmRootRunner({ defaultNpmRoot: redactedRoot });
431+
await expect(
432+
resolveGlobalInstallTarget({
433+
manager: "npm",
434+
runCommand,
435+
timeoutMs: 1000,
436+
}),
437+
).resolves.toEqual({
438+
manager: "npm",
439+
command: "npm",
440+
globalRoot: null,
441+
packageRoot: null,
442+
});
443+
});
444+
});
436445

437-
await expect(
438-
resolveGlobalInstallTarget({
439-
manager: "npm",
440-
runCommand,
441-
timeoutMs: 1000,
442-
pkgRoot,
443-
}),
444-
).resolves.toEqual({
446+
it("preserves literal star segments reported by pnpm", async () => {
447+
await withTempDir({ prefix: "openclaw-update-pnpm-stars-" }, async (base) => {
448+
const globalRoot = path.join(base, "pnpm", "***", "5", "node_modules");
449+
const runCommand: CommandRunner = async (argv) => {
450+
if (argv[0] === "pnpm") {
451+
return { stdout: `${globalRoot}\n`, stderr: "", code: 0 };
452+
}
453+
throw new Error(`unexpected command: ${argv.join(" ")}`);
454+
};
455+
456+
await expect(
457+
resolveGlobalInstallTarget({
458+
manager: "pnpm",
459+
runCommand,
460+
timeoutMs: 1000,
461+
}),
462+
).resolves.toEqual({
463+
manager: "pnpm",
464+
command: "pnpm",
465+
globalRoot,
466+
packageRoot: path.join(globalRoot, "openclaw"),
467+
});
468+
});
469+
});
470+
471+
it("rejects multiline package-manager root output", async () => {
472+
await withTempDir({ prefix: "openclaw-update-multiline-root-" }, async (base) => {
473+
const firstRoot = path.join(base, "first", "lib", "node_modules");
474+
const secondRoot = path.join(base, "second", "lib", "node_modules");
475+
const runCommand: CommandRunner = async () => ({
476+
stdout: `${firstRoot}\n${secondRoot}\n`,
477+
stderr: "",
478+
code: 0,
479+
});
480+
481+
await expect(
482+
resolveGlobalInstallTarget({
445483
manager: "npm",
446-
command: "npm",
447-
globalRoot,
448-
packageRoot: pkgRoot,
449-
});
484+
runCommand,
485+
timeoutMs: 1000,
486+
}),
487+
).resolves.toEqual({
488+
manager: "npm",
489+
command: "npm",
490+
globalRoot: null,
491+
packageRoot: null,
450492
});
451493
});
452494
});

‎src/infra/update-global.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
readPackageDistInventoryIfPresent,
1818
} from "./package-dist-inventory.js";
1919
import { readPackageVersion } from "./package-json.js";
20+
import { parseGlobalRootOutput } from "./package-manager-output.js";
2021
import { applyPathPrepend } from "./path-prepend.js";
2122
import { parseSemver } from "./runtime-guard.js";
2223

@@ -753,14 +754,7 @@ async function resolveGlobalRoot(
753754
if (!res || res.code !== 0) {
754755
return null;
755756
}
756-
const root = res.stdout.trim();
757-
if (!root || root.includes("***")) {
758-
// npm >= 11 redacts UUID-like path segments in stdout to literal "***".
759-
// Trusting a poisoned path creates a literal "***" directory tree and leaves
760-
// the live install stale, so treat it as an unresolved probe and fall back.
761-
return null;
762-
}
763-
return root;
757+
return parseGlobalRootOutput(resolved.manager, res.stdout);
764758
}
765759

766760
/**
@@ -854,7 +848,7 @@ export async function detectGlobalInstallManagerForRoot(
854848
if (!res || res.code !== 0) {
855849
continue;
856850
}
857-
const globalRoot = res.stdout.trim();
851+
const globalRoot = parseGlobalRootOutput(manager, res.stdout);
858852
if (!globalRoot) {
859853
continue;
860854
}

0 commit comments

Comments
 (0)