Bridge and git gate: repo-root and excluded-dir containment by path.relative, not a hardcoded backslash
github-draft-pr threw 'Target path escapes repo root' on Linux and the gate never excluded its own wrapper dir. One helper, tools/path-within.mjs, replaces the three copies; case-insensitivity now follows the platform's path flavour. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a1b173079d
commit
0ffd97baed
5 changed files with 73 additions and 9 deletions
50
tests/path-within.test.mjs
Normal file
50
tests/path-within.test.mjs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import path from "node:path";
|
||||
import { isWithin } from "../tools/path-within.mjs";
|
||||
|
||||
const flavours = [
|
||||
{ name: "posix", pathApi: path.posix, root: "/srv/repo", sep: "/", inside: "/srv/repo/src/a.txt", sibling: "/srv/repo-evil/a.txt", other: "/etc/passwd" },
|
||||
{ name: "win32", pathApi: path.win32, root: "C:\\Work\\Repo", sep: "\\", inside: "C:\\Work\\Repo\\src\\a.txt", sibling: "C:\\Work\\Repo-evil\\a.txt", other: "D:\\Work\\Repo\\a.txt" },
|
||||
];
|
||||
|
||||
for (const f of flavours) {
|
||||
const opts = { pathApi: f.pathApi };
|
||||
test(`${f.name}: a path inside the root, and the root itself, are within`, () => {
|
||||
assert.equal(isWithin(f.root, f.inside, opts), true);
|
||||
assert.equal(isWithin(f.root, f.root, opts), true);
|
||||
assert.equal(isWithin(f.root, f.root + f.sep, opts), true);
|
||||
});
|
||||
|
||||
test(`${f.name}: a sibling sharing the root as a name prefix is refused`, () => {
|
||||
assert.equal(isWithin(f.root, f.sibling, opts), false);
|
||||
});
|
||||
|
||||
test(`${f.name}: a dot-dot escape and an unrelated absolute path are refused`, () => {
|
||||
assert.equal(isWithin(f.root, f.root + f.sep + ["..", "x"].join(f.sep), opts), false);
|
||||
assert.equal(isWithin(f.root, f.other, opts), false);
|
||||
assert.equal(isWithin(f.root, f.root + f.sep + "..", opts), false);
|
||||
});
|
||||
|
||||
test(`${f.name}: a name that merely starts with two dots is inside`, () => {
|
||||
assert.equal(isWithin(f.root, f.root + f.sep + "..cache" + f.sep + "x", opts), true);
|
||||
});
|
||||
|
||||
test(`${f.name}: excluded dir, strictly beneath it vs not`, () => {
|
||||
const excluded = f.root + f.sep + "tools" + f.sep + "git-gate";
|
||||
const strict = { ...opts, allowSelf: false };
|
||||
assert.equal(isWithin(excluded, excluded + f.sep + "git.cmd", strict), true);
|
||||
assert.equal(isWithin(excluded, excluded, strict), false);
|
||||
assert.equal(isWithin(excluded, excluded + "-other" + f.sep + "git", strict), false);
|
||||
assert.equal(isWithin(excluded, f.root + f.sep + "bin" + f.sep + "git", strict), false);
|
||||
});
|
||||
}
|
||||
|
||||
test("win32: comparison ignores case, as Windows paths do", () => {
|
||||
assert.equal(isWithin("C:\\Work\\Repo", "c:\\work\\repo\\SRC\\a.txt", { pathApi: path.win32 }), true);
|
||||
assert.equal(isWithin("C:\\Work\\Repo", "c:\\work\\repo-evil\\a.txt", { pathApi: path.win32 }), false);
|
||||
});
|
||||
|
||||
test("posix: comparison is case-sensitive", () => {
|
||||
assert.equal(isWithin("/srv/repo", "/srv/Repo/a.txt", { pathApi: path.posix }), false);
|
||||
});
|
||||
|
|
@ -4,6 +4,7 @@ import { mkdir, readFile, unlink, writeFile } from "node:fs/promises";
|
|||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { isWithin } from "./path-within.mjs";
|
||||
import {
|
||||
beginCrossingReceipt,
|
||||
crossingProvenanceFromOptions,
|
||||
|
|
@ -182,7 +183,7 @@ async function createGitHubDraftPr(options) {
|
|||
}
|
||||
|
||||
const targetPath = resolve(repoRoot, relativePath);
|
||||
if (!targetPath.toLowerCase().startsWith(`${repoRoot.toLowerCase()}\\`) && targetPath.toLowerCase() !== repoRoot.toLowerCase()) {
|
||||
if (!isWithin(repoRoot, targetPath)) {
|
||||
throw new Error(`Target path escapes repo root: ${relativePath}`);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#!/usr/bin/env node
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { isWithin } from "../path-within.mjs";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const wrapperDir = dirname(fileURLToPath(import.meta.url));
|
||||
|
|
@ -90,9 +91,7 @@ function resolveExecutable(command, excludedDir) {
|
|||
}
|
||||
|
||||
function isWithinExcludedDir(candidate, excludedDir) {
|
||||
const normalizedCandidate = resolve(candidate).toLowerCase();
|
||||
const normalizedExcludedDir = `${resolve(excludedDir).toLowerCase()}\\`;
|
||||
return normalizedCandidate.startsWith(normalizedExcludedDir);
|
||||
return isWithin(excludedDir, candidate, { allowSelf: false });
|
||||
}
|
||||
|
||||
function isWindowsCommandScript(command) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#!/usr/bin/env node
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { isWithin } from "../path-within.mjs";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const wrapperDir = dirname(fileURLToPath(import.meta.url));
|
||||
|
|
@ -59,9 +60,7 @@ function resolveExecutable(command, excludedDir) {
|
|||
}
|
||||
|
||||
function isWithinExcludedDir(candidate, excludedDir) {
|
||||
const normalizedCandidate = resolve(candidate).toLowerCase();
|
||||
const normalizedExcludedDir = `${resolve(excludedDir).toLowerCase()}\\`;
|
||||
return normalizedCandidate.startsWith(normalizedExcludedDir);
|
||||
return isWithin(excludedDir, candidate, { allowSelf: false });
|
||||
}
|
||||
|
||||
function findGitSubcommand(input) {
|
||||
|
|
|
|||
15
tools/path-within.mjs
Normal file
15
tools/path-within.mjs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import path from "node:path";
|
||||
|
||||
// True when `candidate` is `root` itself (only if allowSelf) or lies beneath it.
|
||||
// Containment is path.relative-based, so it follows the host's separator and
|
||||
// case rules; pass path.win32 or path.posix to pin a flavour.
|
||||
export function isWithin(root, candidate, { pathApi = path, allowSelf = true } = {}) {
|
||||
const relative = pathApi.relative(pathApi.resolve(root), pathApi.resolve(candidate));
|
||||
if (relative === "") {
|
||||
return allowSelf;
|
||||
}
|
||||
if (pathApi.isAbsolute(relative) || relative === ".." || relative.startsWith(`..${pathApi.sep}`)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Loading…
Reference in a new issue