diff options
| author | Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> | 2026-08-06 00:47:15 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-06 00:47:15 +0530 |
| commit | d49068d9ae55ca15f3b58c000fcbb27a59bdb895 (patch) | |
| tree | c848dacf3d4e79995c9012a40e43a7d064a0fd07 /test/util/paths-test.ts | |
| parent | d9bc46b508f8c6f7e0f0ff5d54d804340f3e6c80 (diff) | |
fix: accept Windows script paths and start the daemon from a real install (#23)
Two independent Windows/production defects reported in #22.
browser_run_script rejected every absolute path on Windows. The allowed-root
check compared with a hardcoded `${root}/`, but path.resolve returns
backslash-separated paths there, so a file inside os.tmpdir() was refused even
though the rejection message listed tmpdir as allowed. Decide containment with
path.relative instead: it is separator-agnostic and carries each platform's own
case rules, so it also accepts a differing drive-letter case that a path.sep
patch would still reject. Escapes via `..` and sibling directories sharing the
root's name prefix stay rejected, as before.
The daemon could not start under `npm omit=dev`: it is spawned through tsx,
which was declared only as a dev dependency. Moving tsx to dependencies is not
enough on its own — pi installs extensions into one flat hoisted tree, so tsx
lands beside the package and the fixed <pkg>/node_modules/.bin/tsx lookup still
misses, falling through to an `npx tsx` that resolves against the wrong cwd and
dies against stdio: "ignore". Resolve the CLI entry with Node's own resolver,
which walks up to the hoisted copy. That removes the shell entirely, and with it
the Windows .cmd shim, windowsVerbatimArguments, and the manual quoting that
paths containing spaces needed.
Both fixes live in platform-branching code, so their tests are registered in the
three-OS CI matrix that already exists for that purpose — a Linux-only run
cannot exercise either one.
Diffstat (limited to 'test/util/paths-test.ts')
| -rw-r--r-- | test/util/paths-test.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/util/paths-test.ts b/test/util/paths-test.ts new file mode 100644 index 0000000..4fff247 --- /dev/null +++ b/test/util/paths-test.ts @@ -0,0 +1,50 @@ +// Runs on every OS in the CI matrix on purpose: `isPathWithin` delegates to `node:path`, so +// only a real Windows run proves the backslash and drive-letter-case cases the old check failed. + +import { test, describe } from "node:test"; +import assert from "node:assert/strict"; +import { tmpdir } from "node:os"; +import { join, resolve, sep } from "node:path"; +import { isPathWithin } from "../../src/util/paths"; + +const root = resolve(tmpdir()); + +describe("path containment", () => { + test("a directory contains itself", () => { + assert.equal(isPathWithin(root, root), true); + }); + + test("a direct child is contained", () => { + assert.equal(isPathWithin(root, join(root, "script.js")), true); + }); + + test("a nested child is contained", () => { + assert.equal(isPathWithin(root, join(root, "a", "b", "script.js")), true); + }); + + test("a native-separator path is contained", () => { + assert.equal(isPathWithin(root, `${root}${sep}script.js`), true); + }); + + test("an unnormalised path that still lands inside is contained", () => { + assert.equal(isPathWithin(root, join(root, "a", "..", "script.js")), true); + }); + + test("an unnormalised path that escapes the root is rejected", () => { + assert.equal(isPathWithin(root, join(root, "..", "..", "evil.js")), false); + }); + + test("a sibling directory sharing the root's name prefix is rejected", () => { + assert.equal(isPathWithin(root, `${root}-sibling${sep}script.js`), false); + }); + + test("the root's own parent is rejected", () => { + assert.equal(isPathWithin(root, join(root, "..")), false); + }); + + test("the roots the run-script tool allows accept a file written into them", () => { + for (const allowed of [tmpdir(), process.cwd()]) { + assert.equal(isPathWithin(allowed, join(allowed, "bh-script.mjs")), true, allowed); + } + }); +}); |
