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.

feat(relay): the desktop phone relay proves its host key and stops on a proof refusal

review5336e29d0ad1
diff --git a/src/main/remote/standing-host.test.ts b/src/main/remote/standing-host.test.ts
index 2111a99df..b157f0815 100644
--- a/src/main/remote/standing-host.test.ts
+++ b/src/main/remote/standing-host.test.ts
@@ -7,11 +7,23 @@
// own bookkeeping.
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
-import { initPlatform, resetPlatformForTests } from '../../core/platform'
+import { writeFileSync } from 'node:fs'
+import { join } from 'node:path'
+import nacl from 'tweetnacl'
+import { initPlatform, platform, resetPlatformForTests } from '../../core/platform'
import { fakePlatform } from '../../core/platform-fake'
import { presenceHub } from '../../core/presence/hub'
import type { ApprovedDevices } from './approved-devices-core'
import type { HostSession, HostSessionOptions } from './host-service'
+import { createTestPopServer } from '../../core/relay/relay-pop.test-server'
+import { POP_REFUSED_MESSAGE } from '../../core/relay/relay-pop'
+
+// The host's key pair: a REAL X25519 pair, so the standing host can compute a key proof that the
+// test pop server (a mirror of the backend's verify) accepts or refuses on the real bytes.
+const hostKeys = nacl.box.keyPair()
+const hostPubB64 = Buffer.from(hostKeys.publicKey).toString('base64')
+// Swappable: null = the free tier (mint by deviceId), a string = a stored Pro entitlement.
+let storedEntitlement: string | null = 'entitlement'
const ipc: Record<string, (e: unknown, msg: unknown) => any> = {}
const errorBoxes: Array<{ title: string; body: string }> = []
@@ -30,7 +42,7 @@ vi.mock('electron', () => ({
vi.mock('../../core/pty-manager', () => ({ PtyManager: class {} }))
vi.mock('../../core/license', () => ({
isPremium: () => true,
- getStoredEntitlement: () => 'entitlement'
+ getStoredEntitlement: () => storedEntitlement
}))
vi.mock('./host-canvas-hub', () => ({
initHostCanvasHub: () => {},
@@ -39,7 +51,14 @@ vi.mock('./host-canvas-hub', () => ({
}))
let disk: ApprovedDevices = { pubkeys: [] }
const persist = vi.fn(async (update: (s: ApprovedDevices) => ApprovedDevices) => { disk = update(disk) })
-vi.mock('./relay-advertise', () => ({ writeRelayAdvertisement: async () => {}, removeRelayAdvertisement: async () => {} }))
+// Counted: stop() withdraws the advertisement, which is how a refused host stops inviting phones.
+let advertisementsRemoved = 0
+vi.mock('./relay-advertise', () => ({
+ writeRelayAdvertisement: async () => {},
+ removeRelayAdvertisement: async () => {
+ advertisementsRemoved += 1
+ }
+}))
// Per-role pin stores (approved-devices.ts), in memory. The 'phone' store — the one this module
// must write — is `disk`; every other role lives in `otherPins`, so a pin landing in the WRONG
// store is visible to the assertions instead of indistinguishable from the right one.
@@ -67,7 +86,7 @@ vi.mock('./approved-devices', () => {
retireLegacyPinFile: async () => 0
}
})
-vi.mock('./e2ee', () => ({ publicKeyToB64: () => 'host-pub' }))
+vi.mock('./e2ee', () => ({ publicKeyToB64: (k: Uint8Array) => Buffer.from(k).toString('base64') }))
const sessions: Array<{ opts: HostSessionOptions; session: HostSession; closed: number }> = []
@@ -81,7 +100,7 @@ vi.mock('./host-service', () => ({
relayAllowed: () => true,
loadOrCreateKeyPair: async () => {
if (keyError) throw keyError
- return { publicKey: new Uint8Array(), secretKey: new Uint8Array() }
+ return hostKeys
},
connectHostSession: (opts: HostSessionOptions): HostSession => {
const entry = { opts, closed: 0, session: null as unknown as HostSession }
@@ -104,10 +123,56 @@ import { initStandingHost, tokenTtlMs } from './standing-host'
import { revokeAllPhones, revokePeerKey } from './peer-revoke'
import { IPC } from '../../shared/ipc'
-/** Let the async connectOne() chain (token mint, keypair) settle. */
+/** Let the async connectOne() chain (keypair, key-proof challenge, token mint) settle. */
async function settle(): Promise<void> {
- for (let i = 0; i < 12; i++) await Promise.resolve()
+ for (let i = 0; i < 40; i++) await Promise.resolve()
+}
+
+// ── fetch, routed by URL ──────────────────────────────────────────────────────────────────────
+// A mint is now TWO requests: `/v1/relay/challenge` (the key-proof challenge), then
+// `/v1/relay/host-token`. Every "how many mints" assertion counts host-token calls only.
+interface FakeRes {
+ ok: boolean
+ status: number
+ headers?: Headers
+ json: () => Promise<unknown>
+}
+type Route = (init: RequestInit) => FakeRes | Promise<FakeRes>
+const res = (status: number, body: unknown = {}, headers?: Headers): FakeRes => ({
+ ok: status >= 200 && status < 300,
+ status,
+ ...(headers ? { headers } : {}),
+ json: async () => body
+})
+/** A backend that predates the key proof: no challenge route. */
+const challengeUnsupported: Route = () => res(404)
+const hostTokenOk: Route = () => res(200, { pairingToken: 'tok', hostId: 'host', exp: 0 })
+/** A challenge route answered by the backend's own issue() (mirrored in relay-pop.test-server). */
+const challengeFrom =
+ (pop: ReturnType<typeof createTestPopServer>): Route =>
+ (init) => {
+ const b = JSON.parse(String(init.body)) as { hostPublicKeyB64: string; purpose: 'host-token' | 'push' }
+ return res(200, pop.issue(b.hostPublicKeyB64, b.purpose))
+ }
+
+let fetchMock: ReturnType<typeof vi.fn<(url: string, init: RequestInit) => Promise<FakeRes>>>
+function routeFetch(r: { challenge?: Route; hostToken?: Route } = {}): void {
+ const challenge = r.challenge ?? challengeUnsupported
+ const hostToken = r.hostToken ?? hostTokenOk
+ fetchMock = vi.fn(async (url: string, init: RequestInit) => {
+ if (url.endsWith('/v1/relay/challenge')) return challenge(init)
+ if (url.endsWith('/v1/relay/host-token')) return hostToken(init)
+ throw new Error(`unexpected fetch ${url}`)
+ })
+ vi.stubGlobal('fetch', fetchMock)
}
+const callsTo = (suffix: string): Array<[string, RequestInit]> =>
+ fetchMock.mock.calls.filter(([url]) => url.endsWith(suffix))
+/** Host-token mints only — the challenge request is not a mint. */
+const mintCalls = (): Array<[string, RequestInit]> => callsTo('/v1/relay/host-token')
+const challengeCalls = (): Array<[string, RequestInit]> => callsTo('/v1/relay/challenge')
+const mintBody = (i = -1): Record<string, unknown> =>
+ JSON.parse(String(mintCalls().at(i)![1].body)) as Record<string, unknown>
function phones(): number {
return presenceHub.peers().filter((p) => p.kind === 'phone').length
@@ -143,14 +208,10 @@ beforeEach(() => {
for (const k of Object.keys(otherPins)) delete otherPins[k]
persist.mockImplementation(async (update) => { disk = update(disk) })
keyError = null
+ storedEntitlement = 'entitlement'
+ advertisementsRemoved = 0
for (const key of Object.keys(ipc)) delete ipc[key]
- vi.stubGlobal(
- 'fetch',
- vi.fn(async () => ({
- ok: true,
- json: async () => ({ pairingToken: 'tok', hostId: 'host', exp: 0 })
- }))
- )
+ routeFetch()
})
afterEach(() => {
@@ -394,16 +455,15 @@ describe('standing host: a refused token mint backs off', () => {
// pool back up on a microtask even after a FAILED mint, so the backoff scheduleReconnect()
// had just armed never got a chance to run.
vi.useFakeTimers()
- const fetchMock = vi.fn(async () => ({ ok: false, status: 429, json: async () => ({}) }))
- vi.stubGlobal('fetch', fetchMock)
+ routeFetch({ hostToken: () => res(429) })
const host = makeHost()
host.syncFromSettings()
for (let i = 0; i < 20; i++) await settle()
- expect(fetchMock).toHaveBeenCalledTimes(1)
+ expect(mintCalls()).toHaveLength(1)
// The backoff then retries — refusal is not a permanent stop.
await vi.advanceTimersByTimeAsync(1000)
for (let i = 0; i < 5; i++) await settle()
- expect(fetchMock).toHaveBeenCalledTimes(2)
+ expect(mintCalls()).toHaveLength(2)
host.stop()
})
})
@@ -418,15 +478,11 @@ describe('standing host: a listener the relay drops backs off (relay unreachable
// 2½ minutes. Every mint succeeded (resetting the backoff), every socket died at once, and
// onClose re-minted immediately — ~30 mints in 3 s until the API's per-IP limit answered 429.
vi.useFakeTimers()
- const fetchMock = vi.fn(async () => ({
- ok: true,
- json: async () => ({ pairingToken: 'tok', hostId: 'host', exp: 0 })
- }))
- vi.stubGlobal('fetch', fetchMock)
+ routeFetch()
const host = makeHost()
host.syncFromSettings()
for (let i = 0; i < 10; i++) await settle()
- expect(fetchMock).toHaveBeenCalledTimes(1)
+ expect(mintCalls()).toHaveLength(1)
const dropNewest = async (): Promise<void> => {
sessions.at(-1)!.opts.onClose() // the relay drops the idle listener on its own
@@ -434,25 +490,21 @@ describe('standing host: a listener the relay drops backs off (relay unreachable
}
// Drop #1: nothing immediate, one re-mint after 1 s.
await dropNewest()
- expect(fetchMock).toHaveBeenCalledTimes(1)
+ expect(mintCalls()).toHaveLength(1)
await vi.advanceTimersByTimeAsync(1000)
- expect(fetchMock).toHaveBeenCalledTimes(2)
+ expect(mintCalls()).toHaveLength(2)
// Drop #2 right after that SUCCESSFUL mint: the delay grew to 2 s — the mint did not reset it.
await dropNewest()
await vi.advanceTimersByTimeAsync(1999)
- expect(fetchMock).toHaveBeenCalledTimes(2)
+ expect(mintCalls()).toHaveLength(2)
await vi.advanceTimersByTimeAsync(1)
- expect(fetchMock).toHaveBeenCalledTimes(3)
+ expect(mintCalls()).toHaveLength(3)
host.stop()
})
it('a listener that lives to its refresh resets the backoff', async () => {
vi.useFakeTimers()
- const fetchMock = vi.fn(async () => ({
- ok: true,
- json: async () => ({ pairingToken: 'tok', hostId: 'host', exp: 0 })
- }))
- vi.stubGlobal('fetch', fetchMock)
+ routeFetch()
const host = makeHost()
host.syncFromSettings()
for (let i = 0; i < 10; i++) await settle()
@@ -461,14 +513,14 @@ describe('standing host: a listener the relay drops backs off (relay unreachable
await vi.advanceTimersByTimeAsync(1000)
sessions.at(-1)!.opts.onClose()
await vi.advanceTimersByTimeAsync(2000)
- const before = fetchMock.mock.calls.length
+ const before = mintCalls().length
// …then the listener holds for a full token lifetime (refresh at 120 − 30 = 90 s).
await vi.advanceTimersByTimeAsync(90_000)
- expect(fetchMock.mock.calls.length).toBe(before + 1) // the refresh re-mint
+ expect(mintCalls().length).toBe(before + 1) // the refresh re-mint
// A drop now waits 1 s again, not 4 s.
sessions.at(-1)!.opts.onClose()
await vi.advanceTimersByTimeAsync(1000)
- expect(fetchMock.mock.calls.length).toBe(before + 2)
+ expect(mintCalls().length).toBe(before + 2)
host.stop()
})
})
@@ -493,20 +545,327 @@ describe('standing host: token refresh is immune to this machine\'s clock error'
vi.useFakeTimers()
const serverNow = Date.parse('Sun, 27 Sep 2026 08:00:00 GMT')
vi.setSystemTime(serverNow + 75_000)
- const fetchMock = vi.fn(async () => ({
- ok: true,
- headers: new Headers({ date: new Date(serverNow).toUTCString() }),
- json: async () => ({ pairingToken: 'tok', hostId: 'host', exp: serverNow / 1000 + 120 })
- }))
- vi.stubGlobal('fetch', fetchMock)
+ routeFetch({
+ hostToken: () =>
+ res(
+ 200,
+ { pairingToken: 'tok', hostId: 'host', exp: serverNow / 1000 + 120 },
+ new Headers({ date: new Date(serverNow).toUTCString() })
+ )
+ })
const host = makeHost()
host.syncFromSettings()
for (let i = 0; i < 10; i++) await settle()
- expect(fetchMock).toHaveBeenCalledTimes(1)
+ expect(mintCalls()).toHaveLength(1)
await vi.advanceTimersByTimeAsync(60_000)
- expect(fetchMock).toHaveBeenCalledTimes(1) // the old code had re-minted 4 times by now
+ expect(mintCalls()).toHaveLength(1) // the old code had re-minted 4 times by now
await vi.advanceTimersByTimeAsync(30_000)
- expect(fetchMock).toHaveBeenCalledTimes(2)
+ expect(mintCalls()).toHaveLength(2)
+ host.stop()
+ })
+})
+
+describe('standing host: host key proof-of-possession on the host-token mint', () => {
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ /** A pre-seeded device id, so the free tier's subject is a known value. */
+ function seedDeviceId(id: string): void {
+ writeFileSync(join(platform().userDataDir, 'device-id'), id, 'utf-8')
+ }
+
+ it('Pro: the mint carries popChallenge + popProof beside the entitlement, proved with subject ""', async () => {
+ const pop = createTestPopServer()
+ routeFetch({ challenge: challengeFrom(pop) })
+ const host = makeHost()
+ host.setEnabled(true)
+ await settle()
+
+ expect(challengeCalls()).toHaveLength(1)
+ const chBody = JSON.parse(String(challengeCalls()[0][1].body)) as Record<string, unknown>
+ expect(chBody).toEqual({ hostPublicKeyB64: hostPubB64, purpose: 'host-token' })
+
+ expect(mintCalls()).toHaveLength(1)
+ const body = mintBody()
+ expect(body.entitlement).toBe('entitlement')
+ expect(body.hostPublicKeyB64).toBe(hostPubB64)
+ expect(body).not.toHaveProperty('deviceId')
+ expect(typeof body.popChallenge).toBe('string')
+ expect(typeof body.popProof).toBe('string')
+ // The backend's own verify (mirrored in the test server) accepts it for the Pro subject ''.
+ expect(
+ pop.verify({
+ hostPublicKeyB64: hostPubB64,
+ purpose: 'host-token',
+ subject: '',
+ popChallenge: body.popChallenge,
+ popProof: body.popProof
+ })
+ ).toBe(true)
+ // ONE abort signal covers the challenge and the mint.
+ expect(challengeCalls()[0][1].signal).toBeInstanceOf(AbortSignal)
+ expect(mintCalls()[0][1].signal).toBe(challengeCalls()[0][1].signal)
+ expect(sessions).toHaveLength(1)
+ host.stop()
+ })
+
+ it('free tier: the proof subject is getDeviceId(), the same id the body sends', async () => {
+ storedEntitlement = null
+ seedDeviceId('device-fixed')
+ const pop = createTestPopServer()
+ routeFetch({ challenge: challengeFrom(pop) })
+ const host = makeHost()
+ host.setEnabled(true)
+ await settle()
+
+ expect(mintCalls()).toHaveLength(1)
+ const body = mintBody()
+ expect(body.deviceId).toBe('device-fixed')
+ expect(body).not.toHaveProperty('entitlement')
+ const proof = {
+ hostPublicKeyB64: hostPubB64,
+ purpose: 'host-token' as const,
+ popChallenge: body.popChallenge,
+ popProof: body.popProof
+ }
+ // A proof for the Pro subject would not pass for a free mint (checked first: a mismatch does
+ // not consume the single-use nonce).
+ expect(pop.verify({ ...proof, subject: '' })).toBe(false)
+ expect(pop.verify({ ...proof, subject: 'device-fixed' })).toBe(true)
+ host.stop()
+ })
+
+ it('free tier on first launch: the proof subject and the body deviceId are ONE read', async () => {
+ // No device-id file yet: getDeviceId() mints a fresh uuid and writes it ASYNC, so a second
+ // call before that write lands mints ANOTHER uuid. Reading it twice sends a body deviceId the
+ // proof was not computed over, which the backend refuses as pop_invalid — a terminal stop.
+ storedEntitlement = null
+ const pop = createTestPopServer()
+ routeFetch({ challenge: challengeFrom(pop) })
+ const host = makeHost()
+ host.setEnabled(true)
+ await settle()
+
+ expect(mintCalls()).toHaveLength(1)
+ const body = mintBody()
+ expect(typeof body.deviceId).toBe('string')
+ expect(
+ pop.verify({
+ hostPublicKeyB64: hostPubB64,
+ purpose: 'host-token',
+ subject: body.deviceId as string,
+ popChallenge: body.popChallenge,
+ popProof: body.popProof
+ })
+ ).toBe(true)
+ host.stop()
+ })
+
+ it('an old backend (challenge 404/405) gets the legacy mint: no popChallenge, no popProof', async () => {
+ for (const status of [404, 405]) {
+ routeFetch({ challenge: () => res(status) })
+ const host = makeHost()
+ host.setEnabled(true)
+ await settle()
+ expect(mintCalls()).toHaveLength(1)
+ expect(mintBody()).toEqual({ entitlement: 'entitlement', hostPublicKeyB64: hostPubB64 })
+ expect(sessions.length).toBeGreaterThan(0)
+ host.stop()
+ sessions.length = 0
+ }
+ })
+
+ it.each(['pop_required', 'pop_invalid'])(
+ 'a %s refusal of a PROVEN mint stops hosting, says so once, and never re-mints',
+ async (error) => {
+ vi.useFakeTimers()
+ const pop = createTestPopServer()
+ routeFetch({ challenge: challengeFrom(pop), hostToken: () => res(403, { error }) })
+ const host = makeHost()
+ host.setEnabled(true)
+ for (let i = 0; i < 5; i++) await settle()
+
+ expect(mintCalls()).toHaveLength(1)
+ expect(mintBody()).toHaveProperty('popProof')
+ expect(sessions).toHaveLength(0)
+ expect(errorBoxes).toHaveLength(1)
+ expect(errorBoxes[0].body).toContain(POP_REFUSED_MESSAGE)
+ // Terminal: no reconnect timer is left armed.
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(mintCalls()).toHaveLength(1)
+ expect(challengeCalls()).toHaveLength(1)
+ expect(errorBoxes).toHaveLength(1)
+ host.stop()
+ }
+ )
+
+ it('a refusal on the replacement mint while a phone is bridged tears hosting down (stop)', async () => {
+ // The realistic moment for a refusal: a phone bridges, the pool mints a replacement listener,
+ // and THAT mint is refused. Stopping must cut the live session, withdraw the advertisement and
+ // leave nothing that re-mints — a late socket close must not raise a second dialog.
+ vi.useFakeTimers()
+ const pop = createTestPopServer()
+ let mints = 0
+ routeFetch({
+ challenge: challengeFrom(pop),
+ hostToken: (init) => (++mints === 1 ? hostTokenOk(init) : res(403, { error: 'pop_invalid' }))
+ })
+ const host = makeHost()
+ host.setEnabled(true)
+ for (let i = 0; i < 5; i++) await settle()
+ expect(sessions).toHaveLength(1)
+
+ sessions[0].opts.onPeerReady(sessions[0].session) // bridged → the pool mints a replacement
+ for (let i = 0; i < 5; i++) await settle()
+ expect(mintCalls()).toHaveLength(2)
+ expect(errorBoxes).toHaveLength(1)
+ expect(errorBoxes[0].body).toContain(POP_REFUSED_MESSAGE)
+ expect(sessions[0].closed).toBe(1) // the bridged phone session is cut
+ expect(phones()).toBe(0)
+ expect(advertisementsRemoved).toBe(1) // phones stop minting against a host that is gone
+
+ sessions[0].opts.onClose() // a late transport close of the cut session
+ for (let i = 0; i < 5; i++) await settle()
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(mintCalls()).toHaveLength(2)
+ expect(errorBoxes).toHaveLength(1)
+ host.stop()
+ })
+
+ it('a pop_invalid refusal is terminal even for an unproven (legacy) mint', async () => {
+ vi.useFakeTimers()
+ routeFetch({ hostToken: () => res(403, { error: 'pop_invalid' }) })
+ const host = makeHost()
+ host.setEnabled(true)
+ for (let i = 0; i < 5; i++) await settle()
+ expect(mintBody()).not.toHaveProperty('popProof')
+ expect(errorBoxes).toHaveLength(1)
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(mintCalls()).toHaveLength(1)
+ host.stop()
+ })
+
+ it('pop_required on an UNPROVEN mint (challenge 404, backend mid-redeploy) is transient: backoff, fresh challenge', async () => {
+ // A reverse proxy answers 404 for /challenge while the backend redeploys; the unproven mint
+ // that follows can land on the fresh backend, which requires a proof from a host it has seen
+ // prove before. Stopping would end hosting for good over a redeploy.
+ vi.useFakeTimers()
+ const pop = createTestPopServer()
+ let redeploying = true
+ routeFetch({
+ challenge: (init) => (redeploying ? res(404) : challengeFrom(pop)(init)),
+ hostToken: (init) => {
+ const b = JSON.parse(String(init.body)) as Record<string, unknown>
+ return b.popProof ? hostTokenOk(init) : res(403, { error: 'pop_required' })
+ }
+ })
+ const host = makeHost()
+ host.setEnabled(true)
+ for (let i = 0; i < 5; i++) await settle()
+
+ expect(mintCalls()).toHaveLength(1)
+ expect(mintBody()).not.toHaveProperty('popProof')
+ expect(errorBoxes).toHaveLength(0) // not a refusal
+ expect(sessions).toHaveLength(0)
+
+ redeploying = false
+ await vi.advanceTimersByTimeAsync(1000) // the first backoff step
+ expect(challengeCalls()).toHaveLength(2) // the retry asks for a FRESH challenge
+ expect(mintCalls()).toHaveLength(2)
+ expect(mintBody()).toHaveProperty('popProof')
+ expect(sessions).toHaveLength(1)
+ expect(errorBoxes).toHaveLength(0)
+ host.stop()
+ })
+
+ it('a 403 that is not a key-proof refusal stays an ordinary failure (backoff, no dialog)', async () => {
+ vi.useFakeTimers()
+ const pop = createTestPopServer()
+ routeFetch({ challenge: challengeFrom(pop), hostToken: () => res(403, { error: 'forbidden' }) })
+ const host = makeHost()
+ host.setEnabled(true)
+ for (let i = 0; i < 5; i++) await settle()
+ expect(mintCalls()).toHaveLength(1)
+ expect(errorBoxes).toHaveLength(0)
+ await vi.advanceTimersByTimeAsync(1000)
+ expect(mintCalls()).toHaveLength(2)
+ host.stop()
+ })
+
+ it.each<[string, Route]>([
+ ['503', () => res(503)],
+ ['500', () => res(500)],
+ ['429', () => res(429)],
+ ['a network error', () => Promise.reject(new TypeError('fetch failed'))],
+ ['a malformed 200', () => res(200, { nope: true })],
+ // A server key the proof cannot use: the all-zero key gives a degenerate shared secret.
+ ['a 200 with an unusable server key', () => res(200, { challenge: 'c.x', serverPublicKeyB64: 'A'.repeat(43) + '=' })]
+ ])('the challenge answering %s makes NO mint and backs off', async (_label, challenge) => {
+ // Never an unproven mint after a transient challenge failure: to a host the backend has seen
+ // prove before, that mint is a 403 — a terminal stop — over a blip.
+ vi.useFakeTimers()
+ routeFetch({ challenge })
+ const host = makeHost()
+ host.setEnabled(true)
+ for (let i = 0; i < 5; i++) await settle()
+ expect(challengeCalls()).toHaveLength(1)
+ expect(mintCalls()).toHaveLength(0)
+ expect(errorBoxes).toHaveLength(0)
+ // The existing backoff: one retry after 1 s.
+ await vi.advanceTimersByTimeAsync(999)
+ expect(challengeCalls()).toHaveLength(1)
+ await vi.advanceTimersByTimeAsync(1)
+ expect(challengeCalls()).toHaveLength(2)
+ expect(mintCalls()).toHaveLength(0)
+ host.stop()
+ })
+
+ it('a challenge that never answers is aborted by the 8 s mint timeout (no mint, backoff)', async () => {
+ vi.useFakeTimers()
+ routeFetch({
+ challenge: (init) =>
+ new Promise<FakeRes>((_resolve, reject) => {
+ init.signal?.addEventListener('abort', () => reject(new Error('aborted')))
+ })
+ })
+ const host = makeHost()
+ host.setEnabled(true)
+ await vi.advanceTimersByTimeAsync(7999)
+ expect(challengeCalls()).toHaveLength(1)
+ expect(challengeCalls()[0][1].signal?.aborted).toBe(false)
+ await vi.advanceTimersByTimeAsync(1)
+ expect(challengeCalls()[0][1].signal?.aborted).toBe(true)
+ expect(mintCalls()).toHaveLength(0)
+ await vi.advanceTimersByTimeAsync(1000) // then the backoff retries
+ expect(challengeCalls()).toHaveLength(2)
+ host.stop()
+ })
+
+ it('ONE 8 s timer covers the challenge AND the mint (a slow challenge leaves the mint the rest)', async () => {
+ vi.useFakeTimers()
+ const pop = createTestPopServer()
+ routeFetch({
+ challenge: (init) =>
+ new Promise<FakeRes>((resolve) => {
+ setTimeout(() => resolve(challengeFrom(pop)(init) as FakeRes), 5000)
+ }),
+ hostToken: (init) =>
+ new Promise<FakeRes>((_resolve, reject) => {
+ init.signal?.addEventListener('abort', () => reject(new Error('aborted')))
+ })
+ })
+ const host = makeHost()
+ host.setEnabled(true)
+ await vi.advanceTimersByTimeAsync(5000)
+ expect(challengeCalls()).toHaveLength(1)
+ expect(mintCalls()).toHaveLength(1) // the challenge answered at 5 s, the mint is in flight
+ const signal = mintCalls()[0][1].signal!
+ await vi.advanceTimersByTimeAsync(2999)
+ expect(signal.aborted).toBe(false)
+ await vi.advanceTimersByTimeAsync(1) // 8 s after the challenge STARTED, not 8 s after the mint
+ expect(signal.aborted).toBe(true)
+ expect(sessions).toHaveLength(0)
host.stop()
})
})
diff --git a/src/main/remote/standing-host.ts b/src/main/remote/standing-host.ts
index 4f1d9bc3d..16888fd10 100644
--- a/src/main/remote/standing-host.ts
+++ b/src/main/remote/standing-host.ts
@@ -42,6 +42,13 @@ import { isPinned, pinDevice } from './approved-devices-core'
import { phonePins } from './approved-devices'
import { registerPeerSessionKiller } from './peer-revoke'
import { createPhoneApprovals } from '../../core/phone-approval'
+import {
+ POP_REFUSED_MESSAGE,
+ computePopProof,
+ fetchPopChallenge,
+ popRefusalOf,
+ type PopRefusal
+} from '../../core/relay/relay-pop'
// Re-mint the token this long before its expiry (TTL is ~120s). Floored so a bogus/short exp can't
// spin us.
@@ -80,23 +87,79 @@ export function tokenTtlMs(exp: number, serverDate: string | null, localNowMs: n
* Mint a standing host token from the API. Pro proves entitlement; the free tier sends
* its deviceId instead (backend admits it against the server-side free-tier policy —
* until that ships, the mint fails and free hosting simply stays down, i.e. today's
- * behavior). Returns null on any failure.
+ * behavior).
+ *
+ * When the backend supports it, the mint also carries a proof that this process holds the host's
+ * secret key (relay-pop.ts), so a join code alone can no longer mint this host's tokens. Only a
+ * challenge answered 404/405 means "this backend predates the proof" and gets the legacy mint; any
+ * other challenge failure is transient and mints NOTHING: to a host the backend has seen prove
+ * before, an unproven mint is a 403, which would stop hosting over a blip.
+ *
+ * Returns `{ refused }` for a terminal key-proof refusal (the caller stops hosting and says so) and
+ * null for every other failure (the caller backs off and retries). One 403 is deliberately NOT
+ * terminal: `pop_required` for a mint sent WITHOUT a proof because the challenge said 404/405. A
+ * reverse proxy answers 404 while the backend redeploys, and the unproven mint that follows can land
+ * on the fresh backend; stopping there would end hosting for good over a redeploy, so it backs off
+ * and the next attempt asks for a challenge again.
*/
async function mintHostToken(
entitlement: string | null,
- hostPublicKeyB64: string
-): Promise<HostTokenResponse | null> {
+ keys: KeyPair
+): Promise<HostTokenResponse | { refused: PopRefusal } | null> {
+ const hostPublicKeyB64 = publicKeyToB64(keys.publicKey)
+ // Read ONCE: the proof's subject must be the exact id the body sends. On a first launch
+ // getDeviceId() mints a uuid and writes it asynchronously, so a second call before that write
+ // lands mints another one, and the backend would refuse the pair as pop_invalid (terminal).
+ const deviceId = entitlement ? null : getDeviceId()
+ const base = entitlement ? { entitlement, hostPublicKeyB64 } : { deviceId, hostPublicKeyB64 }
const ctrl = new AbortController()
+ // One timer for the whole mint: the challenge, the host-token request and its body read.
const timer = setTimeout(() => ctrl.abort(), 8000)
try {
+ const ch = await fetchPopChallenge({
+ apiBase: API_BASE,
+ hostPublicKeyB64,
+ purpose: 'host-token',
+ signal: ctrl.signal
+ })
+ if (!ch.ok && !ch.unsupported) return null // transient: back off, never mint unproven
+ // True only when the challenge said 404/405: the one case this mint goes out unproven although
+ // we hold the key (see the header on what a pop_required then means).
+ const sentUnproven = !ch.ok && ch.unsupported
+ let proof: { popChallenge: string; popProof: string } | null = null
+ if (ch.ok) {
+ try {
+ proof = {
+ popChallenge: ch.challenge,
+ popProof: computePopProof({
+ hostSecretKey: keys.secretKey,
+ hostPublicKeyB64,
+ challenge: ch.challenge,
+ serverPublicKeyB64: ch.serverPublicKeyB64,
+ purpose: 'host-token',
+ // The backend proves host-token against `body.deviceId ?? ''`: '' on the Pro path.
+ subject: deviceId ?? ''
+ })
+ }
+ } catch {
+ // A server key the proof cannot use (malformed, or low-order): back off, never mint.
+ return null
+ }
+ }
const res = await fetch(`${API_BASE}/v1/relay/host-token`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
- body: JSON.stringify(
- entitlement ? { entitlement, hostPublicKeyB64 } : { deviceId: getDeviceId(), hostPublicKeyB64 }
- ),
+ // popChallenge and popProof travel together or not at all: the backend treats a half-present
+ // proof as invalid, and invalid is terminal.
+ body: JSON.stringify({ ...base, ...(proof ?? {}) }),
signal: ctrl.signal
})
+ if (res.status === 403) {
+ const refused = popRefusalOf(403, await res.json().catch(() => null))
+ // A backend that came back mid-redeploy (see the header): transient, not a refusal.
+ if (refused === 'pop_required' && sentUnproven) return null
+ if (refused) return { refused }
+ }
if (!res.ok) return null
const json = (await res.json().catch(() => ({}))) as Partial<HostTokenResponse>
if (!json.pairingToken) return null
@@ -140,6 +203,23 @@ function reportKeyLocked(err: Error): void {
console.error('[standing-host] host identity is locked:', err.message)
}
+/**
+ * The relay refused this host's key proof (`pop_invalid`, or `pop_required` on a proven mint). It
+ * will refuse every retry the same way, so hosting stops and the human is told, once, how to
+ * recover — instead of phone access going quietly dead, or a retry loop spending the host's mints.
+ */
+function reportPopRefused(): void {
+ try {
+ dialog.showErrorBox(
+ 'Remote access stopped',
+ `${POP_REFUSED_MESSAGE}\n\nPhone access is off until then. Turn it back on in Settings → Phone.`
+ )
+ } catch {
+ // No dialog available (headless / very early boot): the console line is the fallback.
+ }
+ console.error('[standing-host] the relay refused this host key proof')
+}
+
export interface StandingHost {
/** Explicit toggle (from the Settings switch). Reconciles the connection immediately. */
setEnabled(enabled: boolean): void
@@ -320,8 +400,14 @@ export function initStandingHost(
scheduleReconnect() // transient disk error: back off and try again
return
}
- const token = await mintHostToken(entitlement, publicKeyToB64(keys.publicKey))
+ const token = await mintHostToken(entitlement, keys)
if (!running) return
+ if (token && 'refused' in token) {
+ // Terminal: every retry would be refused the same way. stop() clears any armed reconnect.
+ stop()
+ reportPopRefused()
+ return
+ }
if (!token) {
scheduleReconnect()
return