git_credentials.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. """Non-interactive HTTPS credentials for Hermes's internal git clones (private plugin/MCP/profile repos).
  2. :func:`noninteractive_git_env` deliberately disables credential helpers, askpass and global git
  3. config so a hostile repo cannot make our plumbing prompt or hang. The cost is that a *private*
  4. repo the user can already clone from their shell fails inside ``hermes plugins install`` with
  5. "could not read Username" (or hangs on a GUI askpass until the timeout). This module resolves a
  6. credential up front, from sources the user already owns, and passes it to git as a one-shot
  7. ``http.<origin>/.extraheader`` in the environment — never in the URL and never in ``.git/config``,
  8. so nothing is persisted into the installed checkout.
  9. Resolution order for an ``https://`` URL:
  10. 1. ``GITHUB_TOKEN`` / ``GH_TOKEN`` (profile-scoped, GitHub hosts only).
  11. 2. ``gh auth token`` (GitHub hosts only; the gh CLI's own login).
  12. 3. ``git credential fill`` against the user's configured credential helpers (any host: GitLab,
  13. Bitbucket, self-hosted) with prompting disabled, so a stored credential is returned and a
  14. missing one fails in ~100 ms instead of asking.
  15. The credential is attached only after an anonymous attempt is refused
  16. (:func:`run_git_with_credential_fallback`), and a refused credential yields to the next one in
  17. the list rather than ending the run — a stale ``GITHUB_TOKEN`` in ``.env`` otherwise shadows a
  18. ``gh auth login`` that still works. Most catalog repos are public, and a stale or
  19. revoked stored token sent pre-emptively turns a clone that works anonymously into a 401 that git
  20. can only answer with the prompt this module disables — "could not read Username for
  21. 'https://github.com': terminal prompts disabled".
  22. """
  23. from __future__ import annotations
  24. import base64
  25. import logging
  26. import os
  27. import re
  28. import shutil
  29. import subprocess
  30. import urllib.parse
  31. from typing import Iterator, Mapping, Optional
  32. from hermes_cli._subprocess_compat import noninteractive_git_env, windows_hide_flags
  33. logger = logging.getLogger(__name__)
  34. _GITHUB_HOSTS = {"github.com", "gist.github.com"}
  35. def _https_origin(url: str) -> Optional[str]:
  36. parsed = urllib.parse.urlsplit(url)
  37. if parsed.scheme != "https" or not parsed.hostname:
  38. return None
  39. host = f"[{parsed.hostname}]" if ":" in parsed.hostname else parsed.hostname
  40. if parsed.port is not None:
  41. host = f"{host}:{parsed.port}"
  42. return f"https://{host}"
  43. def _env_github_token() -> Optional[str]:
  44. from agent.secret_scope import get_secret
  45. return get_secret("GITHUB_TOKEN") or get_secret("GH_TOKEN") or None
  46. def _gh_cli_token() -> Optional[str]:
  47. gh = shutil.which("gh")
  48. if not gh:
  49. return None
  50. try:
  51. env = noninteractive_git_env()
  52. env["GH_PROMPT_DISABLED"] = "1"
  53. # gh echoes an exported GH_TOKEN/GITHUB_TOKEN back instead of its keyring login; that
  54. # token is already candidate 1, and a dead one would hide the login that still works.
  55. for var in ("GH_TOKEN", "GITHUB_TOKEN"):
  56. env.pop(var, None)
  57. result = subprocess.run(
  58. [gh, "auth", "token"], capture_output=True, text=True, encoding="utf-8", errors="replace",
  59. timeout=10, stdin=subprocess.DEVNULL, env=env, creationflags=windows_hide_flags(), check=False)
  60. except (OSError, subprocess.TimeoutExpired) as exc:
  61. logger.debug("gh auth token lookup failed: %s", exc)
  62. return None
  63. if result.returncode != 0:
  64. return None
  65. return result.stdout.strip() or None
  66. def _github_token() -> Optional[str]:
  67. return _env_github_token() or _gh_cli_token()
  68. def _credential_fill(origin: str) -> Optional[tuple[str, str]]:
  69. """``(username, password)`` from the user's own git credential helpers, never prompting."""
  70. git = shutil.which("git")
  71. if not git:
  72. return None
  73. env = dict(os.environ)
  74. env["GIT_TERMINAL_PROMPT"] = "0"
  75. env["GCM_INTERACTIVE"] = "Never"
  76. # A GUI askpass (VS Code, ssh-askpass) would block on a dialog nobody sees.
  77. env.pop("GIT_ASKPASS", None)
  78. env.pop("SSH_ASKPASS", None)
  79. parsed = urllib.parse.urlsplit(origin)
  80. request = f"protocol=https\nhost={parsed.netloc}\n\n"
  81. try:
  82. result = subprocess.run(
  83. [git, "-c", "core.askPass=", "credential", "fill"], input=request, capture_output=True,
  84. text=True, encoding="utf-8", errors="replace", timeout=15, env=env,
  85. creationflags=windows_hide_flags(), check=False)
  86. except (OSError, subprocess.TimeoutExpired) as exc:
  87. logger.debug("git credential fill failed for %s: %s", origin, exc)
  88. return None
  89. if result.returncode != 0:
  90. return None
  91. fields = dict(line.split("=", 1) for line in result.stdout.splitlines() if "=" in line)
  92. if fields.get("password"):
  93. return fields.get("username", ""), fields["password"]
  94. return None
  95. def iter_git_basic_auth(url: str) -> Iterator[tuple[str, tuple[str, str]]]:
  96. """``(source, (username, password))`` for every credential the user owns for *url*'s host,
  97. in precedence order and without duplicates. The remote decides which one works: an expired
  98. ``GITHUB_TOKEN`` left in ``.env`` must not shadow a live ``gh auth login`` (#115257)."""
  99. origin = _https_origin(url)
  100. if origin is None:
  101. return
  102. seen: set[tuple[str, str]] = set()
  103. sources = []
  104. if urllib.parse.urlsplit(origin).hostname in _GITHUB_HOSTS:
  105. sources += [("GITHUB_TOKEN/GH_TOKEN", lambda: _token_pair(_env_github_token())),
  106. ("gh auth token", lambda: _token_pair(_gh_cli_token()))]
  107. sources.append(("git credential helper", lambda: _credential_fill(origin)))
  108. for source, lookup in sources:
  109. auth = lookup()
  110. if auth is not None and auth not in seen:
  111. seen.add(auth)
  112. yield source, auth
  113. def _token_pair(token: Optional[str]) -> Optional[tuple[str, str]]:
  114. return ("x-access-token", token) if token else None
  115. def resolve_git_basic_auth(url: str) -> Optional[tuple[str, str]]:
  116. """``(username, password)`` for *url*, or None for non-HTTPS URLs / no stored credential."""
  117. return next((auth for _source, auth in iter_git_basic_auth(url)), None)
  118. def with_git_auth(env: Mapping[str, str], url: str,
  119. auth: Optional[tuple[str, str]] = None) -> dict[str, str]:
  120. """Copy of *env* (a :func:`noninteractive_git_env` result) that authenticates HTTPS requests to
  121. *url*'s origin via a ``GIT_CONFIG_*`` ``http.<origin>/.extraheader`` entry when a credential is
  122. available (*auth*, else the first stored one); unchanged otherwise. The header lives only in
  123. this process environment."""
  124. env = dict(env)
  125. origin = _https_origin(url)
  126. if origin is None:
  127. return env
  128. if auth is None:
  129. auth = resolve_git_basic_auth(url)
  130. if auth is None:
  131. return env
  132. encoded = base64.b64encode(f"{auth[0]}:{auth[1]}".encode()).decode()
  133. idx = int(env.get("GIT_CONFIG_COUNT", "0") or 0)
  134. env[f"GIT_CONFIG_KEY_{idx}"] = f"http.{origin}/.extraheader"
  135. env[f"GIT_CONFIG_VALUE_{idx}"] = f"Authorization: basic {encoded}"
  136. env["GIT_CONFIG_COUNT"] = str(idx + 1)
  137. return env
  138. def _auth_headers(env: Mapping[str, str]) -> set[str]:
  139. return {v for k, v in env.items() if k.startswith("GIT_CONFIG_VALUE_") and v.startswith("Authorization:")}
  140. # What git prints when the remote wants a credential it could not obtain: the prompt that
  141. # GIT_TERMINAL_PROMPT=0 refused, a rejected credential, or a bare 401/403 from the server.
  142. _CREDENTIAL_REQUIRED_RE = re.compile(
  143. r"could not read (?:username|password)|terminal prompts disabled|authentication failed"
  144. r"|(?:error|status|http)[: ]+40[13]\b",
  145. re.IGNORECASE,
  146. )
  147. def is_credential_required_error(result: subprocess.CompletedProcess) -> bool:
  148. """True when a failed git run says the remote demands a credential (vs. a typo'd URL,
  149. a missing commit, a network error, ...)."""
  150. parts = []
  151. for stream in (result.stderr, result.stdout):
  152. if isinstance(stream, bytes):
  153. stream = stream.decode("utf-8", errors="replace")
  154. parts.append(stream or "")
  155. return _CREDENTIAL_REQUIRED_RE.search("\n".join(parts)) is not None
  156. def run_git_with_credential_fallback(
  157. argv: list[str], url: str, *, env: Mapping[str, str], **run_kwargs,
  158. ) -> subprocess.CompletedProcess:
  159. """Run the git network verb *argv* against *url* anonymously; when the remote refuses with
  160. the credential-required class, rerun with each credential the user owns for that host, in
  161. precedence order, until one is accepted or the failure stops being about credentials. *env*
  162. is a :func:`noninteractive_git_env`; *run_kwargs* must capture output so the refusal can be
  163. classified. Empty *url* means no fallback (local verbs)."""
  164. run_kwargs.setdefault("stdin", subprocess.DEVNULL)
  165. env = dict(env)
  166. # An inherited askpass (VS Code terminal, ksshaskpass) would swallow the remote's 401 into a
  167. # dialog nobody answers: the run hits its timeout instead of failing with "could not read
  168. # Username", and the refusal below is never classified. Same drop _credential_fill does.
  169. env.pop("GIT_ASKPASS", None)
  170. env.pop("SSH_ASKPASS", None)
  171. result = subprocess.run(argv, env=env, **run_kwargs) # noqa: PLW1510 -- forwarding wrapper: callers pass subprocess kwargs via **kwargs (check may arrive through it); an explicit check=False would raise TypeError
  172. if result.returncode == 0 or not url or not is_credential_required_error(result):
  173. return result
  174. auth_env = with_git_auth(env, url)
  175. if auth_env == env:
  176. return result
  177. result = subprocess.run(argv, env=auth_env, **run_kwargs) # noqa: PLW1510 -- forwarding wrapper: callers pass subprocess kwargs via **kwargs (check may arrive through it); an explicit check=False would raise TypeError
  178. sent = _auth_headers(auth_env)
  179. rejected: list[str] = []
  180. # The first credential (typically GITHUB_TOKEN from .env) was refused too: it is stale or
  181. # revoked, not missing. Try the remaining ones the user owns instead of failing on it.
  182. for source, auth in iter_git_basic_auth(url):
  183. if result.returncode == 0 or not is_credential_required_error(result):
  184. break
  185. candidate_env = with_git_auth(env, url, auth)
  186. headers = _auth_headers(candidate_env)
  187. if headers <= sent:
  188. rejected.append(source)
  189. continue
  190. sent |= headers
  191. logger.warning("%s rejected the credential from %s; retrying with %s",
  192. _https_origin(url), ", ".join(rejected) or "the stored credential", source)
  193. result = subprocess.run(argv, env=candidate_env, **run_kwargs) # noqa: PLW1510 -- forwarding wrapper: callers pass subprocess kwargs via **kwargs (check may arrive through it); an explicit check=False would raise TypeError
  194. if result.returncode != 0 and is_credential_required_error(result):
  195. rejected.append(source)
  196. if result.returncode != 0 and "GITHUB_TOKEN/GH_TOKEN" in rejected and isinstance(result.stderr, str):
  197. result.stderr += ("\nhint: the GITHUB_TOKEN/GH_TOKEN in your .env was rejected by GitHub;"
  198. " replace it or remove it (gh auth login is used when it is absent).")
  199. return result