Check source archives when Git is unavailable (by Codex)
This commit is contained in:
1 parent
f7ecb55def
commit
9321dff2e9
2 files changed
+24
-3
No files matched your search
@@ -10,6 +10,7 @@ import dataclasses
|
||||
import json
|
||||
from pathlib import Path, PurePosixPath
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -197,9 +198,12 @@ def scan_blob(name: str, blob: bytes) -> list[Finding]:
|
||||
|
||||
def checked_files(root: Path):
|
||||
"""Use the Git index when available; otherwise inspect a plain source archive."""
|
||||
git = subprocess.run(["git", "-C", str(root), "rev-parse", "--show-toplevel"],
|
||||
capture_output=True, text=True)
|
||||
if git.returncode == 0 and Path(git.stdout.strip()).resolve() == root:
|
||||
try:
|
||||
git = subprocess.run(["git", "-C", str(root), "rev-parse", "--show-toplevel"],
|
||||
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",
|
||||
"--others", "--exclude-standard", "-z"])
|
||||
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("--json", type=Path, help="write a report containing rule names and locations only")
|
||||
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)
|
||||
findings, files = scan_tree(root)
|
||||
commits = blobs = 0
|
||||
|
||||
@@ -7,9 +7,11 @@ import contextlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
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;"))
|
||||
|
||||
|
||||
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):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory(prefix="openbmh-guard-test-")
|
||||
|
||||
Reference in new issue
Block a user