Fix Windows hook path containment

This commit is contained in:
Tak Hoffman 2026-03-19 00:00:50 -05:00
parent 20728e1035
commit 9cd74ca94b
No known key found for this signature in database
2 changed files with 31 additions and 5 deletions

View File

@ -0,0 +1,29 @@
import { afterEach, describe, expect, it, vi } from "vitest";
const originalPlatform = process.platform;
function setPlatform(value: NodeJS.Platform): void {
Object.defineProperty(process, "platform", {
configurable: true,
value,
});
}
afterEach(() => {
setPlatform(originalPlatform);
vi.restoreAllMocks();
});
describe("security scan path guards", () => {
it("uses Windows-aware containment checks for differently normalized paths", async () => {
setPlatform("win32");
const { isPathInside } = await import("./scan-paths.js");
expect(
isPathInside(String.raw`C:\Workspace\Root`, String.raw`c:\workspace\root\hooks\hook`),
).toBe(true);
expect(
isPathInside(String.raw`\\?\C:\Workspace\Root`, String.raw`C:\workspace\root\hooks\hook`),
).toBe(true);
});
});

View File

@ -1,11 +1,8 @@
import fs from "node:fs";
import path from "node:path";
import { isPathInside as isBoundaryPathInside } from "../infra/path-guards.js";
export function isPathInside(basePath: string, candidatePath: string): boolean {
const base = path.resolve(basePath);
const candidate = path.resolve(candidatePath);
const rel = path.relative(base, candidate);
return rel === "" || (!rel.startsWith(`..${path.sep}`) && rel !== ".." && !path.isAbsolute(rel));
return isBoundaryPathInside(basePath, candidatePath);
}
function safeRealpathSync(filePath: string): string | null {