Mirror GitHub commit 954b20931bd23bf971072a5ec79424ac03b09d6b (committed 2026-09-12T11:35:28+03:00), pushed 2026-09-12T08:35:36Z
Browse files- MIRROR_PROVENANCE.md +4 -4
- hackathon-techex/.gitignore +7 -0
- hackathon-techex/README.md +52 -0
- hackathon-techex/README.md.TAG +5 -0
- hackathon-techex/README.md.sha256 +1 -0
- hackathon-techex/docs/BUILD_PLAN.md +2 -0
- hackathon-techex/pyproject.toml +20 -0
- hackathon-techex/run_demo.py +70 -0
- hackathon-techex/sipa_scan_ledger/__init__.py +26 -0
- hackathon-techex/sipa_scan_ledger/adapter.py +26 -0
- hackathon-techex/sipa_scan_ledger/chain.py +74 -0
- hackathon-techex/sipa_scan_ledger/diff.py +38 -0
- hackathon-techex/sipa_scan_ledger/record.py +61 -0
- hackathon-techex/sipa_scan_ledger/verify.py +56 -0
- hackathon-techex/tests/test_adapter.py +35 -0
- hackathon-techex/tests/test_chain.py +33 -0
- hackathon-techex/tests/test_diff.py +37 -0
- hackathon-techex/tests/test_record.py +27 -0
- hackathon-techex/tests/test_verify.py +38 -0
- scripts/hf_mirror_push.py +112 -0
MIRROR_PROVENANCE.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
|
| 3 |
This is not part of the archived repository -- it is written fresh on every push and carries no `.sha256` seal, deliberately. Every *other* file in this mirror is a byte-exact `git archive` of one GitHub commit, sealed at that commit; this file is the one exception, so that the file describing the copy never has to also be true about the copy's own past.
|
| 4 |
|
| 5 |
-
- **Source commit:** [`
|
| 6 |
-
- **Committed upstream:** 2026-09-
|
| 7 |
-
- **Pushed to this mirror:** 2026-09-
|
| 8 |
-
- **Method:** `git archive` of that exact commit, uploaded as one atomic commit via `
|
|
|
|
| 2 |
|
| 3 |
This is not part of the archived repository -- it is written fresh on every push and carries no `.sha256` seal, deliberately. Every *other* file in this mirror is a byte-exact `git archive` of one GitHub commit, sealed at that commit; this file is the one exception, so that the file describing the copy never has to also be true about the copy's own past.
|
| 4 |
|
| 5 |
+
- **Source commit:** [`954b20931bd23bf971072a5ec79424ac03b09d6b`](https://github.com/soulinpsyabstract/eilatsecure/commit/954b20931bd23bf971072a5ec79424ac03b09d6b)
|
| 6 |
+
- **Committed upstream:** 2026-09-12T11:35:28+03:00
|
| 7 |
+
- **Pushed to this mirror:** 2026-09-12T08:35:36Z
|
| 8 |
+
- **Method:** `git archive` of that exact commit, uploaded as one atomic commit via `scripts/hf_mirror_push.py`, which refuses to run on a dirty tree.
|
hackathon-techex/.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.egg-info/
|
| 4 |
+
.pytest_cache/
|
| 5 |
+
build/
|
| 6 |
+
dist/
|
| 7 |
+
scan_history.jsonl
|
hackathon-techex/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# hackathon-techex — Verifiable Scan History
|
| 2 |
+
|
| 3 |
+
New work for the [TechEx Amsterdam Hackathon](https://lablab.ai/ai-hackathons/techex-amsterdam-hackathon)
|
| 4 |
+
(cybersecurity track, online build 16–19 Oct 2026), built on top of the
|
| 5 |
+
existing live EilatSecure scanner — **does not modify** `scan.js` or
|
| 6 |
+
`scan_own_domains.py` at the repo root, only consumes what they already
|
| 7 |
+
produce.
|
| 8 |
+
|
| 9 |
+
Full concept and track status:
|
| 10 |
+
[docs/BUILD_PLAN.md](docs/BUILD_PLAN.md)
|
| 11 |
+
|
| 12 |
+
## The gap
|
| 13 |
+
|
| 14 |
+
A scan today is a one-off JSON blob: nothing proves it wasn't edited after
|
| 15 |
+
the fact, and comparing two scans of the same site means reading two files
|
| 16 |
+
by hand. Both are real gaps in a product that's already live.
|
| 17 |
+
|
| 18 |
+
## Quickstart (no network calls, no changes to the live scanner)
|
| 19 |
+
|
| 20 |
+
```bash
|
| 21 |
+
cd hackathon-techex
|
| 22 |
+
pip install -e ".[dev]"
|
| 23 |
+
python run_demo.py # example data, shaped exactly like the real scanner's output
|
| 24 |
+
pytest -q # 17 tests
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
from sipa_scan_ledger import ScanLedger, ScanRecord, Finding, diff_scans
|
| 29 |
+
|
| 30 |
+
ledger = ScanLedger("scan_history.jsonl")
|
| 31 |
+
ledger.append(ScanRecord("example.com", "2026-10-16T08:00:00Z",
|
| 32 |
+
[Finding("high", "No forced HTTPS redirect")]))
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
## Structure
|
| 36 |
+
|
| 37 |
+
| Module | What it does | Status |
|
| 38 |
+
| --- | --- | --- |
|
| 39 |
+
| `sipa_scan_ledger/record.py` | Fixed schema wrapping the scanner's existing finding format | built, tested |
|
| 40 |
+
| `sipa_scan_ledger/chain.py` + `verify.py` | Hash-chained history + tamper check | built, tested |
|
| 41 |
+
| `sipa_scan_ledger/diff.py` | What changed between two scans of the same target | built, tested |
|
| 42 |
+
| `sipa_scan_ledger/adapter.py` | Converts `scan_own_domains.py`'s real report shape into ledger entries | built, tested |
|
| 43 |
+
|
| 44 |
+
## What's still open (see the plan for skill-matched slots)
|
| 45 |
+
|
| 46 |
+
- Wire the adapter into a real scheduled run of `scan_own_domains.py`
|
| 47 |
+
- A diff view on the EilatSecure landing page, not just terminal output
|
| 48 |
+
- Demo video + lablab.ai submission
|
| 49 |
+
|
| 50 |
+
Team: solo on this hackathon (the only other teammate on the team page
|
| 51 |
+
opted out of TechEx Amsterdam specifically) — the tracks above are scoped
|
| 52 |
+
for anyone who joins, not written assuming they will.
|
hackathon-techex/README.md.TAG
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FILE=README.md
|
| 2 |
+
SHA256=d1c02d3a67a6e9affe2766d3ef920c1922b81bc8049f69dafe22bb7c02891ec6
|
| 3 |
+
SIZE=2134
|
| 4 |
+
DEVICE=SERVER
|
| 5 |
+
TS=2026-09-12__10-54-40
|
hackathon-techex/README.md.sha256
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
d1c02d3a67a6e9affe2766d3ef920c1922b81bc8049f69dafe22bb7c02891ec6 README.md
|
hackathon-techex/docs/BUILD_PLAN.md
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Full concept, what's built, and skill-matched open tracks:
|
| 2 |
+
https://claude.ai/code/artifact/dcb20301-1541-474b-89b4-9923d5269c9a
|
hackathon-techex/pyproject.toml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[build-system]
|
| 2 |
+
requires = ["setuptools>=61"]
|
| 3 |
+
build-backend = "setuptools.build_meta"
|
| 4 |
+
|
| 5 |
+
[project]
|
| 6 |
+
name = "sipa-scan-ledger"
|
| 7 |
+
version = "0.1.0"
|
| 8 |
+
description = "Verifiable, diffable scan history for EilatSecure - TechEx Amsterdam Hackathon entry"
|
| 9 |
+
requires-python = ">=3.10"
|
| 10 |
+
license = { text = "Apache-2.0" }
|
| 11 |
+
dependencies = []
|
| 12 |
+
|
| 13 |
+
[project.optional-dependencies]
|
| 14 |
+
dev = ["pytest>=7"]
|
| 15 |
+
|
| 16 |
+
[tool.setuptools]
|
| 17 |
+
packages = ["sipa_scan_ledger"]
|
| 18 |
+
|
| 19 |
+
[tool.pytest.ini_options]
|
| 20 |
+
testpaths = ["tests"]
|
hackathon-techex/run_demo.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""End-to-end demo: two example self-scan reports (the exact shape
|
| 2 |
+
scan_own_domains.py's main() already produces), fed through the ledger and
|
| 3 |
+
diffed. Example data, clearly marked as such - not a live scan.
|
| 4 |
+
|
| 5 |
+
python run_demo.py
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import tempfile
|
| 11 |
+
|
| 12 |
+
from sipa_scan_ledger.adapter import records_from_self_scan_report
|
| 13 |
+
from sipa_scan_ledger.chain import ScanLedger
|
| 14 |
+
from sipa_scan_ledger.diff import diff_scans
|
| 15 |
+
from sipa_scan_ledger.verify import verify
|
| 16 |
+
|
| 17 |
+
# EXAMPLE reports, shaped exactly like scan_own_domains.py's real output -
|
| 18 |
+
# not a live scan result.
|
| 19 |
+
REPORT_DAY_1 = {
|
| 20 |
+
"ran_at": "2026-10-16T08:00:00+00:00",
|
| 21 |
+
"targets": {
|
| 22 |
+
"sipa-os.org": [],
|
| 23 |
+
"focus.sipa-os.org": [
|
| 24 |
+
{"severity": "medium", "title": "Missing Content-Security-Policy"},
|
| 25 |
+
{"severity": "low", "title": "Missing Referrer-Policy"},
|
| 26 |
+
],
|
| 27 |
+
},
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
REPORT_DAY_8 = {
|
| 31 |
+
"ran_at": "2026-10-23T08:00:00+00:00",
|
| 32 |
+
"targets": {
|
| 33 |
+
"sipa-os.org": [
|
| 34 |
+
{"severity": "high", "title": "TLS cert expires in 6 days"},
|
| 35 |
+
],
|
| 36 |
+
"focus.sipa-os.org": [
|
| 37 |
+
{"severity": "low", "title": "Missing Referrer-Policy"},
|
| 38 |
+
],
|
| 39 |
+
},
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def main() -> None:
|
| 44 |
+
log_path = os.path.join(tempfile.mkdtemp(), "scan_history.jsonl")
|
| 45 |
+
ledger = ScanLedger(log_path)
|
| 46 |
+
|
| 47 |
+
ledger.append_many(records_from_self_scan_report(REPORT_DAY_1))
|
| 48 |
+
ledger.append_many(records_from_self_scan_report(REPORT_DAY_8))
|
| 49 |
+
|
| 50 |
+
print("== sipa-os.org: 8 days apart ==")
|
| 51 |
+
history = ledger.history_for("sipa-os.org")
|
| 52 |
+
d = diff_scans(history[0], history[1])
|
| 53 |
+
print(f" new findings: {d.new_findings}")
|
| 54 |
+
print(f" resolved findings: {d.resolved_findings}")
|
| 55 |
+
print(f" got_worse={d.got_worse()} (a cert expiring soon showed up between scans)")
|
| 56 |
+
|
| 57 |
+
print("\n== focus.sipa-os.org: 8 days apart ==")
|
| 58 |
+
history = ledger.history_for("focus.sipa-os.org")
|
| 59 |
+
d = diff_scans(history[0], history[1])
|
| 60 |
+
print(f" new findings: {d.new_findings}")
|
| 61 |
+
print(f" resolved findings: {d.resolved_findings}")
|
| 62 |
+
print(f" improved={d.improved()} (the CSP finding got fixed between scans)")
|
| 63 |
+
|
| 64 |
+
res = verify(log_path)
|
| 65 |
+
print(f"\nscan history: {res.count} entries - {'OK' if res.ok else 'FAIL: ' + str(res.problems)}")
|
| 66 |
+
print(f"log file: {log_path}")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
main()
|
hackathon-techex/sipa_scan_ledger/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""sipa_scan_ledger: verifiable, diffable history for EilatSecure's passive
|
| 2 |
+
scans. Built for TechEx Amsterdam Hackathon (cybersecurity track) as new
|
| 3 |
+
work on top of the existing, live EilatSecure scanner - it does not modify
|
| 4 |
+
scan.js or scan_own_domains.py, only consumes what they already produce.
|
| 5 |
+
|
| 6 |
+
Today a scan is a one-off JSON blob: no proof it wasn't edited after the
|
| 7 |
+
fact, and no way to see what changed since the last scan of the same target
|
| 8 |
+
without diffing two files by hand. This adds both, without touching the
|
| 9 |
+
scanner itself.
|
| 10 |
+
"""
|
| 11 |
+
from .record import ScanRecord, Finding
|
| 12 |
+
from .chain import ScanLedger
|
| 13 |
+
from .verify import verify, VerifyResult
|
| 14 |
+
from .diff import diff_scans, ScanDiff
|
| 15 |
+
from .adapter import records_from_self_scan_report
|
| 16 |
+
|
| 17 |
+
__all__ = [
|
| 18 |
+
"ScanRecord",
|
| 19 |
+
"Finding",
|
| 20 |
+
"ScanLedger",
|
| 21 |
+
"verify",
|
| 22 |
+
"VerifyResult",
|
| 23 |
+
"diff_scans",
|
| 24 |
+
"ScanDiff",
|
| 25 |
+
"records_from_self_scan_report",
|
| 26 |
+
]
|
hackathon-techex/sipa_scan_ledger/adapter.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Bridges this module to the existing, unmodified scan_own_domains.py.
|
| 2 |
+
|
| 3 |
+
That script's main() builds and prints exactly this shape:
|
| 4 |
+
|
| 5 |
+
{"ran_at": "<iso8601>", "targets": {"<host>": [{"severity", "title", ...}, ...], ...}}
|
| 6 |
+
|
| 7 |
+
(see scan_own_domains.py's own main(), read directly from the repo before
|
| 8 |
+
writing this - not guessed). This adapter turns that report dict into one
|
| 9 |
+
ScanRecord per host. It does not import or call scan_own_domains.py itself,
|
| 10 |
+
so this track can be built and tested without touching the existing script.
|
| 11 |
+
"""
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
from .record import Finding, ScanRecord
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def records_from_self_scan_report(report: dict) -> list[ScanRecord]:
|
| 18 |
+
ran_at = report["ran_at"]
|
| 19 |
+
records = []
|
| 20 |
+
for host, findings in report.get("targets", {}).items():
|
| 21 |
+
records.append(ScanRecord(
|
| 22 |
+
target=host,
|
| 23 |
+
scanned_at=ran_at,
|
| 24 |
+
findings=[Finding.from_dict(f) for f in findings],
|
| 25 |
+
))
|
| 26 |
+
return records
|
hackathon-techex/sipa_scan_ledger/chain.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Append-only, hash-chained history of scans. A business owner - or an
|
| 2 |
+
auditor, or a customer deciding whether to trust the report - can verify
|
| 3 |
+
this file was not edited after the fact, without trusting whoever ran the
|
| 4 |
+
scan."""
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
import hashlib
|
| 8 |
+
import json
|
| 9 |
+
import time
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
from .record import ScanRecord
|
| 13 |
+
|
| 14 |
+
GENESIS = "0" * 64
|
| 15 |
+
_PAYLOAD_KEYS = ("seq", "ts", "target", "scanned_at", "findings")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _canonical(obj) -> bytes:
|
| 19 |
+
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def compute_hash(prev_hash: str, payload: dict) -> str:
|
| 23 |
+
h = hashlib.sha256()
|
| 24 |
+
h.update(prev_hash.encode("ascii"))
|
| 25 |
+
h.update(_canonical({k: payload.get(k) for k in _PAYLOAD_KEYS}))
|
| 26 |
+
return h.hexdigest()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class ScanLedger:
|
| 30 |
+
def __init__(self, path: str | Path):
|
| 31 |
+
self.path = Path(path)
|
| 32 |
+
self._last_hash = GENESIS
|
| 33 |
+
self._seq = 0
|
| 34 |
+
if self.path.exists():
|
| 35 |
+
self._replay()
|
| 36 |
+
|
| 37 |
+
def _replay(self) -> None:
|
| 38 |
+
for line in self.path.read_text(encoding="utf-8").splitlines():
|
| 39 |
+
line = line.strip()
|
| 40 |
+
if not line:
|
| 41 |
+
continue
|
| 42 |
+
rec = json.loads(line)
|
| 43 |
+
self._last_hash = rec["hash"]
|
| 44 |
+
self._seq = rec["seq"] + 1
|
| 45 |
+
|
| 46 |
+
def append(self, record: ScanRecord) -> dict:
|
| 47 |
+
payload = {
|
| 48 |
+
"seq": self._seq,
|
| 49 |
+
"ts": time.time(),
|
| 50 |
+
"target": record.target,
|
| 51 |
+
"scanned_at": record.scanned_at,
|
| 52 |
+
"findings": [f.to_dict() for f in record.findings],
|
| 53 |
+
}
|
| 54 |
+
entry_hash = compute_hash(self._last_hash, payload)
|
| 55 |
+
entry = {**payload, "prev_hash": self._last_hash, "hash": entry_hash}
|
| 56 |
+
with self.path.open("a", encoding="utf-8") as f:
|
| 57 |
+
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
| 58 |
+
self._last_hash = entry_hash
|
| 59 |
+
self._seq += 1
|
| 60 |
+
return entry
|
| 61 |
+
|
| 62 |
+
def append_many(self, records: list[ScanRecord]) -> list[dict]:
|
| 63 |
+
return [self.append(r) for r in records]
|
| 64 |
+
|
| 65 |
+
def entries(self) -> list[dict]:
|
| 66 |
+
if not self.path.exists():
|
| 67 |
+
return []
|
| 68 |
+
return [json.loads(l) for l in self.path.read_text(encoding="utf-8").splitlines() if l.strip()]
|
| 69 |
+
|
| 70 |
+
def history_for(self, target: str) -> list[ScanRecord]:
|
| 71 |
+
"""All scans of one target, in chain order - what diff_scans() compares."""
|
| 72 |
+
return [
|
| 73 |
+
ScanRecord.from_dict(e) for e in self.entries() if e["target"] == target
|
| 74 |
+
]
|
hackathon-techex/sipa_scan_ledger/diff.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""What changed between two scans of the same target. Matches findings by
|
| 2 |
+
title (the same check reported twice, same wording, from the same fixed
|
| 3 |
+
check list scan_own_domains.py / scan.js run) - a human reading this doesn't
|
| 4 |
+
re-read two full reports side by side, they read what's different."""
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
from dataclasses import dataclass, field
|
| 8 |
+
|
| 9 |
+
from .record import ScanRecord
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class ScanDiff:
|
| 14 |
+
target: str
|
| 15 |
+
new_findings: list[dict] = field(default_factory=list) # appeared since last scan
|
| 16 |
+
resolved_findings: list[dict] = field(default_factory=list) # gone since last scan
|
| 17 |
+
unchanged_findings: list[dict] = field(default_factory=list)
|
| 18 |
+
|
| 19 |
+
def got_worse(self) -> bool:
|
| 20 |
+
return len(self.new_findings) > 0
|
| 21 |
+
|
| 22 |
+
def improved(self) -> bool:
|
| 23 |
+
return len(self.resolved_findings) > 0 and not self.new_findings
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def diff_scans(before: ScanRecord, after: ScanRecord) -> ScanDiff:
|
| 27 |
+
if before.target != after.target:
|
| 28 |
+
raise ValueError(f"diffing scans of different targets: {before.target!r} vs {after.target!r}")
|
| 29 |
+
|
| 30 |
+
before_titles = {f.title: f for f in before.findings}
|
| 31 |
+
after_titles = {f.title: f for f in after.findings}
|
| 32 |
+
|
| 33 |
+
new = [after_titles[t].to_dict() for t in after_titles.keys() - before_titles.keys()]
|
| 34 |
+
resolved = [before_titles[t].to_dict() for t in before_titles.keys() - after_titles.keys()]
|
| 35 |
+
unchanged = [after_titles[t].to_dict() for t in after_titles.keys() & before_titles.keys()]
|
| 36 |
+
|
| 37 |
+
return ScanDiff(target=after.target, new_findings=new, resolved_findings=resolved,
|
| 38 |
+
unchanged_findings=unchanged)
|
hackathon-techex/sipa_scan_ledger/record.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The fixed schema a scan turns into. Matches the real shape
|
| 2 |
+
`scan_own_domains.py` and `functions/api/scan.js` already produce -
|
| 3 |
+
`{severity, title}` findings per target - this module doesn't invent a new
|
| 4 |
+
finding format, it wraps the existing one so it can be chained and diffed.
|
| 5 |
+
"""
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from dataclasses import dataclass, field
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass(frozen=True)
|
| 12 |
+
class Finding:
|
| 13 |
+
severity: str # "critical" | "high" | "medium" | "low" - as scan_own_domains.py emits
|
| 14 |
+
title: str
|
| 15 |
+
detail: str = ""
|
| 16 |
+
|
| 17 |
+
@classmethod
|
| 18 |
+
def from_dict(cls, d: dict) -> "Finding":
|
| 19 |
+
return cls(severity=d["severity"], title=d["title"], detail=d.get("detail", ""))
|
| 20 |
+
|
| 21 |
+
def to_dict(self) -> dict:
|
| 22 |
+
d = {"severity": self.severity, "title": self.title}
|
| 23 |
+
if self.detail:
|
| 24 |
+
d["detail"] = self.detail
|
| 25 |
+
return d
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
_SEVERITY_ORDER = {"critical": 3, "high": 2, "medium": 1, "low": 0}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@dataclass
|
| 32 |
+
class ScanRecord:
|
| 33 |
+
target: str
|
| 34 |
+
scanned_at: str # ISO 8601, as produced by scan_own_domains.py / scan.js
|
| 35 |
+
findings: list[Finding] = field(default_factory=list)
|
| 36 |
+
|
| 37 |
+
def worst_severity(self) -> str | None:
|
| 38 |
+
if not self.findings:
|
| 39 |
+
return None
|
| 40 |
+
return max(self.findings, key=lambda f: _SEVERITY_ORDER.get(f.severity, -1)).severity
|
| 41 |
+
|
| 42 |
+
def severity_counts(self) -> dict:
|
| 43 |
+
counts = {"critical": 0, "high": 0, "medium": 0, "low": 0}
|
| 44 |
+
for f in self.findings:
|
| 45 |
+
counts[f.severity] = counts.get(f.severity, 0) + 1
|
| 46 |
+
return counts
|
| 47 |
+
|
| 48 |
+
def to_dict(self) -> dict:
|
| 49 |
+
return {
|
| 50 |
+
"target": self.target,
|
| 51 |
+
"scanned_at": self.scanned_at,
|
| 52 |
+
"findings": [f.to_dict() for f in self.findings],
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
@classmethod
|
| 56 |
+
def from_dict(cls, d: dict) -> "ScanRecord":
|
| 57 |
+
return cls(
|
| 58 |
+
target=d["target"],
|
| 59 |
+
scanned_at=d["scanned_at"],
|
| 60 |
+
findings=[Finding.from_dict(f) for f in d.get("findings", [])],
|
| 61 |
+
)
|
hackathon-techex/sipa_scan_ledger/verify.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Recompute the hash chain from scratch. Run it against a ledger file you
|
| 2 |
+
did not produce and it still tells you whether the record is intact."""
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import json
|
| 6 |
+
import sys
|
| 7 |
+
from dataclasses import dataclass, field
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
from .chain import GENESIS, _PAYLOAD_KEYS, compute_hash
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class VerifyResult:
|
| 15 |
+
ok: bool
|
| 16 |
+
count: int
|
| 17 |
+
problems: list[str] = field(default_factory=list)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def verify(path: str | Path) -> VerifyResult:
|
| 21 |
+
path = Path(path)
|
| 22 |
+
if not path.exists():
|
| 23 |
+
return VerifyResult(False, 0, [f"no such log: {path}"])
|
| 24 |
+
problems: list[str] = []
|
| 25 |
+
entries = [json.loads(l) for l in path.read_text(encoding="utf-8").splitlines() if l.strip()]
|
| 26 |
+
prev = GENESIS
|
| 27 |
+
for i, e in enumerate(entries):
|
| 28 |
+
if e.get("seq") != i:
|
| 29 |
+
problems.append(f"entry {i}: seq is {e.get('seq')}, expected {i}")
|
| 30 |
+
if e.get("prev_hash") != prev:
|
| 31 |
+
problems.append(f"entry {i}: chain broken")
|
| 32 |
+
expected = compute_hash(e.get("prev_hash", ""), {k: e.get(k) for k in _PAYLOAD_KEYS})
|
| 33 |
+
if e.get("hash") != expected:
|
| 34 |
+
problems.append(f"entry {i}: hash mismatch (altered)")
|
| 35 |
+
prev = e.get("hash", "")
|
| 36 |
+
return VerifyResult(ok=not problems, count=len(entries), problems=problems)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def main(argv: list[str] | None = None) -> int:
|
| 40 |
+
argv = list(sys.argv[1:] if argv is None else argv)
|
| 41 |
+
if not argv:
|
| 42 |
+
print("usage: python -m sipa_scan_ledger.verify <ledger.jsonl>")
|
| 43 |
+
return 2
|
| 44 |
+
res = verify(argv[0])
|
| 45 |
+
print(f"entries: {res.count}")
|
| 46 |
+
if res.ok:
|
| 47 |
+
print("OK - chain intact, nothing altered")
|
| 48 |
+
return 0
|
| 49 |
+
print("FAIL:")
|
| 50 |
+
for p in res.problems:
|
| 51 |
+
print(f" - {p}")
|
| 52 |
+
return 1
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
raise SystemExit(main())
|
hackathon-techex/tests/test_adapter.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sipa_scan_ledger.adapter import records_from_self_scan_report
|
| 2 |
+
|
| 3 |
+
# Shape copied from scan_own_domains.py's own main(): report = {"ran_at": ..., "targets": {host: findings}}
|
| 4 |
+
SAMPLE_REPORT = {
|
| 5 |
+
"ran_at": "2026-10-16T08:00:00+00:00",
|
| 6 |
+
"targets": {
|
| 7 |
+
"sipa-os.org": [],
|
| 8 |
+
"focus.sipa-os.org": [
|
| 9 |
+
{"severity": "medium", "title": "Missing Content-Security-Policy"},
|
| 10 |
+
],
|
| 11 |
+
"shell.sipa-os.org": [
|
| 12 |
+
{"severity": "critical", "title": "Unreachable over HTTPS", "detail": "timed out"},
|
| 13 |
+
],
|
| 14 |
+
},
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_one_record_per_target():
|
| 19 |
+
records = records_from_self_scan_report(SAMPLE_REPORT)
|
| 20 |
+
assert len(records) == 3
|
| 21 |
+
targets = {r.target for r in records}
|
| 22 |
+
assert targets == {"sipa-os.org", "focus.sipa-os.org", "shell.sipa-os.org"}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_scanned_at_taken_from_ran_at():
|
| 26 |
+
records = records_from_self_scan_report(SAMPLE_REPORT)
|
| 27 |
+
assert all(r.scanned_at == "2026-10-16T08:00:00+00:00" for r in records)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_findings_preserved():
|
| 31 |
+
records = records_from_self_scan_report(SAMPLE_REPORT)
|
| 32 |
+
by_target = {r.target: r for r in records}
|
| 33 |
+
assert by_target["shell.sipa-os.org"].findings[0].severity == "critical"
|
| 34 |
+
assert by_target["shell.sipa-os.org"].findings[0].detail == "timed out"
|
| 35 |
+
assert by_target["sipa-os.org"].findings == []
|
hackathon-techex/tests/test_chain.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sipa_scan_ledger.chain import GENESIS, ScanLedger
|
| 2 |
+
from sipa_scan_ledger.record import Finding, ScanRecord
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def test_chain_links(tmp_path):
|
| 6 |
+
log = ScanLedger(tmp_path / "l.jsonl")
|
| 7 |
+
e1 = log.append(ScanRecord("a.com", "t1", [Finding("low", "x")]))
|
| 8 |
+
e2 = log.append(ScanRecord("b.com", "t2", []))
|
| 9 |
+
assert e1["prev_hash"] == GENESIS
|
| 10 |
+
assert e2["prev_hash"] == e1["hash"]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_reopen_continues_sequence(tmp_path):
|
| 14 |
+
p = tmp_path / "l.jsonl"
|
| 15 |
+
log = ScanLedger(p)
|
| 16 |
+
log.append(ScanRecord("a.com", "t1", []))
|
| 17 |
+
last = log.append(ScanRecord("a.com", "t2", []))
|
| 18 |
+
|
| 19 |
+
reopened = ScanLedger(p)
|
| 20 |
+
nxt = reopened.append(ScanRecord("a.com", "t3", []))
|
| 21 |
+
assert nxt["seq"] == 2
|
| 22 |
+
assert nxt["prev_hash"] == last["hash"]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_history_for_filters_by_target(tmp_path):
|
| 26 |
+
log = ScanLedger(tmp_path / "l.jsonl")
|
| 27 |
+
log.append(ScanRecord("a.com", "t1", [Finding("low", "x")]))
|
| 28 |
+
log.append(ScanRecord("b.com", "t1", [Finding("high", "y")]))
|
| 29 |
+
log.append(ScanRecord("a.com", "t2", []))
|
| 30 |
+
|
| 31 |
+
history = log.history_for("a.com")
|
| 32 |
+
assert len(history) == 2
|
| 33 |
+
assert [r.scanned_at for r in history] == ["t1", "t2"]
|
hackathon-techex/tests/test_diff.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from sipa_scan_ledger.diff import diff_scans
|
| 4 |
+
from sipa_scan_ledger.record import Finding, ScanRecord
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_new_finding_detected():
|
| 8 |
+
before = ScanRecord("a.com", "t1", [Finding("low", "Missing HSTS")])
|
| 9 |
+
after = ScanRecord("a.com", "t2", [Finding("low", "Missing HSTS"), Finding("high", "No forced HTTPS redirect")])
|
| 10 |
+
d = diff_scans(before, after)
|
| 11 |
+
assert len(d.new_findings) == 1
|
| 12 |
+
assert d.new_findings[0]["title"] == "No forced HTTPS redirect"
|
| 13 |
+
assert d.got_worse()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_resolved_finding_detected():
|
| 17 |
+
before = ScanRecord("a.com", "t1", [Finding("high", "No forced HTTPS redirect")])
|
| 18 |
+
after = ScanRecord("a.com", "t2", [])
|
| 19 |
+
d = diff_scans(before, after)
|
| 20 |
+
assert len(d.resolved_findings) == 1
|
| 21 |
+
assert d.improved()
|
| 22 |
+
assert not d.got_worse()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_unchanged_findings_tracked():
|
| 26 |
+
before = ScanRecord("a.com", "t1", [Finding("low", "Missing HSTS")])
|
| 27 |
+
after = ScanRecord("a.com", "t2", [Finding("low", "Missing HSTS")])
|
| 28 |
+
d = diff_scans(before, after)
|
| 29 |
+
assert len(d.unchanged_findings) == 1
|
| 30 |
+
assert not d.new_findings and not d.resolved_findings
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def test_different_targets_raises():
|
| 34 |
+
before = ScanRecord("a.com", "t1", [])
|
| 35 |
+
after = ScanRecord("b.com", "t2", [])
|
| 36 |
+
with pytest.raises(ValueError):
|
| 37 |
+
diff_scans(before, after)
|
hackathon-techex/tests/test_record.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sipa_scan_ledger.record import Finding, ScanRecord
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_roundtrip():
|
| 5 |
+
r = ScanRecord("example.com", "2026-10-16T00:00:00Z",
|
| 6 |
+
[Finding("high", "No forced HTTPS redirect")])
|
| 7 |
+
d = r.to_dict()
|
| 8 |
+
back = ScanRecord.from_dict(d)
|
| 9 |
+
assert back == r
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_worst_severity():
|
| 13 |
+
r = ScanRecord("x", "t", [Finding("low", "a"), Finding("critical", "b"), Finding("medium", "c")])
|
| 14 |
+
assert r.worst_severity() == "critical"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_worst_severity_none_when_clean():
|
| 18 |
+
r = ScanRecord("x", "t", [])
|
| 19 |
+
assert r.worst_severity() is None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def test_severity_counts():
|
| 23 |
+
r = ScanRecord("x", "t", [Finding("low", "a"), Finding("low", "b"), Finding("high", "c")])
|
| 24 |
+
counts = r.severity_counts()
|
| 25 |
+
assert counts["low"] == 2
|
| 26 |
+
assert counts["high"] == 1
|
| 27 |
+
assert counts["critical"] == 0
|
hackathon-techex/tests/test_verify.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
from sipa_scan_ledger.chain import ScanLedger
|
| 4 |
+
from sipa_scan_ledger.record import Finding, ScanRecord
|
| 5 |
+
from sipa_scan_ledger.verify import verify
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _populate(path):
|
| 9 |
+
log = ScanLedger(path)
|
| 10 |
+
log.append(ScanRecord("a.com", "t1", [Finding("high", "No forced HTTPS redirect")]))
|
| 11 |
+
log.append(ScanRecord("a.com", "t2", []))
|
| 12 |
+
return log
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def test_clean_ledger_verifies(tmp_path):
|
| 16 |
+
p = tmp_path / "l.jsonl"
|
| 17 |
+
_populate(p)
|
| 18 |
+
res = verify(p)
|
| 19 |
+
assert res.ok
|
| 20 |
+
assert res.count == 2
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_missing_file(tmp_path):
|
| 24 |
+
res = verify(tmp_path / "nope.jsonl")
|
| 25 |
+
assert not res.ok
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_altered_entry_is_caught(tmp_path):
|
| 29 |
+
p = tmp_path / "l.jsonl"
|
| 30 |
+
_populate(p)
|
| 31 |
+
lines = p.read_text().splitlines()
|
| 32 |
+
tampered = json.loads(lines[0])
|
| 33 |
+
tampered["findings"] = [] # quietly erase the finding
|
| 34 |
+
lines[0] = json.dumps(tampered)
|
| 35 |
+
p.write_text("\n".join(lines) + "\n")
|
| 36 |
+
res = verify(p)
|
| 37 |
+
assert not res.ok
|
| 38 |
+
assert any("altered" in x for x in res.problems)
|
scripts/hf_mirror_push.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""hf_mirror_push -- same convention as sipa-os-governance/scripts/hf_mirror_push.py,
|
| 3 |
+
adapted for this repo (no dataset-specific citation checkers here to run against
|
| 4 |
+
the archived tree; eilatsecure has none).
|
| 5 |
+
|
| 6 |
+
It:
|
| 7 |
+
1. Refuses to run if `git status --porcelain` is not empty.
|
| 8 |
+
2. Resolves HEAD to a single sha and archives *that exact commit's tree*
|
| 9 |
+
via `git archive`, not the working directory.
|
| 10 |
+
3. Writes that sha (plus UTC push time) into MIRROR_PROVENANCE.md -- a
|
| 11 |
+
file that does NOT come out of the archive and carries no .sha256 of
|
| 12 |
+
its own, so the seal never has to be true about the mirror's own past.
|
| 13 |
+
4. Uploads the archived tree plus that one added file as ONE atomic
|
| 14 |
+
commit via upload_folder, with delete_patterns=["*"] so a file
|
| 15 |
+
deleted upstream also disappears from the mirror.
|
| 16 |
+
|
| 17 |
+
Requires HF_TOKEN in the environment and huggingface_hub installed.
|
| 18 |
+
"""
|
| 19 |
+
import os
|
| 20 |
+
import subprocess
|
| 21 |
+
import sys
|
| 22 |
+
import tarfile
|
| 23 |
+
import tempfile
|
| 24 |
+
from datetime import datetime, timezone
|
| 25 |
+
from pathlib import Path
|
| 26 |
+
|
| 27 |
+
REPO_ROOT = Path(__file__).resolve().parent.parent
|
| 28 |
+
HF_REPO_ID = "SoulInPsyAbstract/eilatsecure"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def run(cmd: list[str], **kw) -> str:
|
| 32 |
+
return subprocess.run(cmd, cwd=REPO_ROOT, capture_output=True, text=True, check=True, **kw).stdout.strip()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def refuse_if_dirty() -> None:
|
| 36 |
+
status = run(["git", "status", "--porcelain"])
|
| 37 |
+
if status:
|
| 38 |
+
print("[hf_mirror_push] REFUSED: working tree is dirty, not pushing a mixed-moment mirror.")
|
| 39 |
+
print(status)
|
| 40 |
+
sys.exit(1)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def current_sha() -> str:
|
| 44 |
+
return run(["git", "rev-parse", "HEAD"])
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def commit_timestamp(sha: str) -> str:
|
| 48 |
+
return run(["git", "show", "-s", "--format=%cI", sha])
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def archive_commit_to(sha: str, dest: Path) -> None:
|
| 52 |
+
dest.mkdir(parents=True, exist_ok=True)
|
| 53 |
+
archive_path = dest.parent / f"{sha}.tar"
|
| 54 |
+
with open(archive_path, "wb") as f:
|
| 55 |
+
subprocess.run(["git", "archive", "--format=tar", sha], cwd=REPO_ROOT, stdout=f, check=True)
|
| 56 |
+
with tarfile.open(archive_path) as tar:
|
| 57 |
+
tar.extractall(dest, filter="data")
|
| 58 |
+
archive_path.unlink()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def write_provenance_file(tree: Path, sha: str, commit_ts: str) -> str:
|
| 62 |
+
pushed_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 63 |
+
content = (
|
| 64 |
+
"# Mirror provenance\n\n"
|
| 65 |
+
"This is not part of the archived repository -- it is written fresh on every "
|
| 66 |
+
"push and carries no `.sha256` seal, deliberately. Every *other* file in this "
|
| 67 |
+
"mirror is a byte-exact `git archive` of one GitHub commit, sealed at that "
|
| 68 |
+
"commit; this file is the one exception, so that the file describing the copy "
|
| 69 |
+
"never has to also be true about the copy's own past.\n\n"
|
| 70 |
+
f"- **Source commit:** [`{sha}`](https://github.com/soulinpsyabstract/eilatsecure/commit/{sha})\n"
|
| 71 |
+
f"- **Committed upstream:** {commit_ts}\n"
|
| 72 |
+
f"- **Pushed to this mirror:** {pushed_at}\n"
|
| 73 |
+
"- **Method:** `git archive` of that exact commit, uploaded as one atomic "
|
| 74 |
+
"commit via `scripts/hf_mirror_push.py`, which refuses to run on a dirty tree.\n"
|
| 75 |
+
)
|
| 76 |
+
(tree / "MIRROR_PROVENANCE.md").write_text(content)
|
| 77 |
+
return f"Mirror GitHub commit {sha} (committed {commit_ts}), pushed {pushed_at}"
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def main() -> int:
|
| 81 |
+
refuse_if_dirty()
|
| 82 |
+
sha = current_sha()
|
| 83 |
+
commit_ts = commit_timestamp(sha)
|
| 84 |
+
print(f"[hf_mirror_push] pushing commit {sha} ({commit_ts})")
|
| 85 |
+
|
| 86 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 87 |
+
tree = Path(tmp) / "tree"
|
| 88 |
+
archive_commit_to(sha, tree)
|
| 89 |
+
|
| 90 |
+
commit_msg = write_provenance_file(tree, sha, commit_ts)
|
| 91 |
+
|
| 92 |
+
from huggingface_hub import HfApi
|
| 93 |
+
|
| 94 |
+
token = os.environ.get("HF_TOKEN") or os.environ.get("HF_TOKEN_GRAND")
|
| 95 |
+
if not token:
|
| 96 |
+
print("[hf_mirror_push] REFUSED: no HF_TOKEN in environment.")
|
| 97 |
+
return 1
|
| 98 |
+
|
| 99 |
+
api = HfApi(token=token)
|
| 100 |
+
api.upload_folder(
|
| 101 |
+
folder_path=str(tree),
|
| 102 |
+
repo_id=HF_REPO_ID,
|
| 103 |
+
repo_type="dataset",
|
| 104 |
+
commit_message=commit_msg,
|
| 105 |
+
delete_patterns=["*"],
|
| 106 |
+
)
|
| 107 |
+
print(f"[hf_mirror_push] DONE: mirror now projects commit {sha}, single atomic push.")
|
| 108 |
+
return 0
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
sys.exit(main())
|