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.

security(remote/4d): surface revoke persistence + teardown failures (no silent success)

enesd204b8aece39
diff --git a/src/main/remote/revocation.test.ts b/src/main/remote/revocation.test.ts
index 4d4a39d35..f8c7132a9 100644
--- a/src/main/remote/revocation.test.ts
+++ b/src/main/remote/revocation.test.ts
@@ -29,10 +29,12 @@ describe('createRevoker: unpins AND fires the kill hook', () => {
}
}
const revoker = createRevoker(deps)
- await revoker.revoke('peerKey')
+ const result = await revoker.revoke('peerKey')
expect(isPinned(store, 'peerKey')).toBe(false) // unpinned on disk
expect(killed).toEqual(['peerKey']) // hook fired exactly once, with the peer id
+ // Happy path reports both steps succeeded, so the UI can show a truthful "Removed".
+ expect(result).toEqual({ persisted: true, killed: true })
// Unpin must be PERSISTED before the live session is cut, so a reconnect racing the kill is
// already refused by the pin check. (Unpin alone would leave the open socket fully privileged.)
expect(order).toEqual(['save', 'onRevoke'])
@@ -43,22 +45,80 @@ describe('createRevoker: unpins AND fires the kill hook', () => {
const revoker = createRevoker({
load: async () => ({ pubkeys: [] }),
save: async () => {},
- onRevoke: (id) => killed.push(id)
+ onRevoke: (id) => { killed.push(id) }
})
- await revoker.revoke('ghost')
+ const result = await revoker.revoke('ghost')
expect(killed).toEqual(['ghost']) // a live-but-unpinned session must still be killable
+ expect(result).toEqual({ persisted: true, killed: true })
})
- it('fires the kill hook even when persistence fails (the kill must not depend on the disk)', async () => {
+ it('fires the kill hook even when persistence fails, AND surfaces persisted:false (no false "Removed")', async () => {
+ // A throwing save must NOT be reported as success: temp+rename means the OLD pinned file survives,
+ // so the revoked peer could reconnect and auto-approve. The caller must see persisted:false and retry.
const killed: string[] = []
const revoker = createRevoker({
load: async () => pinDevice({ pubkeys: [] }, 'peerKey'),
save: async () => {
throw new Error('disk full')
},
- onRevoke: (id) => killed.push(id)
+ onRevoke: (id) => { killed.push(id) }
})
- await expect(revoker.revoke('peerKey')).resolves.toBeUndefined()
+ const result = await revoker.revoke('peerKey')
+ expect(killed).toEqual(['peerKey']) // the kill still fires (immediacy) ...
+ expect(result).toEqual({ persisted: false, killed: true }) // ... but the failure is REPORTED, not swallowed
+ })
+
+ it('surfaces persisted:false when load() throws (pin never removed), and still fires the kill', async () => {
+ // Finding 3: a throwing load is swallowed by the same catch today. If it throws, the pin was never
+ // even read, so it certainly survives — this must be persisted:false, not a silent success.
+ const killed: string[] = []
+ const revoker = createRevoker({
+ load: async () => {
+ throw new Error('read fault')
+ },
+ save: async () => {},
+ onRevoke: (id) => { killed.push(id) }
+ })
+ const result = await revoker.revoke('peerKey')
expect(killed).toEqual(['peerKey'])
+ expect(result).toEqual({ persisted: false, killed: true })
+ })
+
+ it('awaits an async onRevoke and surfaces a rejecting teardown (no unhandled rejection)', async () => {
+ // 4c's teardown (close relay conn → dropClient → presence leave) is async. A rejection must be
+ // observable via killed:false, not dropped as an unhandled rejection with a half-torn-down socket.
+ let store: ApprovedDevices = pinDevice({ pubkeys: [] }, 'peerKey')
+ const revoker = createRevoker({
+ load: async () => store,
+ save: async (s) => {
+ store = s
+ },
+ onRevoke: async () => {
+ throw new Error('relay close failed')
+ }
+ })
+ const result = await revoker.revoke('peerKey')
+ expect(isPinned(store, 'peerKey')).toBe(false) // unpin still persisted first
+ expect(result).toEqual({ persisted: true, killed: false }) // teardown failure surfaced, not swallowed
+ })
+
+ it('awaits an async onRevoke on the happy path (resolves only once the cut is real)', async () => {
+ const order: string[] = []
+ let store: ApprovedDevices = pinDevice({ pubkeys: [] }, 'peerKey')
+ const revoker = createRevoker({
+ load: async () => store,
+ save: async (s) => {
+ order.push('save')
+ store = s
+ },
+ onRevoke: async () => {
+ // Defer so a non-awaiting revoke() would resolve before the teardown completed.
+ await Promise.resolve()
+ order.push('onRevoke')
+ }
+ })
+ const result = await revoker.revoke('peerKey')
+ expect(result).toEqual({ persisted: true, killed: true })
+ expect(order).toEqual(['save', 'onRevoke']) // still save-before-kill, and the kill fully completed
})
})
diff --git a/src/main/remote/revocation.ts b/src/main/remote/revocation.ts
index c30b1a8ee..11ee085bf 100644
--- a/src/main/remote/revocation.ts
+++ b/src/main/remote/revocation.ts
@@ -21,8 +21,13 @@ export function revoke(store: ApprovedDevices, peerKeyB64: string): ApprovedDevi
return unpinDevice(store, peerKeyB64)
}
-/** 4c implements this to KILL the live session (see file header). */
-export type OnRevoke = (peerId: string) => void
+/**
+ * 4c implements this to KILL the live session (see file header). The teardown is async in 4c
+ * (close relay conn → PtyManager.dropClient → presenceHub.leave), so the hook may return a Promise;
+ * `createRevoker` awaits it so `revoke()` only resolves once the cut is real and a teardown
+ * rejection is observable rather than an unhandled rejection. A plain sync `void` impl still fits.
+ */
+export type OnRevoke = (peerId: string) => void | Promise<void>
export interface RevocationDeps {
load(): Promise<ApprovedDevices>
@@ -30,24 +35,68 @@ export interface RevocationDeps {
onRevoke: OnRevoke
}
+/**
+ * Outcome of a revoke. Both steps are reported independently so the caller (4c's trust UI) can
+ * always know the session was cut AND whether it must retry the unpin.
+ */
+export interface RevokeResult {
+ /**
+ * `true` iff the unpin was loaded and written to disk. `false` means the pin may SURVIVE — the
+ * real adapter's `saveApprovedDevices` does temp+rename, so a failed write leaves the OLD still-
+ * pinned file byte-for-byte intact — so the revoked peer could reconnect and auto-approve with no
+ * SAS. The caller MUST retry and MUST NOT show a "Removed" success.
+ */
+ persisted: boolean
+ /**
+ * `true` iff `onRevoke` (the live-session teardown) completed without throwing. `false` means the
+ * socket may be half-torn-down and the cut is not confirmed.
+ */
+ killed: boolean
+}
+
/**
* Build the revoke controller. `revoke(peerKeyB64)` unpins the device on disk FIRST (so a crash
* between the two steps, or a reconnect racing the teardown, still leaves the peer refused by the
* pin check), THEN fires `onRevoke` to cut the open socket. The kill hook fires even when the key
* was not pinned — a live, never-pinned session must still be killable — and even when the disk
* write failed: a persistence error must never leave a revoked peer connected.
+ *
+ * Neither failure is swallowed. A persistence failure surfaces as `persisted:false` and a teardown
+ * rejection as `killed:false`, so the trust UI can never report a false "Removed" while the pin
+ * survives on disk (the security hole this module exists to prevent). A status object (rather than a
+ * reject) is used because the kill genuinely DID fire even when persistence fails — the caller must
+ * be able to distinguish "session cut, retry the unpin" from a total failure, which a single reject
+ * cannot express.
*/
-export function createRevoker(deps: RevocationDeps): { revoke(peerKeyB64: string): Promise<void> } {
+export function createRevoker(deps: RevocationDeps): {
+ revoke(peerKeyB64: string): Promise<RevokeResult>
+} {
return {
async revoke(peerKeyB64) {
+ let persisted = false
try {
const store = await deps.load()
await deps.save(revoke(store, peerKeyB64))
+ persisted = true
} catch {
- // Non-fatal for the KILL: fall through to onRevoke regardless. (Worst case the pin survives
- // a crash and the operator must revoke again — but the peer is off the wire either way.)
+ // load() or save() threw. Do NOT swallow silently: the on-disk pin may be intact, so the
+ // revoked peer could reconnect and auto-approve. Report persisted:false so the caller retries
+ // and the UI cannot show "Removed". We STILL fire the kill below — the live socket must drop
+ // regardless of the disk.
+ persisted = false
}
- deps.onRevoke(peerKeyB64)
+
+ let killed = false
+ try {
+ await deps.onRevoke(peerKeyB64)
+ killed = true
+ } catch {
+ // The async teardown rejected. Surface it as killed:false instead of leaking an unhandled
+ // rejection: the socket may be half-closed and the caller must know the cut is unconfirmed.
+ killed = false
+ }
+
+ return { persisted, killed }
}
}
}