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.

test(watch-link): pin the HKDF info and salt order to the relay's deriveSessionKey

review23cac3ac3290
diff --git a/src/core/watch-link/wire.test.ts b/src/core/watch-link/wire.test.ts
index 522e0581a..96f211e37 100644
--- a/src/core/watch-link/wire.test.ts
+++ b/src/core/watch-link/wire.test.ts
@@ -2,11 +2,11 @@ import { describe, it, expect } from 'vitest'
import { hkdfSync } from 'node:crypto'
import nacl from 'tweetnacl'
import { encodePtyData, encodeArgs, parseRpcMessage } from '../../shared/rpc'
-import { decrypt, encrypt } from '../relay/e2ee'
+import { decrypt, deriveSessionKey, encrypt, randomSessionNonce } from '../relay/e2ee'
import { hkdfSha256 } from '../../shared/watch-link/hkdf'
import { sealBox, openBox, withHeader, readHeader, encodePtyFrame, decodePtyFrame, parseTunnelJson, RELAY_SESSION_INFO } from '../../shared/watch-link/wire'
import { sanitizeChatText, sanitizeChatName, isWatchEndReason, WATCH_CHAT_CAST, WATCH_EVENT_PREFIX } from '../../shared/watch-link/protocol'
-import { utf8 } from '../../shared/watch-link/bytes'
+import { concatBytes, utf8 } from '../../shared/watch-link/bytes'
describe('the wire rules match the relay they were copied from', () => {
it('HKDF equals node:crypto', async () => {
@@ -14,6 +14,12 @@ describe('the wire rules match the relay they were copied from', () => {
const ours = await hkdfSha256(ikm, salt, utf8(RELAY_SESSION_INFO), 32)
expect(ours).toEqual(new Uint8Array(hkdfSync('sha256', ikm, salt, utf8(RELAY_SESSION_INFO), 32)))
})
+ it("HKDF info and salt order equal the relay's deriveSessionKey", async () => {
+ // The test above feeds OUR info string to both sides, so it cannot see the relay's change.
+ // This one derives the relay's session key from its own code: salt = hostNonce ‖ clientNonce.
+ const base = nacl.randomBytes(32), hn = randomSessionNonce(), cn = randomSessionNonce()
+ expect(await hkdfSha256(base, concatBytes(hn, cn), utf8(RELAY_SESSION_INFO), 32)).toEqual(deriveSessionKey(base, hn, cn))
+ })
it('a box sealed here opens with e2ee.decrypt and vice versa', () => {
const key = nacl.randomBytes(32), plain = utf8('hello')
expect(decrypt(sealBox(plain, key), key)).toEqual(plain)
diff --git a/src/shared/watch-link/wire.ts b/src/shared/watch-link/wire.ts
index fa627064c..8d7ac9e64 100644
--- a/src/shared/watch-link/wire.ts
+++ b/src/shared/watch-link/wire.ts
@@ -1,8 +1,12 @@
// The relay's wire rules, restated without Node so the browser can speak them. The SOURCE of truth
-// is src/core/relay/relay-socket.ts + e2ee.ts + src/shared/rpc.ts; src/core/watch-link/wire.test.ts
-// pins this copy to them byte for byte, so a change there fails there before it can strand the
-// viewer page. (The test lives in core because it imports e2ee.ts, which the web tsconfig project
-// cannot see.)
+// is src/core/relay/relay-socket.ts + e2ee.ts + src/shared/rpc.ts, and two tests pin this copy to it:
+// - src/core/watch-link/wire.test.ts compares against the originals directly: the seq header, the
+// sealed box (both directions), the pty frame, the tunnel JSON, and the HKDF info string + salt
+// order (against e2ee's deriveSessionKey). An edit to any of those in the originals fails it.
+// - TAG_*, ROLE_* and NONCE_BYTES are module-private in relay-socket.ts, so nothing compares them
+// directly; src/core/watch-link/client.test.ts pins them END TO END, by running the client that
+// uses them against the real host-role relay socket.
+// Both tests live in core because they import core modules the web tsconfig project cannot see.
//
// Sealed box: nonce(24) ‖ nacl.box.after(plain). Plain: [role:1][seqHi u32 LE][seqLo u32 LE][tag:1][body].
import nacl from 'tweetnacl'