Spaces:
Running
Running
Add honest deployment-source attestation
#2
by betterwithage - opened
- README.md +4 -0
- server.py +40 -0
- szl_source_attestation.py +98 -0
- tests/test_source_attestation.py +57 -0
README.md
CHANGED
|
@@ -79,6 +79,10 @@ Part of the **SZL Holdings** governed-AI estate — *governed AI you can prove*:
|
|
| 79 |
|
| 80 |
- **Flagship:** [a11oy command console → a-11-oy.com](https://a-11-oy.com)
|
| 81 |
- **Orgs:** [GitHub · szl-holdings](https://github.com/szl-holdings) · [Hugging Face · SZLHOLDINGS](https://huggingface.co/SZLHOLDINGS)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
- **Related Spaces:** [◇ holographic](https://huggingface.co/spaces/SZLHOLDINGS/holographic) · [🫀 anatomy](https://huggingface.co/spaces/SZLHOLDINGS/anatomy) · [🦅 killinchu](https://huggingface.co/spaces/SZLHOLDINGS/killinchu)
|
| 83 |
|
| 84 |
**Status:** responding as of 2026-07-09 (HF Space root probe, this session).
|
|
|
|
| 79 |
|
| 80 |
- **Flagship:** [a11oy command console → a-11-oy.com](https://a-11-oy.com)
|
| 81 |
- **Orgs:** [GitHub · szl-holdings](https://github.com/szl-holdings) · [Hugging Face · SZLHOLDINGS](https://huggingface.co/SZLHOLDINGS)
|
| 82 |
+
|
| 83 |
+
## Deployment-source attestation
|
| 84 |
+
|
| 85 |
+
`GET /.well-known/szl-source.json` reports the measured Hugging Face deployment revision. No verifiable direct GitHub source is currently declared, so the source relation remains `UNKNOWN_SOURCE_RELATION`; GitHub parity and reproducible builds are explicitly `NOT_CLAIMED`.
|
| 86 |
- **Related Spaces:** [◇ holographic](https://huggingface.co/spaces/SZLHOLDINGS/holographic) · [🫀 anatomy](https://huggingface.co/spaces/SZLHOLDINGS/anatomy) · [🦅 killinchu](https://huggingface.co/spaces/SZLHOLDINGS/killinchu)
|
| 87 |
|
| 88 |
**Status:** responding as of 2026-07-09 (HF Space root probe, this session).
|
server.py
CHANGED
|
@@ -17,10 +17,23 @@ verify fetches to a11oy / killinchu / a-11-oy.com), so the hologram and the live
|
|
| 17 |
HUD keep working. Everything else is 'self' — zero CDN.
|
| 18 |
"""
|
| 19 |
import functools
|
|
|
|
|
|
|
| 20 |
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
| 21 |
|
|
|
|
|
|
|
| 22 |
PORT = 7860
|
| 23 |
DIRECTORY = "/app"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
CONTENT_SECURITY_POLICY = (
|
| 26 |
"default-src 'self'; "
|
|
@@ -37,6 +50,33 @@ CONTENT_SECURITY_POLICY = (
|
|
| 37 |
|
| 38 |
|
| 39 |
class HardenedHandler(SimpleHTTPRequestHandler):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def end_headers(self):
|
| 41 |
self.send_header("Content-Security-Policy", CONTENT_SECURITY_POLICY)
|
| 42 |
self.send_header(
|
|
|
|
| 17 |
HUD keep working. Everything else is 'self' — zero CDN.
|
| 18 |
"""
|
| 19 |
import functools
|
| 20 |
+
import json
|
| 21 |
+
import urllib.parse
|
| 22 |
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
| 23 |
|
| 24 |
+
from szl_source_attestation import build_attestation
|
| 25 |
+
|
| 26 |
PORT = 7860
|
| 27 |
DIRECTORY = "/app"
|
| 28 |
+
SPACE_ID = "SZLHOLDINGS/cosmos"
|
| 29 |
+
SOURCE_OBSERVATION = {
|
| 30 |
+
"repository": None,
|
| 31 |
+
"commit": None,
|
| 32 |
+
"path": None,
|
| 33 |
+
"relation": "no-verifiable-direct-source-observed",
|
| 34 |
+
"state": "UNKNOWN",
|
| 35 |
+
"evidence_url": None,
|
| 36 |
+
}
|
| 37 |
|
| 38 |
CONTENT_SECURITY_POLICY = (
|
| 39 |
"default-src 'self'; "
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
class HardenedHandler(SimpleHTTPRequestHandler):
|
| 53 |
+
def _send_json(self, payload):
|
| 54 |
+
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
| 55 |
+
self.send_response(200)
|
| 56 |
+
self.send_header("Content-Type", "application/json; charset=utf-8")
|
| 57 |
+
self.send_header("Content-Length", str(len(body)))
|
| 58 |
+
self.send_header("Cache-Control", "no-store")
|
| 59 |
+
self.send_header("X-SZL-Transport-State", str(payload["transport_state"]))
|
| 60 |
+
self.send_header("X-SZL-Evidence-State", str(payload["evidence_state"]))
|
| 61 |
+
self.send_header("X-SZL-Verification-State", str(payload["verification_state"]))
|
| 62 |
+
self.end_headers()
|
| 63 |
+
self.wfile.write(body)
|
| 64 |
+
|
| 65 |
+
def do_GET(self):
|
| 66 |
+
parsed = urllib.parse.urlsplit(self.path)
|
| 67 |
+
if parsed.path == "/.well-known/szl-source.json":
|
| 68 |
+
force = urllib.parse.parse_qs(parsed.query).get("refresh") == ["1"]
|
| 69 |
+
self._send_json(
|
| 70 |
+
build_attestation(
|
| 71 |
+
SPACE_ID,
|
| 72 |
+
SOURCE_OBSERVATION,
|
| 73 |
+
"UNKNOWN_SOURCE_RELATION",
|
| 74 |
+
force=force,
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
return
|
| 78 |
+
super().do_GET()
|
| 79 |
+
|
| 80 |
def end_headers(self):
|
| 81 |
self.send_header("Content-Security-Policy", CONTENT_SECURITY_POLICY)
|
| 82 |
self.send_header(
|
szl_source_attestation.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Honest deployment-source attestation shared by the small public Spaces.
|
| 2 |
+
|
| 3 |
+
The deployed Hugging Face revision is measured independently of any GitHub
|
| 4 |
+
source observation. A source reference never implies artifact parity or a
|
| 5 |
+
reproducible build.
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
import os
|
| 11 |
+
import re
|
| 12 |
+
import threading
|
| 13 |
+
import time
|
| 14 |
+
import urllib.request
|
| 15 |
+
from datetime import datetime, timezone
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
_SHA = re.compile(r"^[0-9a-f]{40}$")
|
| 19 |
+
_CACHE_LOCK = threading.Lock()
|
| 20 |
+
_CACHE: dict[str, dict[str, object]] = {}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _now_iso() -> str:
|
| 24 |
+
return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _valid_sha(value: object) -> str | None:
|
| 28 |
+
candidate = str(value or "").strip().lower()
|
| 29 |
+
return candidate if _SHA.fullmatch(candidate) else None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def measure_hf_revision(space_id: str, force: bool = False) -> dict[str, object]:
|
| 33 |
+
env_revision = _valid_sha(os.environ.get("SPACE_REPOSITORY_COMMIT"))
|
| 34 |
+
if env_revision:
|
| 35 |
+
return {
|
| 36 |
+
"hf_revision": env_revision,
|
| 37 |
+
"revision_state": "MEASURED",
|
| 38 |
+
"measurement_method": "SPACE_REPOSITORY_COMMIT",
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
now = time.monotonic()
|
| 42 |
+
with _CACHE_LOCK:
|
| 43 |
+
cached = _CACHE.get(space_id)
|
| 44 |
+
if not force and cached and now - float(cached["stored_at"]) < 60:
|
| 45 |
+
return dict(cached["measurement"]) # type: ignore[arg-type]
|
| 46 |
+
|
| 47 |
+
revision = None
|
| 48 |
+
error = None
|
| 49 |
+
req = urllib.request.Request(
|
| 50 |
+
f"https://huggingface.co/api/spaces/{space_id}?expand[]=sha",
|
| 51 |
+
headers={"Accept": "application/json", "User-Agent": "szl-source-attestation/1.0"},
|
| 52 |
+
)
|
| 53 |
+
try:
|
| 54 |
+
with urllib.request.urlopen(req, timeout=4) as response:
|
| 55 |
+
revision = _valid_sha(json.load(response).get("sha"))
|
| 56 |
+
except Exception as exc:
|
| 57 |
+
error = type(exc).__name__
|
| 58 |
+
|
| 59 |
+
measurement: dict[str, object] = {
|
| 60 |
+
"hf_revision": revision,
|
| 61 |
+
"revision_state": "MEASURED" if revision else "UNAVAILABLE",
|
| 62 |
+
"measurement_method": "HUGGINGFACE_API" if revision else "UNAVAILABLE",
|
| 63 |
+
}
|
| 64 |
+
if error:
|
| 65 |
+
measurement["measurement_error"] = error
|
| 66 |
+
with _CACHE_LOCK:
|
| 67 |
+
_CACHE[space_id] = {"stored_at": time.monotonic(), "measurement": dict(measurement)}
|
| 68 |
+
return measurement
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def build_attestation(
|
| 72 |
+
space_id: str,
|
| 73 |
+
source: dict[str, object],
|
| 74 |
+
alignment_state: str,
|
| 75 |
+
force: bool = False,
|
| 76 |
+
) -> dict[str, object]:
|
| 77 |
+
measurement = measure_hf_revision(space_id, force=force)
|
| 78 |
+
return {
|
| 79 |
+
"schema": "szl.deployment-source/v1",
|
| 80 |
+
"observed_at": _now_iso(),
|
| 81 |
+
"transport_state": "REACHABLE",
|
| 82 |
+
"evidence_state": "COMPUTED" if measurement["hf_revision"] else "UNAVAILABLE",
|
| 83 |
+
"verification_state": "STRUCTURAL_ONLY",
|
| 84 |
+
"authority_state": "READ_ONLY",
|
| 85 |
+
"source": dict(source),
|
| 86 |
+
"deployment": {"hf_space": space_id, **measurement},
|
| 87 |
+
"alignment_state": alignment_state,
|
| 88 |
+
"attestation_state": "UNSIGNED_STRUCTURAL",
|
| 89 |
+
"claims": {
|
| 90 |
+
"github_parity": "NOT_CLAIMED",
|
| 91 |
+
"reproducible_build": "NOT_CLAIMED",
|
| 92 |
+
},
|
| 93 |
+
"limits": [
|
| 94 |
+
"The Hugging Face revision is measured independently from the source observation.",
|
| 95 |
+
"A GitHub reference does not establish deployed-artifact equivalence.",
|
| 96 |
+
"This unsigned structural attestation does not claim a reproducible build or GitHub parity.",
|
| 97 |
+
],
|
| 98 |
+
}
|
tests/test_source_attestation.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import functools
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import threading
|
| 6 |
+
import unittest
|
| 7 |
+
from http.server import ThreadingHTTPServer
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from unittest.mock import patch
|
| 10 |
+
from urllib.request import urlopen
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 14 |
+
sys.path.insert(0, str(ROOT))
|
| 15 |
+
import server # noqa: E402
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class SourceAttestationTests(unittest.TestCase):
|
| 19 |
+
def test_unknown_source_does_not_claim_parity(self):
|
| 20 |
+
with patch.dict(os.environ, {"SPACE_REPOSITORY_COMMIT": "a" * 40}):
|
| 21 |
+
payload = server.build_attestation(
|
| 22 |
+
server.SPACE_ID,
|
| 23 |
+
server.SOURCE_OBSERVATION,
|
| 24 |
+
"UNKNOWN_SOURCE_RELATION",
|
| 25 |
+
)
|
| 26 |
+
self.assertEqual("szl.deployment-source/v1", payload["schema"])
|
| 27 |
+
self.assertEqual("a" * 40, payload["deployment"]["hf_revision"])
|
| 28 |
+
self.assertEqual("MEASURED", payload["deployment"]["revision_state"])
|
| 29 |
+
self.assertEqual("UNKNOWN", payload["source"]["state"])
|
| 30 |
+
self.assertIsNone(payload["source"]["repository"])
|
| 31 |
+
self.assertEqual("NOT_CLAIMED", payload["claims"]["github_parity"])
|
| 32 |
+
self.assertEqual("NOT_CLAIMED", payload["claims"]["reproducible_build"])
|
| 33 |
+
|
| 34 |
+
def test_well_known_route_returns_uncacheable_json(self):
|
| 35 |
+
handler = functools.partial(server.HardenedHandler, directory=str(ROOT))
|
| 36 |
+
httpd = ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
| 37 |
+
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
|
| 38 |
+
thread.start()
|
| 39 |
+
try:
|
| 40 |
+
with patch.dict(os.environ, {"SPACE_REPOSITORY_COMMIT": "b" * 40}):
|
| 41 |
+
with urlopen(
|
| 42 |
+
f"http://127.0.0.1:{httpd.server_port}/.well-known/szl-source.json",
|
| 43 |
+
timeout=3,
|
| 44 |
+
) as response:
|
| 45 |
+
payload = json.load(response)
|
| 46 |
+
self.assertEqual(200, response.status)
|
| 47 |
+
self.assertEqual("no-store", response.headers["Cache-Control"])
|
| 48 |
+
self.assertEqual("COMPUTED", response.headers["X-SZL-Evidence-State"])
|
| 49 |
+
self.assertEqual("b" * 40, payload["deployment"]["hf_revision"])
|
| 50 |
+
finally:
|
| 51 |
+
httpd.shutdown()
|
| 52 |
+
httpd.server_close()
|
| 53 |
+
thread.join(timeout=3)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
unittest.main()
|