diff --git a/src/main/runtime/e2ee-keypair.ts b/src/main/runtime/e2ee-keypair.ts index 9ffb9844e867..c949257a105d 100644 --- a/src/main/runtime/e2ee-keypair.ts +++ b/src/main/runtime/e2ee-keypair.ts @@ -9,11 +9,12 @@ import { isUnreadableError, writeSecureJsonFile } from '../../shared/secure-file' -import { E2EE_KEYPAIR_FILENAME } from './mobile-pairing-files' +import { + E2EE_KEYPAIR_FILENAME as KEYPAIR_FILENAME, + MAX_E2EE_KEYPAIR_FILE_BYTES as MAX_KEYPAIR_FILE_BYTES +} from '../../shared/runtime-e2ee-keypair-file' -const KEYPAIR_FILENAME = E2EE_KEYPAIR_FILENAME const KEYPAIR_VERSION = 1 -const MAX_KEYPAIR_FILE_BYTES = 8 * 1024 type KeypairFile = { v: number diff --git a/src/main/runtime/mobile-pairing-files.ts b/src/main/runtime/mobile-pairing-files.ts index 40e145417c94..96ab6770964e 100644 --- a/src/main/runtime/mobile-pairing-files.ts +++ b/src/main/runtime/mobile-pairing-files.ts @@ -1,5 +1,7 @@ +import { E2EE_KEYPAIR_FILENAME } from '../../shared/runtime-e2ee-keypair-file' + export const DEVICE_REGISTRY_FILENAME = 'orca-devices.json' -export const E2EE_KEYPAIR_FILENAME = 'orca-e2ee-keypair.json' +export { E2EE_KEYPAIR_FILENAME } // Migrate these together so device tokens and E2EE material never split across dirs. export const MOBILE_PAIRING_USERDATA_FILES = [ diff --git a/src/shared/runtime-e2ee-keypair-file.ts b/src/shared/runtime-e2ee-keypair-file.ts new file mode 100644 index 000000000000..1951916442ea --- /dev/null +++ b/src/shared/runtime-e2ee-keypair-file.ts @@ -0,0 +1,32 @@ +import { existsSync } from 'node:fs' +import { join } from 'node:path' +import { readNodeFileSyncWithinLimit } from './node-bounded-file-reader' + +export const E2EE_KEYPAIR_FILENAME = 'orca-e2ee-keypair.json' +export const MAX_E2EE_KEYPAIR_FILE_BYTES = 8 * 1024 + +/** This profile's runtime public key, or null when the profile has none or it cannot be read. */ +export function readProfileRuntimePublicKey(userDataPath: string): Buffer | null { + const filePath = join(userDataPath, E2EE_KEYPAIR_FILENAME) + if (!existsSync(filePath)) { + return null + } + try { + const raw: unknown = JSON.parse( + readNodeFileSyncWithinLimit(filePath, MAX_E2EE_KEYPAIR_FILE_BYTES).buffer.toString('utf8') + ) + if (typeof raw !== 'object' || raw === null || !('publicKeyB64' in raw)) { + return null + } + const key = + typeof raw.publicKeyB64 === 'string' ? Buffer.from(raw.publicKeyB64, 'base64') : null + return key && key.length === 32 ? key : null + } catch { + return null + } +} + +export function isProfileRuntimePublicKey(userDataPath: string, publicKeyB64: string): boolean { + const own = readProfileRuntimePublicKey(userDataPath) + return own !== null && own.equals(Buffer.from(publicKeyB64, 'base64')) +} diff --git a/src/shared/runtime-environment-store.test.ts b/src/shared/runtime-environment-store.test.ts index d7af1e7b5419..e5bbe45e33d3 100644 --- a/src/shared/runtime-environment-store.test.ts +++ b/src/shared/runtime-environment-store.test.ts @@ -41,6 +41,57 @@ describe('runtime environment store', () => { } }) + function writeProfileKeypair(userDataPath: string, fill: number): void { + const publicKeyB64 = Buffer.from(new Uint8Array(32).fill(fill)).toString('base64') + writeFileSync( + join(userDataPath, 'orca-e2ee-keypair.json'), + JSON.stringify({ v: 1, publicKeyB64, secretKeyB64: publicKeyB64 }) + ) + } + + it('refuses a pairing code that this same Orca profile issued', () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-env-store-')) + tempDirs.push(userDataPath) + writeProfileKeypair(userDataPath, 1) + + expect(() => + addEnvironmentFromPairingCode(userDataPath, { + name: 'myself', + pairingCode: pairingCode('ws://192.0.2.10:6768') + }) + ).toThrow(/this Orca itself/) + expect(listEnvironments(userDataPath)).toEqual([]) + }) + + it('refuses re-pairing a saved server with a code this same Orca profile issued', () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-env-store-')) + tempDirs.push(userDataPath) + const environment = addEnvironmentFromPairingCode(userDataPath, { + name: 'dev box', + pairingCode: pairingCode() + }) + writeProfileKeypair(userDataPath, 1) + + expect(() => + updateEnvironmentFromPairingCode(userDataPath, environment.id, { + pairingCode: pairingCode('ws://192.0.2.10:6768') + }) + ).toThrow(RuntimeEnvironmentStoreError) + expect(listEnvironments(userDataPath)).toEqual([environment]) + }) + + it('pairs a server with a different key even at a loopback address', () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-env-store-')) + tempDirs.push(userDataPath) + writeProfileKeypair(userDataPath, 2) + + const environment = addEnvironmentFromPairingCode(userDataPath, { + name: 'tunnel', + pairingCode: pairingCode('ws://127.0.0.1:6768') + }) + expect(listEnvironments(userDataPath)).toEqual([environment]) + }) + it('rejects duplicate server names instead of silently replacing the saved server', () => { const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-env-store-')) tempDirs.push(userDataPath) diff --git a/src/shared/runtime-environment-store.ts b/src/shared/runtime-environment-store.ts index c5b9591c007f..919eb6500639 100644 --- a/src/shared/runtime-environment-store.ts +++ b/src/shared/runtime-environment-store.ts @@ -1,6 +1,7 @@ import { randomUUID } from 'node:crypto' import { parsePairingCode, type PairingOffer } from './pairing' import { classifyRemotePairingHostname } from './remote-pairing-address' +import { isProfileRuntimePublicKey } from './runtime-e2ee-keypair-file' import { createEnvironmentFromPairingOffer, getPreferredPairingOffer, @@ -40,13 +41,7 @@ export function addEnvironmentFromPairingCode( connectionDependency?: 'ssh-tunnel' } ): KnownRuntimeEnvironment { - const offer = parsePairingCode(args.pairingCode) - if (!offer) { - throw new RuntimeEnvironmentStoreError( - 'invalid_argument', - 'Invalid pairing code. Expected an orca://pair?... URL or bare pairing payload.' - ) - } + const offer = parseRemotePairingCode(userDataPath, args.pairingCode) const store = readPersistedEnvironmentStore(userDataPath) const now = args.now ?? Date.now() const existing = store.environments.find((entry) => entry.name === args.name) @@ -76,6 +71,24 @@ export function addEnvironmentFromPairingCode( return environment } +function parseRemotePairingCode(userDataPath: string, pairingCode: string): PairingOffer { + const offer = parsePairingCode(pairingCode) + if (!offer) { + throw new RuntimeEnvironmentStoreError( + 'invalid_argument', + 'Invalid pairing code. Expected an orca://pair?... URL or bare pairing payload.' + ) + } + // Why the key, not the address: a loopback or LAN address proves nothing either way. + if (isProfileRuntimePublicKey(userDataPath, offer.publicKeyB64)) { + throw new RuntimeEnvironmentStoreError( + 'invalid_argument', + 'This pairing code is for this Orca itself. Generate the code on the other computer instead.' + ) + } + return offer +} + export function removeEnvironment(userDataPath: string, selector: string): KnownRuntimeEnvironment { const environment = resolveEnvironmentFromStore(readEnvironmentStore(userDataPath), selector) assertNoIndependentSshAccess(environment) @@ -93,13 +106,7 @@ export function updateEnvironmentFromPairingCode( selector: string, args: { pairingCode: string; now?: number } ): KnownRuntimeEnvironment { - const offer = parsePairingCode(args.pairingCode) - if (!offer) { - throw new RuntimeEnvironmentStoreError( - 'invalid_argument', - 'Invalid pairing code. Expected an orca://pair?... URL or bare pairing payload.' - ) - } + const offer = parseRemotePairingCode(userDataPath, args.pairingCode) assertNoIndependentSshAccess( resolveEnvironmentFromStore(readEnvironmentStore(userDataPath), selector) )