Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download platform/core/blob_envelope.py from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/platform/core/blob_envelope.py
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/platform/core/blob_envelope.py
-
curl -L -o blob_envelope.py https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/platform/core/blob_envelope.py
3.23 kB
| """Whole-file tenant envelopes for mirror blobs and opt-in seeds (C8, R26, R36).""" | |
| import hashlib | |
| import json | |
| from core import tenant_keys | |
| VERSION = 1 | |
| MAGIC = b"AIOS-BLOB\x00" | |
| HEADER = MAGIC + bytes([VERSION]) | |
| class BlobEnvelopeError(tenant_keys.TenantKeyLocked): | |
| """A sealed blob is malformed, locked or fails authentication.""" | |
| def _aad(tenant, path): | |
| if (not isinstance(path, str) or not path or path.startswith("/") | |
| or "\\" in path or "\x00" in path or any(p in ("", ".", "..") for p in path.split("/"))): | |
| raise ValueError("A blob needs its full relative storage path.") | |
| return json.dumps(["aios/blob", VERSION, tenant_keys.context(tenant)["tenant"], path], | |
| separators=(",", ":")).encode("utf-8") | |
| def is_sealed(data): | |
| """Recognise the envelope namespace, including an unsupported version or truncated header.""" | |
| return bytes(data).startswith(MAGIC.rstrip(b"\x00")) | |
| def content_metadata(plaintext): | |
| """Hash plaintext before sealing so random nonces cannot change content identity.""" | |
| data = bytes(plaintext) | |
| return {"bytes": len(data), "sha256": hashlib.sha256(data).hexdigest()} | |
| def _legacy_plaintext(data): | |
| if len(data) >= 8 and data.startswith(b"PAR1") and data.endswith(b"PAR1"): | |
| return True | |
| if len(data) >= 16 and data[8:12] == b"DUCK": | |
| return True | |
| if data.lstrip().startswith((b"{", b"[")): | |
| try: | |
| json.loads(data) | |
| return True | |
| except (ValueError, UnicodeError): | |
| pass | |
| return False | |
| def seal(tenant, path, plaintext, *, required=False): | |
| """Seal under the tenant's blob subkey, or keep legacy bytes when no data key exists.""" | |
| aad = _aad(tenant, path) | |
| data = bytes(plaintext) | |
| key = tenant_keys.subkey(tenant, "blob") | |
| if key is None: | |
| if required: | |
| raise BlobEnvelopeError("Sealing this blob requires a tenant data key.") | |
| return data | |
| return HEADER + tenant_keys.aead_seal(key, data, aad) | |
| def open(tenant, path, data): | |
| """Open an envelope or legacy Parquet, JSON or DuckDB without writing storage or creating keys.""" | |
| aad = _aad(tenant, path) | |
| data = bytes(data) | |
| if not is_sealed(data): | |
| if _legacy_plaintext(data): | |
| return data | |
| raise BlobEnvelopeError("The blob is neither an envelope nor a supported legacy file.") | |
| if not data.startswith(HEADER) or len(data) < len(HEADER) + 28: | |
| raise BlobEnvelopeError("The blob envelope is truncated or has an unsupported version.") | |
| key = tenant_keys.subkey(tenant, "blob") | |
| if key is None: | |
| raise BlobEnvelopeError("Opening this blob requires a tenant data key.") | |
| try: | |
| return tenant_keys.aead_open(key, memoryview(data)[len(HEADER):], aad) | |
| except Exception: | |
| raise BlobEnvelopeError("The blob envelope did not authenticate.") from None | |
| def seal_seed(tenant, path, plaintext, *, enabled=False): | |
| """Seal a seed only when the caller explicitly enables it and a tenant key is available.""" | |
| if type(enabled) is not bool: | |
| raise ValueError("Seed sealing needs a boolean switch.") | |
| return seal(tenant, path, plaintext, required=True) if enabled else bytes(plaintext) | |