52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
"""Verify pinned frontend/core source bytes; never import or execute upstream code."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
from urllib.request import urlopen
|
|
|
|
|
|
def verify(directory: Path, fetch: bool = False) -> dict:
|
|
manifest = json.loads((directory / "sources.json").read_text())
|
|
verified = []
|
|
for source in manifest["sources"]:
|
|
path = directory / source["local_file"]
|
|
if fetch:
|
|
with urlopen(source["raw_url"], timeout=30) as response:
|
|
raw = response.read()
|
|
else:
|
|
raw = path.read_bytes()
|
|
if hashlib.sha256(raw).hexdigest() != source["sha256"]:
|
|
raise ValueError(f"哈希不一致:{source['id']}")
|
|
lines = raw.decode("utf-8").splitlines()
|
|
for start, end in source["lines"]:
|
|
if not 1 <= start <= end <= len(lines):
|
|
raise ValueError(f"行号越界:{source['id']}")
|
|
if fetch:
|
|
path.write_bytes(raw)
|
|
verified.append({"id": source["id"], "sha256": source["sha256"]})
|
|
return {
|
|
"authority": "public_reference_only",
|
|
"sources_verified": verified,
|
|
"upstream_code_executed": False,
|
|
"target_version_match": "unknown",
|
|
}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--source-dir", type=Path, required=True)
|
|
parser.add_argument("--fetch", action="store_true")
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
report = verify(args.source_dir, args.fetch)
|
|
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
|
print(f"verified {len(report['sources_verified'])} sources; no upstream execution")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|