fix(hardware): resolve probe binaries via absolute path to block Windows CWD planting (BE-3434)#567
Conversation
…ows CWD planting (BE-3434)
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
There was a problem hiding this comment.
🔍 Cursor Review — Consolidated panel
Triggered by @mattmillerai.
Found 4 finding(s).
| Severity | Count |
|---|---|
| 🟡 Medium | 1 |
| 🟢 Low | 3 |
Panel: 6/8 reviewers contributed findings.
Reviewers that did not contribute: kimi-k2.5:adversarial (empty), kimi-k2.5:edge-case (empty)
…3434) Address cursor-review findings on the probe binary CWD guard: - Medium (5/8): the old subtree check rejected any binary whose CWD is an ancestor, so running `comfy env` from `C:\Windows` (or a drive root) silently disabled GPU/CPU detection for `C:\Windows\System32\nvidia-smi.exe`. Reject only a binary sitting *directly* in the CWD (the actual planting signature); a legitimate system binary in a subdirectory is untouched. - Low (1/8): apply the guard on every platform, not just Windows — a `.`/empty entry in POSIX `$PATH` lets `shutil.which` return a CWD match too. - Low (1/8): normalize both paths with `os.path.normcase` so Windows' case-insensitivity can't fail the guard open. - Low (4/8): guard `_run` against an empty `cmd` so it degrades to None instead of raising IndexError, honoring the never-raise contract. Renames `_is_within_cwd` -> `_is_planted_in_cwd`; updates and extends tests (subdirectory-of-CWD allowed, POSIX plant rejected, empty-cmd). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ELI-5
comfy envpeeks at your machine's hardware by running little programs likenvidia-smiandsysctl. Before this change it asked for them by bare name — and on Windows, "runnvidia-smi" means "look in the folder I'm standing in first." So if youcdinto a folder a bad actor prepared and it contained a fakenvidia-smi.exe,comfy envwould run their program. This change looks up the real program on your system PATH and runs it by its full absolute path instead, so the folder you happen to be in can't hijack the probe.What & why
Follow-up to #562 (deferred review thread r3607162549).
hardware.py's_runinvoked probe binaries (nvidia-smi,rocm-smi,sysctl) by bare name. On WindowsCreateProcesssearches the current working directory, so runningcomfy envfrom an attacker-controlled directory could execute a plantednvidia-smi.exe(low severity, binary-planting).Fix (
comfy_cli/hardware.py):_resolve_binary(name)resolvescmd[0]viashutil.which()(a trusted PATH lookup) and returns the absolute path;_runnow invokes[resolved, *cmd[1:]]. IfwhichreturnsNone(binary absent) the probe is skipped, exactly matching the module's existing degrade-to-Nonecontract.shutil.whichon Windows can itself resolve against the CWD, so a resolved path that provably lives insideos.getcwd()is additionally rejected (_is_within_cwd, usingos.path.commonpathonrealpath-resolved paths — component-wise, symlink-safe). A legitimate system binary (e.g.nvidia-smi.exeunderSystem32) is unaffected.timeout=5bound and the never-raise contract are preserved (_resolve_binary/_is_within_cwdare fully wrapped and degrade toNone/False).POSIX is behavior-neutral:
subprocesswithoutshell=Truealready usesexecvp(PATH search, never CWD), andshutil.whichresolves the same binary — so only Windows semantics are tightened. This is why the CWD guard is Windows-gated.Tests
tests/comfy_cli/test_hardware.py— added two classes (7 tests):_runinvokes the resolved absolute path / skips when absent / never raises on resolve failure; the Windows CWD guard rejects a CWD-planted binary, allows a System32 binary, and is not applied off-Windows. Full module suite: 25 passed.ruff check+ruff format --checkclean.Notes / judgment calls
matt/be-3399-hardware-block) becausehardware.pyonly exists on that unmerged branch — base will auto-retarget tomainwhen feat: add cross-platform hardware block to comfy env --json (BE-3399) #562 merges.cuda_detect.pyhas a similar bare-namenvidia-smiinvocation, but it predates feat: add cross-platform hardware block to comfy env --json (BE-3399) #562 and this ticket scopes specifically tohardware.py:_run. Flagging for a possible separate follow-up.shutil.whichon the default PATH (rather than restricting to hardcoded system dirs) — the CWD-exclusion guard is the ticket's primary suggested mitigation and is more robust than an allowlist that could miss the NVIDIA driver's install location.