Skip to content

Commit e892ca4

Browse files
committed
test(security): cover canonical path containment behavior
1 parent 152cc53 commit e892ca4

4 files changed

Lines changed: 67 additions & 93 deletions

File tree

‎src/commands/onboarding-plugin-install.test.ts‎

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,71 +1822,5 @@ describe("ensureOnboardingPluginInstalled", () => {
18221822
expect(captured?.options).toEqual([{ value: "skip", label: "Skip for now" }]);
18231823
});
18241824
});
1825-
1826-
it("rejects local install paths when relative resolution looks cross-drive", async () => {
1827-
await withTestDir({ prefix: "openclaw-onboarding-install-cross-drive-" }, async (temp) => {
1828-
const workspaceDir = path.join(temp, "workspace");
1829-
const pluginDir = path.join(workspaceDir, "plugins", "demo");
1830-
await fs.mkdir(path.join(workspaceDir, ".git"), { recursive: true });
1831-
await fs.mkdir(pluginDir, { recursive: true });
1832-
const realWorkspaceDir = await fs.realpath(workspaceDir);
1833-
1834-
const originalRelative = path.relative;
1835-
const originalIsAbsolute = path.isAbsolute;
1836-
const relativeSpy = vi.spyOn(path, "relative").mockImplementation((from, to) => {
1837-
if (
1838-
typeof from === "string" &&
1839-
typeof to === "string" &&
1840-
from === realWorkspaceDir &&
1841-
to === path.join(realWorkspaceDir, "plugins", "demo")
1842-
) {
1843-
return "D:\\evil";
1844-
}
1845-
return originalRelative(from, to);
1846-
});
1847-
const isAbsoluteSpy = vi.spyOn(path, "isAbsolute").mockImplementation((value) => {
1848-
if (value === "D:\\evil") {
1849-
return true;
1850-
}
1851-
return originalIsAbsolute(value);
1852-
});
1853-
1854-
try {
1855-
let captured:
1856-
| {
1857-
options: Array<{
1858-
value: "clawhub" | "npm" | "local" | "skip";
1859-
label: string;
1860-
hint?: string;
1861-
}>;
1862-
}
1863-
| undefined;
1864-
1865-
await ensureOnboardingPluginInstalled({
1866-
cfg: {},
1867-
entry: {
1868-
pluginId: "demo-plugin",
1869-
label: "Demo Plugin",
1870-
install: {
1871-
localPath: "plugins/demo",
1872-
},
1873-
},
1874-
prompter: {
1875-
select: vi.fn(async (input) => {
1876-
captured = input;
1877-
return "skip";
1878-
}),
1879-
} as never,
1880-
runtime: {} as never,
1881-
workspaceDir,
1882-
});
1883-
1884-
expect(captured?.options).toEqual([{ value: "skip", label: "Skip for now" }]);
1885-
} finally {
1886-
relativeSpy.mockRestore();
1887-
isAbsoluteSpy.mockRestore();
1888-
}
1889-
});
1890-
});
18911825
});
18921826
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */

‎src/infra/path-env.test.ts‎

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -505,33 +505,43 @@ describe("ensureOpenClawCliOnPath", () => {
505505
expect(updated).not.toContain(path.join("npm-prefix", "bin"));
506506
});
507507

508-
it("ignores package-manager env roots derived from the active workspace", () => {
509-
const homeDir = abs("/tmp/openclaw-path/home");
510-
const cwd = path.join(homeDir, "workspace");
511-
const appBinDir = path.join(homeDir, "app-bin");
512-
const appCli = path.join(appBinDir, "openclaw");
513-
const pnpmHome = path.join(cwd, ".pnpm");
514-
const npmPrefix = path.join(cwd, ".npm-prefix");
515-
for (const dir of [homeDir, cwd, appBinDir, pnpmHome, path.join(pnpmHome, "bin"), npmPrefix]) {
516-
setDir(dir);
517-
}
518-
setDir(path.join(npmPrefix, "bin"));
519-
setExe(appCli);
520-
resetBootstrapEnv("/usr/bin:/bin");
521-
process.env.PNPM_HOME = pnpmHome;
522-
process.env.NPM_CONFIG_PREFIX = npmPrefix;
508+
it.each([".pnpm", "..cache"])(
509+
"ignores package-manager env roots derived from the active workspace (%s)",
510+
(packageManagerDir) => {
511+
const homeDir = abs("/tmp/openclaw-path/home");
512+
const cwd = path.join(homeDir, "workspace");
513+
const appBinDir = path.join(homeDir, "app-bin");
514+
const appCli = path.join(appBinDir, "openclaw");
515+
const pnpmHome = path.join(cwd, packageManagerDir);
516+
const npmPrefix = path.join(cwd, ".npm-prefix");
517+
for (const dir of [
518+
homeDir,
519+
cwd,
520+
appBinDir,
521+
pnpmHome,
522+
path.join(pnpmHome, "bin"),
523+
npmPrefix,
524+
]) {
525+
setDir(dir);
526+
}
527+
setDir(path.join(npmPrefix, "bin"));
528+
setExe(appCli);
529+
resetBootstrapEnv("/usr/bin:/bin");
530+
process.env.PNPM_HOME = pnpmHome;
531+
process.env.NPM_CONFIG_PREFIX = npmPrefix;
523532

524-
const updated = bootstrapPath({
525-
execPath: appCli,
526-
cwd,
527-
homeDir,
528-
platform: "linux",
529-
});
533+
const updated = bootstrapPath({
534+
execPath: appCli,
535+
cwd,
536+
homeDir,
537+
platform: "linux",
538+
});
530539

531-
expect(updated).not.toContain(pnpmHome);
532-
expect(updated).not.toContain(path.join(pnpmHome, "bin"));
533-
expect(updated).not.toContain(path.join(npmPrefix, "bin"));
534-
});
540+
expect(updated).not.toContain(pnpmHome);
541+
expect(updated).not.toContain(path.join(pnpmHome, "bin"));
542+
expect(updated).not.toContain(path.join(npmPrefix, "bin"));
543+
},
544+
);
535545

