forked from mtusi2026/mtusi2026lab7
91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
"""Check submission files and all reachable Git objects against local secrets."""
|
|
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def strings(value):
|
|
if isinstance(value, dict):
|
|
for child in value.values():
|
|
yield from strings(child)
|
|
elif isinstance(value, list):
|
|
for child in value:
|
|
yield from strings(child)
|
|
elif isinstance(value, str) and len(value) >= 20:
|
|
yield value.encode()
|
|
|
|
|
|
def main():
|
|
known = set()
|
|
for directory in [ROOT / "secrets", ROOT / "operator"]:
|
|
for path in directory.rglob("*"):
|
|
if not path.is_file():
|
|
continue
|
|
raw = path.read_bytes().strip()
|
|
if path.suffix == ".json":
|
|
if path.name in {
|
|
"service-secrets.json",
|
|
"operator-token.json",
|
|
"secrets.json",
|
|
} or path.name.startswith("unseal-"):
|
|
known.update(strings(json.loads(raw)))
|
|
elif path.name == "s3-rotation.json":
|
|
journal = json.loads(raw)
|
|
known.update(strings(journal.get("previous_values", {})))
|
|
known.update(strings(journal.get("next_values", {})))
|
|
elif len(raw) >= 20 and (
|
|
path.suffix != ".pem" or "key" in path.name
|
|
):
|
|
known.add(raw)
|
|
|
|
def inspect(data):
|
|
assert not any(value in data for value in known), (
|
|
"Local private material was found in submission content"
|
|
)
|
|
assert b"/" + b"Users/" not in data, "Machine path in submission"
|
|
|
|
result = subprocess.check_output(
|
|
[
|
|
"git",
|
|
"ls-files",
|
|
"-z",
|
|
"--cached",
|
|
"--others",
|
|
"--exclude-standard",
|
|
],
|
|
cwd=ROOT,
|
|
)
|
|
files = 0
|
|
for name in result.split(b"\0"):
|
|
if name and (path := ROOT / name.decode()).is_file():
|
|
inspect(path.read_bytes())
|
|
files += 1
|
|
objects = subprocess.check_output(
|
|
["git", "rev-list", "--objects", "--all"], cwd=ROOT
|
|
).splitlines()
|
|
with subprocess.Popen(
|
|
["git", "cat-file", "--batch"],
|
|
cwd=ROOT,
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
) as process:
|
|
for line in objects:
|
|
process.stdin.write(line.split()[0] + b"\n")
|
|
process.stdin.flush()
|
|
header = process.stdout.readline().split()
|
|
assert len(header) == 3
|
|
inspect(process.stdout.read(int(header[2])))
|
|
assert process.stdout.read(1) == b"\n"
|
|
process.stdin.close()
|
|
assert process.wait(timeout=10) == 0
|
|
print(
|
|
f"PASS: {files} submission files, {len(objects)} reachable objects, {len(known)} local private values absent"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|