Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): make a leaked PORTOS_DATA_ROOT harmless in worktree agents
[._.]/ Adam Eivy470edd5e1936
diff --git a/ecosystem.config.cjs b/ecosystem.config.cjsindex 68a6a42cc5..301582d307 100644--- a/ecosystem.config.cjs+++ b/ecosystem.config.cjs@@ -8,12 +8,7 @@ const IS_WIN = process.platform === 'win32';// Shared env inherited by all apps (merged into each app's env)const BASE_ENV = {NODE_ENV: 'development',- TZ: 'UTC', // All log timestamps and Date operations in UTC- // Pin the install root explicitly so data-root resolution never derives it- // from the executing file's location. A server/agent booted from inside a CoS- // git worktree (data/cos/worktrees/agent-*) would otherwise resolve `data/`- // to the worktree's nonexistent tree and crash boot migrations (#1947).- PORTOS_DATA_ROOT: __dirname+ TZ: 'UTC' // All log timestamps and Date operations in UTC};// Read a couple of machine-local settings from .env (pm2 doesn't auto-load it@@ -69,6 +64,15 @@ module.exports = {windowsHide: IS_WIN,env: {...BASE_ENV,+ // Pin the install root explicitly so data-root resolution never derives+ // it from the executing file's location. A server booted from inside a+ // CoS git worktree (data/cos/worktrees/agent-*) would otherwise resolve+ // `data/` to the worktree's nonexistent tree and crash boot migrations+ // (#1947). Set ONLY here (not BASE_ENV) so the portos-cos runner — which+ // spreads its env into agent CLI children — never leaks it into worktree+ // agents; fileUtils/resolveInstallRoot also refuse a leaked pin when the+ // executing code is itself in a worktree, as belt-and-suspenders.+ PORTOS_DATA_ROOT: __dirname,PORT: PORTS.API,PORTOS_HTTP_PORT: PORTS.API_LOCAL, // Loopback HTTP mirror when HTTPS is activeHOST: '0.0.0.0',diff --git a/server/lib/dataRoot.js b/server/lib/dataRoot.jsindex 74f4f34ef8..7d47f1e5ec 100644--- a/server/lib/dataRoot.js+++ b/server/lib/dataRoot.js@@ -23,26 +23,36 @@ export const DATA_ROOT_ENV = 'PORTOS_DATA_ROOT';/*** Resolve the PortOS install root (the parent of `data/` and `data.reference/`).*- * Prefers an explicit `PORTOS_DATA_ROOT` env var (set at real process launch)- * over the caller's `import.meta.url`-derived fallback, so a process booted- * from inside a git worktree still resolves to the real install. Returns- * `fallbackRoot` unchanged when the env var is unset or blank — preserving the- * existing behavior for installs that never set it (backward compatible).+ * Prefers an explicit `PORTOS_DATA_ROOT` env var (set at a real launch in+ * `ecosystem.config.cjs`) over the caller's `import.meta.url`-derived fallback,+ * pinning the primary install's root independent of the executing-file location.+ * Returns `fallbackRoot` unchanged when the env var is unset or blank —+ * preserving the existing behavior for installs that never set it (backward+ * compatible).*- * By design this is pinned only at a REAL launch (`ecosystem.config.cjs`). It is- * intentionally NOT propagated into CoS agent worktree shells (`shellService`- * rebuilds a scrubbed env): a worktree process must NOT silently run migrations- * or data writes against the real `data/` tree using worktree-version code — a- * version-skew data-corruption hazard, and a `:5555` collision with the real- * server. There the env var stays unset, so resolution falls back to the- * worktree path and `isWorktreeRoot()` makes the boot-migration pass skip- * (no crash). An agent that genuinely wants the real data tree opts in by- * setting `PORTOS_DATA_ROOT` explicitly.+ * The var is set ONLY on the `portos-server` app (not the shared `BASE_ENV`), so+ * the `portos-cos` runner — which spreads its `process.env` into agent CLI+ * children — never carries it into worktree agents. As belt-and-suspenders, this+ * resolver ALSO refuses the pin whenever the executing location (`fallbackRoot`)+ * is itself a worktree checkout: even a leaked `PORTOS_DATA_ROOT` can't make+ * worktree-version code resolve `PATHS.data` to the LIVE install and read/write+ * real data. In that case resolution stays on the worktree path, where the+ * migration backstop safely skips and `PATHS.data` points at the worktree's own+ * (empty) tree — no crash, no live-data corruption.** @param {string} fallbackRoot absolute path derived from the caller's location* @returns {string} the install root to use*/export function resolveInstallRoot(fallbackRoot) {+ // Leak-safety (#1947): a process whose CODE physically lives inside a worktree+ // checkout must NEVER honor a PORTOS_DATA_ROOT pin. The var is inherited by+ // long-lived PM2 processes and can leak into agent child envs (spread of+ // process.env), so a worktree-launched command could otherwise resolve+ // PATHS.data to the LIVE install and let worktree-version code read/write real+ // data. When the fallback (the executing location) is itself a worktree, ignore+ // the override and stay on the worktree path — where the migration backstop+ // safely skips and PATHS.data points at the worktree's own (empty) tree.+ if (isWorktreeRoot(fallbackRoot)) return fallbackRoot;const override = process.env[DATA_ROOT_ENV];if (typeof override === 'string' && override.trim() !== '') {const trimmed = override.trim();diff --git a/server/lib/dataRoot.test.js b/server/lib/dataRoot.test.jsindex 05046867a9..f41ef28f9c 100644--- a/server/lib/dataRoot.test.js+++ b/server/lib/dataRoot.test.js@@ -32,10 +32,13 @@ describe('resolveInstallRoot', () => {expect(out).not.toBe(REAL_ROOT);});- it('pins the real root even when the fallback is a worktree path', () => {+ it('IGNORES the pin when the fallback is a worktree checkout (leak safety #1947)', () => {+ // A worktree-executing process must never honor a (possibly-leaked) pin —+ // resolving its data root to the live install would let worktree code+ // read/write real data. It stays on the worktree path instead.process.env[DATA_ROOT_ENV] = REAL_ROOT;const worktreeFallback = join(REAL_ROOT, 'data', 'cos', 'worktrees', 'agent-abc');- expect(resolveInstallRoot(worktreeFallback)).toBe(REAL_ROOT);+ expect(resolveInstallRoot(worktreeFallback)).toBe(worktreeFallback);});});