Skip to content

The agent that grows with you

fix(update-check): a token GitHub refuses with 403 falls back to anonymous

Teknium6560a973e068
diff --git a/hermes_cli/source_check.py b/hermes_cli/source_check.py
index 5a67a576988..6d2f267d5fa 100644
--- a/hermes_cli/source_check.py
+++ b/hermes_cli/source_check.py
@@ -115,8 +115,9 @@ def _github_compare_behind(current_rev: str, target_rev: str, repository: str =
def _request(url: str, accept: str = "application/vnd.github+json") -> str:
"""GET an api.github.com resource with the credential ladder in hermes_cli.github_api.
- A token GitHub rejects (401) drops this request to anonymous rather than
- failing the check on a stale credential.
+ A token GitHub rejects drops this request to anonymous rather than failing the
+ check on a stale credential: 401, or a 403 that is not a spent rate limit (a
+ suspended or restricted account's token, still printed by ``gh auth token``, #134335).
"""
from hermes_cli.github_api import github_token
@@ -124,7 +125,8 @@ def _request(url: str, accept: str = "application/vnd.github+json") -> str:
try:
return _request_with(url, accept, token)
except urllib.error.HTTPError as exc:
- if token is None or exc.code != 401:
+ spent = exc.headers is not None and exc.headers.get("x-ratelimit-remaining") == "0"
+ if token is None or exc.code not in (401, 403) or spent:
exc.hermes_authenticated = token is not None # which quota a 403/429 spent
raise
logger.debug("GitHub rejected the configured token; retrying anonymously")
diff --git a/tests/hermes_cli/test_source_check_refused_token.py b/tests/hermes_cli/test_source_check_refused_token.py
new file mode 100644
index 00000000000..fd6ed903da6
--- /dev/null
+++ b/tests/hermes_cli/test_source_check_refused_token.py
@@ -0,0 +1,52 @@
+"""#134335: a token GitHub refuses with 403 (suspended/restricted account, still printed by
+`gh auth token`) falls back to anonymous like a 401 does; a spent rate limit does not.
+
+Real HTTP server on loopback, real urllib, real credential ladder (GITHUB_TOKEN rung).
+"""
+
+import http.server
+import threading
+import urllib.error
+
+import pytest
+
+from hermes_cli import source_check
+
+
+class _Api(http.server.BaseHTTPRequestHandler):
+ spent = False
+
+ def do_GET(self):
+ if self.headers.get("Authorization"):
+ self.send_response(403)
+ self.send_header("x-ratelimit-remaining", "0" if self.spent else "4999")
+ self.end_headers()
+ self.wfile.write(b'{"message": "Forbidden"}')
+ return
+ self.send_response(200)
+ self.end_headers()
+ self.wfile.write(b'{"sha": "anonymous-ok"}')
+
+ def log_message(self, *args):
+ pass
+
+
+def api(monkeypatch):
+ monkeypatch.setenv("GITHUB_TOKEN", "stale-token-of-a-suspended-account")
+ server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), _Api)
+ threading.Thread(target=server.serve_forever, daemon=True).start()
+ yield f"http://127.0.0.1:{server.server_address[1]}/repos/o/r/commits/main"
+ server.shutdown()
+
+
+def test_a_refused_token_falls_back_to_anonymous(api, monkeypatch):
+ monkeypatch.setattr(_Api, "spent", False)
+ assert source_check._request(api) == '{"sha": "anonymous-ok"}'
+
+
+def test_a_spent_rate_limit_still_fails(api, monkeypatch):
+ monkeypatch.setattr(_Api, "spent", True)
+ with pytest.raises(urllib.error.HTTPError) as caught:
+ source_check._request(api)
+ assert caught.value.code == 403 and caught.value.hermes_authenticated is True