Skip to content

harden(pre-release): UNIVERSAL_ENV_STRIP wiring + remove unsafe env-mutation + 4 missed wire-ins - #51

Merged
thehoff merged 2 commits into
developfrom
harden/pre-release-fixes
May 18, 2026
Merged

thehoff merged 2 commits into
developfrom
harden/pre-release-fixes

Conversation

@thehoff

@thehoff thehoff commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Pre-release review of harden PRs #41-#47 (codex + local sanity) surfaced 5 must-fix items before tagging v0.1.8. Consolidated here.

Fixes

  • F8 architectural: UNIVERSAL_ENV_STRIP was documentation-only — extracted apply_universal_env_strip() helper and wired into all 15 per-tool secure_*_command helpers. LD_PRELOAD/DYLD_*/BASH_FUNC_*/PROMPT_COMMAND/PERL5OPT/LUA_INIT now actually stripped from every wrapped tool's child env.
  • F9 unsoundness: removed unsafe { env::remove_var } race against the telemetry thread in cloud_fallback_hardening. Refactored to spawn the hardened Command itself (via secure_*_command) so the fallback path stays protected — codex P1 caught the regression from the first attempt; this addresses it.
  • F1/F2/F4/F5 missed wire-ins:
    • format_cmd.rs:77 "black" | "ruff" → secure_python_command
    • lint_cmd.rs:102 python linter → secure_python_command
    • utils.rs::ruby_exec bundler branch → secure_ruby_command
    • utils.rs::package_manager_exec all 4 branches → secure_node_command (transparently hardens prettier/vitest/format callers)

Deferred to follow-up

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)
  • Empirical: kubectl deny still fires on --kubeconfig, exit 2 with the documented error

Reviewed by Codex

  • P1 on first F9 attempt: dropped env strip on fallback path → refactored to spawn hardened Command instead, regression closed

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

thehoff and others added 2 commits May 18, 2026 19:45
…n, plug 4 missed wire-ins

Pre-release codex+claude review of harden PRs #41-#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 #42/#43/#44/#46/#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 #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 #41 #42 #43 #44 #46 #47

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Codex pre-release adversarial review <[email protected]>
Codex P1 on the F9 fix: the previous attempt removed the unsound
`unsafe { env::remove_var }` block but left the fallback path with no
env stripping at all. clap-rejected invocations of kubectl/docker/aws/
psql/curl/wget would then spawn via the normal raw `resolved_command`
path, inheriting the caller's full environment — re-opening
LD_PRELOAD/DYLD_INSERT_LIBRARIES/BASH_ENV/RUBYOPT/KUBECONFIG/etc.
injection vectors that the per-tool harden PRs closed in the normal
Commands::* dispatch.

Refactor: cloud_fallback_hardening now spawns the hardened Command
itself (via the appropriate secure_*_command helper) and returns the
child's exit code. The function takes responsibility for the WHOLE
fallback execution for cloud tools instead of validating args then
deferring to the unhardened raw spawn. No unsafe env mutation needed
— Command::env_remove (called by the secure_*_command helpers) is
safe and only affects the spawned child.

Net effect:
- Normal Commands::Kubectl/Docker/Aws/Psql/Curl/Wget dispatch: hardened
  by per-tool wire-ins (already correct)
- Fallback path (clap parse-failure, e.g. `kubectl --kubeconfig …`):
  now ALSO hardened via secure_*_command spawn — codex regression
  closed

Co-Authored-By: Codex review <[email protected]>
@thehoff
thehoff merged commit b23dba1 into develop May 18, 2026
3 checks passed
@thehoff
thehoff deleted the harden/pre-release-fixes branch May 18, 2026 09:50
thehoff added a commit that referenced this pull request May 18, 2026
Manual version bump for the post-#51 zero-trust hardening series.
release-please workflow is untracked (.github excluded in fa9fca6) so
the bump + tag + GH release are cut by hand for this cycle.

Headline: zero-trust CLI wrapper layer across 15 secure_*_command
helpers + universal env-strip + RCE-vector deny-lists for rg/find/
git/cargo/node/python/ruby/jvm/dotnet/go/k8s/docker/aws/psql/curl/wget.

19 merged PRs since v0.1.6. See PR #52 body / release notes for full
list.
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
…thehoff#50)

Wraps gh, glab, and gt (Graphite) with the same secure_*_command pattern
already applied to git, kubectl, docker, aws, psql, curl, wget. Each
gets its own env-strip list, optional arg deny-list, and end-to-end
integration tests.

