Skip to content

harden(gh/glab/gt): zero-trust env strip + arg deny + passthrough fix (#50) - #58

Merged
thehoff merged 1 commit into
developfrom
harden/gh-glab-gt-50
May 18, 2026
Merged

thehoff merged 1 commit into
developfrom
harden/gh-glab-gt-50

Conversation

@thehoff

@thehoff thehoff commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Wraps gh, glab, gt with secure_*_command pattern: per-tool env strip + optional arg deny + UNIVERSAL_ENV_STRIP. Auth tokens preserved.

What gets stripped vs preserved

Tool Stripped Preserved (auth/context)
gh GH_CONFIG_DIR, GH_EDITOR, GH_BROWSER, GH_PAGER, GH_PATH, BROWSER GH_TOKEN, GITHUB_TOKEN, GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN, GH_HOST, GH_REPO
glab GLAB_CONFIG_DIR, GLAB_EDITOR, GLAB_BROWSER, GLAB_PAGER, BROWSER GITLAB_TOKEN, GLAB_TOKEN, GITLAB_HOST
gt GT_EDITOR, GT_BROWSER, GT_PAGER, BROWSER + full git env deny GRAPHITE_TOKEN

All three also inherit UNIVERSAL_ENV_STRIP (LD_PRELOAD, DYLD_, BASH_FUNC_, EDITOR, VISUAL, PAGER, etc.).

Arg deny

  • gh extension exec <name> rejected (arbitrary extension binary). extension install allowed (deliberate user action).
  • glab/gt: no eval-equivalent subcommands; stubs for shape.

Pre-PR review fixes (4 caught + addressed in commit cc7b3bd)

P0 critical — run_passthrough helpers inside gh_cmd.rs / glab_cmd.rs / gt_cmd.rs were calling shared runner::run_passthrough which uses resolved_command(), not secure_*_command(). Every passthrough path (gh pr merge, glab api, unknown gt subcommands) was spawning with the FULL unsanitised environment — same class of bug as F8 from #51 (doc-only wrap). Fixed by switching all inner passthrough helpers to run_passthrough_cmd(secure_X_command()).

P1 — GITHUB_ENTERPRISE_TOKEN (no GH_ prefix) is a real gh GHES fallback per cli.github.com docs. Behaviour was already correct (not stripped) 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; loop short-circuits on second positional anyway.

P3 — shared cloud_deny_message hardcoded issue #38. Added cloud_deny_message_with_issue(tool, op, issue) variant so gh deny correctly cites #50.

Pinning tests (8 unit + 4 integration)

Unit in secure_cloud_tests: gh_strip_list_contains_redirect_overrides, gh_strip_list_preserves_auth_tokens, 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: 4 tests including gh_extension_exec_is_rejected (binary-independent).

Verified

Not in this PR

Closes #50.

…#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 #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 #38 (cloud-tools issue).
  Added cloud_deny_message_with_issue(tool, op, issue) variant so the gh
  deny correctly cites #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 #50 message

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

Closes #50.
@thehoff
thehoff merged commit 88bc0af into develop May 18, 2026
3 checks passed
@thehoff
thehoff deleted the harden/gh-glab-gt-50 branch May 18, 2026 12:31
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
…off#57)

`tests/git_hardening.rs::make_repo()` ran `git commit` with the
developer's global git config inherited. When the host has
`commit.gpgsign=true` pointing at a signing agent (1Password,
gpg-agent, ssh-agent) and that agent is unavailable or locked,
ALL 5 fixture-using tests die at `commit -q -m init` with
"1Password: agent returned an error" / "fatal: failed to write
commit object" — and the failure looks like the security test
broke, not the fixture.

This bit the thehoff#50 (gh/glab/gt) and thehoff#58 (review fixups) commits
in the same session: the bug-bypass tests passed, the env-strip
tests panicked at the fixture, and the user had to context-switch
to figure out it was 1Password not the security work.

## Fix

Two layers of insurance in `make_repo()`:
1. Pass `-c commit.gpgsign=false -c tag.gpgsign=false` on every
   git invocation in the helper. These override the inherited
   global config for the duration of the call.
2. Also `git config commit.gpgsign false` + `tag.gpgsign false`
   on the freshly-init'd repo so any future helper that forgets
   the `-c` flags still gets a hermetic commit.

Belt-and-braces because layer 1 alone is sufficient today, but
layer 2 protects against drift in helpers that add new git calls
later.

## Verified
Before this PR (on develop tip `b5442fe`):
  test result: FAILED. 1 passed; 5 failed.

After this PR:
  test result: ok. 6 passed; 0 failed.

All other 8 integration suites continue to pass.

Closes thehoff#57.
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
…+ annotate scaffolding

`cargo build` was emitting 80+ unused-* warnings, drowning real signal
from genuine new dead-code introductions (e.g. PR thehoff#58 unused imports
went unnoticed for a session). Zero-warning cargo build is the baseline.

## Orphan files deleted (-2953 lines)

Audited as fully unreferenced (zero external `use crate::...::X` paths
outside the file itself); confirmed by checking that all module-level
items are unused in production and the deleted .rs files have no
external test depending on their public API.

- `src/analytics/security_cmd.rs` (617 lines) -- superseded by
  `src/hooks/tirith_gate.rs::run_security_dashboard` which backs the
  current `contextcrawler security` subcommand
- `src/analytics/jsonl_rewriter.rs` (557 lines)
- `src/analytics/session_compact_cmd.rs` (292 lines) -- session-compact
  feature inherited from the contextzip merge that never got wired
- `src/analytics/ansi_filter.rs` (198 lines) -- only used by the
  jsonl_rewriter above
- `src/cmds/cloud/web_cmd.rs` (472 lines) -- contextzip HTML extractor
  that was never wired through clap; `cmds/cloud/mod.rs` uses
  `automod::dir!` so the file got auto-imported as a module without a
  call site
- `src/core/error_cmd.rs` (817 lines) -- language-specific stack-trace
  compactors that also never got wired

Also dropped from mod.rs:
- `src/analytics/mod.rs` -- 4 `pub mod` lines
- `src/core/mod.rs` -- `pub mod error_cmd;` line

## Legacy hook constants WIRED (instead of deleted)

`LEGACY_CLAUDE_HOOK_COMMAND` and `LEGACY_CURSOR_HOOK_COMMAND` were
defined as constants but only ever referenced as literal strings
elsewhere. Wiring them properly closes two latent bugs:

- `src/hooks/integrity.rs::run_verify`: only checked the legacy literal
  "rtk hook claude" in `settings.json`. Freshly-installed users running
  the current `CLAUDE_HOOK_COMMAND` ("contextcrawler hook claude") saw
  "hook not installed" even after a successful `contextcrawler init -g`.
  Fixed: accept either current or legacy.
- `src/hooks/init.rs` cursor hook detection (line 3083 + 3258): only
  matched the current command. Legacy installs got missed by the
  skip-install detector AND the uninstall remover. Fixed: accept both.

## Intentional scaffolding annotated #[allow(dead_code)]

These are future-wired structures that earn their warnings by design.
Annotating with intent so the next reader knows they're not orphans:

- `core::utils::ToolPolicy` + `secure_command_with_policy` +
  `check_args_with_policy` + `policy_deny_message` -- the worked
  refactor target documented in the module header. Per-tool helpers
  will migrate onto it.
- `main::web_ssrf_block_reason` -- IP classification helper from the
  deleted web_cmd.rs. Preserved for future SSRF wiring of curl/wget
  filter or a Rust HTTP client; the classification logic is the
  expensive part to re-derive.
- `cmds::git::git::filter_branch_output` -- back-compat wrapper kept
  for the back-compat test surface (production switched to
  filter_branch_output_with_mode in thehoff#56).
- `cmds::system::grep_cmd::has_format_flag_raw` -- OsString-typed
  sibling of the existing String-typed helper; one helper per arg-type
  so callers don't need to allocate.

## Unused imports removed

- `src/cmds/js/lint_cmd.rs` -- `resolved_command` (replaced by
  `secure_python_command` / `package_manager_exec` earlier this session)
- `src/cmds/system/grep_cmd.rs` -- `resolved_command` moved under
  `#[cfg(test)]` (production uses `secure_rg_command`, tests use the
  raw baseline)

## Verified

- cargo build clean: **0 warnings** (was 80+)
- cargo test --bin contextcrawler: 2116 pass / 0 fail (was 2156;
  delta is the ~40 tests inside the deleted orphan files)
- All 9 integration suites green

No production behaviour changes apart from the two latent-bug fixes
(integrity check + cursor hook detection) which are pure improvements.
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(gh/glab/gt): wrap GitHub/GitLab/git-town CLIs with secure_* helpers (chain to git)

1 participant