Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.
TypeScript browser-shellcontrol-planefile-managerhomelabmobile-firstnextjsptyreactself-hostedserver-managementshadcn-uisysadmintailwindcssterminaltypescriptvpsweb-desktopweb-terminal
fix(workflows): pin private storage reads and atomic writes
Rahman87f07dd888cf
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.mdindex c377dfcb..00e873d1 100644--- a/docs/CHANGELOG.md+++ b/docs/CHANGELOG.md@@ -13,6 +13,7 @@ this is the *what*, and it is what Settings → About shows as “What's new”.**Fixed**+- `workflows` pin private storage reads and atomic writes- `integrations` preserve implicit direct connection compatibility- `workflows` harden reconciled artifact history reads- `integrations` prefer GitHub OAuthdiff --git a/docs/PROGRESS.md b/docs/PROGRESS.mdindex bb4d0390..541c3d01 100644--- a/docs/PROGRESS.md+++ b/docs/PROGRESS.md@@ -1,3 +1,21 @@+## 2026-09-17 — Descriptor-pinned Workflow storage hardening++The PR/branch audit exposed default-branch security annotations for Workflow run,+graph, version, variable, cache and recipe-archive storage. These stores now reuse+the existing owner-root and ancestor-pinning authority, open each file without+following links, inspect and read the same descriptor, bound reads against file+growth, and atomically write through a pinned parent. Version listing and exact+revision lookup both enforce full snapshot filenames, byte limits and matching+graph/revision identity. Receipt pruning still reads only bounded metadata rather+than parsing every receipt; existing formats, quotas and revision guards remain.++Added real filesystem regressions for symlinks, symlinked ancestors, FIFOs, unsafe+permissions, oversized/growing files, mismatched versions and unchanged atomic+serialization. The installed node-gyp PATH setup now separates assignment/export+so command failures are not masked. Scanner settings, ignores and alert thresholds+are unchanged. Passing local tests or scans does not close GitHub alerts: the+candidate still requires the normal PR checks and production release path.+## 2026-09-17 — Recover dormant PR/branch work without overwriting active agentsReconciled the verified performance candidate, the preserved GitHub OAuth-first preference,diff --git a/lib/workflow/cache-store.ts b/lib/workflow/cache-store.tsindex 1f24a824..4751f318 100644--- a/lib/workflow/cache-store.ts+++ b/lib/workflow/cache-store.ts@@ -1,9 +1,10 @@-import { createHash, randomUUID } from "node:crypto";-import { promises as fs } from "node:fs";+import { createHash } from "node:crypto";import path from "node:path";import { withSecurityStoreLock } from "@/lib/security-store-lock";import { agentSessionsDir } from "@/lib/agent/session-paths";+import { readWorkflowJson, writeWorkflowFile } from "./private-file";+const MAX_FILE_BYTES = 2 * 1024 * 1024;const MAX_VALUE_BYTES = 64 * 1024;type CacheEntry = { key: string; value: unknown; updatedAt: string; expiresAt?: string };@@ -16,14 +17,15 @@ function normalizedKey(raw: string) { const value = raw.trim(); if (!value || vafunction expired(entry: CacheEntry, now = Date.now()) { return Boolean(entry.expiresAt && Date.parse(entry.expiresAt) <= now); }async function read(file: string): Promise<CacheFile> {try {- const stat = await fs.lstat(file); if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_FILE_BYTES || (stat.mode & 0o077)) throw new Error("unsafe workflow cache store");- const parsed = JSON.parse(await fs.readFile(file, "utf8")) as CacheFile; if (parsed.version !== 1 || !parsed.entries || typeof parsed.entries !== "object") throw new Error("invalid workflow cache store");+ const parsed = await readWorkflowJson(file, MAX_FILE_BYTES, "workflow cache store") as CacheFile;+ if (parsed.version !== 1 || !parsed.entries || typeof parsed.entries !== "object") throw new Error("invalid workflow cache store");return parsed;} catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return { version: 1, entries: {} }; throw error; }}async function write(file: string, data: CacheFile) {- await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 }); const body = JSON.stringify(data); if (Buffer.byteLength(body) > MAX_FILE_BYTES) throw new Error("workflow cache store exceeds 2 MiB");- const temp = `${file}.${randomUUID()}.tmp`; try { await fs.writeFile(temp, body, { flag: "wx", mode: 0o600 }); await fs.rename(temp, file); await fs.chmod(file, 0o600); } finally { await fs.unlink(temp).catch(() => undefined); }+ const body = JSON.stringify(data);+ if (Buffer.byteLength(body) > MAX_FILE_BYTES) throw new Error("workflow cache store exceeds 2 MiB");+ await writeWorkflowFile(file, body);}export async function workflowCacheGet(principal: string, rawKey: string) {const file = target(principal), key = normalizedKey(rawKey); return withSecurityStoreLock(file, async () => {diff --git a/lib/workflow/graph-run-store.ts b/lib/workflow/graph-run-store.tsindex 5446e390..9f727dd9 100644--- a/lib/workflow/graph-run-store.ts+++ b/lib/workflow/graph-run-store.ts@@ -1,10 +1,10 @@-import { randomUUID } from "node:crypto";-import { promises as fs } from "node:fs";import path from "node:path";import { agentSessionsDir } from "@/lib/agent/session-paths";import { withSecurityStoreLock } from "@/lib/security-store-lock";import type { WorkflowGraphRun } from "@/lib/contracts/workflow-graph";+import { listWorkflowFiles, readWorkflowJson, removeWorkflowFile, workflowFileMtime, writeWorkflowFile } from "./private-file";+const MAX_RUN_BYTES = 1024 * 1024;const MAX_RECEIPTS = 1000;@@ -13,19 +13,10 @@ function file(owner: string, id: string): string {if (!/^[a-f0-9]{64}$/.test(owner) || !/^[a-f0-9]{32}$/.test(id)) throw new Error("invalid workflow graph run identity");return path.join(root(), owner, `${id}.json`);}-async function ensureOwnerDir(owner: string): Promise<void> {- const dir = path.dirname(file(owner, "0".repeat(32)));- await fs.mkdir(dir, { recursive: true, mode: 0o700 });- const stat = await fs.lstat(dir);- if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("unsafe workflow graph run directory");- await fs.chmod(dir, 0o700).catch(() => undefined);-}export async function readWorkflowGraphRun(owner: string, id: string): Promise<WorkflowGraphRun | null> {try {- const target = file(owner, id), stat = await fs.lstat(target);- if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_RUN_BYTES || (stat.mode & 0o077)) throw new Error("unsafe workflow graph run");- const parsed = JSON.parse(await fs.readFile(target, "utf8")) as WorkflowGraphRun;+ const parsed = await readWorkflowJson(file(owner, id), MAX_RUN_BYTES, "workflow graph run") as WorkflowGraphRun;if (parsed.version !== 1 || parsed.owner !== owner || parsed.id !== id || !Array.isArray(parsed.nodes)) throw new Error("invalid workflow graph run");return parsed;} catch (error) {@@ -35,13 +26,9 @@ export async function readWorkflowGraphRun(owner: string, id: string): Promise<W}export async function writeWorkflowGraphRun(run: WorkflowGraphRun): Promise<void> {- await ensureOwnerDir(run.owner);- const target = file(run.owner, run.id), temp = `${target}.${randomUUID()}.tmp`, body = JSON.stringify(run);+ const body = JSON.stringify(run);if (Buffer.byteLength(body) > MAX_RUN_BYTES) throw new Error("workflow graph run receipt exceeds 1 MiB");- try {- await fs.writeFile(temp, body, { flag: "wx", mode: 0o600 });- await fs.rename(temp, target);- } finally { await fs.unlink(temp).catch(() => undefined); }+ await writeWorkflowFile(file(run.owner, run.id), body);}export function lockWorkflowGraphRun<T>(owner: string, id: string, fn: () => Promise<T>): Promise<T> {@@ -49,15 +36,15 @@ export function lockWorkflowGraphRun<T>(owner: string, id: string, fn: () => Pro}export async function pruneWorkflowGraphRuns(owner: string): Promise<void> {- await ensureOwnerDir(owner);- const dir = path.dirname(file(owner, "0".repeat(32))), entries = await fs.readdir(dir);- const receipts: Array<{ name: string; mtime: number }> = [];+ const dir = path.dirname(file(owner, "0".repeat(32))), entries = await listWorkflowFiles(dir);+ const receipts: Array<{ id: string; mtime: number }> = [];for (const name of entries) {if (!/^[a-f0-9]{32}\.json$/.test(name)) continue;- const stat = await fs.stat(path.join(dir, name)); receipts.push({ name, mtime: stat.mtimeMs });+ const id = name.slice(0, 32), mtime = await workflowFileMtime(file(owner, id));+ receipts.push({ id, mtime });}receipts.sort((a, b) => b.mtime - a.mtime);- for (const old of receipts.slice(MAX_RECEIPTS - 1)) await fs.unlink(path.join(dir, old.name)).catch(() => undefined);+ for (const old of receipts.slice(MAX_RECEIPTS - 1)) await removeWorkflowFile(file(owner, old.id)).catch(() => undefined);}export function publicWorkflowGraphRun(run: WorkflowGraphRun) {@@ -67,8 +54,7 @@ export function publicWorkflowGraphRun(run: WorkflowGraphRun) {export type WorkflowGraphRunFilter = { graphId?: string; state?: WorkflowGraphRun["state"]; limit?: number; offset?: number };export async function listWorkflowGraphRuns(owner: string, filter: WorkflowGraphRunFilter = {}) {- await ensureOwnerDir(owner);- const dir = path.dirname(file(owner, "0".repeat(32))), names = (await fs.readdir(dir)).filter((name) => /^[a-f0-9]{32}\.json$/.test(name));+ const dir = path.dirname(file(owner, "0".repeat(32))), names = (await listWorkflowFiles(dir)).filter((name) => /^[a-f0-9]{32}\.json$/.test(name));const rows: WorkflowGraphRun[] = [];for (const name of names.slice(0, MAX_RECEIPTS)) {const row = await readWorkflowGraphRun(owner, name.slice(0, 32)).catch(() => null); if (!row) continue;diff --git a/lib/workflow/graph-store.ts b/lib/workflow/graph-store.tsindex b69e8080..c88e90d2 100644--- a/lib/workflow/graph-store.ts+++ b/lib/workflow/graph-store.ts@@ -8,6 +8,8 @@ import { normalizeWorkflowIntent, parseWorkflowGraphDefinition, type WorkflowGraimport { withSecurityStoreLock } from "@/lib/security-store-lock";import { saveWorkflowGraphVersion, type WorkflowGraphVersionReason } from "./graph-version-store";+import { readWorkflowJson, writeWorkflowFile } from "./private-file";+const STORE_VERSION = 2;const MAX_STORE_BYTES = 4 * 1024 * 1024;const MAX_GRAPHS = 200;@@ -23,14 +25,6 @@ function file(owner: string): string {}function graphRevision(graph: Omit<WorkflowGraph, "revision">): string { return hash(JSON.stringify(graph)); }-async function safeDirectory(owner: string): Promise<string> {- const dir = path.dirname(file(owner));- await fs.mkdir(dir, { recursive: true, mode: 0o700 });- const stat = await fs.lstat(dir);- if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("unsafe workflow graph directory");- await fs.chmod(dir, 0o700).catch(() => undefined);- return dir;-}function parseStoredGraph(raw: unknown): WorkflowGraph {if (!raw || typeof raw !== "object") throw new Error("invalid stored workflow graph");@@ -43,9 +37,7 @@ function parseStoredGraph(raw: unknown): WorkflowGraph {async function readStore(owner: string, principal = ""): Promise<WorkflowGraphStore> {const target = file(owner);try {- const stat = await fs.lstat(target);- if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_STORE_BYTES || (stat.mode & 0o077)) throw new Error("unsafe workflow graph store");- const parsed: unknown = JSON.parse(await fs.readFile(target, "utf8"));+ const parsed = await readWorkflowJson(target, MAX_STORE_BYTES, "workflow graph store");if (!parsed || typeof parsed !== "object") throw new Error("invalid workflow graph store");const row = parsed as { version?: unknown; owner?: unknown; principal?: unknown; graphs?: unknown };if (row.owner !== owner || !Array.isArray(row.graphs) || row.graphs.length > MAX_GRAPHS) throw new Error("invalid workflow graph store");@@ -59,14 +51,9 @@ async function readStore(owner: string, principal = ""): Promise<WorkflowGraphSt}async function writeStore(store: WorkflowGraphStore): Promise<void> {- await safeDirectory(store.owner);- const target = file(store.owner), temp = `${target}.${randomUUID()}.tmp`;const body = JSON.stringify(store, null, 2) + "\n";if (Buffer.byteLength(body) > MAX_STORE_BYTES) throw new Error("workflow graph store exceeds 4 MiB");- try {- await fs.writeFile(temp, body, { flag: "wx", mode: 0o600 });- await fs.rename(temp, target);- } finally { await fs.unlink(temp).catch(() => undefined); }+ await writeWorkflowFile(file(store.owner), body);}function withGraphLock<T>(owner: string, fn: () => Promise<T>): Promise<T> {diff --git a/lib/workflow/graph-version-security.test.ts b/lib/workflow/graph-version-security.test.tsnew file mode 100644index 00000000..05c8ffa8--- /dev/null+++ b/lib/workflow/graph-version-security.test.ts@@ -0,0 +1,42 @@+import { promises as fs } from "node:fs";+import os from "node:os";+import path from "node:path";+import { afterEach, expect, it, vi } from "vitest";+import { listWorkflowGraphVersions, readWorkflowGraphVersion, saveWorkflowGraphVersion } from "./graph-version-store";+import type { WorkflowGraph } from "@/lib/contracts/workflow-graph";+const roots: string[] = [];+async function fixture() {+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-version-security-")); roots.push(root);+ vi.stubEnv("OS_AGENT_SESSIONS_DIR", root);+ const owner = "a".repeat(64), revision = "b".repeat(64), graphId = "fixture";+ const dir = path.join(root, ".workflow-graph-versions", owner, graphId);+ await fs.mkdir(dir, { recursive: true, mode: 0o700 });+ return { root, owner, revision, graphId, dir };+}+afterEach(async () => { vi.unstubAllEnvs(); for (const root of roots.splice(0)) await fs.rm(root, { recursive: true, force: true }); });+it("uses an anchored snapshot filename rather than accepting an arbitrary suffix", async () => {+ const f = await fixture();+ await fs.writeFile(path.join(f.dir, `unexpected-${f.revision}.json`), "{}", { mode: 0o600 });+ expect(await readWorkflowGraphVersion(f.owner, f.graphId, f.revision)).toBeNull();+ expect(await listWorkflowGraphVersions(f.owner, f.graphId)).toEqual([]);+});+it("refuses symlink snapshots in both the history list and exact revision reads", async () => {+ const f = await fixture(), outside = path.join(f.root, "outside.json");+ await fs.writeFile(outside, "{}", { mode: 0o600 });+ await fs.symlink(outside, path.join(f.dir, `1-${f.revision}.json`));+ await expect(readWorkflowGraphVersion(f.owner, f.graphId, f.revision)).rejects.toThrow();+ await expect(listWorkflowGraphVersions(f.owner, f.graphId)).rejects.toThrow();+});+it("rejects oversized listing snapshots and mismatched graph identity", async () => {+ const f = await fixture(), file = path.join(f.dir, `1-${f.revision}.json`);+ await fs.writeFile(file, " ".repeat(321 * 1024), { mode: 0o600 });+ await expect(listWorkflowGraphVersions(f.owner, f.graphId)).rejects.toThrow("unsafe workflow version snapshot");+ await fs.writeFile(file, JSON.stringify({ revision: f.revision, savedAt: new Date(1).toISOString(), reason: "create", graph: { id: "another", revision: f.revision, name: "Other", nodes: [] } }));+ await expect(readWorkflowGraphVersion(f.owner, f.graphId, f.revision)).rejects.toThrow("invalid workflow version snapshot");+});+it("rejects an invalid revision before creating a version file", async () => {+ const f = await fixture();+ const graph = { id: f.graphId, revision: "../../escape" } as WorkflowGraph;+ await expect(saveWorkflowGraphVersion(f.owner, graph, "create")).rejects.toThrow("invalid workflow revision");+ expect(await fs.readdir(f.dir)).toEqual([]);+});diff --git a/lib/workflow/graph-version-store.ts b/lib/workflow/graph-version-store.tsindex 616cbc23..306cbee3 100644--- a/lib/workflow/graph-version-store.ts+++ b/lib/workflow/graph-version-store.ts@@ -1,45 +1,53 @@-import { randomUUID } from "node:crypto";-import { promises as fs } from "node:fs";import path from "node:path";import type { WorkflowGraph } from "@/lib/contracts/workflow-graph";import { agentSessionsDir } from "@/lib/agent/session-paths";+import { listWorkflowFiles, readWorkflowJson, removeWorkflowFile, writeWorkflowFile } from "./private-file";const MAX_VERSIONS = 100;const MAX_BYTES = 320 * 1024;+const REVISION = /^[a-f0-9]{64}$/;+const SNAPSHOT_NAME = /^\d+-[a-f0-9]{64}\.json$/;export type WorkflowGraphVersionReason = "create" | "update" | "restore" | "import" | "template" | "ai-assisted";export type WorkflowGraphVersion = { revision: string; savedAt: string; reason: WorkflowGraphVersionReason; graph: WorkflowGraph };-function safe(owner: string, graphId: string) {+function directory(owner: string, graphId: string) {if (!/^[a-f0-9]{64}$/.test(owner) || !/^[A-Za-z0-9][A-Za-z0-9_.:-]{0,95}$/.test(graphId)) throw new Error("invalid workflow version identity");+ return path.join(agentSessionsDir(), ".workflow-graph-versions", owner, graphId);+}+function filename(row: WorkflowGraphVersion) {+ if (!REVISION.test(row.revision) || !Number.isFinite(Date.parse(row.savedAt))) throw new Error("invalid workflow revision");+ return `${Date.parse(row.savedAt)}-${row.revision}.json`;+}+async function snapshot(dir: string, graphId: string, name: string): Promise<WorkflowGraphVersion> {+ if (!SNAPSHOT_NAME.test(name)) throw new Error("invalid workflow version filename");+ const row = await readWorkflowJson(path.join(dir, path.basename(name)), MAX_BYTES, "workflow version snapshot") as WorkflowGraphVersion;+ if (!row || !row.graph || row.graph.id !== graphId || row.graph.revision !== row.revision || !Array.isArray(row.graph.nodes) ||+ typeof row.graph.name !== "string" || typeof row.reason !== "string" || filename(row) !== name) throw new Error("invalid workflow version snapshot");+ return row;}-function directory(owner: string, graphId: string) { safe(owner, graphId); return path.join(agentSessionsDir(), ".workflow-graph-versions", owner, graphId); }-function filename(row: WorkflowGraphVersion) { return `${Date.parse(row.savedAt)}-${row.revision}.json`; }export async function saveWorkflowGraphVersion(owner: string, graph: WorkflowGraph, reason: WorkflowGraphVersionReason): Promise<void> {- const dir = directory(owner, graph.id); await fs.mkdir(dir, { recursive: true, mode: 0o700 }); await fs.chmod(dir, 0o700).catch(() => undefined);+ const dir = directory(owner, graph.id);const row: WorkflowGraphVersion = { revision: graph.revision, savedAt: new Date().toISOString(), reason, graph: structuredClone(graph) };- const body = JSON.stringify(row), target = path.join(dir, filename(row)), temp = `${target}.${randomUUID()}.tmp`;+ const body = JSON.stringify(row), target = path.join(dir, filename(row));if (Buffer.byteLength(body) > MAX_BYTES) throw new Error("workflow version snapshot too large");- try { await fs.writeFile(temp, body, { flag: "wx", mode: 0o600 }); await fs.rename(temp, target); } finally { await fs.unlink(temp).catch(() => undefined); }- const entries = (await fs.readdir(dir)).filter((name) => /^\d+-[a-f0-9]{64}\.json$/.test(name)).sort().reverse();- for (const name of entries.slice(MAX_VERSIONS)) await fs.unlink(path.join(dir, name)).catch(() => undefined);+ await writeWorkflowFile(target, body);+ const entries = (await listWorkflowFiles(dir)).filter((name) => SNAPSHOT_NAME.test(name)).sort().reverse();+ for (const name of entries.slice(MAX_VERSIONS)) await removeWorkflowFile(path.join(dir, path.basename(name))).catch(() => undefined);}export async function listWorkflowGraphVersions(owner: string, graphId: string) {- const dir = directory(owner, graphId); let entries: string[];- try { entries = await fs.readdir(dir); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return []; throw error; }- const rows = entries.filter((name) => /^\d+-[a-f0-9]{64}\.json$/.test(name)).sort().reverse().slice(0, MAX_VERSIONS);+ const dir = directory(owner, graphId);+ const rows = (await listWorkflowFiles(dir)).filter((name) => SNAPSHOT_NAME.test(name)).sort().reverse().slice(0, MAX_VERSIONS);return Promise.all(rows.map(async (name) => {- const row = JSON.parse(await fs.readFile(path.join(dir, name), "utf8")) as WorkflowGraphVersion;+ const row = await snapshot(dir, graphId, name);return { revision: row.revision, savedAt: row.savedAt, reason: row.reason, name: row.graph.name, nodeCount: row.graph.nodes.length };}));}export async function readWorkflowGraphVersion(owner: string, graphId: string, revision: string): Promise<WorkflowGraphVersion | null> {- if (!/^[a-f0-9]{64}$/.test(revision)) throw new Error("invalid workflow revision");- const dir = directory(owner, graphId); let entries: string[];- try { entries = await fs.readdir(dir); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; throw error; }- const name = entries.find((entry) => entry.endsWith(`-${revision}.json`)); if (!name) return null;- const stat = await fs.lstat(path.join(dir, name)); if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_BYTES || (stat.mode & 0o077)) throw new Error("unsafe workflow version snapshot");- return JSON.parse(await fs.readFile(path.join(dir, name), "utf8")) as WorkflowGraphVersion;+ if (!REVISION.test(revision)) throw new Error("invalid workflow revision");+ const dir = directory(owner, graphId);+ const name = (await listWorkflowFiles(dir)).find((entry) => SNAPSHOT_NAME.test(entry) && entry.endsWith(`-${revision}.json`));+ return name ? snapshot(dir, graphId, name) : null;}diff --git a/lib/workflow/private-file.test.ts b/lib/workflow/private-file.test.tsnew file mode 100644index 00000000..a91c196f--- /dev/null+++ b/lib/workflow/private-file.test.ts@@ -0,0 +1,93 @@+import { constants, promises as fs } from "node:fs";+import { execFileSync } from "node:child_process";+import os from "node:os";+import path from "node:path";+import { afterEach, describe, expect, it, vi } from "vitest";+import { listWorkflowFiles, readWorkflowJson, removeWorkflowFile, writeWorkflowFile } from "./private-file";++const roots: string[] = [];+async function fixture() { const root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-workflow-file-")); roots.push(root); return root; }+afterEach(async () => { vi.restoreAllMocks(); for (const root of roots.splice(0)) await fs.rm(root, { recursive: true, force: true }); });++describe("private workflow file descriptors", () => {+ it("preserves atomic file format, permissions and directory listing", async () => {+ const root = await fixture(), target = path.join(root, "nested", "store.json");+ await writeWorkflowFile(target, '{"value":1}');+ expect(await readWorkflowJson(target, 100, "fixture")).toEqual({ value: 1 });+ await writeWorkflowFile(target, '{"value":2}');+ expect(await readWorkflowJson(target, 100, "fixture")).toEqual({ value: 2 });+ expect((await fs.stat(target)).mode & 0o777).toBe(0o600);+ expect(await listWorkflowFiles(path.dirname(target))).toEqual(["store.json"]);+ await removeWorkflowFile(target);+ await expect(readWorkflowJson(target, 100, "fixture")).rejects.toMatchObject({ code: "ENOENT" });+ });+ it("refuses final symlinks, oversized data, public permissions and FIFOs without blocking", async () => {+ const root = await fixture(), target = path.join(root, "store.json"), outside = path.join(root, "outside.json");+ await fs.writeFile(outside, '{"secret":"untouched"}', { mode: 0o600 });+ await fs.symlink(outside, target);+ await expect(readWorkflowJson(target, 100, "fixture")).rejects.toThrow("unsafe fixture");+ await fs.unlink(target);+ await fs.writeFile(target, " ".repeat(1024), { mode: 0o600 });+ await expect(readWorkflowJson(target, 100, "fixture")).rejects.toThrow("unsafe fixture");+ await fs.writeFile(target, "{}"); await fs.chmod(target, 0o644);+ await expect(readWorkflowJson(target, 100, "fixture")).rejects.toThrow("unsafe fixture");+ await fs.unlink(target); execFileSync("mkfifo", ["-m", "600", target]);+ await expect(readWorkflowJson(target, 100, "fixture")).rejects.toThrow("unsafe fixture");+ expect(await fs.readFile(outside, "utf8")).toBe('{"secret":"untouched"}');+ });+ it("rejects a symlinked parent before reads or writes can touch its destination", async () => {+ const root = await fixture(), outside = await fixture(), alias = path.join(root, "alias");+ await fs.symlink(outside, alias);+ await expect(writeWorkflowFile(path.join(alias, "store.json"), "{}")).rejects.toThrow();+ await expect(readWorkflowJson(path.join(alias, "store.json"), 100, "fixture")).rejects.toThrow();+ expect(await fs.readdir(outside)).toEqual([]);+ });+ it("does not reopen a pathname after validating a file descriptor", async () => {+ const root = await fixture(), target = path.join(root, "store.json");+ await fs.writeFile(target, '{"value":1}', { mode: 0o600 });+ const namedRead = vi.spyOn(fs, "readFile").mockRejectedValue(new Error("path read forbidden"));+ expect(await readWorkflowJson(target, 100, "fixture")).toEqual({ value: 1 });+ expect(namedRead).not.toHaveBeenCalled();+ });+ it("keeps the validated snapshot readable during a legitimate atomic replacement", async () => {+ const root = await fixture(), target = path.join(root, "store.json");+ await fs.writeFile(target, '{"value":"original"}', { mode: 0o600 });+ const original = fs.open.bind(fs);+ vi.spyOn(fs, "open").mockImplementation(async (...args) => {+ const handle = await original(...args);+ if (String(args[0]).endsWith("/store.json") && args[1] === (constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK)) {+ const stat = handle.stat.bind(handle); let first = true;+ vi.spyOn(handle, "stat").mockImplementation(async () => {+ const result = await stat();+ if (first) {+ first = false;+ await fs.writeFile(target + ".replacement", '{"value":"replacement"}', { mode: 0o600 });+ await fs.rename(target + ".replacement", target);+ }+ return result;+ });+ }+ return handle;+ });+ expect(await readWorkflowJson(target, 100, "fixture")).toEqual({ value: "original" });+ expect(await fs.readFile(target, "utf8")).toBe('{"value":"replacement"}');+ });+ it("detects file growth after opening without reading an unbounded stream", async () => {+ const root = await fixture(), target = path.join(root, "store.json");+ await fs.writeFile(target, "{}", { mode: 0o600 });+ const original = fs.open.bind(fs);+ vi.spyOn(fs, "open").mockImplementation(async (...args) => {+ const handle = await original(...args);+ if (String(args[0]).endsWith("/store.json") && args[1] === (constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK)) {+ const stat = handle.stat.bind(handle); let first = true;+ vi.spyOn(handle, "stat").mockImplementation(async () => {+ const result = await stat();+ if (first) { first = false; await fs.appendFile(target, " ".repeat(1024)); }+ return result;+ });+ }+ return handle;+ });+ await expect(readWorkflowJson(target, 100, "fixture")).rejects.toThrow("changed while reading");+ });+});diff --git a/lib/workflow/private-file.ts b/lib/workflow/private-file.tsnew file mode 100644index 00000000..118840d0--- /dev/null+++ b/lib/workflow/private-file.ts@@ -0,0 +1,83 @@+import { randomUUID } from "node:crypto";+import { constants, promises as fs } from "node:fs";+import path from "node:path";+import { pinSecurityStorePath } from "@/lib/security-store-path";++/** Read one bounded snapshot through the same descriptor that was validated.+ * The existing security-store authority pins every ancestor; a pathname swap+ * cannot redirect either the size/permission check or the subsequent read.+ */+export async function readWorkflowJson(file: string, maximumBytes: number, label: string): Promise<unknown> {+ const pinned = await pinSecurityStorePath(file);+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;+ try {+ handle = await fs.open(/* turbopackIgnore: true */ pinned.file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);+ const before = await handle.stat();+ if (!before.isFile() || before.size <= 0 || before.size > maximumBytes || (before.mode & 0o077) || before.uid !== process.getuid?.()) {+ throw new Error(`unsafe ${label}`);+ }+ const bytes = Buffer.alloc(before.size + 1);+ let used = 0;+ while (used < bytes.length) {+ const { bytesRead } = await handle.read(bytes, used, bytes.length - used, used);+ if (!bytesRead) break;+ used += bytesRead;+ }+ const after = await handle.stat();+ // Atomic replacement unlinks the old inode and changes ctime, while its+ // open descriptor remains a valid immutable snapshot. Only that case is+ // permitted; growth, content timestamps and unsafe permissions still fail.+ const replacedSnapshot = after.nlink === 0 && after.dev === before.dev && after.ino === before.ino;+ if (used !== before.size || after.size !== before.size || after.mtimeMs !== before.mtimeMs ||+ (after.ctimeMs !== before.ctimeMs && !replacedSnapshot) || (after.mode & 0o077) || after.uid !== before.uid) {+ throw new Error(`${label} changed while reading`);+ }+ return JSON.parse(bytes.subarray(0, used).toString("utf8"));+ } catch (error) {+ if (["ELOOP", "ENOTDIR"].includes((error as NodeJS.ErrnoException).code ?? "")) throw new Error(`unsafe ${label}`);+ throw error;+ } finally { await handle?.close(); await pinned.directory.close(); }+}++/** Preserve the existing atomic JSON format; never chmod or reopen a renamed pathname. */+export async function writeWorkflowFile(file: string, body: string): Promise<void> {+ const pinned = await pinSecurityStorePath(file);+ const temporary = `${pinned.file}.${randomUUID()}.tmp`;+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;+ try {+ await pinned.directory.chmod(0o700);+ handle = await fs.open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);+ await handle.writeFile(body, "utf8");+ await handle.close(); handle = undefined;+ await fs.rename(temporary, pinned.file);+ } finally {+ await handle?.close();+ await fs.unlink(temporary).catch(() => undefined);+ await pinned.directory.close();+ }+}++/** Directory entries are only names, never trusted paths. Callers validate each basename. */+export async function listWorkflowFiles(directory: string): Promise<string[]> {+ const pinned = await pinSecurityStorePath(path.join(directory, ".index"));+ try { return await fs.readdir(/* turbopackIgnore: true */ path.dirname(pinned.file)); }+ finally { await pinned.directory.close(); }+}++export async function removeWorkflowFile(file: string): Promise<void> {+ const pinned = await pinSecurityStorePath(file);+ try { await fs.unlink(pinned.file); }+ finally { await pinned.directory.close(); }+}++/** Pruning needs timestamps, not JSON parsing or receipt contents. */+export async function workflowFileMtime(file: string): Promise<number> {+ const pinned = await pinSecurityStorePath(file);+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;+ try {+ handle = await fs.open(/* turbopackIgnore: true */ pinned.file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);+ const stat = await handle.stat();+ if (!stat.isFile() || (stat.mode & 0o077) || stat.uid !== process.getuid?.()) throw new Error("unsafe workflow receipt");+ return stat.mtimeMs;+ } finally { await handle?.close(); await pinned.directory.close(); }+}diff --git a/lib/workflow/recipe-archive.ts b/lib/workflow/recipe-archive.tsindex 0a7c55af..8d680753 100644--- a/lib/workflow/recipe-archive.ts+++ b/lib/workflow/recipe-archive.ts@@ -1,11 +1,12 @@-import { createHash, randomUUID } from "node:crypto";-import { constants, promises as fs } from "node:fs";+import { createHash } from "node:crypto";import path from "node:path";import { normalizeRecipes } from "./sanitize";import { workflowStorePath } from "./storage";import type { LearnedRecipe, RecipeAccess } from "./types";import { allows } from "@/lib/capabilities/scope";+import { listWorkflowFiles, readWorkflowJson, writeWorkflowFile } from "./private-file";+const SEGMENT_RE = /^segment-[a-f0-9]{32}\.json$/;const MAX_SEGMENT_BYTES = 2 * 1024 * 1024;const MAX_ARCHIVED_RECIPES = 5000;@@ -15,42 +16,30 @@ function archiveDir(): string { return `${workflowStorePath()}.archive-v1`; }function body(recipes: LearnedRecipe[], archivedAt: string): string {return JSON.stringify({ schemaVersion: 1, archivedAt, recipes: Object.fromEntries(recipes.map((recipe) => [recipe.id, recipe])) } satisfies Segment);}-async function secureDir(): Promise<string> {- const dir = archiveDir(); await fs.mkdir(dir, { recursive: true, mode: 0o700 }); await fs.chmod(dir, 0o700).catch(() => undefined);- const stat = await fs.lstat(dir); if (!stat.isDirectory() || stat.isSymbolicLink() || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) throw new Error("unsafe workflow recipe archive directory");- return dir;-}async function readSegment(file: string): Promise<Segment> {- let handle: Awaited<ReturnType<typeof fs.open>> | undefined;- try {- handle = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW); const stat = await handle.stat();- if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_SEGMENT_BYTES || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) throw new Error("workflow recipe archive segment has an invalid file shape");- const parsed = JSON.parse(await handle.readFile("utf8")) as Partial<Segment>;- if (parsed.schemaVersion !== 1 || typeof parsed.archivedAt !== "string" || !Number.isFinite(Date.parse(parsed.archivedAt)) || !parsed.recipes || typeof parsed.recipes !== "object") throw new Error("workflow recipe archive segment has an invalid schema");- const recipes = normalizeRecipes(parsed.recipes);- return { schemaVersion: 1, archivedAt: new Date(parsed.archivedAt).toISOString(), recipes };- } finally { await handle?.close().catch(() => undefined); }+ const parsed = await readWorkflowJson(file, MAX_SEGMENT_BYTES, "workflow recipe archive segment") as Partial<Segment>;+ if (parsed.schemaVersion !== 1 || typeof parsed.archivedAt !== "string" || !Number.isFinite(Date.parse(parsed.archivedAt)) || !parsed.recipes || typeof parsed.recipes !== "object") throw new Error("workflow recipe archive segment has an invalid schema");+ const recipes = normalizeRecipes(parsed.recipes);+ return { schemaVersion: 1, archivedAt: new Date(parsed.archivedAt).toISOString(), recipes };}export async function archiveLearnedRecipes(recipes: LearnedRecipe[]): Promise<{ recipes: number; segment?: string }> {if (!recipes.length) return { recipes: 0 };const archivedAt = new Date().toISOString(), snapshot = body(recipes, archivedAt);if (Buffer.byteLength(snapshot) > MAX_SEGMENT_BYTES) throw new Error("workflow recipe archive segment exceeds 2 MiB");- const dir = await secureDir(), digest = createHash("sha256").update(snapshot).digest("hex").slice(0, 32), name = `segment-${digest}.json`, file = path.join(dir, name);+ const dir = archiveDir(), digest = createHash("sha256").update(snapshot).digest("hex").slice(0, 32), name = `segment-${digest}.json`, file = path.join(dir, name);try { const existing = await readSegment(file); if (Object.keys(existing.recipes).length === recipes.length) return { recipes: recipes.length, segment: name }; }catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }- const tmp = `${file}.${randomUUID()}.tmp`;- try { await fs.writeFile(tmp, snapshot, { flag: "wx", mode: 0o600 }); await fs.rename(tmp, file); await fs.chmod(file, 0o600); }- finally { await fs.unlink(tmp).catch(() => undefined); }+ await writeWorkflowFile(file, snapshot);return { recipes: recipes.length, segment: name };}export async function listArchivedLearnedRecipes(access: RecipeAccess): Promise<LearnedRecipe[]> {- let names: string[]; try { names = (await fs.readdir(archiveDir())).filter((name) => SEGMENT_RE.test(name)).sort().reverse().slice(0, 4096); }+ let names: string[]; try { names = (await listWorkflowFiles(archiveDir())).filter((name) => SEGMENT_RE.test(name)).sort().reverse().slice(0, 4096); }catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return []; throw error; }const byId = new Map<string, LearnedRecipe>();for (const name of names) {- const segment = await readSegment(path.join(archiveDir(), name));+ const segment = await readSegment(path.join(archiveDir(), path.basename(name)));for (const recipe of Object.values(segment.recipes)) {const visible = access.ownerView || (recipe.actor === access.actor && allows(access.scope, recipe.scope));if (!visible) continue;diff --git a/lib/workflow/variables.ts b/lib/workflow/variables.tsindex 7b5ba288..41581a9d 100644--- a/lib/workflow/variables.ts+++ b/lib/workflow/variables.ts@@ -1,17 +1,27 @@-import { randomUUID } from "node:crypto";-import { promises as fs } from "node:fs";import path from "node:path";import { agentSessionsDir } from "@/lib/agent/session-paths";import { withSecurityStoreLock } from "@/lib/security-store-lock";import { workflowGraphOwner } from "./graph-store";+import { readWorkflowJson, writeWorkflowFile } from "./private-file";+export type WorkflowVariableValue = string | number | boolean | null | Record<string, unknown> | unknown[];type Row = { value: WorkflowVariableValue; secret: boolean; updatedAt: string };type Store = { version: 1; owner: string; values: Record<string, Row> };const KEY = /^[A-Z][A-Z0-9_]{0,63}$/;function file(owner: string) { if (!/^[a-f0-9]{64}$/.test(owner)) throw new Error("invalid workflow variable owner"); return path.join(agentSessionsDir(), ".workflow-variables", owner, "variables.json"); }-async function read(owner: string): Promise<Store> { try { const target=file(owner), stat=await fs.lstat(target); if(!stat.isFile()||stat.isSymbolicLink()||stat.size>256*1024||(stat.mode&0o077)) throw new Error("unsafe workflow variable store"); const row=JSON.parse(await fs.readFile(target,"utf8")) as Store; if(row.version!==1||row.owner!==owner||!row.values||typeof row.values!=="object") throw new Error("invalid workflow variable store"); return row; } catch(error){ if((error as NodeJS.ErrnoException).code==="ENOENT") return {version:1,owner,values:{}}; throw error; } }-async function write(store: Store){ const target=file(store.owner),dir=path.dirname(target); await fs.mkdir(dir,{recursive:true,mode:0o700}); const body=JSON.stringify(store),tmp=`${target}.${randomUUID()}.tmp`; if(Buffer.byteLength(body)>256*1024) throw new Error("workflow variable store too large"); try{await fs.writeFile(tmp,body,{flag:"wx",mode:0o600});await fs.rename(tmp,target);}finally{await fs.unlink(tmp).catch(()=>undefined);} }+async function read(owner: string): Promise<Store> {+ try {+ const row = await readWorkflowJson(file(owner), 256 * 1024, "workflow variable store") as Store;+ if (row.version !== 1 || row.owner !== owner || !row.values || typeof row.values !== "object") throw new Error("invalid workflow variable store");+ return row;+ } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return { version: 1, owner, values: {} }; throw error; }+}+async function write(store: Store) {+ const body = JSON.stringify(store);+ if (Buffer.byteLength(body) > 256 * 1024) throw new Error("workflow variable store too large");+ await writeWorkflowFile(file(store.owner), body);+}function validValue(value: unknown): asserts value is WorkflowVariableValue { const body=JSON.stringify(value); if(value===undefined||!body||Buffer.byteLength(body)>16*1024) throw new Error("workflow variable value invalid"); }export async function listWorkflowVariables(principal:string){ const owner=workflowGraphOwner(principal),store=await read(owner); return Object.entries(store.values).sort(([a],[b])=>a.localeCompare(b)).map(([key,row])=>({key,secret:row.secret,updatedAt:row.updatedAt,...(!row.secret?{value:structuredClone(row.value)}:{})})); }export async function setWorkflowVariable(principal:string,key:string,value:unknown,secret=false){ if(!KEY.test(key)) throw new Error("workflow variable key must be A-Z, 0-9, underscore"); validValue(value); const owner=workflowGraphOwner(principal); return withSecurityStoreLock(file(owner),async()=>{const store=await read(owner); if(!Object.hasOwn(store.values,key)&&Object.keys(store.values).length>=128) throw new Error("workflow variable limit reached"); store.values[key]={value:structuredClone(value),secret:Boolean(secret),updatedAt:new Date().toISOString()}; await write(store); return {key,secret:Boolean(secret),updatedAt:store.values[key]!.updatedAt};}); }diff --git a/scripts/install/runtime-build.sh b/scripts/install/runtime-build.shindex efa1eba8..0a79cf7e 100755--- a/scripts/install/runtime-build.sh+++ b/scripts/install/runtime-build.sh@@ -23,7 +23,8 @@ NODE_GYP_BIN="$NODE_GYP_PREFIX/node_modules/.bin/node-gyp"ensure_node_gyp_runner() {if [ -x "$NODE_GYP_BIN" ] && [ "$("$NODE_GYP_BIN" --version 2>/dev/null || true)" = "v$NODE_GYP_VERSION" ]; then- export PATH="$(dirname "$NODE_GYP_BIN"):$PATH"+ PATH="$(dirname "$NODE_GYP_BIN"):$PATH"+ export PATHreturnficommand -v npm >/dev/null 2>&1 || die "npm is required to provision pinned node-gyp for node-pty."@@ -32,7 +33,8 @@ ensure_node_gyp_runner() {npm install --prefix "$NODE_GYP_PREFIX" --no-save --ignore-scripts "node-gyp@$NODE_GYP_VERSION" >/dev/null[ -x "$NODE_GYP_BIN" ] || die "node-gyp bootstrap did not produce $NODE_GYP_BIN"[ "$("$NODE_GYP_BIN" --version 2>/dev/null || true)" = "v$NODE_GYP_VERSION" ] || die "unexpected node-gyp version after bootstrap"- export PATH="$(dirname "$NODE_GYP_BIN"):$PATH"+ PATH="$(dirname "$NODE_GYP_BIN"):$PATH"+ export PATH}node_pty_ready() {