The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): never pin an unvalidated profile into a spawn argv
Brooklyn Nicholson3edb20075943
diff --git a/apps/desktop/electron/backend-command.test.ts b/apps/desktop/electron/backend-command.test.tsindex f4d8edc62eb..e9ed91de33c 100644--- a/apps/desktop/electron/backend-command.test.ts+++ b/apps/desktop/electron/backend-command.test.ts@@ -94,3 +94,21 @@ test('sourceDeclaresServe does not false-positive on the substring "server"', ()assert.equal(sourceDeclaresServe(oldSource), false)})++test('serveBackendArgs drops a profile value that is not a valid profile id', () => {+ // The roster/SSH bridge can hand us unvalidated values (a numeric id, an empty+ // string, a display label): a non-slug must never reach the backend spawn argv,+ // where it would bootstrap a phantom profile directory (#88842).+ const base = ['serve', '--host', '127.0.0.1', '--port', '0']+ assert.deepEqual(serveBackendArgs(0 as unknown as string), base)+ assert.deepEqual(serveBackendArgs(''), base)+ assert.deepEqual(serveBackendArgs(' '), base)+ assert.deepEqual(serveBackendArgs('Not A Slug!'), base)+})++test('serveBackendArgs keeps a valid profile id pinned, normalized like the CLI', () => {+ assert.deepEqual(serveBackendArgs('worker'), ['--profile', 'worker', 'serve', '--host', '127.0.0.1', '--port', '0'])+ assert.deepEqual(serveBackendArgs('a-1_b'), ['--profile', 'a-1_b', 'serve', '--host', '127.0.0.1', '--port', '0'])+ assert.deepEqual(serveBackendArgs(' Worker '), ['--profile', 'worker', 'serve', '--host', '127.0.0.1', '--port', '0'])+ assert.deepEqual(serveBackendArgs('default'), ['--profile', 'default', 'serve', '--host', '127.0.0.1', '--port', '0'])+})diff --git a/apps/desktop/electron/backend-command.ts b/apps/desktop/electron/backend-command.tsindex e38d5275715..efeaffea229 100644--- a/apps/desktop/electron/backend-command.ts+++ b/apps/desktop/electron/backend-command.ts@@ -11,12 +11,17 @@//// These helpers are pure so they can be unit-tested without Electron.+import { backendProfileArg } from './profile-id-guard'+/*** Build the canonical headless backend argv (always `serve`).* @param {string} [profile] optional Hermes profile to pin via `--profile`.*/export function serveBackendArgs(profile?: string) {- const head = profile ? ['--profile', profile] : []+ // A non-slug value (numeric roster id, display label) must never cross into+ // spawn argv: the CLI used to str()-coerce it into a phantom profiles/0 dir (#88842).+ const pinned = backendProfileArg(profile)+ const head = pinned ? ['--profile', pinned] : []return [...head, 'serve', '--host', '127.0.0.1', '--port', '0']}diff --git a/apps/desktop/electron/external-terminal.test.ts b/apps/desktop/electron/external-terminal.test.tsindex c5bac2c989d..d4ec13c78d0 100644--- a/apps/desktop/electron/external-terminal.test.ts+++ b/apps/desktop/electron/external-terminal.test.ts@@ -131,3 +131,15 @@ test('Linux falls down the emulator ladder and omits a flagless terminal', () =>test('Linux with no emulator installed reports no launch', () => {assert.equal(resolveTerminalLaunch({ findOnPath: never, platform: 'linux', scriptPath: '/tmp/x.sh' }), null)})++test('tuiResumeArgs drops a profile value that is not a valid profile id', () => {+ // A non-slug (numeric roster id, display label) must never cross into the+ // TUI launch argv — the CLI used to str()-coerce it into profiles/0 (#88842).+ assert.deepEqual(tuiResumeArgs('sess', 0 as unknown as string), ['--tui', '--resume', 'sess'])+ assert.deepEqual(tuiResumeArgs('sess', ''), ['--tui', '--resume', 'sess'])+ assert.deepEqual(tuiResumeArgs('sess', 'Not A Slug!'), ['--tui', '--resume', 'sess'])+})++test('tuiResumeArgs normalizes a valid profile id like the CLI', () => {+ assert.deepEqual(tuiResumeArgs('sess', 'Work'), ['--profile', 'work', '--tui', '--resume', 'sess'])+})diff --git a/apps/desktop/electron/external-terminal.ts b/apps/desktop/electron/external-terminal.tsindex 07cfe53a170..fbd87ffe370 100644--- a/apps/desktop/electron/external-terminal.ts+++ b/apps/desktop/electron/external-terminal.ts@@ -25,9 +25,13 @@// Everything here is pure so it can be unit-tested without Electron; the side// effects (writing the script, spawning) live in main.ts.+import { backendProfileArg } from './profile-id-guard'+/** Argv for resuming a session in the TUI, profile-pinned when we know it. */export function tuiResumeArgs(sessionId: string, profile?: string): string[] {- const head = profile ? ['--profile', profile] : []+ // A non-slug profile value must never cross into spawn argv (#88842).+ const pinned = backendProfileArg(profile)+ const head = pinned ? ['--profile', pinned] : []return [...head, '--tui', '--resume', sessionId]}diff --git a/apps/desktop/electron/profile-id-guard.ts b/apps/desktop/electron/profile-id-guard.tsnew file mode 100644index 00000000000..83c4e108386--- /dev/null+++ b/apps/desktop/electron/profile-id-guard.ts@@ -0,0 +1,26 @@+// Profile ids that cross into backend/TUI spawn argv must be non-empty+// PROFILE_ID_RE slugs. Anything else (a numeric roster/DB id, an empty string,+// a display label with spaces) must never reach `--profile <value>`: the CLI's+// normalize_profile_name used to str()-coerce non-strings, bootstrapping a+// phantom `profiles/0/` directory with state.db and SOUL.md but no config.yaml+// (#88842). This module mirrors the CLI's normalize semantics (trim ++// case-fold + slug test) without pulling desktop-profile's fs/path imports+// into the pure spawn-arg builders that need it.++export const PROFILE_ID_ARG_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/++/**+ * A profile value safe to pin via `--profile <name>`, normalized the way the+ * CLI would normalize it; `null` when the value must not cross into argv.+ */+export function backendProfileArg(profile: unknown): string | null {+ if (typeof profile !== 'string') {+ return null+ }+ const name = profile.trim().toLowerCase()+ if (!name) {+ return null+ }+ // `default` is the RE-legal alias for ~/.hermes itself; every other id is a slug.+ return name === 'default' || PROFILE_ID_ARG_RE.test(name) ? name : null+}diff --git a/apps/desktop/electron/remote-lifecycle.test.ts b/apps/desktop/electron/remote-lifecycle.test.tsindex 6d03b98e16e..38db8856d8e 100644--- a/apps/desktop/electron/remote-lifecycle.test.ts+++ b/apps/desktop/electron/remote-lifecycle.test.ts@@ -960,6 +960,24 @@ test('buildSpawnCommand is headless serve, detached, token not in argv', () => {assert.ok(!cmd.includes('HERMES_DASHBOARD_SESSION_TOKEN'), 'token env var must not appear')})+test('buildSpawnCommand never pins a non-slug profile into the remote argv', () => {+ // The roster/SSH bridge hands the profile verbatim; a numeric id or display+ // label must never cross into the remote serve argv, where the CLI used to+ // str()-coerce it into a phantom profiles/0/ directory (#88842).+ const bad = buildSpawnCommand('/x/hermes', 0 as unknown as string, { logPath: spawnLogPath(OWNERSHIP_ID, SPAWN_NONCE) })+ assert.ok(!bad.includes('--profile'), 'a non-string profile must not be pinned')++ const empty = buildSpawnCommand('/x/hermes', '', { logPath: spawnLogPath(OWNERSHIP_ID, SPAWN_NONCE) })+ assert.ok(!empty.includes('--profile'), 'an empty profile must not be pinned')++ const label = buildSpawnCommand('/x/hermes', 'My Profile!', { logPath: spawnLogPath(OWNERSHIP_ID, SPAWN_NONCE) })+ assert.ok(!label.includes('--profile'), 'a non-slug label must not be pinned')++ const good = buildSpawnCommand('/x/hermes', 'Work', { logPath: spawnLogPath(OWNERSHIP_ID, SPAWN_NONCE) })+ assert.ok(good.includes('--profile'), 'a valid profile stays pinned')+ assert.ok(good.includes("'work'"), 'the profile is normalized like the CLI would')+})+test.skipIf(process.platform === 'win32')('detached backend does not inherit the update mutex descriptor',async (): Promise<void> => {diff --git a/apps/desktop/electron/remote-lifecycle.ts b/apps/desktop/electron/remote-lifecycle.tsindex df1a36c0759..5078f53bcc2 100644--- a/apps/desktop/electron/remote-lifecycle.ts+++ b/apps/desktop/electron/remote-lifecycle.ts@@ -29,6 +29,7 @@ import crypto from 'node:crypto'import { READY_IN_MERGED_OUTPUT_RE } from './backend-ready'import { parseRemoteProfileListing } from './connection-registry'+import { backendProfileArg } from './profile-id-guard'import { assertBootstrapNotSuperseded, withRemoteTimeout } from './ssh-connection'const LOCKFILE_SCHEMA_VERSION = 2@@ -1151,7 +1152,11 @@ async function terminateOwnedDashboardForUpdate(ssh, expected) {// fd-detachment is already handled by </dev/null + redirect + &).function buildSpawnCommand(hermesPath, profile, opts: any = {}) {const hermes = expandRemotePath(hermesPath)- const profileArgs = profile ? `--profile ${shq(profile)} ` : ''+ // The roster/SSH bridge hands us the profile verbatim: a non-slug value must never+ // cross into the remote spawn argv, where the CLI used to str()-coerce it into a+ // phantom profiles/0/ directory (#88842).+ const pinned = backendProfileArg(profile)+ const profileArgs = pinned ? `--profile ${shq(pinned)} ` : ''const logPath = expandRemotePath(opts.logPath)const tokenFilePath = opts.tokenFilePathconst tokenArg = tokenFilePath ? ` --ssh-session-token-file ${expandRemotePath(tokenFilePath)}` : ''