Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/runtime/e2ee-keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/main/runtime/mobile-pairing-files.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
32 changes: 32 additions & 0 deletions src/shared/runtime-e2ee-keypair-file.ts
Original file line number Diff line number Diff line change
@@ -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'))
}
51 changes: 51 additions & 0 deletions src/shared/runtime-environment-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 21 additions & 14 deletions src/shared/runtime-environment-store.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
)
Expand Down
Loading