← Back to Pull requests

fix(cli): treat a wrapper-polluted gh auth token capture as no token #138457

Open
unknown opened this pull request with 609 lines diff across 7 files
View on GitHub
unknown unknown commented on Oct 14, 2026 15:26 UTC
Author

What does this PR do?

Fixes the updater failing closed whenever gh resolves to a PATH wrapper that prints to stdout (#138444) — mise/asdf shims, corporate hooks. gh auth token's capture then holds wrapper chatter plus the token; the embedded newline makes http.client reject the Authorization header (Invalid header value b'Bearer …') before any bytes are sent, so the 401→anonymous fallback never engages and hermes update (and the Desktop update check, which shares the ladder) aborts.

Two complementary hardenings, both suggested in the issue:

1. Capture time (root fix) — hermes_cli/github_api.py::_gh_cli_token() no longer trusts the raw capture. The token is the last non-empty line, and only when it is shaped like a GitHub token (gh[a-z]_… classic prefixes, github_pat_… fine-grained). Anything else means no token: the ladder continues to anonymous, with a debug log (the capture itself is never logged).
2. Request time (defense in depth) — hermes_cli/source_check.py::_request() catches the invalid-header ValueError and retries anonymously, mirroring the existing 401 fallback. This also covers a malformed GITHUB_TOKEN/GH_TOKEN env value, and it keeps that ValueError's message — which quotes the header, token included — out of the user-facing failure copy (describe_github_failure renders unknown exceptions as api.github.com: {exc}).

The env rungs are deliberately not shape-checked: a token the user set explicitly is their intent, and the request-time fallback already covers it.

Related Issue

Fixes #138444

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • Changes Made

  • hermes_cli/github_api.py — _gh_cli_token() takes the last non-empty line of the capture and requires a GitHub token shape (_GITHUB_TOKEN_SHAPE); a non-matching capture is treated as no token (debug-logged without the capture, ladder falls through to anonymous).

  • hermes_cli/source_check.py — _request() retries anonymously on an invalid-header ValueError (only when a token was used; with no token the error still propagates); docstring updated.

  • tests/hermes_cli/test_github_token_capture.py — new: wrapper noise dropped, clean capture kept, no-shape capture ignored, fine-grained shape accepted, non-zero exit ignored, probe-once caching preserved, ValueError → anonymous retry, malformed token never leaks into logs, unrelated ValueError still propagates.
  • How to Test

    1. pytest tests/hermes_cli/test_github_token_capture.py -q — Observed result: 9 passed.
    2. Ladder regressions: pytest tests/hermes_cli/test_source_check.py tests/hermes_cli/test_doctor_github_token_probe.py -q — Observed result: 87 passed. (tests/hermes_cli/test_source_release_channels.py has 2 failures on this host that reproduce identically on a clean upstream/main worktree — home_io_guard environment noise, files untouched by this PR.)
    3. python -m ruff check hermes_cli/github_api.py hermes_cli/source_check.py tests/hermes_cli/test_github_token_capture.py — Observed result: All checks passed!.

    Checklist

    Code

  • I've read the Contributing Guide

  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)

  • I searched for existing PRs to make sure this isn't a duplicate

  • My PR contains only changes related to this fix/feature (no unrelated commits)

  • I've run pytest tests/ -q and all tests pass — targeted suites quoted above; the changed-code suites are green on this host

  • python scripts/check passes — ruff check green on all three changed files; host ruff format --check diverges only on pre-existing code (reproduces on untouched files such as hermes_cli/main.py), formatting of the changed lines follows the file's existing style

  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)

  • I've tested on my platform: macOS 26 (arm64)
  • Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — docstrings updated in place; or N/A beyond that

  • I've updated cli-config.yaml.example if I added/changed config keys — N/A

  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — N/A

  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — the gh probe is already Windows-hardened via windows_hide_flags(); the change itself is pure-Python string handling

  • I've updated tool descriptions/schemas if I changed tool behavior — N/A
  • Diff: 609 lines (+456 / -153) across 7 files Reviewer: PRATHAMESH75 Review duration: 72550m
    All Vouch review confidence checks passed
    Residual risk 0.44 = Change Risk (0.80) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.80 · 609 lines diff across 7 files
    0.80
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.384
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.080
    Real PR Conversation Timeline
    1 comments
    "Review active on #138457"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 44.7% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 80.3% (HIGH) · Higher = larger blast radius
    80.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 44.4% · Moderate risk
    44.4%