The recoverable replay key was stable per (hostContentSecret, sessionId) while the agent-side sealer resets its deterministic-nonce seq to 0 on every restart/re-attach → two sealer generations sealed distinct plaintext under the SAME (key, nonce). Add a required 'epoch' to ReplayKeyParams, fold it into the K_content HKDF salt (sessionId U+001F epoch), mint a fresh epoch per createReplaySealer generation and expose it, and thread it through ReplaySource so the browser re-derives the matching key. Fresh epoch per generation ⇒ fresh key ⇒ seq=0 can never collide; recoverability within a generation is preserved. Touches relay-contracts/relay-e2e/agent/relay-web. Green: contracts 81, e2e 78, agent 133, web 99; tsc clean. Regression proves same seq-0 nonce, different key.
75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { deriveContentKey, encodeEnvelope, openReplayCiphertext, randomBytes, sealReplayFrame } from 'relay-e2e'
|
|
|
|
// Mock the xterm modules so the DEFAULT (non-injected) terminal loaders run without a real canvas.
|
|
const writes: string[] = []
|
|
vi.mock('@xterm/xterm', () => ({
|
|
Terminal: class {
|
|
cols = 80
|
|
rows = 24
|
|
constructor(_opts?: unknown) {}
|
|
loadAddon(_a: unknown): void {}
|
|
open(_el: unknown): void {}
|
|
write(data: string): void {
|
|
writes.push(data)
|
|
}
|
|
onData(_cb: (d: string) => void): void {}
|
|
dispose(): void {}
|
|
},
|
|
}))
|
|
vi.mock('@xterm/addon-fit', () => ({
|
|
FitAddon: class {
|
|
fit(): void {}
|
|
},
|
|
}))
|
|
|
|
import { mountTerminalView } from '../src/terminal-view'
|
|
import { mountPreviewClient, type ReplaySource } from '../src/preview-client'
|
|
import type { TerminalTransport } from '../src/ws-transport'
|
|
|
|
const tick = () => new Promise((r) => setTimeout(r, 0))
|
|
|
|
describe('default (real-xterm) loaders behind the DI seam', () => {
|
|
it('mountTerminalView loads the default xterm + FitAddon and sends attach', async () => {
|
|
const root = document.createElement('div')
|
|
Object.defineProperty(root, 'clientWidth', { value: 100 })
|
|
Object.defineProperty(root, 'clientHeight', { value: 100 })
|
|
const sent: Uint8Array[] = []
|
|
const transport: TerminalTransport = {
|
|
open: async () => {},
|
|
send: (b) => sent.push(b),
|
|
onMessage: () => {},
|
|
onClose: () => {},
|
|
close: () => {},
|
|
}
|
|
const view = mountTerminalView(root, transport) // NO createTerminal → default loader path
|
|
await tick()
|
|
await tick()
|
|
const first = JSON.parse(new TextDecoder().decode(sent[0]!))
|
|
expect(first.type).toBe('attach')
|
|
view.dispose()
|
|
})
|
|
|
|
it('mountPreviewClient renders via the default read-only xterm loader', async () => {
|
|
const secret = randomBytes(32)
|
|
const epoch = 'gen-1' // FIX 3b / F6: seal + declare under the same generation.
|
|
const k = deriveContentKey({ hostContentSecret: secret, sessionId: 's', alg: 'aes-256-gcm', epoch })
|
|
const replay: ReplaySource = {
|
|
sessionId: 's',
|
|
alg: 'aes-256-gcm',
|
|
epoch,
|
|
frames: [encodeEnvelope(sealReplayFrame(k, 0n, new TextEncoder().encode('DEFAULT-XTERM')))],
|
|
}
|
|
const card = document.createElement('div')
|
|
const client = mountPreviewClient(card, replay, secret, { cols: 80, rows: 24 }, {
|
|
deriveContentKey,
|
|
openReplayCiphertext,
|
|
})
|
|
await client.render()
|
|
await tick()
|
|
await tick()
|
|
expect(writes).toContain('DEFAULT-XTERM')
|
|
client.dispose()
|
|
})
|
|
})
|