Files
orca/src/relay/git-handler-status-ops.ts
T
Jinwoo HongandOrca ea9a718e29 fix(ssh): remove relay FS path allowlist to support symlinks outside workspace (#1661) (#1672)
When a remote SSH workspace contains a symlink whose target lies outside
the registered repo/worktree roots, file reads failed with 'Path outside
authorized workspace'. This silently broke common workflows: HPC dataset
mounts, multi-checkout repos, dotfile editing, and any cross-mount
symlink.

Drop `RelayContext.authorizedRoots`, `validatePath`, and
`validatePathResolved` along with all ~33 call sites in fs-handler.ts
and git-handler.ts. The relay's threat model becomes 'the relay runs as
the SSH user and trusts the renderer.'

Why this is acceptable: `pty.spawn` and `git.exec` already concede the
same threat. A renderer that wants to reach `/etc/passwd` can spawn a
shell or run `git -C /etc cat-file`; the FS allowlist was friction, not
a security boundary. Intra-worktree path checks in `getDiff` and
`discard` are intentionally preserved.

Back-compat preserved: `session.registerRoot` (notification + request)
remains a valid RPC, retained as no-ops on new relays. Old main + new
relay and new main + old relay both keep working through the upgrade
window. `registerRelayRoots` is also kept for the same reason. A
narrowed error-translation block in `worktree-remote.ts` handles old
relays still surfacing the legacy error string to users.

Tests: removed two negative-allowlist tests; added a positive control
('reads files outside any registered root') and a direct regression
test for #1661 ('reads files via symlinks resolving outside the
workspace'). All 469 relay/SSH/IPC tests pass.

See docs/relay-fs-allowlist-removal.md for the full rationale,
back-compat matrix, alternatives considered, and follow-up cleanup
plan.

Closes #1661

Co-authored-by: Orca <help@stably.ai>
2026-05-10 15:59:54 -07:00

96 lines
2.7 KiB
TypeScript

/**
* Status and conflict-detection operations extracted from git-handler.ts.
*
* Why: oxlint max-lines (300) requires splitting large files.
* These functions are pure data operations on git state — no class coupling.
*/
import * as path from 'path'
import { existsSync } from 'fs'
import { readFile } from 'fs/promises'
import { parseStatusOutput, parseUnmergedEntry } from './git-handler-utils'
import type { GitExec } from './git-handler-ops'
export async function resolveGitDir(worktreePath: string): Promise<string> {
const dotGitPath = path.join(worktreePath, '.git')
try {
const contents = await readFile(dotGitPath, 'utf-8')
const match = contents.match(/^gitdir:\s*(.+)\s*$/m)
if (match) {
return path.resolve(worktreePath, match[1])
}
} catch {
// .git is a directory, not a file
}
return dotGitPath
}
export async function detectConflictOperation(worktreePath: string): Promise<string> {
const gitDir = await resolveGitDir(worktreePath)
try {
if (existsSync(path.join(gitDir, 'MERGE_HEAD'))) {
return 'merge'
}
if (
existsSync(path.join(gitDir, 'rebase-merge')) ||
existsSync(path.join(gitDir, 'rebase-apply'))
) {
return 'rebase'
}
if (existsSync(path.join(gitDir, 'CHERRY_PICK_HEAD'))) {
return 'cherry-pick'
}
} catch {
// fs error — treat as no conflict operation
}
return 'unknown'
}
export async function getStatusOp(
git: GitExec,
params: Record<string, unknown>
): Promise<{
entries: Record<string, unknown>[]
conflictOperation: string
head?: string
branch?: string
}> {
const worktreePath = params.worktreePath as string
const conflictOperation = await detectConflictOperation(worktreePath)
const entries: Record<string, unknown>[] = []
let head: string | undefined
let branch: string | undefined
try {
// Why: -c core.quotePath=false keeps non-ASCII filenames as raw UTF-8 in
// git's stdout instead of C-style octal escapes; without it the parsed
// entry.path renders as gibberish in the source-control sidebar and
// downstream blob lookups miss.
const { stdout } = await git(
[
'-c',
'core.quotePath=false',
'status',
'--porcelain=v2',
'--branch',
'--untracked-files=all'
],
worktreePath
)
const parsed = parseStatusOutput(stdout)
entries.push(...parsed.entries)
head = parsed.head
branch = parsed.branch
for (const uLine of parsed.unmergedLines) {
const entry = parseUnmergedEntry(worktreePath, uLine)
if (entry) {
entries.push(entry)
}
}
} catch {
// not a git repo or git not available
}
return { entries, conflictOperation, head, branch }
}