Skip to content

fix(hardware): resolve probe binaries via absolute path to block Windows CWD planting (BE-3434)#567

Open
mattmillerai wants to merge 2 commits into
matt/be-3399-hardware-blockfrom
matt/be-3434-probe-absolute-path
Open

fix(hardware): resolve probe binaries via absolute path to block Windows CWD planting (BE-3434)#567
mattmillerai wants to merge 2 commits into
matt/be-3399-hardware-blockfrom
matt/be-3434-probe-absolute-path

Conversation

@mattmillerai

Copy link
Copy Markdown
Collaborator

ELI-5

comfy env peeks at your machine's hardware by running little programs like nvidia-smi and sysctl. Before this change it asked for them by bare name — and on Windows, "run nvidia-smi" means "look in the folder I'm standing in first." So if you cd into a folder a bad actor prepared and it contained a fake nvidia-smi.exe, comfy env would 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 _run invoked probe binaries (nvidia-smi, rocm-smi, sysctl) by bare name. On Windows CreateProcess searches the current working directory, so running comfy env from an attacker-controlled directory could execute a planted nvidia-smi.exe (low severity, binary-planting).

Fix (comfy_cli/hardware.py):

  • New _resolve_binary(name) resolves cmd[0] via shutil.which() (a trusted PATH lookup) and returns the absolute path; _run now invokes [resolved, *cmd[1:]]. If which returns None (binary absent) the probe is skipped, exactly matching the module's existing degrade-to-None contract.
  • Windows defense-in-depth: shutil.which on Windows can itself resolve against the CWD, so a resolved path that provably lives inside os.getcwd() is additionally rejected (_is_within_cwd, using os.path.commonpath on realpath-resolved paths — component-wise, symlink-safe). A legitimate system binary (e.g. nvidia-smi.exe under System32) is unaffected.
  • The timeout=5 bound and the never-raise contract are preserved (_resolve_binary/_is_within_cwd are fully wrapped and degrade to None/False).

POSIX is behavior-neutral: subprocess without shell=True already uses execvp (PATH search, never CWD), and shutil.which resolves 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): _run invokes 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 --check clean.

Notes / judgment calls

@mattmillerai mattmillerai added agent-coded PR authored by the agent-work loop cursor-review Request Cursor bot review labels Jul 18, 2026
@mattmillerai
mattmillerai marked this pull request as ready for review July 18, 2026 01:55
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Jul 18, 2026
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: b476d64f-5418-4f49-b4ea-ffffd6599cc3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch matt/be-3434-probe-absolute-path
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch matt/be-3434-probe-absolute-path

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 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)

Comment thread comfy_cli/hardware.py Outdated
Comment thread comfy_cli/hardware.py Outdated
Comment thread comfy_cli/hardware.py
Comment thread comfy_cli/hardware.py Outdated
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-coded PR authored by the agent-work loop bug Something isn't working cursor-review Request Cursor bot review size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant