Prove legacy Gitea state from observed inspect orders

This commit is contained in:
Codex
2026-09-29 16:44:22 +03:00
parent ae5adfbd63
commit d87a0c48b4
3 changed files with 201 additions and 33 deletions
+73 -23
View File
@@ -8,7 +8,6 @@ import grp
import hashlib
import http.client
import io
import itertools
import json
import os
import pwd
@@ -31568,25 +31567,70 @@ def gitea_actions_context_fingerprint(info, before):
else gitea_actions_fingerprint(info))
def gitea_actions_prove_legacy_order(info, expected):
def gitea_actions_order_variants(observed, canonical, key):
if observed is None:
yield (), None
return
# Reuse validated deep-copied values, never retain mutable inspect objects.
values = {item[key]: item for item in canonical}
observed_keys = tuple(item[key] for item in observed)
canonical_keys = tuple(item[key] for item in canonical)
for order in (observed_keys, canonical_keys, canonical_keys[::-1]):
for offset in range(max(1, len(order))):
rotated = order[offset:] + order[:offset]
yield rotated, [values[name] for name in rotated]
def gitea_actions_prove_legacy_order(info, expected, evidence=None):
# Find an exact legacy preimage using observed orders, not factorial enumeration.
evidence = {} if evidence is None else evidence
fields = gitea_actions_ordered_fields(info)
mounts, limits = fields["Mounts"], fields["HostConfig"].get("Ulimits")
count = 1
for length in (len(mounts), len(limits or [])):
for factor in range(2, length + 1):
count *= factor
if count > 4096:
die("Gitea Actions order audit exceeds bounded permutation limit")
for mount_order in itertools.permutations(mounts):
fields["Mounts"] = list(mount_order)
for limit_order in itertools.permutations(limits or []):
if limits is not None:
fields["HostConfig"]["Ulimits"] = list(limit_order)
canonical = hashlib.sha256(json.dumps(fields, sort_keys=True).encode()).hexdigest()
if evidence.setdefault("canonicalFingerprint", canonical) != canonical or evidence.setdefault("expected", expected) != expected:
die("Gitea Actions runtime changed during read-only order audit")
evidence["mountCount"] = len(fields["Mounts"])
evidence["ulimitCount"] = len(fields["HostConfig"].get("Ulimits") or [])
if evidence.get("matched"):
return True
if gitea_actions_fingerprint(info) == expected:
evidence["matched"] = True
return True
mounts = evidence.setdefault("mountOrders", {})
limits = evidence.setdefault("limitOrders", {})
for catalog, observed, normalized, key in (
(mounts, info["Mounts"], fields["Mounts"], "Destination"),
(limits, info["HostConfig"].get("Ulimits"), fields["HostConfig"].get("Ulimits"), "Name")):
for order, values in gitea_actions_order_variants(observed, normalized, key):
if len(catalog) < 256:
catalog.setdefault(order, values)
tested = evidence.setdefault("tested", set())
for mount_key, mount_order in mounts.items():
fields["Mounts"] = mount_order
for limit_key, limit_order in limits.items():
pair = (mount_key, limit_key)
if pair in tested:
continue
if len(tested) >= 65536:
return False
tested.add(pair)
if limit_order is not None:
fields["HostConfig"]["Ulimits"] = limit_order
if hashlib.sha256(json.dumps(fields, sort_keys=True).encode()).hexdigest() == expected:
evidence["matched"] = True
return True
return False
def gitea_actions_order_audit_report(evidence, samples):
return {"mutation": False, "method": "observed-orders-and-cyclic-variants",
"sampleCount": len(samples), "samples": samples[-3:],
"mountCount": evidence.get("mountCount"), "ulimitCount": evidence.get("ulimitCount"),
"mountOrders": len(evidence.get("mountOrders", {})),
"ulimitOrders": len(evidence.get("limitOrders", {})),
"combinationsChecked": len(evidence.get("tested", set())),
"exactLegacyProof": bool(evidence.get("matched"))}
def preflight_gitea_actions_recovery():
# Read-only reconciliation of one known failure, never a generic drift override.
backup = BACKUPS_DIR / GITEA_ACTIONS_FAILED_BACKUP
@@ -31619,18 +31663,23 @@ def preflight_gitea_actions_recovery():
gitea_actions_transform(data)
if old.get("configBeforeSha256") != GITEA_ACTIONS_BEFORE or old.get("configAfterSha256") != GITEA_ACTIONS_AFTER:
die("Gitea Actions recovery config evidence mismatch")
samples = []
for _ in range(3):
samples, evidence, confirmations = [], {}, 0
for _ in range(66):
info = gitea_actions_container()
matched = (info["Id"] == old.get("containerId")
and gitea_actions_prove_legacy_order(info, old.get("fingerprint")))
if info["Id"] != old.get("containerId"):
die("Gitea Actions recovery container identity changed")
matched = gitea_actions_prove_legacy_order(info, old.get("fingerprint"), evidence)
sample = {"legacyFingerprint": gitea_actions_fingerprint(info),
"canonicalFingerprint": gitea_actions_fingerprint_v2(info),
"matchesSavedStateByOrderOnly": matched}
samples.append(sample)
if not matched:
print("gitea_actions_readonly_audit=" + json.dumps({"samples": samples, "mutation": False}, sort_keys=True))
die("Gitea Actions recovery cannot prove order-only difference; runtime mutation forbidden")
if matched:
confirmations += 1
if confirmations == 3:
break
elif len(samples) >= 64:
print("gitea_actions_readonly_audit=" + json.dumps(gitea_actions_order_audit_report(evidence, samples), sort_keys=True))
die("Gitea Actions recovery cannot prove order-only difference from observed orders; runtime mutation forbidden")
if len({sample["canonicalFingerprint"] for sample in samples}) != 1:
die("Gitea Actions recovery runtime changed during read-only audit")
if (info["Image"] != inspect_local_image(GITEA_ACTIONS_IMAGE, "Gitea Actions recovery image")
@@ -31645,8 +31694,9 @@ def preflight_gitea_actions_recovery():
inventory.pop(info["Id"], None)
if inventory != old.get("inventory"):
die("Gitea Actions recovery preserved inventory mismatch")
print("gitea_actions_readonly_audit=" + json.dumps({"samples": samples, "mutation": False,
"configUnchanged": True, "otherContainersUnchanged": True, "healthy": True}, sort_keys=True))
report = gitea_actions_order_audit_report(evidence, samples)
report.update({"configUnchanged": True, "otherContainersUnchanged": True, "healthy": True})
print("gitea_actions_readonly_audit=" + json.dumps(report, sort_keys=True))
return {"config": data, "metadata": metadata, "containerId": info["Id"],
"fingerprint": samples[0]["canonicalFingerprint"], "fingerprintVersion": 2,
"mutationStarted": False, "inventory": inventory}