Skip to content

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

Closed

Description

@thehoff

Goal

Zero-trust layer on cargo wrapping. cargo build itself runs arbitrary code by design (build.rs, proc-macros) — we accept that surface. What we DON'T need to accept is env-injected RUSTC_WRAPPER redirecting EVERY rustc invocation to an attacker binary, including for benign cargo check runs that should never escalate.

Pattern

Same as #33 — secure_cargo_command() + check_forbidden_cargo_args() in src/core/utils.rs, wire into src/cmds/rust/cargo_cmd.rs.

Env vars to strip

  • RUSTC_WRAPPER, RUSTC_WORKSPACE_WRAPPER, RUSTC (override default rustc binary)
  • CARGO_TARGET_*_RUNNER, CARGO_TARGET_*_LINKER (per-target runner/linker)
  • RUSTFLAGS, RUSTDOCFLAGS (-C linker=evil, -C link-arg=...)
  • CARGO_HOME (alternate config.toml with credential-provider + registry redirects)
  • CARGO_BUILD_RUSTC*
  • CC, CXX, PKG_CONFIG (cgo / build-script chain to alternate compiler/linker)
  • CARGO_NET_GIT_FETCH_WITH_CLI (chains to git env surface — separate fix in git PR)

Args to reject

  • cargo --config target.*.runner=..., --config target.*.linker=..., --config build.rustc-wrapper=...
  • cargo install --git <url>, cargo install --path <path> (or whitelist crate names)

Tests

tests/security/cargo_hardening.rs:

  • RUSTC_WRAPPER=/tmp/evil.sh contextcrawler cargo check does NOT execute evil.sh
  • --config 'target.x86_64-apple-darwin.runner="evil"' rejected
  • Normal cargo --version, cargo check still work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions