Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

refactor(hooks): give the tunnel probe its own /verify path

Claude Code7ec0a956eb0d
diff --git a/src/core/agents/hook-server.ts b/src/core/agents/hook-server.ts
index e8a22c168..6b47725f0 100644
--- a/src/core/agents/hook-server.ts
+++ b/src/core/agents/hook-server.ts
@@ -308,6 +308,24 @@ class HookServer {
}
req.setTimeout(SLOWLORIS_MS, () => req.destroy())
const reqUrl = new URL(req.url ?? '/', 'http://127.0.0.1')
+ // THE TUNNEL PROBE. `RemoteHooks.verifyTunnel` curls this through the reverse socket and
+ // requires exactly 204 before it will write the remote endpoint file or install a single
+ // hook script. It proves ONE thing — the socket reaches this server — and it must answer on
+ // the bearer alone, with no node identity of any kind, permanently: the caller is the
+ // desktop itself, `verify` is not a node id, and a 403 here would silently cost that host
+ // its whole remote hook + skill install.
+ //
+ // Until this route existed, `/hook/verify` 204'd only because the probe sends no `payload`
+ // field and fell out of the generic branch — a coincidence that the identity label on
+ // `/hook/*` would have turned into a 403 for any probe carrying a token. `/hook/verify`
+ // therefore stays answering forever: a host connected by an older desktop still has that
+ // path baked into the script on its disk.
+ if (reqUrl.pathname === '/verify' || reqUrl.pathname === '/hook/verify') {
+ await readBody(req) // drain, so the probe's body is never left unread on the socket
+ res.writeHead(204)
+ res.end()
+ return
+ }
if (reqUrl.pathname.startsWith('/codex-thread/')) {
await this.handleCodexThread(reqUrl.pathname, req, res)
return
diff --git a/src/core/agents/hook-verify-route.test.ts b/src/core/agents/hook-verify-route.test.ts
new file mode 100644
index 000000000..ef8f05415
--- /dev/null
+++ b/src/core/agents/hook-verify-route.test.ts
@@ -0,0 +1,115 @@
+// The tunnel probe gets a route of its own, and that route is deliberately identity-free.
+//
+// Before this, `RemoteHooks.verifyTunnel` POSTed `nodeId=verify` at `/hook/verify` — there was no
+// such route; `verify` was an agentId falling through the generic `/hook/<agentId>` branch, and it
+// answered 204 only because the probe sends no `payload` field. That is a coincidence, not a
+// contract, and it is the kind that breaks silently: the FIRST identity check to land on `/hook/*`
+// would have started judging the probe, and a probe that stops answering 204 disables the whole
+// remote hook + skill install for that host with no error anyone sees.
+//
+// So: `/verify` answers 204 on the bearer alone, forever, and `/hook/verify` keeps doing the same
+// forever too, because a host connected by an older desktop still has the old script on disk.
+import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest'
+import { mkdtempSync, rmSync, readFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join, resolve } from 'node:path'
+import { hookServer } from './hook-server'
+import { nodeAuthToken } from './node-auth-token'
+import { initPlatform, resetPlatformForTests } from '../platform'
+import { fakePlatform } from '../platform-fake'
+
+const SECRET = Buffer.alloc(32, 3)
+
+let dir = ''
+let listenerCalls = 0
+let rawCalls = 0
+
+// Byte-for-byte what remote-hooks' curl sends: bearer header, form body, nodeId=verify.
+function probe(path: string, extra: Record<string, string> = {}): Promise<Response> {
+ return fetch(`http://127.0.0.1:${hookServer.getPort()}${path}`, {
+ method: 'POST',
+ headers: {
+ 'X-Nodeterm-Hook-Token': hookServer.getToken(),
+ 'content-type': 'application/x-www-form-urlencoded',
+ ...extra
+ },
+ body: 'nodeId=verify'
+ })
+}
+
+beforeAll(async () => {
+ dir = mkdtempSync(join(tmpdir(), 'hooksrv-verify-'))
+ resetPlatformForTests()
+ initPlatform(fakePlatform({ userDataDir: dir }))
+ await hookServer.start()
+ // Identity ARMED — the whole point is that the probe is unaffected by it.
+ hookServer.setNodeAuthSecret(SECRET)
+ hookServer.setListener(() => {
+ listenerCalls++
+ })
+ hookServer.setRawListener(() => {
+ rawCalls++
+ })
+})
+
+afterAll(() => {
+ hookServer.clearNodeAuthSecretForTests()
+ hookServer.stop()
+ rmSync(dir, { recursive: true, force: true })
+})
+
+beforeEach(() => {
+ listenerCalls = 0
+ rawCalls = 0
+})
+
+describe('the tunnel probe', () => {
+ it('answers 204 on the BEARER ALONE — no node token, ever', async () => {
+ const res = await probe('/verify')
+ expect(res.status).toBe(204)
+ // It is a liveness probe, not an event: nothing downstream should hear about it.
+ expect(listenerCalls).toBe(0)
+ expect(rawCalls).toBe(0)
+ })
+
+ it('still answers /hook/verify for a host whose script predates this release', async () => {
+ const res = await probe('/hook/verify')
+ expect(res.status).toBe(204)
+ expect(listenerCalls).toBe(0)
+ expect(rawCalls).toBe(0)
+ })
+
+ // The route takes NO identity at all, so there is nothing for a token — good, absent or forged —
+ // to fail. `verify` is not a node id and never will be, so a 403 here could only ever be a
+ // false one, and a false 403 here silently costs the host its hooks.
+ it('answers 204 even when a FORGED node token is presented — it judges no identity', async () => {
+ const good = nodeAuthToken(SECRET, 'verify')
+ const forged = good.slice(0, -1) + (good.endsWith('A') ? 'B' : 'A')
+ expect((await probe('/verify', { 'X-Nodeterm-Node-Token': forged })).status).toBe(204)
+ expect((await probe('/hook/verify', { 'X-Nodeterm-Node-Token': forged })).status).toBe(204)
+ })
+
+ // Identity-free is not auth-free: the app-wide bearer still gates the socket.
+ it('is still refused without the bearer', async () => {
+ const res = await fetch(`http://127.0.0.1:${hookServer.getPort()}/verify`, {
+ method: 'POST',
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
+ body: 'nodeId=verify'
+ })
+ expect(res.status).toBe(403)
+ })
+
+ it('is a POST-only route, like every other', async () => {
+ const res = await fetch(`http://127.0.0.1:${hookServer.getPort()}/verify`, { method: 'GET' })
+ expect(res.status).toBe(404)
+ })
+
+ it('is what the remote tunnel probe actually curls', () => {
+ const src = readFileSync(
+ resolve(__dirname, '../../..', 'src/main/remote-ssh/remote-hooks.ts'),
+ 'utf8'
+ )
+ expect(src).toContain('http://localhost/verify')
+ expect(src).not.toContain('http://localhost/hook/verify')
+ })
+})
diff --git a/src/main/remote-ssh/remote-hooks.ts b/src/main/remote-ssh/remote-hooks.ts
index e3984643c..93f081b2b 100644
--- a/src/main/remote-ssh/remote-hooks.ts
+++ b/src/main/remote-ssh/remote-hooks.ts
@@ -614,7 +614,12 @@ export class RemoteHooks {
try {
const cmd =
`curl -s -m 5 -o /dev/null -w '%{http_code}' -X POST --unix-socket ${posixQuote(sock)} ` +
- `-H ${posixQuote(`x-nodeterm-hook-token: ${token}`)} http://localhost/hook/verify --data nodeId=verify`
+ // `/verify` — the probe's OWN route, which answers 204 on the bearer alone and takes no
+ // node identity at all. It used to POST `/hook/verify`, where it 204'd only as a side
+ // effect of sending no payload; the identity label on `/hook/*` would have started judging
+ // it there. `nodeId=verify` is kept in the body only so an OLDER desktop's script and this
+ // one send the same bytes; the route reads nothing.
+ `-H ${posixQuote(`x-nodeterm-hook-token: ${token}`)} http://localhost/verify --data nodeId=verify`
const r = await this.r.run(childArgs(conn, controlPath, cmd))
return r.code === 0 && r.stdout.trim() === '204'
} catch {