|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Offline token transport regressions through production API/Git consumers.""" |
| 3 | +import hashlib |
| 4 | +import json |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import tempfile |
| 11 | +import unittest |
| 12 | + |
| 13 | +sys.dont_write_bytecode = True |
| 14 | +import verify_github_token as check |
| 15 | + |
| 16 | +ROOT = Path(__file__).resolve().parent.parent |
| 17 | +REAL_GH = shutil.which("gh") |
| 18 | +TOKENS = { |
| 19 | + "classic": "ghs_" + "A1b2" * 9, |
| 20 | + # Deliberately unsigned fixtures with underscores, hyphens and two dots. |
| 21 | + "stateless_520": "ghs_12345_" + "a_B-" * 20 + "." + "c_D-" * 60 + "." + "s_T-" * 47, |
| 22 | + "stateless_long": "ghs_12345_" + "a_B-" * 100 + "." + "c_D-" * 200 + "." + "s_T-" * 300, |
| 23 | + "oauth": "gho_" + "C3d4" * 9, |
| 24 | +} |
| 25 | + |
| 26 | +FAKE_GH = r'''#!/usr/bin/env python3 |
| 27 | +import hashlib, json, os, sys |
| 28 | +args = sys.argv[1:] |
| 29 | +token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") or os.environ.get("FIXTURE_LOGIN_TOKEN", "") |
| 30 | +if not token or hashlib.sha256(token.encode()).hexdigest() != os.environ["FIXTURE_TOKEN_HASH"]: |
| 31 | + sys.exit(1) |
| 32 | +if args[:2] == ["auth", "token"]: |
| 33 | + print(token) |
| 34 | +elif args[:2] == ["auth", "git-credential"]: |
| 35 | + sys.stdin.read() |
| 36 | + if os.environ.get("FIXTURE_TRUNCATE"): |
| 37 | + token = token[:40] |
| 38 | + print("username=x-access-token\npassword=" + token + "\n") |
| 39 | +elif args[0] == "api": |
| 40 | + failure = os.environ.get("FIXTURE_API_ERROR") |
| 41 | + if failure and failure in args[1]: |
| 42 | + print(token, file=sys.stderr) # A failing tool must not leak into check logs. |
| 43 | + sys.exit(1) |
| 44 | + data = {"object": {"type": "commit", "sha": "a" * 40}} if args[1].endswith("git/ref/heads/main") else {"full_name": "FileMintApp/homebrew-tap"} |
| 45 | + if "--include" in args: |
| 46 | + print("HTTP/2.0 200 OK\nContent-Type: application/json\n") |
| 47 | + print(json.dumps(data)) |
| 48 | +else: |
| 49 | + sys.exit(1) |
| 50 | +''' |
| 51 | + |
| 52 | + |
| 53 | +class GitHubTokenTests(unittest.TestCase): |
| 54 | + def setUp(self): |
| 55 | + temporary = tempfile.TemporaryDirectory(prefix="filemint-token-tests-") |
| 56 | + self.addCleanup(temporary.cleanup) |
| 57 | + self.directory = Path(temporary.name) |
| 58 | + binary = self.directory / "gh" |
| 59 | + binary.write_text(FAKE_GH) |
| 60 | + binary.chmod(0o755) |
| 61 | + self.environment = dict(os.environ, PATH=str(self.directory) + os.pathsep + os.environ["PATH"], |
| 62 | + GH_HOST="github.com", GH_PROMPT_DISABLED="1") |
| 63 | + for key in ("GH_TOKEN", "GITHUB_TOKEN", "FIXTURE_LOGIN_TOKEN", "FIXTURE_TRUNCATE", "FIXTURE_API_ERROR"): |
| 64 | + self.environment.pop(key, None) |
| 65 | + |
| 66 | + def run_check(self, token, source="GH_TOKEN", **environment): |
| 67 | + env = dict(self.environment, FIXTURE_TOKEN_HASH=hashlib.sha256(token.encode()).hexdigest(), |
| 68 | + **{source: token}, **environment) |
| 69 | + result = subprocess.run([sys.executable, str(ROOT / "scripts/verify_github_token.py")], |
| 70 | + cwd=ROOT, env=env, capture_output=True, text=True, timeout=30) |
| 71 | + self.assertNotIn(token, result.stdout + result.stderr) |
| 72 | + return result |
| 73 | + |
| 74 | + def test_all_token_shapes_reach_release_tap_and_git_consumers_intact(self): |
| 75 | + self.assertEqual(len(TOKENS["classic"]), 40) |
| 76 | + self.assertEqual(len(TOKENS["stateless_520"]), 520) |
| 77 | + self.assertGreater(len(TOKENS["stateless_long"]), 2048) |
| 78 | + for label, token in TOKENS.items(): |
| 79 | + with self.subTest(format=label): |
| 80 | + result = self.run_check(token) |
| 81 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 82 | + self.assertEqual(json.loads(result.stdout), { |
| 83 | + "status": "passed", "release_api": "passed", "tap_api": "passed", |
| 84 | + "git_credential": "passed", "installation_token": label != "oauth", |
| 85 | + "token_length": len(token), "token_dots": token.count(".")}) |
| 86 | + |
| 87 | + def test_github_token_and_existing_login_fallbacks(self): |
| 88 | + for source in ("GITHUB_TOKEN", "FIXTURE_LOGIN_TOKEN"): |
| 89 | + with self.subTest(source=source): |
| 90 | + self.assertEqual(self.run_check(TOKENS["stateless_520"], source).returncode, 0) |
| 91 | + |
| 92 | + def test_truncated_credentials_fail_without_printing_token(self): |
| 93 | + result = self.run_check(TOKENS["stateless_long"], FIXTURE_TRUNCATE="1") |
| 94 | + self.assertNotEqual(result.returncode, 0) |
| 95 | + self.assertIn("did not preserve the complete token", result.stderr) |
| 96 | + |
| 97 | + def test_failed_apis_do_not_relay_sensitive_tool_stderr(self): |
| 98 | + for repository in ("repos/FileMintApp/FileMint/", "repos/FileMintApp/homebrew-tap"): |
| 99 | + with self.subTest(repository=repository): |
| 100 | + result = self.run_check(TOKENS["stateless_520"], FIXTURE_API_ERROR=repository) |
| 101 | + self.assertNotEqual(result.returncode, 0) |
| 102 | + self.assertIn("API request failed", result.stderr) |
| 103 | + |
| 104 | + def test_missing_credential_fails(self): |
| 105 | + env = dict(self.environment, FIXTURE_TOKEN_HASH="missing") |
| 106 | + result = subprocess.run([sys.executable, str(ROOT / "scripts/verify_github_token.py")], |
| 107 | + env=env, capture_output=True, text=True, timeout=30) |
| 108 | + self.assertNotEqual(result.returncode, 0) |
| 109 | + self.assertIn("No existing GitHub credential", result.stderr) |
| 110 | + |
| 111 | + @unittest.skipUnless(REAL_GH, "GitHub CLI is unavailable; fixture checks still run offline") |
| 112 | + def test_real_git_and_github_cli_preserve_synthetic_tokens_without_network(self): |
| 113 | + for label, token in TOKENS.items(): |
| 114 | + with self.subTest(format=label): |
| 115 | + environment = dict(os.environ, GH_TOKEN=token, GH_HOST="github.com", |
| 116 | + GH_PROMPT_DISABLED="1") |
| 117 | + environment.pop("GH_DEBUG", None) |
| 118 | + check.credential_round_trip(token, environment) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + unittest.main() |
0 commit comments