Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(runtime): delegated devices follow the device file, and quarantine keeps external adds (#36222)

Neil067a0dd1b4b9
diff --git a/src/main/runtime/delegated-mobile-devices.ts b/src/main/runtime/delegated-mobile-devices.ts
index 6335d07fda7..21b26a83fa1 100644
--- a/src/main/runtime/delegated-mobile-devices.ts
+++ b/src/main/runtime/delegated-mobile-devices.ts
@@ -14,6 +14,10 @@ export function planDelegatedMobileDevices(
phones: readonly DelegatedPhone[],
now = Date.now()
): { nextDevices: DeviceEntry[]; entries: DelegatedMobileDeviceEntry[] } {
+ // A parent removed meanwhile (e.g. on disk) must not mint children nobody can revoke with it.
+ if (!devices.some((device) => device.deviceId === parent.deviceId)) {
+ return { nextDevices: devices, entries: [] }
+ }
let nextDevices = devices
const entries = phones.map((phone) => {
const existing = nextDevices.find(
diff --git a/src/main/runtime/device-entry.ts b/src/main/runtime/device-entry.ts
index 1dd3f9e3940..502aa813d91 100644
--- a/src/main/runtime/device-entry.ts
+++ b/src/main/runtime/device-entry.ts
@@ -86,6 +86,35 @@ export function isStillAuthenticated(
)
}
+/** A delegated child authenticates only while its parent desktop is still paired. */
+function withPairedParent(
+ device: DeviceEntry,
+ devices: readonly DeviceEntry[]
+): DeviceEntry | null {
+ const { parentDeviceId } = device
+ return parentDeviceId === undefined || devices.some((d) => d.deviceId === parentDeviceId)
+ ? device
+ : null
+}
+
+export function findTokenDevice(
+ devices: readonly DeviceEntry[],
+ token: string
+): DeviceEntry | null {
+ const device = token && devices.find((d) => d.credentialKind === undefined && d.token === token)
+ return device ? withPairedParent(device, devices) : null
+}
+
+export function findPrincipalDevice(
+ devices: readonly DeviceEntry[],
+ principal: AuthenticatedPrincipalBinding
+): DeviceEntry | null {
+ const device = devices.find((d) => d.deviceId === principal.deviceId)
+ return device && isStillAuthenticated(device, principal)
+ ? withPairedParent(device, devices)
+ : null
+}
+
export function mobilePairingConnectionModeOf(
device: DeviceEntry | null
): MobilePairingConnectionMode | null {
diff --git a/src/main/runtime/device-registry-delegated-parent.test.ts b/src/main/runtime/device-registry-delegated-parent.test.ts
new file mode 100644
index 00000000000..687460cda2f
--- /dev/null
+++ b/src/main/runtime/device-registry-delegated-parent.test.ts
@@ -0,0 +1,78 @@
+// Removing a desktop on disk (or by any path short of revokeRuntimeAccess) must also stop the
+// phones it delegated: their rows can outlive it, so auth checks the parent.
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { DeviceRegistry } from './device-registry'
+import { DEVICE_REGISTRY_FILENAME } from './mobile-pairing-files'
+
+let dir: string
+
+beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'orca-device-registry-parent-'))
+})
+
+afterEach(() => {
+ rmSync(dir, { recursive: true, force: true })
+})
+
+function removeOnDisk(deviceId: string): void {
+ const path = join(dir, DEVICE_REGISTRY_FILENAME)
+ const rows: { deviceId: string }[] = JSON.parse(readFileSync(path, 'utf8'))
+ writeFileSync(path, JSON.stringify(rows.filter((row) => row.deviceId !== deviceId)))
+}
+
+function delegatedRegistry() {
+ const registry = new DeviceRegistry(dir)
+ const desktop = registry.addDevice('desktop', 'runtime')
+ registry.updateLastSeen(desktop.deviceId)
+ const [child] = registry.upsertDelegatedMobileDevices(desktop, [
+ { phoneKey: 'phone-1', name: 'Pixel' }
+ ])
+ if (!child) {
+ throw new Error('expected a delegated child')
+ }
+ const principal = {
+ deviceId: child.deviceId,
+ credentialRevision: child.credentialRevision ?? 0,
+ deviceToken: child.token
+ }
+ return { registry, desktop, child, principal }
+}
+
+describe('delegated phone auth follows its parent desktop', () => {
+ it('accepts a delegated phone while its desktop is paired', () => {
+ const { registry, child, principal } = delegatedRegistry()
+
+ expect(registry.validateToken(child.token)?.deviceId).toBe(child.deviceId)
+ expect(registry.resolveAuthenticatedPrincipal(principal)?.deviceId).toBe(child.deviceId)
+ })
+
+ it('refuses it once an admin removes the desktop on disk', () => {
+ const { registry, desktop, child, principal } = delegatedRegistry()
+
+ removeOnDisk(desktop.deviceId)
+
+ expect(registry.validateToken(child.token)).toBeNull()
+ expect(registry.resolveAuthenticatedPrincipal(principal)).toBeNull()
+ })
+
+ it('refuses it when the desktop row is removed without the cascade', () => {
+ const { registry, desktop, child, principal } = delegatedRegistry()
+
+ expect(registry.removeDevice(desktop.deviceId)).toBe(true)
+
+ expect(registry.validateToken(child.token)).toBeNull()
+ expect(registry.resolveAuthenticatedPrincipal(principal)).toBeNull()
+ })
+
+ it('leaves phones paired directly to this host alone', () => {
+ const { registry, desktop } = delegatedRegistry()
+ const phone = registry.addDevice('phone', 'mobile')
+
+ removeOnDisk(desktop.deviceId)
+
+ expect(registry.validateToken(phone.token)?.deviceId).toBe(phone.deviceId)
+ })
+})
diff --git a/src/main/runtime/device-registry-disk-merge.ts b/src/main/runtime/device-registry-disk-merge.ts
index 970db535cfd..35f86fff20f 100644
--- a/src/main/runtime/device-registry-disk-merge.ts
+++ b/src/main/runtime/device-registry-disk-merge.ts
@@ -31,8 +31,9 @@ export function mergeDeviceRegistrySave(
* Writes saves merged over the file as it is now (atomically, through the secure-file stage and
* rename). Remembers every id this process removed, recorded before the write so a failed save
* (such as the load-time cleanup of session-only rows) still counts: a stale writer putting one of
- * those back must not get it re-adopted. A file that became unreadable or corrupt, or that this
- * process quarantined itself, proves nothing about removals, so then the save is written as is.
+ * those back must not get it re-adopted. A file that became unreadable or corrupt proves nothing
+ * about removals, so then the save is written as is; after this process's own quarantine, the save
+ * keeps only rows another writer added since.
*/
export class DeviceRegistryWriter {
private readonly removedIds = new Set<string>()
@@ -82,11 +83,14 @@ export class DeviceRegistryWriter {
this.removedIds.add(deviceId)
}
}
- const disk = this.quarantinedUntilWritten ? null : readDeviceRegistryFile(this.registryPath)
+ const disk = readDeviceRegistryFile(this.registryPath)
+ // After our own quarantine the file's absence is no removal evidence, but rows another writer
+ // (the pairing CLI) added since are still kept: merge with no known baseline.
+ const baseline = this.quarantinedUntilWritten ? [] : before
const merged =
- !disk || disk.unreadable || disk.corruptFilePending
+ disk.unreadable || disk.corruptFilePending
? [...next]
- : mergeDeviceRegistrySave(before, next, disk, this.removedIds)
+ : mergeDeviceRegistrySave(baseline, next, disk, this.removedIds)
const write = options.durable ? writeDurableSecureJsonFile : writeSecureJsonFile
// Stat after our rename can't tell our write from one right after it; re-read once instead.
this.readStamp = null
diff --git a/src/main/runtime/device-registry-external-edit.test.ts b/src/main/runtime/device-registry-external-edit.test.ts
index c88a8bd40fa..e65b70a715a 100644
--- a/src/main/runtime/device-registry-external-edit.test.ts
+++ b/src/main/runtime/device-registry-external-edit.test.ts
@@ -161,7 +161,10 @@ describe('device registry saves over an externally edited file', () => {
})
// Review of #35483: a parent must never be handed the token of a child the file had dropped.
- it('returns no delegated child the save dropped because it was removed on disk', () => {
+ it.each([
+ ['unchanged', 'Pixel'],
+ ['renamed', 'Pixel 9']
+ ])('mints a fresh delegated child for one removed on disk (%s)', (_case, name) => {
const { registry, desktop } = pairedRegistry()
const [child] = registry.upsertDelegatedMobileDevices(desktop, [
{ phoneKey: 'phone-1', name: 'Pixel' }
@@ -171,13 +174,25 @@ describe('device registry saves over an externally edited file', () => {
}
removeOnDisk(child.deviceId)
- // A rename makes the plan rewrite the child row, which the merge then drops.
+ const returned = registry.upsertDelegatedMobileDevices(desktop, [{ phoneKey: 'phone-1', name }])
+
+ expect(returned).toHaveLength(1)
+ expect(returned[0]?.deviceId).not.toBe(child.deviceId)
+ expect(fileIds()).not.toContain(child.deviceId)
+ expect(registry.validateToken(child.token)).toBeNull()
+ expect(registry.validateToken(returned[0]?.token ?? '')?.name).toBe(name)
+ })
+
+ it('mints no delegated child for a parent removed on disk', () => {
+ const { registry, desktop } = pairedRegistry()
+
+ removeOnDisk(desktop.deviceId)
const returned = registry.upsertDelegatedMobileDevices(desktop, [
- { phoneKey: 'phone-1', name: 'Pixel 9' }
+ { phoneKey: 'phone-1', name: 'Pixel' }
])
expect(returned).toEqual([])
- expect(fileIds()).not.toContain(child.deviceId)
- expect(registry.validateToken(child.token)).toBeNull()
+ expect(fileIds()).not.toContain(desktop.deviceId)
+ expect(registry.listDelegatedMobileDevices(desktop.deviceId)).toEqual([])
})
})
diff --git a/src/main/runtime/device-registry-own-quarantine.test.ts b/src/main/runtime/device-registry-own-quarantine.test.ts
index d55e55ef24d..e77f9cc5f69 100644
--- a/src/main/runtime/device-registry-own-quarantine.test.ts
+++ b/src/main/runtime/device-registry-own-quarantine.test.ts
@@ -71,6 +71,22 @@ describe('device registry after quarantining its own corrupt file', () => {
expect(registry.validateToken(phone.token)?.deviceId).toBe(phone.deviceId)
})
+ it('keeps a device another writer added between its quarantine and its write', () => {
+ const { registry, phone } = partiallyCorruptRegistry()
+ vi.spyOn(secureFile, 'writeSecureJsonFile').mockImplementationOnce(() => {
+ throw new Error('disk full')
+ })
+ expect(() => registry.addDevice('new desktop', 'runtime')).toThrow('disk full')
+ // E.g. the pairing CLI on the host, writing through its own registry instance.
+ const cliPaired = new DeviceRegistry(dir).addDevice('cli pairing', 'runtime')
+
+ const added = registry.addDevice('new desktop', 'runtime')
+
+ expect(fileIds()).toEqual([phone.deviceId, added.deviceId, cliPaired.deviceId])
+ expect(registry.validateToken(cliPaired.token)?.deviceId).toBe(cliPaired.deviceId)
+ expect(registry.validateToken(phone.token)?.deviceId).toBe(phone.deviceId)
+ })
+
it('still revokes everything when the file is deleted after its own write landed', () => {
const { registry, phone } = partiallyCorruptRegistry()
registry.addDevice('new desktop', 'runtime')
diff --git a/src/main/runtime/device-registry.ts b/src/main/runtime/device-registry.ts
index 4bdb37f38e0..60b9de61c55 100644
--- a/src/main/runtime/device-registry.ts
+++ b/src/main/runtime/device-registry.ts
@@ -20,7 +20,8 @@ import { validGrants } from './device-registry-row-validation'
import {
isPendingTokenRow,
mobilePairingConnectionModeOf,
- isStillAuthenticated,
+ findPrincipalDevice,
+ findTokenDevice,
type AuthenticatedPrincipalBinding,
lookupDeviceKeyRow,
sameGrants,
@@ -135,6 +136,8 @@ export class DeviceRegistry {
parent: DeviceEntry,
phones: readonly DelegatedPhone[]
): DelegatedMobileDeviceEntry[] {
+ // Plan over the file as it is: an unchanged child removed on disk would skip the save's merge.
+ this.adoptExternalEdits()
const { nextDevices, entries } = planDelegatedMobileDevices(this.devices, parent, phones)
if (nextDevices !== this.devices) {
this.devices = this.save(nextDevices)
@@ -200,10 +203,7 @@ export class DeviceRegistry {
validateToken(token: string): DeviceEntry | null {
this.adoptExternalEdits()
- return (
- (token && this.devices.find((d) => d.credentialKind === undefined && d.token === token)) ||
- null
- )
+ return findTokenDevice(this.devices, token)
}
// Clients erase keys on unknown; reserve that verdict for readable absence.
@@ -246,8 +246,7 @@ export class DeviceRegistry {
resolveAuthenticatedPrincipal(principal: AuthenticatedPrincipalBinding): DeviceEntry | null {
this.adoptExternalEdits()
- const device = this.getDevice(principal.deviceId)
- return device && isStillAuthenticated(device, principal) ? device : null
+ return findPrincipalDevice(this.devices, principal)
}
updateLastSeen(deviceId: string): void {