Initial project source
这个提交包含在:
+56
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded syntax/secret validation; never scans history or invokes runtime."""
|
||||
import ast
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SHAPES = re.compile(r"-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----|AKIA[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{50,}|xox[baprs]-[A-Za-z0-9-]{20,}")
|
||||
def main(paths):
|
||||
if not paths:
|
||||
raise ValueError("provide explicit files")
|
||||
secrets = []
|
||||
env = ROOT / "script/datagov/.env.datagov"
|
||||
if env.is_file():
|
||||
for line in env.read_text().splitlines():
|
||||
key, sep, value = line.partition("=")
|
||||
if sep and re.search(r"PASSWORD|TOKEN|JWT_SECRET|IDENTITY_(KEY|VALUE)", key) and len(value.strip("'\"")) >= 12:
|
||||
secrets.append(value.strip("'\""))
|
||||
errors = []
|
||||
for name in dict.fromkeys(paths):
|
||||
p = ROOT / name
|
||||
if p.is_symlink() or not p.resolve().is_relative_to(ROOT) or not p.is_file():
|
||||
errors.append((name, "not a regular project file")); continue
|
||||
if ".local-backups" in p.parts or p.name == ".env.datagov":
|
||||
errors.append((name, "private file is not reviewable input")); continue
|
||||
try:
|
||||
data = p.read_text()
|
||||
if SHAPES.search(data) or any(value in data for value in secrets):
|
||||
errors.append((name, "secret detected; contents withheld")); continue
|
||||
if p.suffix == ".py":
|
||||
ast.parse(data, filename=name)
|
||||
elif p.suffix == ".json":
|
||||
json.loads(data)
|
||||
elif p.suffix in {".sh", ".rb"}:
|
||||
command = ["bash", "-n", str(p)] if p.suffix == ".sh" else ["ruby", "-c", str(p)]
|
||||
if subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode:
|
||||
errors.append((name, "syntax error"))
|
||||
except (UnicodeError, SyntaxError, ValueError):
|
||||
errors.append((name, "invalid syntax or text encoding"))
|
||||
for name, reason in errors:
|
||||
print(f"{name}: {reason}", file=sys.stderr)
|
||||
if errors:
|
||||
return 1
|
||||
print(f"Explicit file checks passed: {len(set(paths))} files")
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
sys.exit(main(sys.argv[1:]))
|
||||
except ValueError as exc:
|
||||
print(str(exc), file=sys.stderr)
|
||||
sys.exit(2)
|
||||
在新工单中引用
屏蔽一个用户