Spaces:
Running
Running
chore(sync): mirror backend .py + Dockerfile to Space (hf-sync-backend)
Browse filesAutomated backend sync from szl-holdings/a11oy main via hf-sync-backend.
Updated (differed from the Space): Dockerfile, a11oy_vertical_feeds.py, serve.py, szl_demo_sign.py
Deleted (gone from the repo + Dockerfile COPY set): (none)
Keeps the Space-built backend (serve.py + the Dockerfile-COPY'd .py
modules) identical to GitHub main so the Space never rebuilds from a
stale backend, new endpoints don't 404 there, and orphaned modules
removed from the repo don't linger in the Space tree.
- Dockerfile +6 -0
- a11oy_vertical_feeds.py +16 -0
- serve.py +18 -0
- szl_demo_sign.py +131 -0
Dockerfile
CHANGED
|
@@ -225,6 +225,12 @@ COPY szl_provenance_receipt.py ./
|
|
| 225 |
# and /api/a11oy/v1/khipu/{organs,chain,verify} 404s live. Reads the shared szl_khipu
|
| 226 |
# DAG in-process (already COPY'd above) — no new deps.
|
| 227 |
COPY szl_khipu_verify.py ./
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
# REGRESSION RESTORE — serve.py imports these (guarded) but their per-file COPY was
|
| 229 |
# dropped, so in the HF image each guarded import falls back to a STUB (merged-but-not-
|
| 230 |
# live) and the copy-sync lockstep guard is red on main. szl_sda was added by the most
|
|
|
|
| 225 |
# and /api/a11oy/v1/khipu/{organs,chain,verify} 404s live. Reads the shared szl_khipu
|
| 226 |
# DAG in-process (already COPY'd above) — no new deps.
|
| 227 |
COPY szl_khipu_verify.py ./
|
| 228 |
+
# DEMO-ONLY signing (Option B): szl_demo_sign exposes the demo-signing-key path so
|
| 229 |
+
# /verify shows a real ECDSA-P256 VERIFIED badge. MUST be per-file COPY'd or the
|
| 230 |
+
# guarded imports in serve.py (/demo-cosign.pub) and a11oy_vertical_feeds (infer
|
| 231 |
+
# fallback) fall back and the demo signature never appears. Production cosign key
|
| 232 |
+
# stays founder-gated; only the demo PUBLIC key ships in this module.
|
| 233 |
+
COPY szl_demo_sign.py ./
|
| 234 |
# REGRESSION RESTORE — serve.py imports these (guarded) but their per-file COPY was
|
| 235 |
# dropped, so in the HF image each guarded import falls back to a STUB (merged-but-not-
|
| 236 |
# live) and the copy-sync lockstep guard is red on main. szl_sda was added by the most
|
a11oy_vertical_feeds.py
CHANGED
|
@@ -241,6 +241,22 @@ def governed_turn(vertical: str, text: str, *, declared: str | None = None,
|
|
| 241 |
receipt = signed.get("receipt", receipt)
|
| 242 |
except Exception as e:
|
| 243 |
dsse = {"signed": False, "honesty": f"sign-unavailable: {e}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
return {
|
| 246 |
"vertical": vertical,
|
|
|
|
| 241 |
receipt = signed.get("receipt", receipt)
|
| 242 |
except Exception as e:
|
| 243 |
dsse = {"signed": False, "honesty": f"sign-unavailable: {e}"}
|
| 244 |
+
# ADDITIVE demo-signing fallback (Option B): when the production cosign key
|
| 245 |
+
# is founder-gated/absent (dsse unsigned), sign with the clearly-labelled
|
| 246 |
+
# DEMO key so /verify can show a REAL in-browser ECDSA-P256 verification.
|
| 247 |
+
# keyid="demo-signing-key" — NEVER mislabelled as the production signature.
|
| 248 |
+
# Fully guarded: any failure keeps the honest UNSIGNED/placeholder behaviour
|
| 249 |
+
# and never breaks the infer path.
|
| 250 |
+
if isinstance(dsse, dict) and not dsse.get("signed") and isinstance(receipt, dict):
|
| 251 |
+
try:
|
| 252 |
+
import szl_demo_sign
|
| 253 |
+
_demo = szl_demo_sign.demo_sign_receipt(receipt)
|
| 254 |
+
if _demo is not None:
|
| 255 |
+
dsse = _demo["dsse"]
|
| 256 |
+
receipt = _demo["receipt"]
|
| 257 |
+
except Exception as e:
|
| 258 |
+
import sys as _dsys
|
| 259 |
+
print(f"[demo-sign] non-fatal, staying UNSIGNED: {e!r}", file=_dsys.stderr)
|
| 260 |
|
| 261 |
return {
|
| 262 |
"vertical": vertical,
|
serve.py
CHANGED
|
@@ -5909,6 +5909,24 @@ async def a11oy_cosign_pub_v2() -> Response:
|
|
| 5909 |
return PlainTextResponse(_A11OY_PUB_PEM, media_type="text/plain")
|
| 5910 |
|
| 5911 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5912 |
# ---- Receipt chain (in-image, hash-chained, signed) ----
|
| 5913 |
def _a11oy_build_chain(n: int = 24) -> dict:
|
| 5914 |
"""Build a deterministic in-image receipt hash-chain. Each receipt commits
|
|
|
|
| 5909 |
return PlainTextResponse(_A11OY_PUB_PEM, media_type="text/plain")
|
| 5910 |
|
| 5911 |
|
| 5912 |
+
# ---- /demo-cosign.pub — DEMO-ONLY public key (PEM, text/plain) -------------
|
| 5913 |
+
# Serves the PUBLIC half of the clearly-labelled demo-signing-key (Option B).
|
| 5914 |
+
# The /verify JS fetches this ONLY when a receipt's keyid == "demo-signing-key",
|
| 5915 |
+
# so a buyer can watch a real in-browser ECDSA-P256 verification succeed while the
|
| 5916 |
+
# PRODUCTION cosign key stays founder-gated and is NEVER placed in this runtime.
|
| 5917 |
+
# This is PUBLIC data; it is NOT the production cosign.pub.
|
| 5918 |
+
@app.get("/demo-cosign.pub")
|
| 5919 |
+
@app.get("/api/a11oy/demo-cosign.pub")
|
| 5920 |
+
async def a11oy_demo_cosign_pub() -> Response:
|
| 5921 |
+
try:
|
| 5922 |
+
import szl_demo_sign
|
| 5923 |
+
return PlainTextResponse(szl_demo_sign.DEMO_COSIGN_PUBLIC_PEM,
|
| 5924 |
+
media_type="text/plain")
|
| 5925 |
+
except Exception:
|
| 5926 |
+
return PlainTextResponse("# demo signing key module unavailable\n",
|
| 5927 |
+
status_code=503)
|
| 5928 |
+
|
| 5929 |
+
|
| 5930 |
# ---- Receipt chain (in-image, hash-chained, signed) ----
|
| 5931 |
def _a11oy_build_chain(n: int = 24) -> dict:
|
| 5932 |
"""Build a deterministic in-image receipt hash-chain. Each receipt commits
|
szl_demo_sign.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 2 |
+
# © 2026 Lutar, Stephen P. — SZL Holdings · ORCID 0009-0001-0110-4173 · Doctrine v11 LOCKED
|
| 3 |
+
"""
|
| 4 |
+
szl_demo_sign — DEMO-ONLY ECDSA-P256 signing for SZL Khipu receipts.
|
| 5 |
+
|
| 6 |
+
WHY THIS EXISTS (read before editing):
|
| 7 |
+
The PRODUCTION cosign private key is founder-gated and is NEVER placed in the
|
| 8 |
+
HF Space runtime (see szl_dsse.py). When it is absent, szl_dsse emits an honest
|
| 9 |
+
UNSIGNED envelope and /verify shows "⚠ UNSIGNED". To let a buyer watch a REAL
|
| 10 |
+
in-browser ECDSA-P256 verification SUCCEED — without ever exposing the
|
| 11 |
+
production key — this module signs with a SEPARATE, clearly-labelled DEMO key.
|
| 12 |
+
|
| 13 |
+
HONESTY CONTRACT (doctrine, non-negotiable):
|
| 14 |
+
- The demo key is a DISTINCT keypair from the production cosign key.
|
| 15 |
+
- Every envelope it produces is stamped keyid="demo-signing-key" and carries a
|
| 16 |
+
human-visible note: "signed with demo-signing-key — NOT the production
|
| 17 |
+
founder-gated cosign key". A demo signature is NEVER labelled as production.
|
| 18 |
+
- The DEMO PUBLIC key is committed here and served at /demo-cosign.pub. The
|
| 19 |
+
DEMO PRIVATE key is loaded ONLY from the Space secret env var
|
| 20 |
+
SZL_DEMO_SIGN_KEY (PKCS8 PEM, optionally base64-wrapped). It is never
|
| 21 |
+
committed to git.
|
| 22 |
+
- If the secret is absent or invalid this module is a no-op: it returns None and
|
| 23 |
+
the caller keeps the honest DSSE_PLACEHOLDER / UNSIGNED behaviour. It NEVER
|
| 24 |
+
fabricates a signature and NEVER raises into the infer path.
|
| 25 |
+
|
| 26 |
+
The envelope is byte-compatible with the /verify WebCrypto verifier: ECDSA-P256
|
| 27 |
+
over the DSSE PAE ("DSSEv1 " ...), exactly as szl_dsse produces for production.
|
| 28 |
+
"""
|
| 29 |
+
from __future__ import annotations
|
| 30 |
+
|
| 31 |
+
import base64
|
| 32 |
+
import os
|
| 33 |
+
import sys
|
| 34 |
+
from datetime import datetime, timezone
|
| 35 |
+
from typing import Any, Optional
|
| 36 |
+
|
| 37 |
+
# Reuse the EXACT PAE + canonical-JSON encoding the production path and the
|
| 38 |
+
# /verify JS already agree on, so a demo signature verifies through the same code.
|
| 39 |
+
import szl_dsse
|
| 40 |
+
|
| 41 |
+
DEMO_KEY_ID = "demo-signing-key"
|
| 42 |
+
DEMO_SIGN_KEY_ENV = "SZL_DEMO_SIGN_KEY"
|
| 43 |
+
DEMO_NOTE = ("signed with demo-signing-key — NOT the production founder-gated "
|
| 44 |
+
"cosign key")
|
| 45 |
+
|
| 46 |
+
# Demo PUBLIC key (SubjectPublicKeyInfo / SPKI PEM). PUBLIC data — safe to commit.
|
| 47 |
+
# Served at /demo-cosign.pub so the /verify JS can fetch + import it. The matching
|
| 48 |
+
# PRIVATE key lives ONLY in the SZL_DEMO_SIGN_KEY Space secret (never committed).
|
| 49 |
+
DEMO_COSIGN_PUBLIC_PEM = """-----BEGIN PUBLIC KEY-----
|
| 50 |
+
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEsy4Rh7qm/NtCPupIoCrs+YLJUwqg
|
| 51 |
+
3WyMBuzMeK+IqTY2X8DCRKTVFqdWktYF6IKBpA2ZiHU5Yl5STf4QqX97/A==
|
| 52 |
+
-----END PUBLIC KEY-----
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _load_demo_private_key():
|
| 57 |
+
"""Load the demo EC private key from the SZL_DEMO_SIGN_KEY secret.
|
| 58 |
+
|
| 59 |
+
Returns None (never raises) if the secret is absent or invalid — the caller
|
| 60 |
+
then keeps the honest UNSIGNED / placeholder behaviour."""
|
| 61 |
+
pem = os.environ.get(DEMO_SIGN_KEY_ENV)
|
| 62 |
+
if not pem:
|
| 63 |
+
return None
|
| 64 |
+
try:
|
| 65 |
+
if "BEGIN" not in pem: # allow base64-wrapped secret (HF UI friendliness)
|
| 66 |
+
pem = base64.b64decode(pem).decode("utf-8")
|
| 67 |
+
from cryptography.hazmat.primitives.serialization import load_pem_private_key
|
| 68 |
+
return load_pem_private_key(pem.encode("utf-8"), password=None)
|
| 69 |
+
except Exception as e: # pragma: no cover - defensive
|
| 70 |
+
print(f"[demo-sign] private key load failed (staying UNSIGNED): {e!r}",
|
| 71 |
+
file=sys.stderr)
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def demo_signing_available() -> bool:
|
| 76 |
+
return _load_demo_private_key() is not None
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def sign_payload_demo(payload_obj: Any,
|
| 80 |
+
payload_type: str = szl_dsse.KHIPU_PAYLOAD_TYPE
|
| 81 |
+
) -> Optional[dict[str, Any]]:
|
| 82 |
+
"""Produce a DSSE envelope over `payload_obj` using the DEMO key.
|
| 83 |
+
|
| 84 |
+
Returns the envelope dict (signed=True, keyid="demo-signing-key") or None if
|
| 85 |
+
the demo secret is absent/invalid. NEVER fabricates a signature."""
|
| 86 |
+
priv = _load_demo_private_key()
|
| 87 |
+
if priv is None:
|
| 88 |
+
return None
|
| 89 |
+
try:
|
| 90 |
+
from cryptography.hazmat.primitives.asymmetric import ec
|
| 91 |
+
from cryptography.hazmat.primitives import hashes
|
| 92 |
+
body = szl_dsse.canonical_json(payload_obj)
|
| 93 |
+
to_sign = szl_dsse.pae(payload_type, body)
|
| 94 |
+
sig = priv.sign(to_sign, ec.ECDSA(hashes.SHA256()))
|
| 95 |
+
return {
|
| 96 |
+
"payloadType": payload_type,
|
| 97 |
+
"payload": base64.b64encode(body).decode("ascii"),
|
| 98 |
+
"signatures": [{"sig": base64.b64encode(sig).decode("ascii"),
|
| 99 |
+
"keyid": DEMO_KEY_ID}],
|
| 100 |
+
"signed": True,
|
| 101 |
+
"_dsse": "DSSEv1",
|
| 102 |
+
"_signed_at": datetime.now(timezone.utc).isoformat(),
|
| 103 |
+
"key_id": DEMO_KEY_ID,
|
| 104 |
+
"key_kind": "demo",
|
| 105 |
+
"verify_key_url": "/demo-cosign.pub",
|
| 106 |
+
"honesty": ("DEMO — real ECDSA-P256-SHA256 over the DSSE PAE, "
|
| 107 |
+
"verifiable in-browser against /demo-cosign.pub. " + DEMO_NOTE
|
| 108 |
+
+ ". The production cosign key stays founder-gated and is "
|
| 109 |
+
"NEVER placed in this runtime."),
|
| 110 |
+
}
|
| 111 |
+
except Exception as e: # pragma: no cover - defensive
|
| 112 |
+
print(f"[demo-sign] sign failed (staying UNSIGNED): {e!r}", file=sys.stderr)
|
| 113 |
+
return None
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def demo_sign_receipt(receipt: dict[str, Any]) -> Optional[dict[str, Any]]:
|
| 117 |
+
"""Demo-sign a Khipu receipt. Returns {receipt, dsse} or None.
|
| 118 |
+
|
| 119 |
+
The returned receipt is stamped with key_id="demo-signing-key" and a
|
| 120 |
+
human-visible signature_status so no surface can mistake it for a production
|
| 121 |
+
signature. The dsse envelope is the cryptographic source of truth and is what
|
| 122 |
+
/verify checks against /demo-cosign.pub. Returns None (caller keeps the honest
|
| 123 |
+
placeholder) when the demo secret is absent."""
|
| 124 |
+
env = sign_payload_demo(receipt, szl_dsse.KHIPU_PAYLOAD_TYPE)
|
| 125 |
+
if env is None:
|
| 126 |
+
return None
|
| 127 |
+
out = dict(receipt)
|
| 128 |
+
out["signature"] = env["signatures"][0]["sig"]
|
| 129 |
+
out["key_id"] = DEMO_KEY_ID
|
| 130 |
+
out["signature_status"] = ("DEMO-SIGNED [demo-signing-key] — " + DEMO_NOTE)
|
| 131 |
+
return {"receipt": out, "dsse": env}
|