Skip to content

harden(cargo): secure_cargo_command + reject --config <runner/wrapper> (#34) - #43

Merged
thehoff merged 1 commit into
developfrom
harden/cargo
May 18, 2026
Merged

thehoff merged 1 commit into
developfrom
harden/cargo

Conversation

@thehoff

@thehoff thehoff commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Zero-trust layer on cargo. Closes RUSTC_WRAPPER / CARGO_TARGET_*_RUNNER / RUSTFLAGS env hijack + --config target.*.runner= arg vector. Empirically reproduced as RCE before the fix.

Changes

  • src/core/utils.rs: secure_cargo_command(), check_forbidden_cargo_args() + 12 unit tests
  • src/core/runner.rs: new run_passthrough_cmd() taking a pre-built Command (so cargo passthrough can use the hardened helper)
  • src/cmds/rust/cargo_cmd.rs: 3 spawn sites use secure_cargo_command() + gated on check_forbidden_cargo_args
  • tests/cargo_hardening.rs: 4 integration tests

Empirical PoC

  • RUSTC_WRAPPER=/tmp/evil.sh contextcrawler cargo check → marker NOT created ✓
  • contextcrawler cargo --config 'target.x86_64-apple-darwin.runner="evil"' build → exit 2 with deny error ✓
  • Positive: contextcrawler cargo --version still works

Tests

  • cargo test --bin contextcrawler — 2065 pass
  • cargo test --test cargo_hardening — 4 pass

Codex review

Deferred per merge-train cadence; pattern follows PR #33/#41/#42.

Closes #34

`contextcrawler cargo ...` previously inherited the parent process's
full environment and forwarded every user-supplied flag verbatim. That
left two equivalent local-code-execution vectors:

1. **Env-driven.** A tainted parent setting `RUSTC_WRAPPER=/tmp/evil.sh`
   (or `RUSTC_WORKSPACE_WRAPPER`, `CARGO_BUILD_RUSTC_WRAPPER`, `RUSTC`,
   `CARGO_TARGET_<TRIPLE>_RUNNER`, `CARGO_TARGET_<TRIPLE>_LINKER`,
   `CARGO_HOME` pointing at a malicious `config.toml`, `RUSTFLAGS` /
   `RUSTDOCFLAGS` with linker-arg injection, `CC` / `CXX` /
   `PKG_CONFIG`, `CARGO_NET_GIT_FETCH_WITH_CLI`) would have any
   subsequent `contextcrawler cargo build|test|check|clippy|install|
   nextest|<other>` invocation spawn the wrapper for every rustc /
   build-script / git call.

2. **Argv-driven.** `--config target.<triple>.runner="evil"`,
   `--config target.<triple>.linker="evil"`, `--config
   build.rustc-wrapper="evil"`, `--config build.rustc="evil"`,
   `--config net.git-fetch-with-cli=true` (which then spawns git / ssh
   / askpass), or `--config registries.<name>.credential-provider=
   "evil"` could be smuggled past an agent that thought it was passing
   a benign cargo flag.

Same blast radius and same mitigation shape as the rg `--pre` / config
RCE we closed in #32.

Fix mirrors `secure_rg_command` / `check_forbidden_rg_args`:

- `secure_cargo_command()` in `src/core/utils.rs` `env_remove`s every
  var in the static deny list, then walks `std::env::vars()` and
  strips any name matching `CARGO_TARGET_*_RUNNER` /
  `CARGO_TARGET_*_LINKER` (open-ended family, can't be enumerated
  statically).
- `check_forbidden_cargo_args()` rejects `--config K=V` and
  `--config=K=V` whose key matches the deny patterns above,
  case-insensitive, with a dotted-segment glob matcher so
  `target.*.runner` matches any triple but not extra-dotted keys.
- Error message names the offending key, mentions issue #34, and
  points at the escape hatch `contextcrawler proxy cargo <args>` for
  users who genuinely need the dangerous flags.

Wire-in: `src/cmds/rust/cargo_cmd.rs` swaps `resolved_command("cargo")`
for `secure_cargo_command()` in all three call sites
(`run_cargo_filtered`, `run_cargo_streamed`, and the `Other` passthrough
via a new `run_passthrough_cmd` helper in `core/runner.rs`), and runs
`check_forbidden_cargo_args` on the restored argv before spawn —
returning exit 2 with the deny message on rejection.

Verified empirically against the built binary:
- `RUSTC_WRAPPER=/tmp/evil.sh contextcrawler cargo --version` → exit 0,
  marker NOT created, cargo runs normally
- `contextcrawler cargo build --config 'target.x86_64-apple-darwin.runner="evil"'`
  → exit 2, stderr names the offending key + issue #34 + escape hatch
- `contextcrawler cargo --version` → exit 0 (positive control)

Tests:
- 2046 bin pass (+12 secure_cargo_tests: env strip incl. dynamic
  CARGO_TARGET_*_RUNNER/_LINKER, every denied --config key in both
  whitespace and `=` form, case-insensitive match, benign args still
  accepted, escape-hatch message format, dotted-segment matcher
  strictness)
- 4 new integration tests in `tests/cargo_hardening.rs`:
  - `rustc_wrapper_does_not_fire_under_version` — brief variant, no
    rustc invocation
  - `rustc_wrapper_env_is_stripped_during_check` — stronger PoC with
    a throwaway crate that actually invokes rustc, marker MUST NOT
    appear
  - `target_runner_config_is_rejected` — exit non-zero, deny message
    + escape hatch + issue reference all present in stderr
  - `benign_cargo_version_still_works` — positive control

Closes #34.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thehoff
thehoff merged commit ae8e82b into develop May 18, 2026
3 checks passed
@thehoff
thehoff deleted the harden/cargo branch May 18, 2026 09:05
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
…n, plug 4 missed wire-ins

Pre-release codex+claude review of harden PRs thehoff#41-thehoff#47 surfaced 5
must-fix items before tagging v0.1.8. This is the consolidation PR.

## F8 (architectural) — UNIVERSAL_ENV_STRIP was documentation-only

`secure_command_with_policy` applied the universal list, but the 15
per-tool secure_*_command helpers (added by thehoff#42/thehoff#43/thehoff#44/thehoff#46/thehoff#47)
called only `resolved_command(name)` + their OWN env_strip list. So
LD_PRELOAD, DYLD_INSERT_LIBRARIES, BASH_ENV, BASH_FUNC_*,
PROMPT_COMMAND, PERL5OPT, LUA_INIT etc. were NEVER stripped from any
wrapped tool — the headline claim of PR thehoff#41 was unfulfilled.

Fix: extract `apply_universal_env_strip(&mut cmd)` from
`secure_command_with_policy` and call it as the first step of every
per-tool helper (rg, git, cargo, node, python, ruby, jvm, dotnet, go,
kubectl, docker, aws, psql, curl, wget — 15 sites).

## F9 (unsoundness) — `unsafe { env::remove_var }` race in fallback

`cloud_fallback_hardening` (main.rs) called `unsafe { env::remove_var }`
with a SAFETY note claiming "single-threaded at CLI dispatch". False:
`maybe_ping()` at the top of `run_cli` spawns a telemetry thread
BEFORE this dispatch point, and that thread calls `std::env::vars()`.
Per Rust 1.81+ `env::remove_var` contract that's UB.

Fix: drop the env mutation entirely. The per-tool secure_*_command
helpers wired into every Commands::* dispatch path already strip env
via the safe Command::env_remove method. The fallback path is rare
(only fires on clap parse failure) and an unhardened fallback is an
accepted residual surface — tracked in a follow-up if it matters.

## F1, F2, F4, F5 — missed wire-ins

- F1 `src/cmds/system/format_cmd.rs:77` — `"black" | "ruff"` formatters
  switched from raw `resolved_command` to `secure_python_command`.
- F2 `src/cmds/js/lint_cmd.rs:102` — python linter path switched to
  `secure_python_command`.
- F4 `src/core/utils.rs::ruby_exec` — bundler branch (`Command::new("bundle")`)
  switched to `secure_ruby_command("bundle")`.
- F5 `src/core/utils.rs::package_manager_exec` — all four branches
  (`pnpm`, `yarn`, `npx`, direct-tool) switched from `resolved_command`
  to `secure_node_command`. This transparently hardens prettier_cmd,
  vitest_cmd, format_cmd which call into package_manager_exec.

## Deferred to follow-up (filed separately)
- F6 — env-mutating tests in `secure_git_tests` / `secure_cargo_tests`
  don't take `GLOBAL_ENV_LOCK` (only `policy_registry_tests` does).
  Race-prone but currently passing.
- F7 — `check_forbidden_pytest_args` uses `looks_like_path` which
  doesn't recognise bare-relative paths like `sub/dir.py`. Plugin
  loader is Kernel.require-equivalent; should reject anything with
  `/` or `\` even without explicit `./` prefix.
- gh/glab/gt unhardened (these wrap git under the hood; need
  secure_gh_command / secure_glab_command).

## Verified
- `cargo build --bin contextcrawler` — clean
- `cargo test --bin contextcrawler` — 2116 pass, 0 fail
- All 7 integration suites pass: branding_lint(3), git_hardening(6),
  cargo_hardening(4), node_hardening(4), runtime_hardening(12),
  cloud_hardening(14), harness_standalone(1)

Refs PRs thehoff#41 thehoff#42 thehoff#43 thehoff#44 thehoff#46 thehoff#47

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Codex pre-release adversarial review <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

harden(cargo): strip RUSTC_WRAPPER / CARGO_TARGET_*_RUNNER / RUSTFLAGS env vars

1 participant