fix(agents): normalize windows workspace path boundary checks (#30766)

Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
Shawn
2026-03-02 13:47:02 -08:00
committed by GitHub
parent a183656f8f
commit ef89b48785
2 changed files with 77 additions and 3 deletions

View File

@@ -0,0 +1,38 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const resolveSandboxInputPathMock = vi.hoisted(() => vi.fn());
vi.mock("./sandbox-paths.js", () => ({
resolveSandboxInputPath: resolveSandboxInputPathMock,
}));
import { toRelativeWorkspacePath } from "./path-policy.js";
describe("toRelativeWorkspacePath (windows semantics)", () => {
beforeEach(() => {
resolveSandboxInputPathMock.mockReset();
resolveSandboxInputPathMock.mockImplementation((filePath: string) => filePath);
});
it("accepts windows paths with mixed separators and case", () => {
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
try {
const root = "C:\\Users\\User\\OpenClaw";
const candidate = "c:/users/user/openclaw/memory/log.txt";
expect(toRelativeWorkspacePath(root, candidate)).toBe("memory\\log.txt");
} finally {
platformSpy.mockRestore();
}
});
it("rejects windows paths outside workspace root", () => {
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
try {
const root = "C:\\Users\\User\\OpenClaw";
const candidate = "C:\\Users\\User\\Other\\log.txt";
expect(() => toRelativeWorkspacePath(root, candidate)).toThrow("Path escapes workspace root");
} finally {
platformSpy.mockRestore();
}
});
});