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.

fix(watch-link): hold a digest of each sealed secret, never its text, and say what a seal refusal loses

reviewb5e419d91cf3
diff --git a/src/core/watch-link/service.ts b/src/core/watch-link/service.ts
index cf08e43a3..7d6c797d6 100644
--- a/src/core/watch-link/service.ts
+++ b/src/core/watch-link/service.ts
@@ -23,7 +23,9 @@
// written must not exist: the server row is revoked), but no longer than PERSIST_TIMEOUT_MS.
// A links file that could not be read (WatchLinkStoreUnreadable) turns this run MEMORY-ONLY: the
// store is latched and never written, links still work and end at quit, and the owner is told on
-// every create (R22, R42c) — as when the keychain refuses to seal (the store answers memory-only).
+// every create (R22, R42c). A keychain that refuses to SEAL is narrower (R45): the store still writes
+// every link it holds a sealed form of — read at boot or sealed earlier this run — so only a link that
+// was never sealed (the one just created) is not saved; it answers memory-only, and the owner is told.
// `init()` writes only when it actually pruned something (R42a), so a boot never rewrites the file.
//
// ENTITLEMENT. A host never mints with an empty entitlement (the API would answer 400, which stops
diff --git a/src/core/watch-link/store.test.ts b/src/core/watch-link/store.test.ts
index a4f2ae4ba..0e9a4e511 100644
--- a/src/core/watch-link/store.test.ts
+++ b/src/core/watch-link/store.test.ts
@@ -1,6 +1,7 @@
import { describe, it, expect, vi } from 'vitest'
import { promises as fsp, existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
+import { createHash } from 'node:crypto'
import { testTmpDir } from '../test-tmp'
import { renameAtomic, writeFileAtomic } from '../fs-atomic'
import { WatchLinkStore, WatchLinkStoreUnreadable, type WatchLinkRecord } from './store'
@@ -480,6 +481,31 @@ describe('a keychain that stops sealing mid-run (R45)', () => {
expect(ids(f)).toEqual([])
})
+ it('keeps a sealed form only for the links in the last saved list (a stopped link\'s form is dropped)', async () => {
+ const f = file()
+ const k = switchable()
+ const s = new WatchLinkStore({ file: f, seal: k.flaky, unseal })
+ expect(await s.save([a, b])).toBe('saved')
+ expect(await s.save([a])).toBe('saved') // b stopped
+ k.state.refuse = true
+ // b again (same id, same secret): its form was dropped with it, so it needs sealing — refused.
+ expect(await s.save([a, b])).toBe('memory-only')
+ expect(ids(f)).toEqual([a.linkId])
+ })
+
+ it('holds a DIGEST of each secret, never its text or bytes', async () => {
+ const f = file()
+ const s = new WatchLinkStore({ file: f, seal, unseal })
+ await s.save([a, c])
+ await s.load()
+ const held = JSON.stringify([...(s as unknown as { sealedForms: Map<string, unknown> }).sealedForms])
+ for (const r of [a, c]) {
+ expect(held).not.toContain(Buffer.from(r.secret).toString('base64'))
+ expect(held).not.toContain(Buffer.from(r.secret).toString('hex'))
+ }
+ expect(held).toContain(createHash('sha256').update(a.secret).digest('hex'))
+ })
+
it('never re-seals a secret it already holds sealed (the file does not churn on every write)', async () => {
const f = file()
let seals = 0
diff --git a/src/core/watch-link/store.ts b/src/core/watch-link/store.ts
index e6e420068..73d247646 100644
--- a/src/core/watch-link/store.ts
+++ b/src/core/watch-link/store.ts
@@ -38,6 +38,7 @@
// `seal` starts throwing only a link it NEVER sealed is left out (reported 'memory-only'); links read
// at boot and links sealed earlier stay on disk, and a link no longer in the list is still dropped.
import { promises as fs } from 'node:fs'
+import { createHash } from 'node:crypto'
import { renameAtomic, writeFileAtomic } from '../fs-atomic'
import { LINK_ID_RE } from '../../shared/watch-link/link'
import type { WatchLinkRole } from '../../shared/watch-link/protocol'
@@ -69,11 +70,13 @@ export class WatchLinkStoreUnreadable extends Error {
}
}
-/** A secret's base64 text and the sealed text the file holds for it. */
+/** A DIGEST of a secret (never its text or bytes: the cache must not be a second plaintext copy)
+ * and the sealed text the file holds for it. */
interface SealedForm {
- secret: string
+ digest: string
sealed: string
}
+const digestOf = (secret: Uint8Array): string => createHash('sha256').update(secret).digest('hex')
interface FileEntry {
linkId: string
@@ -116,8 +119,9 @@ export class WatchLinkStore {
/** Valid entries whose sealed secret this run could not unseal (R42), carried back on every write
* until their own `expiresAt`. Set by each `load()`. */
private opaque: FileEntry[] = []
- /** The sealed form of every secret this run read or sealed, by link id (R45): reused on every
- * write, so a keychain that starts refusing mid-run costs only a link it never sealed. */
+ /** The sealed form of every secret this run read or sealed, by link id, with a digest of the secret
+ * it seals (R45): reused on every write, so a keychain that starts refusing mid-run costs only a
+ * link it never sealed. Replaced by each save's list, so a stopped link's form goes with it. */
private sealedForms = new Map<string, SealedForm>()
constructor(
@@ -201,7 +205,7 @@ export class WatchLinkStore {
continue
}
if (!secret) continue
- if (sealed) sealedForms.set(e.linkId, { secret: Buffer.from(secret).toString('base64'), sealed: e.secret })
+ if (sealed) sealedForms.set(e.linkId, { digest: digestOf(secret), sealed: e.secret })
out.push({ linkId: e.linkId, nodeId: e.nodeId, role: e.role, label: e.label, title: e.title, createdAt: e.createdAt, expiresAt: e.expiresAt, secret })
}
this.opaque = opaque
@@ -260,7 +264,8 @@ export class WatchLinkStore {
let secret = text
if (this.o.seal) {
const known = this.sealedForms.get(r.linkId)
- if (known && known.secret === text) {
+ const digest = digestOf(r.secret)
+ if (known && known.digest === digest) {
// Never re-sealed: the form read at boot or sealed earlier this run is still valid, and a
// keychain that has started refusing must not cost a link it already holds sealed (R45).
secret = known.sealed
@@ -274,7 +279,7 @@ export class WatchLinkStore {
continue
}
}
- sealedNext.set(r.linkId, { secret: text, sealed: secret })
+ sealedNext.set(r.linkId, { digest, sealed: secret })
}
links.push({
linkId: r.linkId, nodeId: r.nodeId, role: r.role, label: r.label, title: r.title,
diff --git a/src/shared/watch-link-types.ts b/src/shared/watch-link-types.ts
index 7f8f50947..9d187930c 100644
--- a/src/shared/watch-link-types.ts
+++ b/src/shared/watch-link-types.ts
@@ -88,8 +88,11 @@ export type WatchLinkNotice =
| { kind: 'joined'; linkId: string; nodeId: string; title: string; viewers: number }
/** A link ended on its own: `revoked` here is a SERVER-side revoke (an owner's own Stop raises none). */
| { kind: 'ended'; linkId: string; nodeId: string; title: string; reason: 'expired' | 'revoked' | 'node-gone' }
- /** Links are not written to disk this run (the keychain refused to seal, or the links file could
- * not be read): they work, and end at quit. Sent at boot and after every create while it holds. */
+ /** A link is not written to disk this run. Two causes: the links file could not be read (then NO
+ * link is saved this run; they work and end at quit — sent at boot and after every create), or the
+ * keychain refused to seal (then only the link just created is not saved; links read at boot or
+ * sealed earlier stay saved — sent after that create). Copy: "only the link just created is not
+ * saved" unless the renderer knows it is the first cause. */
| { kind: 'not-persistent' }
/** `window.nodeTerminal.watchLink`. Desktop: real (preload). Server Edition: real bridge, and create