"""Publication guard tests use synthetic values and read-only Git ignore queries.""" import subprocess import tempfile from pathlib import Path import unittest from unittest.mock import patch import check_public class PublicCheckTests(unittest.TestCase): def test_private_names_are_reported_without_reading_them(self): for path in ("server.json", "deploy/worker.env", "profiles/.codex/auth.json", "fleet.json", "private.pem", ".env.production"): with self.subTest(path=path): self.assertTrue(check_public.sensitive_path(path)) def test_source_and_placeholder_examples_are_allowed(self): for path in ("server.example.json", "src/FinalToken.Core/HistoryStore.cs", "docs/web-dashboard.md", ".env.example", "LICENSE"): self.assertFalse(check_public.sensitive_path(path)) def test_matching_values_never_appear_in_findings(self): synthetic = "sk-" + "ant-" + "x" * 32 findings = check_public.inspect("synthetic.txt", ("header\n" + synthetic).encode()) self.assertEqual(1, len(findings)) self.assertEqual(2, findings[0].line) self.assertEqual("provider token", findings[0].kind) self.assertNotIn(synthetic, repr(findings)) def test_private_key_and_url_credentials_are_detected(self): key = b"-----BEGIN " + b"PRIVATE KEY-----" url = b"https://" + b"fixture:synthetic-password@example.test/repo.git" self.assertEqual({"private key", "credential in URL"}, {f.kind for f in check_public.inspect("fixture", key + b"\n" + url)}) def test_binary_input_is_not_treated_as_source(self): self.assertEqual([], check_public.inspect("image.png", b"\0" + b"sk-" + b"ant-" + b"x" * 32)) def test_staged_secret_is_found_even_when_working_copy_is_clean(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) (root / "source.txt").write_text("clean working copy") staged = ("sk-" + "proj-" + "x" * 32).encode() oid = "a" * 40 def fake_git(where, *args, data=None): if args[0] == "ls-files" and "--stage" in args: return f"100644 {oid} 0\tsource.txt\0".encode() if args[0] == "ls-files": return b"source.txt\0" if args[:2] == ("cat-file", "-s"): return str(len(staged)).encode() if args[:2] == ("cat-file", "blob"): return staged self.fail("Unexpected Git read") with patch.object(check_public, "git", side_effect=fake_git): findings, _, _ = check_public.working_tree(root) self.assertTrue(any(f.path.startswith("index:") and f.kind == "provider token" for f in findings)) def test_working_tree_symlink_is_rejected_before_resolving_its_target(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) with patch.object(check_public, "git", return_value=b"link.cs\0"), \ patch.object(check_public, "index", return_value=([], 0, [])), \ patch.object(Path, "is_symlink", return_value=True), patch.object(Path, "read_bytes", side_effect=AssertionError("must not read")): findings, _, _ = check_public.working_tree(root) self.assertEqual("symlink/outside source tree", findings[0].kind) def test_historical_private_name_is_detected_when_same_blob_had_a_public_name(self): oid = "c" * 40 def fake_git(root, *args, data=None): if args[0] == "rev-list": return b"first\nsecond\n" name = "examples/example.json" if args[-1] == "first" else "auth.json" return f"100644 blob {oid}\t{name}\0".encode() with patch.object(check_public, "git", side_effect=fake_git): paths = check_public.historical_private_paths(Path("unused")) self.assertEqual({oid: {"auth.json"}}, paths) def test_staged_private_file_is_reported_without_opening_its_blob(self): def fake_git(root, *args, data=None): self.assertEqual("ls-files", args[0]) return ("100644 " + "a" * 40 + " 0\tserver.env\0").encode() with patch.object(check_public, "git", side_effect=fake_git): findings, count, _ = check_public.index(Path("unused")) self.assertEqual(0, count) self.assertEqual("index:server.env", findings[0].path) def test_git_ignores_live_runtime_data_but_keeps_examples(self): paths = ["server.json", "deploy/worker.json", "server.env", ".env.production", "transport-keys.json", "fleet.json", "snapshot.json", "history.journal.jsonl", "profiles/.codex/auth.json", "private.key", "logs/session.log"] result = subprocess.run(["git", "check-ignore", "-z", "--stdin"], cwd=check_public.ROOT, input=("\0".join(paths) + "\0").encode(), capture_output=True, check=True) self.assertEqual(set(paths), set(result.stdout.decode().strip("\0").split("\0"))) examples = subprocess.run(["git", "check-ignore", "-z", "--stdin"], cwd=check_public.ROOT, input=b".env.example\0server.example.json\0LICENSE\0", capture_output=True) self.assertEqual(1, examples.returncode) if __name__ == "__main__": unittest.main()