"""Check 15 runs before every commit (ckb-sync-changes): one planted secret per shape and per hiding place must be found, and ordinary text must not.""" import json import shutil import subprocess import sys import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] FIX = ROOT / "tests/fixtures/minimal-kb" LINT = ROOT / ".agents/skills/ckb-lint/scripts/lint_report.py" # Assembled at runtime so no secret-shaped string sits in this repo. GH = "gh" + "p_" + "A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8" KEY = "-----BEGIN " + "RSA PRIVATE KEY-----\nMIIEow\n-----END RSA PRIVATE KEY-----\n" AWS = "aws_secret" + "_access_key = " + "wJalrXUtnFEMI/K7MDENG/bPxRfiCY" + "EXAMPLEKEY" PLANTED = { "raw/inbox/.hidden/notes.md": f"token {GH}\n", # hidden folder "raw/inbox/server.pem": KEY, # .pem "raw/inbox/id_rsa": KEY, # no extension "raw/inbox/.env": "PASSWORD=" + "s3cretPassw0rd\n", # dotfile "wiki/entities/cfg.md": "---\ntype: entity\n---\n\n# Cfg\n\npassword: " + "hunter2hunter2xx\n", "wiki/entities/aws.md": f"---\ntype: entity\n---\n\n# AWS\n\n{AWS}\n", } HARMLESS = { "wiki/entities/prose.md": "---\ntype: entity\n---\n\n# Prose\n\npassword: required\n\n" "SECRET_MAX_BYTES = 5_000_000\napi_key: ${API_KEY}\n", } class SecretScan(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.kb = Path(self.tmp.name) / "kb" shutil.copytree(str(FIX), str(self.kb)) def tearDown(self): self.tmp.cleanup() def plant(self, files): for rel, text in files.items(): p = self.kb / rel p.parent.mkdir(parents=True, exist_ok=True) p.write_text(text, encoding="utf-8") def findings(self, cap=None): code = ("import sys, importlib.util; spec = importlib.util.spec_from_file_location('l', sys.argv[1]); " "l = importlib.util.module_from_spec(spec); spec.loader.exec_module(l); " + (f"l.SECRET_MAX_BYTES = {cap}; " if cap else "") + "import json; print(json.dumps(l.check_secrets()))") r = subprocess.run([sys.executable, "-c", code, str(LINT)], cwd=str(self.kb), capture_output=True, text=True, encoding="utf-8") self.assertEqual(r.returncode, 0, r.stderr) return json.loads(r.stdout) def test_every_planted_secret_is_found(self): self.plant(PLANTED) found = self.findings() for rel in PLANTED: self.assertTrue(any(f.startswith(rel + ":") for f in found), f"{rel} not reported:\n" + "\n".join(found)) def test_findings_never_repeat_the_secret(self): self.plant(PLANTED) text = "\n".join(self.findings()) for secret in (GH, "s3cretPassw0rd", "hunter2hunter2xx", "wJalrXUtnFEMI"): self.assertNotIn(secret, text) def test_ordinary_text_is_not_a_secret(self): self.plant(HARMLESS) self.assertEqual([f for f in self.findings() if "prose.md" in f], []) def test_binary_file_without_extension_is_skipped(self): (self.kb / "raw/inbox").mkdir(parents=True, exist_ok=True) (self.kb / "raw/inbox/blob").write_bytes(b"\0\1\2" + GH.encode()) self.assertEqual([f for f in self.findings() if "blob" in f], []) def test_oversized_file_is_reported_not_silently_passed(self): self.plant({"raw/inbox/big.md": "x" * 500}) found = self.findings(cap=100) self.assertTrue(any("big.md" in f and "not scanned" in f for f in found), found) if __name__ == "__main__": unittest.main()