Repository navigation
fix: harden diagnostic command execution - #74
Merged
Merged
Conversation
This was referenced Sep 28, 2026
Run ps and lsof only from fixed system paths with a fixed environment (LC_ALL=C, LANG=C, system PATH) instead of the caller's, so COLUMNS, LINES, PS_FORMAT, PS_PERSONALITY, and locale variables cannot alter process evidence. Invoke ps with -ww and, on Linux, cross-check its arguments against /proc/<pid>/cmdline: unreadable or truncated argv becomes unknown, and a shared-service token in either source marks the owner as shared. Match the owner's native lock to the intended lock by lsof device and inode fields rather than the rendered name, which lsof escapes for non-ASCII bytes in the C locale. Unresolvable other lock names still fail closed. runCommand now resolves spawn_error for synchronous spawn throws and for EMFILE/ENFILE children without stdio pipes, attaching the error listener before touching stdio. Converting post-SIGTERM failures into an unlock result is left for a follow-up. Tests inject failing ps through an in-process test seam instead of PATH, and cover terminal-width variables, PATH shadowing, non-ASCII Codex homes with real advisory locks, and spawn failure paths. Closes #52 Closes #61 Closes #68 Refs #66
Linux lsof 4.9x can omit the `f` field in `-F0Din` output, so every file set was dropped and the owner lock set came back empty on Linux. Request `-F0fDin`, and also start a new file set when a `D`, `i`, or `n` field repeats, so both the macOS and the Linux layouts parse. Call `child_process.spawn` directly with `shell: false`. Spawn failures are now injected through a module-level test-only override instead of a runCommand parameter. Drop the descriptor-exhaustion test that ran `/bin/sh -c` through runCommand; the stub tests cover the synchronous throw and missing-stdio paths. Refs #68 Refs #66
abruption
force-pushed
the
fix/command-execution-hardening
branch
from
September 29, 2026 00:14
0ffbc0f to
8bc97b5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
runCommandno longer passes the parent environment. Diagnostic commands run with a fixedDIAGNOSTIC_COMMAND_ENV(LC_ALL=C,LANG=C,PATH=/usr/bin:/bin:/usr/sbin:/sbin), soCOLUMNS,PS_FORMAT,PS_PERSONALITY, and similar variables cannot changepsoutput. Everypscall uses-ww. On Linux,psarguments are reconciled with/proc/<pid>/cmdline: if the kernel argv cannot be read, or thepsvalue is a strict prefix of it,argumentsisnullwith anarguments_unverified:/arguments_truncated:error and identity is incomplete. Shared-service classification is true when either source contains a service token.psis resolved only from/bin/psor/usr/bin/ps, andlsofonly from/usr/sbin/lsofor/usr/bin/lsof(regular file with execute permission). There is noPATHfallback; a missing tool is aspawn_errorand inspection becomesunknown.spawnis wrapped intry/catch; theerrorlistener is attached before stdio is touched; a child without stdout/stderr (for exampleEMFILE) resolves asspawn_errorinstead of throwing aTypeErrorand crashing on an unhandlederrorevent.child.killis guarded. Converting failures afterSIGTERMinto an unlock result that keepspid/signalSentis left for a follow-up PR.lsof -a -p PID -F0Dinby device (hex) and inode (decimal), parsed asBigIntand compared withlstat({ bigint: true })of the intended lock. Non-matching candidates are resolved as before and fail closed withowner_lock_file_lookup_failedif unresolvable. A candidate whose path equals the intended canonical path but whose dev/ino differs is now an error.Closes #52
Closes #61
Closes #68
Refs #66
Type
Safety impact
Tightens owner identity and lock-file evidence; every new failure mode fails closed (
unknown/ refused). No change to the signaling path or post-signal verification. The JSON schema is unchanged (argumentswas alreadystring | null; errors are free-form strings).Checklist
npm run checkpassesnpm run lintpassesnpm testpasses (95 pass, 0 fail)npm run smoke:packagepassesTests
app-serverowner underCOLUMNS=40/LINES/PS_FORMATis refused as a shared service; fakeps/lsofearlier inPATHare never executed; a Codex home with a non-ASCII path yieldsownerLockFiles=[lockPath]and unlocks the fixture owner; a second lock in another non-ASCII home is still refused.EBADF); stdio-less child (EMFILE, no response); real descriptor exhaustion withulimit -n 64.parseLsofFiles,parseProcCommandLine,reconcileArguments.psviaPATHnow use a test-only override injected withnode --import test/helpers/diagnostic-override.mjs;srcnever reads the override variables.smoke:platformpasses on macOS arm64.Notes
psdoes not truncate piped output), andps -wwwith-axo/-p ... -oon procps,/proc/<pid>/cmdlineNUL padding for retitled processes, and lsofDencoding versusstat.devwere not measured locally. UnmatchedD/ifalls back to path resolution, which fails closed.?underLC_ALL=C, the prefix comparison does not flag truncation; shared-service detection still uses the union of both sources./proc/<pid>/cmdlineis unreadable (for examplehidepid), the owner identity is incomplete and unlock refuses.ps/lsofoutside the fixed paths (for example NixOS) now fail closed.processCwdon macOS still uses lsof names, so a non-ASCII cwd appears escaped; it is informational only and not used by policy.