Skip to content

The agent that grows with you

fix(desktop): an unsafe gateway control path names the path and its repair

Tekniumf7559466112f
diff --git a/apps/desktop/electron/local-gateway-unsafe-path.test.ts b/apps/desktop/electron/local-gateway-unsafe-path.test.ts
index cdbe1d662e9..38f716ac921 100644
--- a/apps/desktop/electron/local-gateway-unsafe-path.test.ts
+++ b/apps/desktop/electron/local-gateway-unsafe-path.test.ts
@@ -25,3 +25,48 @@ test.each(['Gateway ticket control socket missing', 'Invalid gateway control poi
expect(forget).toHaveBeenCalledOnce()
}
)
+
+// The unsafe refusal is not stale, so nothing re-ensures past it: it must name the path and the
+// repair (like the group-writable home's `chmod g-w`), never the bare 'Unsafe gateway control path'.
+test.skipIf(process.platform === 'win32')('each unsafe control-path refusal names the path and the command that repairs it', async () => {
+ const fs = await import('node:fs/promises')
+ const path = await import('node:path')
+ const { isStaleLocalGatewayError, mintLocalGatewayTicket } = await import('./local-gateway')
+ const { shortSocketTmpDir } = await import('./local-gateway.test-helpers')
+ const home = await shortSocketTmpDir('desktop-unsafe-msg-')
+ const endpoint = (profile_id: string) => ({ profile_id, instance_id: 'owner', authority_epoch: 1, runtime_protocol: 1, api_origin: 'http://127.0.0.1:1234', capabilities: ['session-authority-v1'], supervisor: 'none' })
+
+ const failure = async (profile_id = home) => {
+ const error = await mintLocalGatewayTicket(endpoint(profile_id)).catch(e => e)
+ expect(isStaleLocalGatewayError(error)).toBe(false)
+
+ return error.message
+ }
+
+ const sock = path.join(home, 'gateway.sock')
+ const pointer = path.join(home, 'gateway.sock.path')
+
+ try {
+ await fs.chmod(home, 0o757)
+ expect(await failure()).toBe(`Unsafe gateway control path: profile home '${home}' is writable by other users (mode 757): run chmod o-w '${home}'`)
+
+ await fs.chmod(home, 0o775)
+ expect(await failure()).toBe(`Unsafe gateway control path: profile home is group-writable and this Desktop cannot verify the group is private: run chmod g-w '${home}'`)
+
+ await fs.chmod(home, 0o700)
+ await fs.writeFile(sock, '', { mode: 0o600 })
+ expect(await failure()).toBe(`Unsafe gateway control path: '${sock}' should be a socket but is a regular file: remove it, then run hermes gateway restart`)
+
+ await fs.rm(sock)
+ await fs.writeFile(pointer, '/nowhere/control.sock\n')
+ await fs.chmod(pointer, 0o644)
+ expect(await failure()).toBe(`Unsafe gateway control path: '${pointer}' is accessible to other users (mode 644): run chmod go-rwx '${pointer}'`)
+
+ if (process.getuid?.() !== 0) {
+ expect(await failure('/')).toBe(`Unsafe gateway control path: '/' is owned by uid 0, not this user (uid ${process.getuid?.()}): run sudo chown ${process.getuid?.()} '/'`)
+ }
+ } finally {
+ await fs.chmod(home, 0o700)
+ await fs.rm(home, { recursive: true, force: true })
+ }
+})
diff --git a/apps/desktop/electron/local-gateway.test.ts b/apps/desktop/electron/local-gateway.test.ts
index f2f19108d28..eea8dbfc5cf 100644
--- a/apps/desktop/electron/local-gateway.test.ts
+++ b/apps/desktop/electron/local-gateway.test.ts
@@ -347,7 +347,7 @@ test.skipIf(process.platform === 'win32')('a group-accessible control socket is
try {
const failure = await mintLocalGatewayTicket(endpoint).catch(error => error)
expect(failure).toBeInstanceOf(Error)
- expect(failure.message).toBe('Unsafe gateway control path')
+ expect(failure.message).toBe(`Unsafe gateway control path: '${socketPath}' is accessible to other users (mode 660): run chmod go-rwx '${socketPath}'`)
expect(isStaleLocalGatewayError(failure)).toBe(false)
expect(isStaleLocalGatewayError(new Error('EACCES: permission denied'))).toBe(false)
} finally {
diff --git a/apps/desktop/electron/local-gateway.ts b/apps/desktop/electron/local-gateway.ts
index 7adf3845f61..c34c9a8511c 100644
--- a/apps/desktop/electron/local-gateway.ts
+++ b/apps/desktop/electron/local-gateway.ts
@@ -273,12 +273,38 @@ export function createLocalGatewayDials() {
async function privateNode(file: string, kind: 'directory' | 'socket' | 'file' | 'home'): Promise<boolean> {
const node = await fs.lstat(file)
const valid = { directory: node.isDirectory(), home: node.isDirectory(), socket: node.isSocket(), file: node.isFile() }[kind]
+ const uid = process.getuid?.()
+ const quoted = shellQuote(file)
- if (!valid || node.uid !== process.getuid?.() || (node.mode & (kind === 'home' ? 0o002 : 0o077))) {throw new Error('Unsafe gateway control path')}
+ // Not stale (a re-ensure of the same home would hit the same refusal): name the repair.
+ if (!valid) {
+ const found = node.isSymbolicLink() ? 'a symlink' : node.isDirectory() ? 'a directory' : node.isFile() ? 'a regular file' : node.isSocket() ? 'a socket' : 'another file type'
+
+ // The socket, pointer and fallback directory are the gateway's to recreate; a home is not.
+ throw new Error(kind === 'home'
+ ? `Unsafe gateway control path: profile home ${quoted} is ${found}, not a directory: move it aside and recreate the profile`
+ : `Unsafe gateway control path: ${quoted} should be a ${kind} but is ${found}: remove it, then run hermes gateway restart`)
+ }
+
+ if (node.uid !== uid) {
+ throw new Error(`Unsafe gateway control path: ${quoted} is owned by uid ${node.uid}, not this user (uid ${uid}): run sudo chown ${uid} ${quoted}`)
+ }
+
+ if (kind === 'home' && node.mode & 0o002) {
+ throw new Error(`Unsafe gateway control path: profile home ${quoted} is writable by other users (mode ${(node.mode & 0o777).toString(8)}): run chmod o-w ${quoted}`)
+ }
+
+ if (kind !== 'home' && node.mode & 0o077) {
+ throw new Error(`Unsafe gateway control path: ${quoted} is accessible to other users (mode ${(node.mode & 0o777).toString(8)}): run chmod go-rwx ${quoted}`)
+ }
return kind === 'home' && Boolean(node.mode & 0o020)
}
+function shellQuote(file: string): string {
+ return `'${file.replace(/'/g, `'\\''`)}'`
+}
+
/** The endpoint a `?profile=<name>` request is scoped to on a shared host descriptor: the same
* daemon, ticket minted for the sibling profile's home (`profiles/<name>` under the launch root).
* The gateway refuses a ticket whose profile it does not serve, so a bad name fails closed. */
@@ -315,13 +341,13 @@ export function configurePythonGatewayTicketClient(client: NonNullable<typeof py
}
async function mintGroupWritableHome(endpoint: GatewayEndpoint, purpose: 'interactive' | 'native-http', homes: string[]) {
- if (!pythonTicketClient) {throw new Error('Unsafe gateway control path')}
+ if (!pythonTicketClient) {throw new Error(`Unsafe gateway control path: profile home is group-writable and this Desktop cannot verify the group is private: run chmod g-w ${homes.map(shellQuote).join(' ')}`)}
try {
return await pythonTicketClient(endpoint, purpose)
} catch (error) {
if ((error as { reason?: string })?.reason !== 'unsafe_control_permissions') {throw error}
- const quoted = homes.map(home => `'${home.replace(/'/g, `'\\''`)}'`).join(' ')
+ const quoted = homes.map(shellQuote).join(' ')
throw new Error(`Profile home is writable by a group other accounts share: run chmod g-w ${quoted}`)
}