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

address review (codex): stop folding the POSIX prefixes on Windows, guard the completion hooks

[._.]/ Adam Eivy69e60f848ff9
diff --git a/server/services/runner.js b/server/services/runner.js
index af6959a21e..f4b5ef7fb6 100644
--- a/server/services/runner.js
+++ b/server/services/runner.js
@@ -118,10 +118,16 @@ export async function finalizeRunRecord({ runId, output, exitCode, success, erro
await atomicWrite(metadataPath, metadata).catch(() => {});
+ // Guarded: these hooks are host-supplied, and every caller of this function
+ // runs outside the request lifecycle — the /runs route never awaits its
+ // executor. An unguarded throw (or rejected promise) from a hook propagates
+ // out of finalizeRunRecord, past the caller's own settlement, and lands as an
+ // unhandled rejection with the run stuck looking in-flight. The metadata is
+ // already persisted by this point, so a failing hook must not un-finalize it.
if (success) {
- runnerConfig.hooks?.onRunCompleted?.(metadata, output);
+ safeSettle(() => runnerConfig.hooks?.onRunCompleted?.(metadata, output), 'onRunCompleted');
} else {
- runnerConfig.hooks?.onRunFailed?.(metadata, metadata.error, output);
+ safeSettle(() => runnerConfig.hooks?.onRunFailed?.(metadata, metadata.error, output), 'onRunFailed');
}
return metadata;
diff --git a/server/services/shell.js b/server/services/shell.js
index 621b4fbef5..f3e62f3cbf 100644
--- a/server/services/shell.js
+++ b/server/services/shell.js
@@ -59,23 +59,37 @@ const SAFE_ENV_NAMES_WIN32 = new Set([
'PROGRAMFILES', 'PROGRAMFILES(X86)', 'PROGRAMW6432',
'PROGRAMDATA', 'COMMONPROGRAMFILES', 'COMMONPROGRAMFILES(X86)', 'COMMONPROGRAMW6432',
'TEMP', 'TMP',
- 'NUMBER_OF_PROCESSORS', 'PROCESSOR_ARCHITECTURE', 'OS', 'PSMODULEPATH'
+ 'NUMBER_OF_PROCESSORS', 'PROCESSOR_ARCHITECTURE', 'OS', 'PSMODULEPATH',
+ // Windows spellings of variables the POSIX prefixes above cover only on
+ // POSIX. They are listed here — rather than case-folding the POSIX prefix
+ // list on Windows — because folding a PREFIX list widens it: `NPM_` would
+ // then match npm's lower-case `npm_config__authToken` (a registry credential)
+ // and hand it to an attachable agent shell. An exact-name Set can be matched
+ // case-insensitively with no such reach.
+ 'PATH', 'PATHEXT',
+ 'USERPROFILE', 'USERNAME', 'USERDOMAIN', 'USERDOMAIN_ROAMINGPROFILE',
+ 'HOMEDRIVE', 'HOMEPATH'
]);
/**
* Filter `process.env` down to the allowlist above.
*
- * On Windows, matching folds case: Windows env names are case-insensitive and
- * arrive in mixed case, so the real variable is `Path`, not `PATH`. A
- * case-sensitive `startsWith('PATH')` dropped the child's entire PATH there
- * (while keeping the coincidentally-upper-case `PATHEXT`), leaving the shell
- * unable to resolve any CLI provider.
+ * Two matching rules, deliberately different:
*
- * POSIX matching stays case-SENSITIVE — folding it would WIDEN the allowlist,
- * not preserve it. PortOS starts under `npm run`, which exports dozens of
- * lower-case `npm_config_*` / `npm_package_*` vars (including values from a
- * user's `.npmrc`); upper-casing keys would let every one of them through the
- * `NPM_` prefix into an attachable shell.
+ * - `SAFE_ENV_PREFIXES` is matched **case-sensitively on both platforms**.
+ * These are grouped families, and folding case widens a prefix rather than
+ * preserving it: upper-casing keys would push npm's lower-case
+ * `npm_config_*` / `npm_package_*` vars — `npm_config__authToken` among
+ * them — through the `NPM_` prefix into an attachable shell. PortOS starts
+ * under `npm run`, so those variables are always present.
+ * - `SAFE_ENV_NAMES_WIN32` is matched **case-insensitively, on Windows only,
+ * as exact names**. Windows env names are case-insensitive and arrive in
+ * mixed case (the real variable is `Path`, not `PATH`), and an exact-name
+ * Set can be folded safely because it has no prefix reach.
+ *
+ * Before this split, `Path` was dropped entirely on Windows by the
+ * case-sensitive `startsWith('PATH')` — the agent shell then couldn't resolve
+ * any CLI provider — while the coincidentally-upper-case `PATHEXT` survived.
*
* Exported for tests; `platform` is injectable so the Windows branch is
* testable from any host.
@@ -88,11 +102,8 @@ export function buildSafeEnv(env = process.env, platform = process.platform) {
const isWin32 = platform === 'win32';
const safeEnv = {};
for (const [key, value] of Object.entries(env)) {
- // Windows env names are case-insensitive, so both the prefix test and the
- // exact-name lookup compare against the upper-cased key there.
- const candidate = isWin32 ? key.toUpperCase() : key;
- const allowed = SAFE_ENV_PREFIXES.some(prefix => candidate.startsWith(prefix))
- || (isWin32 && SAFE_ENV_NAMES_WIN32.has(candidate));
+ const allowed = SAFE_ENV_PREFIXES.some(prefix => key.startsWith(prefix))
+ || (isWin32 && SAFE_ENV_NAMES_WIN32.has(key.toUpperCase()));
if (allowed) safeEnv[key] = value;
}
return safeEnv;
diff --git a/server/services/shell.test.js b/server/services/shell.test.js
index 6f75dc4da6..b05bb0006c 100644
--- a/server/services/shell.test.js
+++ b/server/services/shell.test.js
@@ -684,3 +684,34 @@ describe('buildSafeEnv — Windows names are exact, not prefixes', () => {
expect(safe).toHaveProperty('ProgramW6432');
});
});
+
+describe('buildSafeEnv — Windows folding must not widen the POSIX prefixes', () => {
+ // Folding case for the PREFIX list (rather than only for the exact-name
+ // Windows Set) reaches much further than intended: PortOS starts under
+ // `npm run`, so npm's lower-case config vars are always present, and
+ // npm_config__authToken is a registry credential.
+ it('does not admit lower-case npm_config_* on Windows via the NPM_ prefix', () => {
+ const safe = shell.buildSafeEnv({
+ Path: 'C:\\Windows',
+ npm_config__authToken: 'npm-registry-credential',
+ npm_config_registry: 'https://example.com',
+ npm_package_name: 'portos',
+ }, 'win32');
+ expect(safe).toHaveProperty('Path');
+ expect(safe).not.toHaveProperty('npm_config__authToken');
+ expect(safe).not.toHaveProperty('npm_config_registry');
+ expect(safe).not.toHaveProperty('npm_package_name');
+ });
+
+ // The Windows names still need case-insensitive matching — that is the whole
+ // point of the exact-name Set, and `Path` is why the fix exists.
+ it('still matches the Windows names case-insensitively', () => {
+ const safe = shell.buildSafeEnv(
+ { Path: 'C:\\Windows', SystemRoot: 'C:\\Windows', windir: 'C:\\Windows', ComSpec: 'C:\\cmd.exe', ProgramData: 'C:\\PD' },
+ 'win32'
+ );
+ for (const key of ['Path', 'SystemRoot', 'windir', 'ComSpec', 'ProgramData']) {
+ expect(safe, `expected ${key} to survive`).toHaveProperty(key);
+ }
+ });
+});