Skip to content

feat(auth): opt-in strict host credentials for GitHub-class hosts (#4324) - #4615

Merged
Daniel Meppiel (danielmeppiel) merged 6 commits into
mainfrom
danielmeppiel-p1-4324-strict-host-credentials
Nov 21, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 6 commits into
mainfrom
danielmeppiel-p1-4324-strict-host-credentials

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

Opt-in strict host credentials for GitHub-class hosts. APM_STRICT_HOST_CREDENTIALS unset, empty, 0, false, no or off keeps the documented 0.37 behaviour. 1, true, yes or on (case-insensitive) turns strict mode on. Any other value also turns it on (fail closed) and prints one warning naming the value.

In strict mode the owner AuthResolver (src/apm_cli/core/auth.py) reads the setting once and:

  • offers GITHUB_APM_PAT_<ORG>, GITHUB_APM_PAT, GITHUB_TOKEN and GH_TOKEN only to the configured host (GITHUB_HOST, else github.com);
  • for every other github.com / *.ghe.com host, runs gh auth token --hostname and git credential fill (primary chain and the secondary fallback) without GH_TOKEN, GITHUB_TOKEN, GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN, GITHUB_APM_PAT or GITHUB_APM_PAT_*;
  • prints one warning per command per withheld host naming the host, strict mode and the remedy, and repeats it in the auth-failure / not-found reason for that host;
  • changes eligibility only, not attempt order (anonymous-first github.com and APM_GITHUB_AUTH_FIRST unchanged).

Git children already scrub every token variable (_build_git_env), and the version check, marketplace and policy paths all resolve through AuthResolver, so no consumer re-derives host scope.

gh behaviour, executed for real (record requirement)

gh version 2.103.3 (2026-10-26), isolated GH_CONFIG_DIR, env -i, gh auth token --hostname <host> with one inert variable set:

host GH_TOKEN GITHUB_TOKEN GH_ENTERPRISE_TOKEN GITHUB_ENTERPRISE_TOKEN
github.com returned (rc=0) returned (rc=0) rc=1 rc=1
acme.ghe.com returned (rc=0) returned (rc=0) rc=1 rc=1
ghes.example.com rc=1 rc=1 returned (rc=0) returned (rc=0)

So gh hands an inherited GH_TOKEN/GITHUB_TOKEN back for any *.ghe.com host; strict mode therefore scrubs them from the gh child, not only from APM's own lookup.

Auth-expert review

An auth-expert pass found no leak path in strict mode and confirmed byte-identical behaviour with strict off. Folded: unset-variable control row; secondary fallback routed through the strict-aware helpers; GITHUB_HOST with a scheme; no per-org advice on withheld hosts; background lookups (update check) withhold silently; the notice prints immediately (a deferred diagnostic was lost when every package failed validation) and the installed-CLI test asserts the notice and its count. Not folded (latent, no production caller): GitHubDownloader._resolve_dep_token(None) and GitHubTokenManager.get_token_with_credential_fallback (tests only).

Issue and approved scope

Issue: Fixes #4324

Human scope-approval comment: #4324 (comment) (amended record)

Does this PR complete the issue, or what remains? Completes it.

Dependency order: none (independent of open PRs).

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally

  • All existing tests pass

  • Added tests for new functionality (if applicable)

  • tests/spec_conformance/test_security_behavior_reqs.py::test_token_scoped_to_its_host_class loses its auth: opt-in strict mode that scopes *.ghe.com credentials to the configured tenant #4324 strict-xfail and now sets the strict variable; rows also cover the control (acme.ghe.com receives GITHUB_APM_PAT with the variable unset or empty) and hosts outside the GitHub-operated class (no GitHub token, strict or not).

  • tests/integration/test_strict_host_credentials.py (new): real AuthResolver in a fresh process with stub gh and a git credential helper that record every inherited variable name and, like real gh, return GH_TOKEN/GITHUB_TOKEN. In strict mode none of the scrubbed names reaches either child for acme.ghe.com, the distinct inert helper credential is the one resolved, and the configured host (github.com, then GITHUB_HOST=acme.ghe.com) still receives the env token. A value table pins off/on/unrecognised parsing and the warning.

  • tests/integration/test_repository_not_found_lifecycle.py::test_strict_host_credentials_withhold_env_token_from_unconfigured_ghe_host[GITHUB_APM_PAT|GH_TOKEN] (installed CLI, impersonated *.ghe.com harness): strict + env token, apm install lifecycle.ghe.com/... sends that host no Authorization header, exits 1 naming the host and strict mode with the remedy, writes nothing; with GITHUB_HOST set to that host the token is sent.

  • Red without the fix: the strict cases of the new integration module (6 failed, 7 control cases passed), the conformance strict row, and both installed-CLI cases fail on main.

  • pytest -n 2 tests/unit/core tests/integration/test_auth_resolver.py tests/spec_conformance/test_security_behavior_reqs.py tests/test_token_manager.py tests/integration/test_credential_fill_disambiguation.py tests/integration/test_strict_host_credentials.py tests/integration/test_repository_not_found_lifecycle.py (APM_BINARY_PATH=.venv/bin/apm) -> 2691 passed, 31 skipped; 1 failure tests/unit/core/test_auth_credential_report.py::test_hanging_helper_store_never_fails_the_operation also fails on main code on this loaded host (timing; ADO path untouched here). Controls test_cross_host_redirect_drops_every_credential_header and tests/integration/test_auth_resolver.py green.

  • Lint: uvx [email protected] check/format --check, scripts/lint_architecture_boundaries.py --root ., complexity ratchet vs main, tests.quality.taxonomy --changed, scripts/check_test_assertions.py, scripts/lint-auth-signals.sh -> clean.

Changelog

  • One line in one section under ## [Unreleased] in CHANGELOG.md:
    user-visible effect first, 25 words or fewer, ending with (#PR);
    breaking changes only under upgrade notes; no Highlights; see
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit: no spec text change (req-sc-005 already allows basis (c) and requires documenting it).
  • Manifest edit
  • Test edit: @pytest.mark.req("req-sc-005") test un-xfailed and extended.
  • CONFORMANCE.{md,json} regenerated via python -m tests.spec_conformance.gen_statement (req-sc-005 xfail -> active; new "GitHub-operated host class" section documents basis (c) and how strict mode narrows it).
  • N/A -- this PR does not change OpenAPM-observable behaviour.

Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 21, 2026
)

APM_STRICT_HOST_CREDENTIALS=1/true/yes/on (any unrecognised value too,
with a warning) keeps GITHUB_APM_PAT_<ORG>, GITHUB_APM_PAT, GITHUB_TOKEN
and GH_TOKEN on the configured host (GITHUB_HOST, else github.com). For
every other github.com or *.ghe.com host, AuthResolver runs gh auth token
and git credential fill without GitHub-class token variables, warns once
naming the host, and repeats the notice on an auth failure there.

Co-authored-by: Copilot App <[email protected]>
- route the secondary gh/fill fallback through the strict-aware helpers
- parse GITHUB_HOST with a scheme; drop per-org advice on withheld hosts
- print the withheld notice immediately (a deferred one is lost when every
  package fails validation) and repeat it in the not-found reason
- background lookups without external fallback withhold silently
- conformance control covers APM_STRICT_HOST_CREDENTIALS unset

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An early return for stored credentials omits the strict-mode notice and remedy when repository access fails; the source-bearing result needs the notice and coverage for a rejected helper credential.

1 open issue
What changed in this PR

Adds opt-in strict host credentials to APM's authentication resolver, limiting GitHub environment tokens to the configured host while preserving default behavior.

Changes:

  • Restricts token eligibility and scrubs inherited GitHub tokens from credential-helper processes for other GitHub-class hosts.
  • Adds strict-mode diagnostics, integration tests, and installed-CLI regression coverage.
  • Documents the setting and updates host-scoping conformance records.
File Description
tests/​spec_conformance/​test_security_behavior_reqs.py Activates host-scoping checks and adds strict/default cases.
tests/​spec_conformance/​gen_statement.py Documents host-class behavior in generated statements.
tests/​integration/​test_strict_host_credentials.py Tests token scrubbing, configured hosts, and setting values.
tests/​integration/​test_repository_not_found_lifecycle.py Tests installed-CLI credential isolation and failure diagnostics.
src/​apm_cli/​core/​token_manager.py Accepts an explicit environment for gh token lookup.
src/​apm_cli/​core/​auth.py Owns strict-mode parsing, credential eligibility, and diagnostics.
packages/​apm-guide/​.apm/​skills/​apm-usage/​authentication.md Adds strict-mode usage guidance.
docs/​src/​content/​docs/​reference/​environment-variables.md Documents the new environment variable.
docs/​src/​content/​docs/​getting-started/​authentication.md Explains strict host scoping and sign-in remedies.
CONFORMANCE.md Updates host-class documentation and conformance status.
CONFORMANCE.json Records expanded, active host-scoping coverage.
CHANGELOG.md Announces opt-in strict host credentials.

Review effort: Balanced

Comment thread src/apm_cli/core/auth.py Resolved
…#4324)

describe_credential_tried appends the strict-mode notice and remedy to the
credential-tried reason too, so a stored gh or git-helper credential that a
withheld host rejects still names strict mode in the failure.

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit c75c83f into main Nov 21, 2026
62 of 67 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-p1-4324-strict-host-credentials branch November 21, 2026 15:34
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.

auth: opt-in strict mode that scopes *.ghe.com credentials to the configured tenant

3 participants