536546
it("ignores package-manager env roots whose existing parent resolves into the workspace", () => {
537547
const homeDir = abs("/tmp/openclaw-path/home");

‎src/infra/path-guards.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
33
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
44
import {
55
isPathInside,
6+
isPathStrictlyInside,
67
normalizeWindowsPathForComparison,
78
normalizeWindowsPathPreservingCase,
89
} from "./path-guards.js";
@@ -60,6 +61,7 @@ describe("isPathInside", () => {
6061
["/workspace/root", "/workspace/root", true],
6162
["/workspace/root", "/workspace/root/nested/file.txt", true],
6263
["/workspace/root", "/workspace/root/..file.txt", true],
64+
["/workspace/root", "/workspace/root/..cache/cache.json", true],
6365
["/workspace/root", "/workspace/root/../escape.txt", false],
6466
["/workspace/root", "/workspace/rootless/file.txt", false],
6567
["/workspace/root", "/workspace/root/a/b/c/d/e/file.txt", true],
@@ -88,3 +90,26 @@ describe("isPathInside", () => {
8890
}
8991
});
9092
});
93+
94+
describe("isPathStrictlyInside", () => {
95+
it.each([
96+
["/workspace/root", "/workspace/root", false],
97+
["/workspace/root", "/workspace/root/child", true],
98+
["/workspace/root", "/workspace/root/..cache/cache.json", true],
99+
["/workspace/root", "/workspace/root/../escape", false],
100+
])("checks strict posix containment %s -> %s", (basePath, targetPath, expected) => {
101+
expect(isPathStrictlyInside(basePath, targetPath)).toBe(expected);
102+
});
103+
104+
it("uses win32 path semantics for strict containment checks", () => {
105+
setPlatform("win32");
106+
107+
for (const [basePath, targetPath, expected] of [
108+
[String.raw`C:\workspace\root`, String.raw`C:\workspace\root`, false],
109+
[String.raw`C:\workspace\root`, String.raw`C:\workspace\root\..cache\file.txt`, true],
110+
[String.raw`C:\workspace\root`, String.raw`D:\workspace\root\file.txt`, false],
111+
] as const) {
112+
expect(isPathStrictlyInside(basePath, targetPath)).toBe(expected);
113+
}
114+
});
115+
});

‎src/plugins/bundle-mcp.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ describe("loadEnabledBundleMcpConfig", () => {
567567
$schema: AGENT_MCP_SCHEMA,
568568
mcpServers: {
569569
valid: { type: "stdio", command: "node", cwd: "./child" },
570+
dotPrefixedChild: { type: "stdio", command: "node", cwd: "./..cache" },
570571
relativeEscape: { type: "stdio", command: "node", cwd: "./../escape" },
571572
placeholderEscape: {
572573
type: "stdio",
@@ -575,7 +576,7 @@ describe("loadEnabledBundleMcpConfig", () => {
575576
},
576577
},
577578
},
578-
textFiles: { "child/.keep": "" },
579+
textFiles: { "child/.keep": "", "..cache/.keep": "" },
579580
});
580581
const rootRealPath = await fs.realpath(pluginRoot);
581582
const relativeProcessCwd = path.relative(rootRealPath, await fs.realpath(process.cwd()));
@@ -588,11 +589,15 @@ describe("loadEnabledBundleMcpConfig", () => {
588589
cfg: createEnabledBundleConfig(["portable-cwd"]),
589590
});
590591

591-
expect(Object.keys(loaded.config.mcpServers)).toEqual(["valid"]);
592+
expect(Object.keys(loaded.config.mcpServers)).toEqual(["valid", "dotPrefixedChild"]);
592593
await expectResolvedPathEqual(
593594
loaded.config.mcpServers.valid?.cwd,
594595
path.join(pluginRoot, "child"),
595596
);
597+
await expectResolvedPathEqual(
598+
loaded.config.mcpServers.dotPrefixedChild?.cwd,
599+
path.join(pluginRoot, "..cache"),
600+
);
596601
expect(loaded.diagnostics).toHaveLength(2);
597602
expect(loaded.diagnostics.map((entry) => entry.message)).toEqual([
598603
expect.stringContaining('invalid MCP server "relativeEscape"'),

0 commit comments

Comments
 (0)