## What gets stripped
- gh: GH_CONFIG_DIR, GH_EDITOR, GH_BROWSER, GH_PAGER, GH_PATH, BROWSER
- glab: GLAB_CONFIG_DIR, GLAB_EDITOR, GLAB_BROWSER, GLAB_PAGER, BROWSER
- gt: GT_EDITOR, GT_BROWSER, GT_PAGER, BROWSER + full git env deny set
  (gt shells out to git on every operation)
- Plus UNIVERSAL_ENV_STRIP via apply_universal_env_strip (EDITOR, VISUAL,
  PAGER, LD_PRELOAD, DYLD_*, BASH_FUNC_*, etc.)

## What gets preserved (AUTH CONTRACT)
Auth tokens MUST stay in inherited env or non-interactive use breaks.
Pinning tests assert these are NOT in the strip lists:
- gh: GH_TOKEN, GITHUB_TOKEN, GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN
  (no GH_ prefix; gh GHES fallback per cli.github.com docs), GH_HOST, GH_REPO
- glab: GITLAB_TOKEN, GLAB_TOKEN, GITLAB_HOST
- gt: GRAPHITE_TOKEN

## Arg deny-list
- gh: extension exec NAME is rejected (runs arbitrary extension binary).
  extension install IS allowed (deliberate user action, no inline exec).
  Other gh subcommands (workflow run, secret set, repo edit, codespace)
  reviewed and intentionally not denied -- they don't fit the env-hijack
  threat model and restricting them would kill normal usage.
- glab, gt: no eval-equivalent subcommands; checks are stubs for shape.

## Pre-PR review fixes (in same commit)
Pre-merge independent review caught 4 must-fix items:

- P0 critical: run_passthrough helpers in gh/glab/gt_cmd.rs called the
  shared runner::run_passthrough which uses resolved_command() not
  secure_*_command(). Every passthrough path inside the typed routes
  (gh pr merge, glab api, unknown gt subcommands) was spawning children
  with the FULL unsanitised environment -- the wrap was documentation-
  only on those paths, same class of bug as F8 from thehoff#51. Fixed by
  switching all inner passthrough helpers to run_passthrough_cmd with
  the secure_*_command.
- P1: GITHUB_ENTERPRISE_TOKEN (no GH_ prefix) is a real gh GHES fallback
  token. Behaviour already correct (not in strip list) but pinning test
  didn't cover it. Added.
- P2: check_forbidden_gh_args had a take(8) cap that let an attacker pad
  with global flags to push extension exec past the detector. Removed
  the cap; loop already short-circuits on second positional.
- P3: shared cloud_deny_message hardcoded issue thehoff#38 (cloud-tools issue).
  Added cloud_deny_message_with_issue(tool, op, issue) variant so the gh
  deny correctly cites thehoff#50.

## Pinning tests added (8 unit + 4 integration)
Unit (src/core/utils.rs secure_cloud_tests):
- gh_strip_list_contains_redirect_overrides
- gh_strip_list_preserves_auth_tokens (incl GITHUB_ENTERPRISE_TOKEN)
- rejects_gh_extension_exec
- gh_extension_exec_detected_through_many_global_flags (P2 pin)
- gh_deny_message_cites_issue_50 (P3 pin)
- allows_normal_gh_args
- glab_strip_list_contains_redirect_overrides_preserves_tokens
- gt_strip_list_chains_to_git_env_deny

Integration (tests/gh_glab_gt_hardening.rs):
- gh_config_dir_is_stripped (skip if gh not on PATH)
- glab_config_dir_is_stripped (skip if glab not on PATH)
- gh_extension_exec_is_rejected (binary-independent; proves deny path)
- gh_auth_tokens_are_preserved_in_proxy_path (positive baseline)

## Verified
- cargo build clean
- cargo test --bin contextcrawler: 2153 pass, 0 fail
- 8 integration suites green (branding_lint, cargo_hardening,
  node_hardening, runtime_hardening, cloud_hardening, harness_standalone,
  proxy_nudge, new gh_glab_gt_hardening)
- Empirical: gh extension exec evil and
  gh --foo --bar extension exec evil both deny with thehoff#50 message

## Not in this PR
- thehoff#57 -- git_hardening test fragility (gpg agent dependency). Reproduces
  on develop tip independent of this branch; environmental, not a thehoff#50
  regression.

Closes thehoff#50.
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.

1 participant