Repository navigation
fix(orcad): run the remote daemon at the login umask so worktrees it creates are not owner-only - #31135
Conversation
…creates are not owner-only (#31125)
📝 Walkthrough
Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: 🔵 Low · up to
|
…cad runs at the login umask The Gemini usage fetcher rewrote ~/.gemini/oauth_creds.json through a temp file and rename with no mode, so the replacement took the process umask. Under the old inherited umask 077 that happened to be 0600 on SSH hosts; at the login umask it would be 0644. Write it through writeCredentialFileAtomic (0600 staging file, owner-only ACL on Windows). The hostile-host matrix now checks that the deployed orcad runs at the SSH login's umask and that its readiness file stays 0600.
… launch PID file owner-only writeHooksJson now keeps an existing config's mode unless a caller opts out. Rewriting ~/.claude/settings.json (which can hold API keys) and the other agent configs through a temp file used to take the process umask; inside orcad that was 077, and at the login umask it would widen an owner-only file to 0644. The launch runs `umask 077` in the foreground shell rather than inside the backgrounded nohup list, so the PID file the foreground writes is created 0600 instead of relying on a racing chmod. The Gemini credential test now pins umask 022 rather than inheriting the runner's.
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
c31e0304-f1f0-4e1a-ad41-7e27bbd9cbad
📒 Files selected for processing: 5
src/main/rate-limits/gemini-oauth-sources.test.tssrc/main/rate-limits/gemini-oauth-sources.tssrc/main/rate-limits/gemini-usage-fetcher.fallback.test.tssrc/main/rate-limits/gemini-usage-fetcher.test.tssrc/main/ssh/hostile-host-test-support/ssh-hostile-host-managed-orcad.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
… Windows config A preserved Windows mode can be read-only, which would leave an undeletable temp file behind when the rename fails. POSIX keeps the preserved mode.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
🟠 Major · Fail closed when Windows ACL enforcement is unavailable. · gemini-oauth-sources.ts:81-85
src/main/rate-limits/gemini-oauth-sources.ts:81-85
🔒 Security & Privacy | 🟠 Major | ⚡ Quick winFail closed when Windows ACL enforcement is unavailable.
writeCredentialFileAtomicignores the result ofwriteRestrictedWindowsFilebefore renaming the temporary file. When the Windows security addon is unavailable, or when the filesystem drops the ACL, the helper returnsfalseafter creating an unrestricted temporary file.saveGeminiCredentialscan then publish that file with an inherited DACL.Pass a Windows-ACL requirement for this credential save and abort before
renameSyncwhen the restriction result is false. Keep the existing fallback behavior for other credential writers.Suggested fix
export function writeCredentialFileAtomic( path: string, data: Buffer, - shouldPublish?: () => boolean + shouldPublish?: () => boolean, + requireWindowsAcl = false ): boolean { @@ if (process.platform === 'win32') { // Why born restricted: a sibling would otherwise inherit the directory's DACL and replace // the previous credential's owner-only ACL. - writeRestrictedWindowsFile(tempPath, data) + const restricted = writeRestrictedWindowsFile(tempPath, data) + if (requireWindowsAcl && !restricted) { + throw new Error('Unable to enforce owner-only Windows ACL for credential file') + } fsyncFileSync(tempPath) return publishStagedCredential(tempPath, path, shouldPublish)export async function saveGeminiCredentials(creds: GeminiCredentials): Promise<void> { - writeCredentialFileAtomic(OAUTH_CREDS_PATH, Buffer.from(JSON.stringify(creds, null, 2), 'utf-8')) + writeCredentialFileAtomic( + OAUTH_CREDS_PATH, + Buffer.from(JSON.stringify(creds, null, 2), 'utf-8'), + undefined, + true + ) }
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
a534dbdc-a1d7-42c5-9ad6-a639659ef4ee
📒 Files selected for processing: 5
src/main/agent-hooks/installer-utils.test.tssrc/main/agent-hooks/installer-utils.tssrc/main/managed-server/install/orcad-remote-launch.tssrc/main/managed-server/posix-host-op-byte-budget.test.tssrc/main/rate-limits/gemini-oauth-sources.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.
|
I've taken this PR over at the user's request. Thanks @AmethystLiang for the fix and the analysis. Your commit and its authorship are unchanged, and my commits are on top. I didn't rebase or force-push, because the branch already merges cleanly. What I added Once orcad stops inheriting
I also moved Tests
Verified
Proposed follow-up, not in this PR
|
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
56033e5d-074a-4afa-a19a-a15d11962f20
📒 Files selected for processing: 1
src/main/agent-hooks/installer-utils.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.
| const tmpMode = process.platform === 'win32' ? undefined : existingMode | ||
| writeFileSync(tmpPath, serialized, { encoding: 'utf-8', mode: tmpMode, flag: 'wx' }) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C 5 '\bwriteHooksJson\s*\(|\bdefaultMode\s*:' src testsRepository: stablyai/orca
Length of output: 42910
🏁 Script executed:
set -euo pipefail
nl -ba src/main/agent-hooks/installer-utils.ts | sed -n '270,370p'
printf '\n--- defaultMode caller ---\n'
nl -ba src/main/hermes/hermes-home-filesystem.ts | sed -n '35,58p'
printf '\n--- relevant tests and declarations ---\n'
rg -n -C 8 -F -- 'defaultMode' src/main/agent-hooks src/main/hermes
rg -n -C 8 -F -- 'writeHooksJson' src/main/agent-hooks/installer-utils.test.tsRepository: stablyai/orca
Length of output: 18554
🏁 Script executed:
set -euo pipefail
printf '%s\n' '--- Hermes writeConfigFile callers ---'
rg -n -C 8 -F -- 'writeConfigFile(' src tests || test "$?" -eq 1
printf '%s\n' '--- Hermes platform guards ---'
rg -n -C 6 -e 'process\.platform' -e 'isWindows' src/main/hermes src/main | head -240
printf '%s\n' '--- changed lines against supplied base ---'
git diff --no-ext-diff --unified=12 a18b44bfc0bfccaede1d9208dc77a97816b55fe9 df13008f9bf2b1a3d5cad76cb4388b5b2ff3ba9c -- src/main/agent-hooks/installer-utils.tsRepository: stablyai/orca
Length of output: 28283
Preserve defaultMode for new Windows files.
When writePath does not exist, existingMode contains options.defaultMode. The Windows branch replaces it with undefined, and the Windows path skips chmodSync. The new file can therefore use the platform default instead of the requested mode.
Skip the inherited mode for an existing Windows file, but retain defaultMode for a new file.
Suggested fix
--- "a/src/main/agent-hooks/installer-utils.ts"
+++ "b/src/main/agent-hooks/installer-utils.ts"
@@ -333,7 +333,10 @@
try {
// Why not on Windows: a read-only config's mode would make the temp file undeletable on failure.
- const tmpMode = process.platform === 'win32' ? undefined : existingMode
+ const tmpMode =
+ process.platform === 'win32' && options?.preserveMode !== false && existsSync(writePath)
+ ? undefined
+ : existingMode
writeFileSync(tmpPath, serialized, { encoding: 'utf-8', mode: tmpMode, flag: 'wx' })
if (existingMode !== undefined && process.platform !== 'win32') {
// Preserve requested permissions even with a stricter process umask.📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const tmpMode = process.platform === 'win32' ? undefined : existingMode | |
| writeFileSync(tmpPath, serialized, { encoding: 'utf-8', mode: tmpMode, flag: 'wx' }) | |
| const tmpMode = | |
| process.platform === 'win32' && options?.preserveMode !== false && existsSync(writePath) | |
| ? undefined | |
| : existingMode | |
| writeFileSync(tmpPath, serialized, { encoding: 'utf-8', mode: tmpMode, flag: 'wx' }) |
…nd WSL hosts (#31235) * fix(orcad): replace a stale terminal daemon once it drains on Linux and WSL hosts A daemon forked by an older orcad (before #31135, at umask 077) kept serving new terminals after an upgrade if it was busy at startup, because the runtime stale-bundle recycle was macOS-only. orcad now opts in on every platform; the replacement still waits for exactly zero live sessions and treats an unverifiable count as keep. * test(daemon): pin the restart recycle test to linux; tighten wording
ELI5
When Orca starts its server on an SSH machine, it told the server "make every file you create private to you". That was meant for one small file holding a login token. But the server passes that rule on to everything it runs, including
git. So worktrees Orca created came out readable only by you. A Docker container running as a different user, for example, could not read them. Now only the token file and its neighbours stay private. The server, and everything it runs, creates files the way your normal shell would.What Changed
Before: the launch command on the SSH host ran
umask 077and then started orcad from that same shell. orcad kept umask077for its whole life, and so did everything it spawned:git worktree add, the terminal daemon, terminal shells and agents. Every file and directory in a worktree Orca created was0600/0700, while a hand-made clone on the same host was0644/0755. Hosts whose/etc/bashrcresets the umask hid this in terminals. On hosts that don't (Debian/Ubuntu), terminal shells were077too.After:
orcadLaunchCommand(src/main/managed-server/install/orcad-remote-launch.ts) saves the login umask first, then tightens to077exactly as before. Only the daemon process gets the login umask back:nohup) launch: the daemon starts inside{ umask "$orcad_login_umask" && … exec nohup …; } > readiness 2>> log &. The shell opens the readiness and log redirects before the group runs, so they are still created under077. Only the program the group starts gets the login umask.setsid) launch: the child script writes the PID file first, then runsumask "$2"before starting the daemon.The readiness file (which holds the pairing token), the log, the PID file, the slot directory and
~/.orca-remotekeep their owner-only modes.Byte budget: the launch command is one of the host commands already over the 6 KB exec budget (gateways cut exec requests near 10 KB, #30491), and its ceiling may only shrink. To pay for the two umask steps, the readiness, log and stop-request files are now named relative to the
cdinto the slot that already comes before every use. The PID file stays absolute, because the SSH launch backgrounds the whole chain,cdincluded, and writes the PID from outside it. The entry path stays absolute because the liveness check matches it inpsoutput. Net: the SSH launch ceiling drops from 8,122 to 7,130 bytes, and the WSL launch now fits under the budget, so it leaves the over-budget list.Secret files audit. Once orcad stops inheriting
077, any secret it writes outside its data root without an explicit mode would come out readable by others. (The data root,~/.orca, is forced to0700at startup, so files inside it are protected either way.) The audit found two:~/.gemini/oauth_creds.json. When usage tracking refreshes Gemini's OAuth tokens, it rewrote this file through a temp file and a rename, with no mode set. The rename replaces Gemini's own0600file, so the new file took the process umask:0600on SSH hosts only because of the old077, and0644at a022login umask. It now goes through the existingwriteCredentialFileAtomic, which stages the file at0600(owner-only ACL on Windows) and fsyncs it before the rename (src/main/rate-limits/gemini-oauth-sources.ts).Agent config files that Orca rewrites to install its hooks, such as
~/.claude/settings.json, which can hold API keys.writeHooksJsonwrites a temp file and renames it into place. It kept the existing file's mode only when a caller asked, and most callers didn't, so an owner-only config came back at the process umask. It now keeps the existing mode by default, at one choke point, instead of each caller opting in (src/main/agent-hooks/installer-utils.ts).Everything else orcad writes outside the data root already sets its own mode. That covers Codex
auth.json, the agent-hook endpoint file, the daemon socket directory, the SSH control socket directory, staged startup commands, and the Antigravity token.Why
0600/0700modes. None of them sets a restrictive process umask. herdr carries the caller's umask through launchd on purpose, so its terminal panes inherit it.077:0700, and the launch tightens~/.orca-remoteto0700.writeSecureFile, the daemon's private file modes).orca serve.(umask 077; …)subshell: depends on listing every file the launch creates, so a file added later and missed would come out readable. This change keeps everything the launch shell creates private by default, and only the daemon drops back.Follow-up (not in this PR): the root reason the shell needs
077at all is that orcad prints the pairing token on stdout, into a file the shell creates. If orcad wrote that payload itself with0600(behind a version check), the launch command could drop the umask entirely.Live check: the generated launch commands ran for real on a Linux machine, in Docker containers, and on macOS (see Testing). A real orcad deploy is checked by the SSH hostile-hosts lane in CI, which deploys the real orcad build to Debian 10, AlmaLinux 8, Alpine and CentOS 7 containers over SSH. I did not deploy a real orcad to a shared machine by hand.
Taken over from @AmethystLiang at the user's request. Her commit, with her authorship, is unchanged, and the follow-ups are on top.
Linked Issue
Fixes #31125
Visual Proof
N/A. This changes file permissions on the SSH host, with no UI change; the tests below show it.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
orcad-remote-launch-file-modes.test.ts, new case: runs the real generated launch command underumask 022. It checks that a file and a directory the daemon creates are0644/0755, while the readiness, PID and log files stay0600. It failed before this change (the daemon's file was0600).orcad-remote-launch-setsid.test.ts, new Linux-only case: checks the same for the WSL launch.Container runs of the generated commands: both launch modes, in Debian (
dashandbash) and Alpine (busyboxash/setsid), at login umasks022,002,027and077. In every run:0600, and the slot and~/.orca-remotewere0700;Hostile-host Docker matrix (
ssh-hostile-host-managed-orcad.ts): for every Linux cell that activates, it checks that the real deployed orcad's/proc/<pid>/statusumask matches the SSH login's umask and that its readiness file is0600. On main, this check fails, because orcad runs at0077.installer-utils.test.ts, new case: rewriting a0600hook config leaves it0600. It failed before the fix (the file came out0644).gemini-oauth-sources.test.ts, new: at umask022, a refresh over an existing0600credential file leaves it at0600. It failed before the fix (the file came out0644). The two Gemini fetcher tests now mock the credential writer, so they can never write the real~/.gemini.Real-shell matrix of the generated commands (on a Linux machine, in Docker): both launch modes on Debian 10 (
dash, plusbashas the login shell), CentOS 7 (bash4.2) and Alpine (busyboxashandsetsid, pluszshandbashas the login shell), at login umasks022,002,027and077; also macOS/bin/sh(bash 3.2) andzshfor the SSH launch.0600, and the slot and~/.orca-remotewere0700.0077in every row.Suites: the managed-server install suites, the byte-budget test and the SSH session-record test pass (42 files, 599 tests).
pnpm tc:nodeandpnpm run check:code-quality:changedpass.Known unrelated local failure:
src/main/orcad/orcad-forked-child-paths.test.tsfails on macOS because of the/var→/private/vartemp path. It imports nothing this PR changes.Review
Agent skill upstream boundary
Notes
077and thenchmod 600, before orcad writes the token. The private directories are unchanged.077until that daemon is replaced.gitand everything else orcad runs itself are fixed right away.umask 077, so the file was owner-only only if a backgroundchmod 600happened to run after the write. The two umask steps now run in the foreground shell (;instead of&&), so the file is created0600. A delayed write proves it.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)