aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-06 00:47:15 +0530
committerGitHub <noreply@github.com>2026-08-06 00:47:15 +0530
commitd49068d9ae55ca15f3b58c000fcbb27a59bdb895 (patch)
treec848dacf3d4e79995c9012a40e43a7d064a0fd07 /test
parentd9bc46b508f8c6f7e0f0ff5d54d804340f3e6c80 (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')
-rw-r--r--test/util/daemon-socket-test.ts25
-rw-r--r--test/util/paths-test.ts50
2 files changed, 74 insertions, 1 deletions
diff --git a/test/util/daemon-socket-test.ts b/test/util/daemon-socket-test.ts
index eee0500..80a03e4 100644
--- a/test/util/daemon-socket-test.ts
+++ b/test/util/daemon-socket-test.ts
@@ -1,10 +1,11 @@
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { fileURLToPath } from "node:url";
+import { existsSync } from "node:fs";
import { Type } from "typebox";
import type { ExtensionAPI, ToolDefinition } from "@mariozechner/pi-coding-agent";
import type { TSchema } from "typebox";
-import { daemonSocketMayExist } from "../../src/daemon/spawn";
+import { daemonSocketMayExist, daemonSpawnCommand } from "../../src/daemon/spawn";
import { type AnyBrowserToolDefinition, registerBrowserTool } from "../../src/util/tool";
import type { BrowserClient } from "../../src/client";
import { ok } from "../../src/util/result";
@@ -32,6 +33,28 @@ describe("daemon socket presence check", () => {
});
});
+describe("daemon spawn command", () => {
+ test("the daemon runs on this process's node, not a shell", () => {
+ const spec = daemonSpawnCommand();
+ assert.notEqual(spec, null);
+ assert.equal(spec?.command, process.execPath);
+ });
+
+ test("the resolved tsx CLI is a real file on disk", () => {
+ const cli = daemonSpawnCommand()?.args[0];
+ assert.equal(typeof cli, "string");
+ // A hoisted install puts tsx beside the package, so a `<pkg>/node_modules/.bin` guess would miss it.
+ assert.equal(existsSync(String(cli)), true, `tsx CLI not found at ${String(cli)}`);
+ assert.match(String(cli), /cli\.mjs$/);
+ });
+
+ test("the daemon entrypoint is passed to tsx", () => {
+ const script = daemonSpawnCommand()?.args[1];
+ assert.match(String(script), /[\\/]daemon[\\/]index\.ts$/);
+ assert.equal(existsSync(String(script)), true);
+ });
+});
+
type GateProbe = { readonly ran: ReadonlyArray<string>; readonly text: string };
const runGate = async (ensureAlive: boolean | undefined): Promise<GateProbe> => {
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);
+ }
+ });
+});