Fix Windows hook path containment
This commit is contained in:
parent
20728e1035
commit
9cd74ca94b
29
src/security/scan-paths.test.ts
Normal file
29
src/security/scan-paths.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user