Check source archives when Git is unavailable (by Codex)

This commit is contained in:
Codex committed 2026-10-16 20:42:32 +02:00
1 parent f7ecb55def
commit 9321dff2e9
2 files changed
+24 -3

No files matched your search

+9 -3
View File
@@ -10,6 +10,7 @@ import dataclasses
import json import json
from pathlib import Path, PurePosixPath from pathlib import Path, PurePosixPath
import re import re
import shutil
import subprocess import subprocess
import sys import sys
@@ -197,9 +198,12 @@ def scan_blob(name: str, blob: bytes) -> list[Finding]:
def checked_files(root: Path): def checked_files(root: Path):
"""Use the Git index when available; otherwise inspect a plain source archive.""" """Use the Git index when available; otherwise inspect a plain source archive."""
git = subprocess.run(["git", "-C", str(root), "rev-parse", "--show-toplevel"], try:
capture_output=True, text=True) git = subprocess.run(["git", "-C", str(root), "rev-parse", "--show-toplevel"],
if git.returncode == 0 and Path(git.stdout.strip()).resolve() == root: capture_output=True, text=True)
except FileNotFoundError:
git = None
if git is not None and git.returncode == 0 and Path(git.stdout.strip()).resolve() == root:
output = subprocess.check_output(["git", "-C", str(root), "ls-files", "--cached", output = subprocess.check_output(["git", "-C", str(root), "ls-files", "--cached",
"--others", "--exclude-standard", "-z"]) "--others", "--exclude-standard", "-z"])
return sorted({p.decode("utf-8") for p in output.split(b"\0") if p}) return sorted({p.decode("utf-8") for p in output.split(b"\0") if p})
@@ -260,6 +264,8 @@ def main(argv=None):
parser.add_argument("--history", action="store_true", help="also check all reachable commits and their blobs") parser.add_argument("--history", action="store_true", help="also check all reachable commits and their blobs")
parser.add_argument("--json", type=Path, help="write a report containing rule names and locations only") parser.add_argument("--json", type=Path, help="write a report containing rule names and locations only")
args = parser.parse_args(argv) args = parser.parse_args(argv)
if args.history and shutil.which("git") is None:
parser.error("Git is required for history checks")
root = args.root.resolve(strict=True) root = args.root.resolve(strict=True)
findings, files = scan_tree(root) findings, files = scan_tree(root)
commits = blobs = 0 commits = blobs = 0
+15
View File
@@ -7,9 +7,11 @@ import contextlib
import io import io
import json import json
from pathlib import Path from pathlib import Path
import shutil
import subprocess import subprocess
import tempfile import tempfile
import unittest import unittest
from unittest import mock
import check_public_tree as guard import check_public_tree as guard
@@ -71,6 +73,19 @@ class BlobChecks(unittest.TestCase):
self.assertNotIn("credential-pattern", self.rules("src/a.cpp", "const int token = state.seller_team_token;")) self.assertNotIn("credential-pattern", self.rules("src/a.cpp", "const int token = state.seller_team_token;"))
class ArchiveChecks(unittest.TestCase):
def test_archive_is_checked_when_git_is_not_installed(self):
with tempfile.TemporaryDirectory(prefix="openbmh-source-test-") as directory:
root = Path(directory)
(root / "README.md").write_text("Synthetic source\n", encoding="utf-8")
(root / "bad.FBM").write_text("Synthetic refusal case\n", encoding="utf-8")
with mock.patch.object(guard.subprocess, "run", side_effect=FileNotFoundError):
findings, count = guard.scan_tree(root)
self.assertEqual(count, 2)
self.assertIn("binary-or-game-asset", {f.rule for f in findings})
@unittest.skipUnless(shutil.which("git"), "Git is required for repository-history tests")
class RepositoryChecks(unittest.TestCase): class RepositoryChecks(unittest.TestCase):
def setUp(self): def setUp(self):
self.temp = tempfile.TemporaryDirectory(prefix="openbmh-guard-test-") self.temp = tempfile.TemporaryDirectory(prefix="openbmh-guard-test-")