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(remote): persistent safeStorage-backed peer keypair (stable client identity)

enesf35b5357fb7b
diff --git a/src/main/remote/peer-identity.test.ts b/src/main/remote/peer-identity.test.ts
new file mode 100644
index 000000000..6a15f0dad
--- /dev/null
+++ b/src/main/remote/peer-identity.test.ts
@@ -0,0 +1,99 @@
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { promises as fs } from 'fs'
+import os from 'os'
+import path from 'path'
+
+// A real temp userData dir + a swappable fake keychain. Both are read lazily inside the mock, so
+// vitest's hoisting of `vi.mock` above the imports is harmless.
+let userData = ''
+let encryptionAvailable = false
+
+const flip = (b: Buffer) => Buffer.from(b.map((x) => x ^ 0xff))
+
+vi.mock('electron', () => ({
+ app: { getPath: () => userData },
+ safeStorage: {
+ isEncryptionAvailable: () => encryptionAvailable,
+ encryptString: (s: string) => flip(Buffer.from(s, 'utf-8')),
+ decryptString: (b: Buffer) => flip(b).toString('utf-8')
+ }
+}))
+
+import { loadOrCreatePeerKeyPair } from './peer-identity'
+import { publicKeyToB64 } from './e2ee'
+
+const PEER_FILE = 'remote-peer-key.json'
+const peerPath = () => path.join(userData, PEER_FILE)
+const readFile = async () => JSON.parse(await fs.readFile(peerPath(), 'utf-8'))
+
+describe('peer-identity: persistent client keypair', () => {
+ beforeEach(async () => {
+ userData = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-peer-'))
+ encryptionAvailable = false // plaintext path: no keyring needed in CI
+ })
+ afterEach(async () => {
+ await fs.rm(userData, { recursive: true, force: true })
+ })
+
+ it('generates and persists a keypair on first use', async () => {
+ const keys = await loadOrCreatePeerKeyPair()
+ expect(keys.publicKey).toHaveLength(32)
+ expect(keys.secretKey).toHaveLength(32)
+ const onDisk = await readFile()
+ expect(onDisk.publicKey).toBe(publicKeyToB64(keys.publicKey))
+ })
+
+ it('returns the SAME public key across "reconnects" (repeated loads)', async () => {
+ const first = await loadOrCreatePeerKeyPair()
+ const second = await loadOrCreatePeerKeyPair()
+ const third = await loadOrCreatePeerKeyPair()
+ expect(publicKeyToB64(second.publicKey)).toBe(publicKeyToB64(first.publicKey))
+ expect(publicKeyToB64(third.publicKey)).toBe(publicKeyToB64(first.publicKey))
+ expect(Buffer.from(second.secretKey)).toEqual(Buffer.from(first.secretKey))
+ })
+
+ it('does not collide with the host key file', async () => {
+ await loadOrCreatePeerKeyPair()
+ // The host identity lives in remote-host-key.json; the peer must use its OWN file.
+ await expect(fs.access(path.join(userData, 'remote-host-key.json'))).rejects.toThrow()
+ })
+
+ it('writes the key file 0600', async () => {
+ await loadOrCreatePeerKeyPair()
+ const st = await fs.stat(peerPath())
+ expect(st.mode & 0o777).toBe(0o600)
+ })
+
+ it('encrypts the secret at rest when safeStorage is available', async () => {
+ encryptionAvailable = true
+ const keys = await loadOrCreatePeerKeyPair()
+ const onDisk = await readFile()
+ expect(onDisk.secretKey).toBeUndefined()
+ expect(onDisk.secretKeyEnc).toBeTypeOf('string')
+ // ...and still loads back to the same identity.
+ const again = await loadOrCreatePeerKeyPair()
+ expect(Buffer.from(again.secretKey)).toEqual(Buffer.from(keys.secretKey))
+ })
+
+ it('migrates a legacy plaintext file in place, KEEPING the identity', async () => {
+ const first = await loadOrCreatePeerKeyPair() // written plaintext (no keyring)
+ expect((await readFile()).secretKey).toBeTypeOf('string')
+ encryptionAvailable = true // machine gains a keyring
+ const second = await loadOrCreatePeerKeyPair()
+ expect(publicKeyToB64(second.publicKey)).toBe(publicKeyToB64(first.publicKey))
+ const onDisk = await readFile()
+ expect(onDisk.secretKey).toBeUndefined()
+ expect(onDisk.secretKeyEnc).toBeTypeOf('string')
+ })
+
+ it('regenerates and overwrites when the stored file is corrupt', async () => {
+ const first = await loadOrCreatePeerKeyPair()
+ await fs.writeFile(peerPath(), '{"publicKey":"zzz","secretKey":"@@@"}', 'utf-8')
+ const second = await loadOrCreatePeerKeyPair()
+ expect(second.publicKey).toHaveLength(32)
+ expect(publicKeyToB64(second.publicKey)).not.toBe(publicKeyToB64(first.publicKey))
+ // The fresh identity is persisted, so it is itself stable from here on.
+ const third = await loadOrCreatePeerKeyPair()
+ expect(publicKeyToB64(third.publicKey)).toBe(publicKeyToB64(second.publicKey))
+ })
+})
diff --git a/src/main/remote/peer-identity.ts b/src/main/remote/peer-identity.ts
new file mode 100644
index 000000000..ef97968da
--- /dev/null
+++ b/src/main/remote/peer-identity.ts
@@ -0,0 +1,51 @@
+// Persistent CLIENT (peer) identity for the relay transport.
+//
+// Today the relay client mints an EPHEMERAL keypair per connection (client-service.ts:286), so it
+// presents a different box public key on every reconnect and can never be pinned. This gives the
+// client a long-lived NaCl box keypair — the STABLE public identity a host pins on first pair and
+// recognises across reconnects. It mirrors host-service.ts's loadOrCreateKeyPair (same on-disk
+// shape, same failure posture) but uses its OWN file, so the peer and host identities never share
+// bytes. Encrypted at rest with Electron safeStorage; 0600 plaintext fallback when no keyring is
+// available — availability is re-checked on every load, so a machine that gains a keyring migrates
+// in place on the next start, KEEPING the identity.
+//
+// 4c wires this into client-service.ts in place of `genKeyPair()`.
+import { promises as fs } from 'fs'
+import path from 'path'
+import { app, safeStorage } from 'electron'
+import { genKeyPair, type KeyPair } from './e2ee'
+import { encodeKeyFile, decodeKeyFile } from './key-file-codec'
+
+function keyFile(): string {
+ return path.join(app.getPath('userData'), 'remote-peer-key.json')
+}
+
+async function persistKeyPair(keys: KeyPair): Promise<void> {
+ // 0o600 either way: the file still binds the pinned public identity.
+ await fs.writeFile(keyFile(), encodeKeyFile(keys, safeStorage), {
+ encoding: 'utf-8',
+ mode: 0o600
+ })
+}
+
+/**
+ * Load the long-lived client (peer) keypair, generating + persisting it on first use. The public key
+ * is what a host pins, so it must be stable across reconnects and app restarts. A legacy plaintext
+ * file upgrades to the encrypted form in place (same identity); a malformed file or an undecryptable
+ * blob (keychain reset) decodes to null and falls through to a fresh identity, which is written back
+ * so it is itself stable from then on — a new pairing then pins the new key.
+ */
+export async function loadOrCreatePeerKeyPair(): Promise<KeyPair> {
+ try {
+ const decoded = decodeKeyFile(await fs.readFile(keyFile(), 'utf-8'), safeStorage)
+ if (decoded) {
+ if (decoded.migrate) await persistKeyPair(decoded.keys).catch(() => {})
+ return decoded.keys
+ }
+ } catch {
+ // No stored key (first run) or an unreadable file — generate a fresh one below.
+ }
+ const keys = genKeyPair()
+ await persistKeyPair(keys).catch(() => {})
+ return keys
+}