Files
archived-orca/src/shared/remote-runtime/requests/remote-runtime-request-connection.test.ts
T
Neil 0efe201fbf feat(remote-runtime): use E2EE v2 for desktop and CLI connections to remote runtimes (#39218)
* feat(remote-runtime): use E2EE v2 for desktop and CLI connections to remote runtimes

Desktop connections to remote runtimes (orca serve, orcad, managed SSH,
browser bridge) and the orca CLI now use E2EE v2 framing for hosts that
support it, and fall back to the legacy framing for hosts that do not.

- One-shot requests try v2 first. A host that refuses the v2 hello
  (hosts from before v2) or refuses capabilities on v2 token auth gets
  one transparent legacy redial; nothing but the handshake is sent before
  authentication, so the redial is safe.
- After a v2 sign-in succeeds, the host's pinned key is remembered in the
  profile (shared by the app and the CLI). Every connection to that host,
  including long-lived sockets, then uses v2 and never falls back.
- Long-lived sockets keep legacy framing until a host has proven v2, so
  they never see an older host refuse a v2 hello.
- Hosts accept clientCapabilities on direct v2 token auth, so desktop and
  CLI clients keep their capability negotiation on v2. Relay token auth
  stays exact.

* feat(remote-runtime): recover a latched host that was downgraded, without a legacy downgrade path

A host this profile had signed in to over v2 was refused for good if it
was later downgraded below capability-carrying v2 token auth. Hosts now
advertise pairing.e2ee-v2-token-capabilities.v1. When a latched host
refuses v2, a one-shot request asks the host over legacy framing (keyed
to its pinned key, so only the real host can answer) for status.get:

- no capability: the host itself says it was downgraded; the latch is
  dropped and the request goes ahead over legacy;
- capability present, or no answer: the request fails with an error
  that says to update the host or check the network path. Only the
  read-only status check ever ran over legacy.

A refusal faked on the path cannot produce the host's answer, so it
cannot force legacy framing for a latched host.
2026-11-06 17:14:56 -08:00

319 lines
12 KiB
TypeScript

import { federationRelayPrefix } from '../../../main/runtime/orchestration/federation/relay/federation-relay-prefix'
import * as memoryLimits from '../remote-runtime-memory-limits'
import { sendRemoteRuntimeRequest } from '../remote-runtime-client'
import {
pairedRuntimeConnectionFromOffer,
type PairedRuntimeConnection
} from '../pairing/paired-runtime-connection'
import type { AddressInfo } from 'node:net'
import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest'
import { WebSocketServer, type WebSocket } from 'ws'
import { encodePairingOffer, parsePairingCode } from '../pairing/pairing'
import {
decrypt,
deriveSharedKey,
encrypt,
generateKeyPair,
publicKeyFromBase64,
publicKeyToBase64
} from '../e2ee/node/e2ee-crypto'
import { RemoteRuntimeRequestConnection } from './remote-runtime-request-connection'
import { remoteRuntimeClientCapabilities } from '../remote-runtime-client-capabilities'
import { createFakeSpawnedChild } from '../../__fixtures__/fake-spawned-child'
import * as commandResolution from '../../../main/git/command-runner/git-command-resolution'
import * as gitRunner from '../../../main/git/command-runner/git-exec-file'
import {
acquireGitAdmission,
_gitAdmissionSnapshotForTests,
_resetGitAdmissionForTests
} from '../../../main/git/command-runner/git-subprocess-admission'
import { GitAdmissionScheduler } from '../../git/admission/git-admission-scheduler'
const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() }))
vi.mock('node:child_process', async (original) => ({
...(await original()),
execFile: execFileMock
}))
import { getRemoteFileUrl } from '../../../main/git/repo'
type TestServer = {
wss: WebSocketServer
pairing: PairedRuntimeConnection
requests: unknown[]
auths: unknown[]
connectionCount: () => number
}
const servers: WebSocketServer[] = []
afterEach(async () => {
await Promise.all(
servers.splice(0).map(
(server) =>
new Promise<void>((resolve) => {
for (const client of server.clients) {
client.close()
}
server.close(() => resolve())
})
)
)
})
describe('RemoteRuntimeRequestConnection', () => {
it('serializes a fitting 50-item relay page once with the exact socket envelope and maximal token', async () => {
const server = await createServer(undefined, 'x'.repeat(65536))
const serialize = vi.spyOn(memoryLimits, 'serializeRemoteRuntimeRpcRequest')
onTestFinished(() => serialize.mockRestore())
const items = Array.from({ length: 50 }, (_, i) => ({
dispatch_id: 'dispatch_remote',
direction: 'to_worker' as const,
sequence: i + 1,
message_id: `m${i}`,
kind: 'control_message',
payload: JSON.stringify({ body: 'x'.repeat(12 * 1024) })
}))
const params = { dispatchId: 'dispatch_remote', items }
let selectedThrough = 0
const response = await sendRemoteRuntimeRequest(
server.pairing,
'orchestration.federationImport',
params,
5000,
{ orchestrationRequestId: 'before' },
undefined,
[],
federationRelayPrefix('dispatch_remote', items, (last) => {
selectedThrough = last
})
)
expect(response.ok).toBe(true)
expect(server.requests).toHaveLength(1)
expect(server.requests[0]).toMatchObject({
params,
orchestrationRequestId: 'relay_import_dispatch_remote_50'
})
expect(selectedThrough).toBe(50)
expect(Buffer.byteLength(JSON.stringify(server.requests[0]))).toBeLessThanOrEqual(
memoryLimits.REMOTE_RUNTIME_MAX_INBOUND_REQUEST_JSON_BYTES
)
expect(serialize).toHaveBeenCalledTimes(1)
})
async function reuseWithCancellation(wait: 'admission' | 'preflight') {
_resetGitAdmissionForTests(new GitAdmissionScheduler({ generalCap: 1, generalHeadroom: 0 }))
const exec = vi.spyOn(gitRunner, 'gitExecFileAsync')
const metadata = [
'origin\thttps://github.com/org/repo.git (fetch)',
'remote.origin.fetch\n+refs/heads/*:refs/remotes/origin/*\0',
'a'.repeat(40),
'refs/heads/local',
'refs/heads/local\0\0origin\0refs/heads/review/topic\0refs/remotes/origin/topic',
'refs/remotes/origin/topic\0\0\0\0'
]
const routing = { wslDistro: 'Ubuntu', admissionTier: 'interactive' } as const
let holding: ReturnType<typeof acquireGitAdmission> | undefined
const { promise: heldPreflight, resolve: releasePreflight } = Promise.withResolvers<void>()
const preflight = vi
.spyOn(commandResolution, 'pendingWslDirectGitReadEnvironment')
.mockImplementation((args, options) => {
expect(options).toMatchObject(routing)
expect(options.timeout).toBeLessThanOrEqual(5_000)
return wait === 'preflight' && args.some((arg: string) => arg.startsWith('--contains='))
? heldPreflight
: null
})
execFileMock.mockImplementation((_command, _args, _options, callback) => {
const child = createFakeSpawnedChild()
queueMicrotask(() => {
const stdout = metadata.shift() ?? ''
if (!metadata.length && wait === 'admission') {
holding = acquireGitAdmission({ args: ['status'], cwd: '/copy-blocker' })
}
callback(null, stdout, '')
child.emit('close', 0, null)
})
return child
})
const server = await createServer(() => getRemoteFileUrl('/repo', 'a.ts', 10, 20, routing))
const connection = new RemoteRuntimeRequestConnection(server.pairing)
// ast-grep-ignore: test-no-wall-clock -- Measures the real encrypted RPC cancellation deadline.
const start = Date.now()
onTestFinished(async () => {
connection.close()
;(await holding)?.release()
releasePreflight()
preflight.mockRestore()
execFileMock.mockReset()
exec.mockRestore()
_resetGitAdmissionForTests()
})
const response = connection.request('git.remoteFileUrl', {}, 15_000)
await vi.waitFor(() => expect(preflight).toHaveBeenCalledTimes(7))
const optional = preflight.mock.calls.at(-1)
expect(optional?.[0]).toContain(`--contains=${'a'.repeat(40)}`)
if (wait === 'admission') {
expect(_gitAdmissionSnapshotForTests().queuedWaiters).toMatchObject([{ args: optional?.[0] }])
}
await expect(response).resolves.toMatchObject({
ok: true,
result: 'https://github.com/org/repo/blob/review%2Ftopic/a.ts#L10-L20'
})
// ast-grep-ignore: test-no-wall-clock -- The production cancellation timer must actually elapse.
expect(Date.now() - start).toBeGreaterThanOrEqual(5_000)
// ast-grep-ignore: test-no-wall-clock -- The fallback must arrive before the real RPC deadline.
expect(Date.now() - start).toBeLessThan(15_000)
expect(optional?.[1].signal?.aborted).toBe(true)
;(await holding)?.release()
releasePreflight()
await expect(exec.mock.results.at(-1)?.value).rejects.toMatchObject({ name: 'AbortError' })
expect(execFileMock).toHaveBeenCalledTimes(6)
expect(_gitAdmissionSnapshotForTests().queued).toBe(0)
expect(_gitAdmissionSnapshotForTests().budgets.general?.baseUsed).toBe(0)
const second = await connection.request('terminal.send', { terminal: 't1', text: 'ab' }, 1000)
expect(second).toMatchObject({
ok: true,
result: { method: 'terminal.send' },
_meta: { runtimeId: 'runtime-test' }
})
expect(server.connectionCount()).toBe(1)
expect(server.auths).toContainEqual(
expect.objectContaining({
clientCapabilities: remoteRuntimeClientCapabilities()
})
)
expect(server.requests).toMatchObject([
{ method: 'git.remoteFileUrl', params: {} },
{ method: 'terminal.send', params: { terminal: 't1', text: 'ab' } }
])
}
it.each(['admission', 'preflight'] as const)(
'cancels optional %s without late spawn and reuses the encrypted connection',
reuseWithCancellation
)
it('aborts one request without closing the cached connection', async () => {
const server = await createServer()
const connection = new RemoteRuntimeRequestConnection(server.pairing)
const controller = new AbortController()
const pending = connection.request('test.hang', undefined, 60_000, controller.signal)
await vi.waitFor(() =>
expect(server.requests).toContainEqual(expect.objectContaining({ method: 'test.hang' }))
)
controller.abort()
await expect(pending).rejects.toMatchObject({ name: 'AbortError' })
expect(
(connection as unknown as { pendingRequests: Map<string, unknown> }).pendingRequests.size
).toBe(0)
await expect(connection.request('status.get', undefined, 1000)).resolves.toMatchObject({
ok: true,
result: { method: 'status.get' }
})
expect(server.connectionCount()).toBe(1)
connection.close()
})
})
async function createServer(
reply?: () => Promise<unknown>,
deviceToken = 'device-token'
): Promise<TestServer> {
const serverKeyPair = generateKeyPair()
const requests: unknown[] = []
const auths: unknown[] = []
let connectionCount = 0
// host must match the 127.0.0.1 clients dial: a wildcard bind lets a foreign loopback listener claim the port and answer here.
const wss = new WebSocketServer({ host: '127.0.0.1', port: 0 })
servers.push(wss)
wss.on('connection', (ws) => {
connectionCount += 1
let sharedKey: Uint8Array | null = null
let authenticated = false
ws.on('message', async (data, isBinary) => {
if (isBinary) {
return
}
const frame = data.toString()
if (!sharedKey) {
const hello: { type: string; publicKeyB64?: string } = JSON.parse(frame)
if (!hello.publicKeyB64) {
// Legacy-only host: refuses a v2 hello the way hosts before v2 do.
ws.close(4001, 'Invalid e2ee_hello')
return
}
const clientPublicKey = publicKeyFromBase64(hello.publicKeyB64)
sharedKey = deriveSharedKey(serverKeyPair.secretKey, clientPublicKey)
ws.send(JSON.stringify({ type: 'e2ee_ready' }))
return
}
const plaintext = decrypt(frame, sharedKey)
if (plaintext === null) {
return
}
if (!authenticated) {
const auth = JSON.parse(plaintext) as { type: string; deviceToken: string }
auths.push(auth)
expect(auth).toEqual({
type: 'e2ee_auth',
deviceToken,
clientCapabilities: remoteRuntimeClientCapabilities()
})
authenticated = true
sendEncrypted(ws, sharedKey, { type: 'e2ee_authenticated' })
return
}
const request = JSON.parse(plaintext) as {
id: string
method: string
params?: unknown
}
requests.push(request)
if (request.method === 'test.hang') {
return
}
sendEncrypted(ws, sharedKey, {
id: request.id,
ok: true,
result:
reply && request.method === 'git.remoteFileUrl'
? await reply()
: { method: request.method },
_meta: { runtimeId: 'runtime-test' }
})
})
})
await new Promise<void>((resolve) => wss.once('listening', resolve))
const address = wss.address() as AddressInfo
const pairing = parsePairingCode(
encodePairingOffer({
v: 2,
endpoint: `ws://127.0.0.1:${address.port}`,
deviceToken,
publicKeyB64: publicKeyToBase64(serverKeyPair.publicKey)
})
)
if (!pairing) {
throw new Error('Failed to create test pairing')
}
return {
wss,
pairing: pairedRuntimeConnectionFromOffer(pairing),
requests,
auths,
connectionCount: () => connectionCount
}
}
function sendEncrypted(ws: WebSocket, sharedKey: Uint8Array, message: unknown): void {
ws.send(encrypt(JSON.stringify(message), sharedKey))
}