Skip to content

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

fix([issue-1089]): address review — guard realpathSync + test the opt-in gate

Adam Eivy3d1f21bdbcc5
diff --git a/server/lib/workspaceRoots.test.js b/server/lib/workspaceRoots.test.js
index fd078ef359..1ec4fee62e 100644
--- a/server/lib/workspaceRoots.test.js
+++ b/server/lib/workspaceRoots.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, afterEach, vi } from 'vitest';
import { homedir } from 'os';
import { join } from 'path';
import {
@@ -52,3 +52,38 @@ describe('ALLOWED_WORKSPACE_ROOTS', () => {
expect(ALLOWED_WORKSPACE_ROOTS.every(r => typeof r === 'string' && r.length > 0)).toBe(true);
});
});
+
+// PORTOS_WORKSPACE_ROOTS is read once at module load, so these re-import a fresh
+// copy of the module under a stubbed env to exercise the opt-in gate (the actual
+// subject of issue #1089) in both states.
+describe('PORTOS_WORKSPACE_ROOTS opt-in gate', () => {
+ afterEach(() => {
+ vi.unstubAllEnvs();
+ vi.resetModules();
+ });
+
+ it('WORKSPACE_ROOTS_CONFIGURED is false when the env var is unset', async () => {
+ vi.stubEnv('PORTOS_WORKSPACE_ROOTS', '');
+ vi.resetModules();
+ const mod = await import('./workspaceRoots.js');
+ expect(mod.WORKSPACE_ROOTS_CONFIGURED).toBe(false);
+ expect(mod.EXTRA_WORKSPACE_ROOTS).toEqual([]);
+ });
+
+ it('parses colon-separated roots, trimming and dropping empties', async () => {
+ vi.stubEnv('PORTOS_WORKSPACE_ROOTS', ' /srv/repos : : /data/projects ');
+ vi.resetModules();
+ const mod = await import('./workspaceRoots.js');
+ expect(mod.WORKSPACE_ROOTS_CONFIGURED).toBe(true);
+ expect(mod.EXTRA_WORKSPACE_ROOTS).toEqual(['/srv/repos', '/data/projects']);
+ });
+
+ it('folds the configured roots into the allow-list so paths under them pass', async () => {
+ vi.stubEnv('PORTOS_WORKSPACE_ROOTS', '/srv/repos');
+ vi.resetModules();
+ const mod = await import('./workspaceRoots.js');
+ // /srv/repos likely does not exist on the test host, so the root falls back
+ // to its resolved (non-realpath) form — containment still matches descendants.
+ expect(mod.isWithinAllowedRoots('/srv/repos/myapp')).toBe(true);
+ });
+});
diff --git a/server/routes/detect.js b/server/routes/detect.js
index 7921f1b63a..171f9a4753 100644
--- a/server/routes/detect.js
+++ b/server/routes/detect.js
@@ -56,8 +56,17 @@ router.post('/repo', asyncHandler(async (req, res) => {
// Optional confinement: only when the operator has configured workspace roots.
// realpath() first so a symlink can't smuggle a path past the containment check.
+ // realpathSync can throw on a permission/TOCTOU edge (path deleted between the
+ // stat above and here) — convert that to the same valid:false shape the rest of
+ // this route returns for unusable paths, rather than leaking a 500. This is the
+ // sanctioned fs-edge-case exception to the no-try/catch rule (see commands.js).
if (WORKSPACE_ROOTS_CONFIGURED) {
- const realPath = realpathSync(resolve(path));
+ let realPath;
+ try {
+ realPath = realpathSync(resolve(path));
+ } catch {
+ return res.json({ valid: false, error: 'Path is not accessible' });
+ }
if (!isWithinAllowedRoots(realPath)) {
return res.json({
valid: false,