diff --git a/pm-stack/dispatch/README.md b/pm-stack/dispatch/README.md index 31d2a3110c..42fbc50bee 100644 --- a/pm-stack/dispatch/README.md +++ b/pm-stack/dispatch/README.md @@ -34,10 +34,10 @@ Body: "title": "fix(widget): flaky test", // optional; default: prompt's first line "model": "opencode-go/gpt-5.6-luna", // optional "provider/model" or {"providerID","id"} "directory": "/home/user/git/code", // optional; default: the repo's laptop primary. - // OIDC callers may only omit it or repeat the repo's primary — anything - // else is rejected (400) before any side effect (issue #2903). - // Bearer callers keep the free-form directory (deliberate: the bearer - // secret lives on the laptop, so whoever holds it already has the run). + // Callers may only omit it or use the repo's primary or a directory + // inside it — anything else is rejected (400) before any side effect + // (issues #2903, #2664). + // Bearer callers are scoped by the same containment rule (issue #2664). "priority": "high", // optional: integer 10-90 (number or decimal string), or critical|high|normal|low → 10/20/40/70 "issue": true, // optional; default true "wait": true, // optional; default true @@ -141,12 +141,12 @@ Every endpoint except `GET /health` requires `Authorization: Bearer …`: the claim need not equal the request body's `repo`. Repos outside the allowlist can neither call nor be targeted. Bearer callers are bound only by the allowlist on the target. - **Directory scoping (issue #2903):** OIDC callers may only omit - `directory` or repeat the target repo's laptop primary clone — any - other value is rejected with 400 before the issue, task, or session - is created. Bearer callers keep the free-form directory as a - deliberate choice: the static secret lives on the laptop, so its - holder already has the run the directory would grant. + **Directory scoping (issues #2903, #2664):** every caller + (OIDC and bearer alike) may only omit `directory` (or send a blank + one, which resolves to the default) or use the target repo's laptop + primary clone or a directory inside it — any other value (relative + paths, other repos' clones, arbitrary absolute paths) is rejected + with 400 before the issue, task, or session is created. See `examples/dispatch-workflow.yml`. - **Static bearer secret**: `DISPATCH_API_TOKEN` (in the laptop's `~/secrets/dispatch.env`), compared in constant time — the shape a diff --git a/pm-stack/dispatch/src/validate.ts b/pm-stack/dispatch/src/validate.ts index 077d582a54..973a01eb3b 100644 --- a/pm-stack/dispatch/src/validate.ts +++ b/pm-stack/dispatch/src/validate.ts @@ -107,6 +107,14 @@ function optString(body: Record, field: string): string | undef return v; } +/** Like optString, but an empty or whitespace-only string counts as omitted + * (issue #2664): a blank title must fall back to the prompt-derived title and + * a blank directory to the repo default, not defeat them with "". */ +function optNonBlankString(body: Record, field: string): string | undefined { + const v = optString(body, field); + return v !== undefined && v.trim() !== "" ? v : undefined; +} + function optBool(body: Record, field: string, dflt: boolean): boolean { const v = body[field]; if (v === undefined || v === null) return dflt; @@ -131,22 +139,23 @@ export function parseSessionRequest(body: unknown): SessionRequest { return { repo: reqString(b, "repo"), prompt: reqString(b, "prompt"), - title: optString(b, "title"), + title: optNonBlankString(b, "title"), model: parseModel(b.model), - directory: optString(b, "directory"), + directory: optNonBlankString(b, "directory"), priority: resolvePriority(b.priority), issue: optBool(b, "issue", true), wait: optBool(b, "wait", true), }; } -/** Session-directory scoping for POST /v1/sessions (issue #2903). - * OIDC callers (workflows in allowlisted repos) may only run inside the - * target repo's laptop primary clone: directory omitted (default) or - * exactly the repo's scoped directory. Anything else is rejected BEFORE - * any issue/task/session is created. Bearer callers hold the laptop - * secret, so they keep the free-form directory — a deliberate choice, - * documented in the README. */ +/** Session-directory scoping for POST /v1/sessions (issues #2903, #2664). + * One rule for every caller type (OIDC and bearer alike): a missing, empty, + * or whitespace-only directory resolves to the target repo's clone, and an + * explicit directory must be an absolute, normalized path equal to or inside + * that clone. Relative paths, other repos' clones, and arbitrary absolute + * paths are rejected BEFORE any issue/task/session is created. (This + * supersedes the #2903-era bearer free-form directory; extra roots and + * allowlists stay out of scope per design issue #2668.) */ export function resolveSessionDirectory( callerType: "oidc" | "bearer", repo: string, @@ -154,15 +163,34 @@ export function resolveSessionDirectory( repoDirs: Record, ): string { const scoped = repoDirs[repo]; - if (directory === undefined) return scoped!; - if (callerType === "bearer") return directory; - const norm = (d: string) => (d.length > 1 ? d.replace(/\/+$/, "") : d); - if (scoped === undefined || norm(directory) !== norm(scoped)) { - throw new ValidationError( - `directory: OIDC callers may only omit directory or use the repo clone (${scoped ?? "unknown"})`, - ); + if (scoped === undefined) { + throw new ValidationError(`directory: no repo directory is configured for ${repo}`); + } + const root = normalizeDirectory(scoped); + if (directory === undefined || directory.trim() === "") return root; + const norm = normalizeDirectory(directory); + if (norm === root || norm.startsWith(`${root}/`)) return norm; + throw new ValidationError( + `directory: ${callerType} callers may only run inside the target repo clone (${root}) or a directory within it`, + ); +} + +/** Lexical absolute-path normalization: collapses duplicate slashes, "." + * and ".." segments and trailing slashes. Relative paths are rejected. */ +function normalizeDirectory(d: string): string { + if (!d.startsWith("/")) { + throw new ValidationError(`directory: must be an absolute path, got "${d}"`); + } + const parts: string[] = []; + for (const seg of d.split("/")) { + if (seg === "" || seg === ".") continue; + if (seg === "..") { + parts.pop(); + continue; + } + parts.push(seg); } - return scoped; + return `/${parts.join("/")}`; } export interface TaskRequest { diff --git a/pm-stack/dispatch/test/validate.test.ts b/pm-stack/dispatch/test/validate.test.ts index 7841bcaf90..aa76fc3e85 100644 --- a/pm-stack/dispatch/test/validate.test.ts +++ b/pm-stack/dispatch/test/validate.test.ts @@ -221,7 +221,56 @@ describe("session directory scoping (issue #2903)", () => { expect(resolveSessionDirectory("bearer", code, undefined, repoDirs)).toBe("/home/user/git/code"); }); - test("bearer + foreign directory is the documented deliberate choice", () => { - expect(resolveSessionDirectory("bearer", code, "/tmp/ops-scratch", repoDirs)).toBe("/tmp/ops-scratch"); + test("bearer foreign directory is rejected too (issue #2664: one rule for both caller types)", () => { + expect(() => resolveSessionDirectory("bearer", code, "/tmp/ops-scratch", repoDirs)).toThrow(ValidationError); + expect(() => resolveSessionDirectory("bearer", code, "/etc", repoDirs)).toThrow(ValidationError); + expect(() => resolveSessionDirectory("bearer", code, "/home/user/git/opencode", repoDirs)).toThrow(ValidationError); + }); + + test("blank directory resolves to the repo clone for both caller types (issue #2664)", () => { + expect(resolveSessionDirectory("oidc", code, "", repoDirs)).toBe("/home/user/git/code"); + expect(resolveSessionDirectory("bearer", code, " ", repoDirs)).toBe("/home/user/git/code"); + }); + + test("a directory inside the repo clone is accepted for both caller types (issue #2664)", () => { + expect(resolveSessionDirectory("oidc", code, "/home/user/git/code/pkgs", repoDirs)).toBe("/home/user/git/code/pkgs"); + expect(resolveSessionDirectory("bearer", code, "/home/user/git/code/pkgs/", repoDirs)).toBe("/home/user/git/code/pkgs"); + }); + + test("paths are normalized before the containment check (issue #2664)", () => { + expect(resolveSessionDirectory("oidc", code, "/home/user/git/code/./pkgs/../pkgs", repoDirs)).toBe("/home/user/git/code/pkgs"); + expect(() => resolveSessionDirectory("oidc", code, "/home/user/git/code/../opencode", repoDirs)).toThrow(ValidationError); + expect(() => resolveSessionDirectory("bearer", code, "/home/user/git/code/../../etc", repoDirs)).toThrow(ValidationError); + }); + + test("relative directories are rejected for both caller types (issue #2664)", () => { + expect(() => resolveSessionDirectory("oidc", code, "pkgs/id", repoDirs)).toThrow(ValidationError); + expect(() => resolveSessionDirectory("bearer", code, "./pkgs", repoDirs)).toThrow(ValidationError); + }); + + test("a sibling whose name shares the clone prefix is not inside it (issue #2664)", () => { + expect(() => resolveSessionDirectory("oidc", code, "/home/user/git/code-evil", repoDirs)).toThrow(ValidationError); + }); +}); + +describe("session blank title/directory parsing (issue #2664)", () => { + test("empty and whitespace-only title/directory parse as omitted", () => { + const r = parseSessionRequest({ repo: "developing-today/code", prompt: "do the thing", title: " ", directory: "" }); + expect(r.title).toBeUndefined(); + expect(r.directory).toBeUndefined(); + const r2 = parseSessionRequest({ repo: "developing-today/code", prompt: "do the thing", title: "", directory: " \t " }); + expect(r2.title).toBeUndefined(); + expect(r2.directory).toBeUndefined(); + }); + + test("nonblank title and directory are preserved verbatim", () => { + const r = parseSessionRequest({ + repo: "developing-today/code", + prompt: "do the thing", + title: "My session", + directory: "/home/user/git/code/pkgs", + }); + expect(r.title).toBe("My session"); + expect(r.directory).toBe("/home/user/git/code/pkgs"); }); });