mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* fix(cli): check the runtime source before sending A terminal Orca launches now carries which Orca launched it (ORCA_RUNTIME_SOURCE_ID = the profile's installation id, ORCA_RUNTIME_SOURCE_INCARNATION = the launching runtime id). Before a request is sent, the CLI compares that with the runtime the profile's runtime record names. Same runtime: sent as before. Different runtime: a read-only status.get asks which profile it serves; the same profile (a restart or update) is accepted, anything else is refused before the request reaches it. The vars cross into WSL through WSLENV. The SSH bridge stamps the bridging runtime itself rather than trusting the remote env. The existing orchestration compatibility stamp is untouched. * fix(orcad): drop an inherited runtime source at startup * fix(cli): an explicit switch to another profile is not fenced The stamp also carries the launching runtime's userData path. A CLI that dials a different profile than the one that launched its terminal, and not the platform default an unpinned terminal falls back to, was pointed there on purpose (orca-dev from a packaged Orca's terminal), so the fence does not apply. * refactor(cli): reuse the shared userData path comparison --------- Co-authored-by: m4air <m4air@Mac.localdomain>
88 lines
3.6 KiB
TypeScript
88 lines
3.6 KiB
TypeScript
import type { RuntimeMetadata } from '../../shared/runtime-bootstrap'
|
|
import { isSameUserDataPath } from '../../shared/serve-user-data-path'
|
|
import { readRuntimeSourceStamp, type RuntimeSourceStamp } from '../../shared/runtime-source-env'
|
|
import type { RuntimeStatus } from '../../shared/runtime-types'
|
|
import { getPlatformUserDataPath } from './metadata'
|
|
import { RuntimeClientError, type RuntimeRpcResponse } from './types'
|
|
|
|
type StatusProbe = (metadata: RuntimeMetadata) => Promise<RuntimeRpcResponse<RuntimeStatus>>
|
|
|
|
/**
|
|
* The stamp to enforce for a CLI dialing `userDataPath`. Explicitly choosing another profile (e.g.
|
|
* `orca-dev` from a packaged Orca's terminal) is the user's target, not a fallback, so it is not fenced.
|
|
*/
|
|
export function resolveFencedRuntimeSource(
|
|
stamp: RuntimeSourceStamp | null,
|
|
userDataPath: string,
|
|
platformDefaultPath: string | null
|
|
): RuntimeSourceStamp | null {
|
|
if (!stamp?.profilePath || isSameUserDataPath(userDataPath, stamp.profilePath)) {
|
|
return stamp
|
|
}
|
|
// Why: the platform default is what an unpinned terminal falls back to, which is the case to fence.
|
|
return platformDefaultPath !== null && isSameUserDataPath(userDataPath, platformDefaultPath)
|
|
? stamp
|
|
: null
|
|
}
|
|
|
|
/**
|
|
* Refuses, before the request is sent, a runtime other than the one that launched this process.
|
|
* A new incarnation of the same source (restart, update) is accepted so surviving panes keep working.
|
|
*/
|
|
export class RuntimeSourceFence {
|
|
private readonly verifiedRuntimeIds = new Set<string>()
|
|
|
|
constructor(private readonly expected: RuntimeSourceStamp | null) {
|
|
if (expected) {
|
|
this.verifiedRuntimeIds.add(expected.incarnation)
|
|
}
|
|
}
|
|
|
|
async check(metadata: RuntimeMetadata, method: string, probe: StatusProbe): Promise<void> {
|
|
// Why status.get is exempt: it is the read-only probe itself.
|
|
if (!this.expected || method === 'status.get') {
|
|
return
|
|
}
|
|
if (this.verifiedRuntimeIds.has(metadata.runtimeId)) {
|
|
return
|
|
}
|
|
// Why trust the answer: the transport rejects a response from any runtime but metadata's.
|
|
const response = await probe(metadata)
|
|
if (response.ok === false) {
|
|
throw new RuntimeClientError(
|
|
'runtime_source_unverifiable',
|
|
`Could not confirm that the running Orca is the one that launched this terminal: ${response.error.message}`
|
|
)
|
|
}
|
|
const sourceId = response.result.hostDescriptor?.installationId
|
|
if (!sourceId) {
|
|
throw new RuntimeClientError(
|
|
'runtime_source_unverifiable',
|
|
'The running Orca does not report which profile it serves, so this terminal cannot confirm it is the Orca that launched it. Nothing was sent. Open a new terminal from the Orca you want to control.'
|
|
)
|
|
}
|
|
if (sourceId !== this.expected.sourceId) {
|
|
throw new RuntimeClientError(
|
|
'runtime_source_mismatch',
|
|
'The running Orca is not the one that launched this terminal (another profile or instance took over its runtime record). Nothing was sent. Open a new terminal from the Orca you want to control.'
|
|
)
|
|
}
|
|
this.verifiedRuntimeIds.add(metadata.runtimeId)
|
|
}
|
|
}
|
|
|
|
export function createRuntimeSourceFence(
|
|
env: Readonly<Record<string, string | undefined>>,
|
|
userDataPath: string
|
|
): RuntimeSourceFence {
|
|
let platformDefaultPath: string | null = null
|
|
try {
|
|
platformDefaultPath = getPlatformUserDataPath()
|
|
} catch {
|
|
// Why: no resolvable default (Windows without APPDATA) means nothing can fall back to it.
|
|
}
|
|
return new RuntimeSourceFence(
|
|
resolveFencedRuntimeSource(readRuntimeSourceStamp(env), userDataPath, platformDefaultPath)
|
|
)
|
|
}
|