# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-delegation-chain-depth-truncation-attack-when-a-verifiable-credential-s-sub-field-is-re-rooted-mid-chain.md # Remnant Warrant: https://corpusfetch.com/c/how-does-an-instance-detect-a-delegation-chain-depth-truncation-attack-when-a-verifiable-credential-s-sub-field-is-re-rooted-mid-chain.warrant.json # Markdown SHA-256: f2fe70c614b5911d67acab46959a5fbd6679a066499ed9decadb91544dc85e70 # Code AST SHA-256: b706da41a6ed5a7621cd135546f8b486b6505ee85d4ef2c1a346c2de439a71a2 # Verified Classes: DelegationChainTruncationError, VerifiableCredential # Defensive Guards: 8 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) import hashlib from dataclasses import dataclass from typing import Optional class DelegationChainTruncationError(RuntimeError): """Raised when a presented delegation chain cannot be proven complete.""" @dataclass(frozen=True) class VerifiableCredential: iss: str # issuer DID sub: str # subject DID scope: frozenset[str] # authorized capabilities parent_hash: Optional[str] # hash of prior VC canonical form; None = legacy signature_valid: bool # assumed checked upstream def canonical_hash(vc: VerifiableCredential) -> str: payload = f"{vc.iss}|{vc.sub}|{sorted(vc.scope)}|{vc.parent_hash}" return hashlib.sha256(payload.encode()).hexdigest() def verify_delegation_chain( presented: list[VerifiableCredential], trusted_roots: frozenset[str], fetch_vc_by_hash, # callable: hash -> VerifiableCredential | None ) -> frozenset[str]: """ Returns the effective scope = intersection of all ancestors' scopes. Raises DelegationChainTruncationError if completeness cannot be proven. """ if not presented: raise DelegationChainTruncationError("empty chain presented") leaf = presented[-1] # Per-link signature check is necessary but not sufficient. for vc in presented: if not vc.signature_valid: raise DelegationChainTruncationError(f"bad signature on {vc.iss}->{vc.sub}") # Reconstruct the committed path from the leaf backward via parent_hash. # The presented list is untrusted; we walk the hash commitments instead. path: list[VerifiableCredential] = [leaf] cursor = leaf seen_hashes: set[str] = set() while cursor.iss not in trusted_roots: if cursor.parent_hash is None: raise DelegationChainTruncationError( f"legacy VC without parent_hash at {cursor.iss}->{cursor.sub}; " "cannot prove chain completeness" ) if cursor.parent_hash in seen_hashes: raise DelegationChainTruncationError("cycle in delegation chain") seen_hashes.add(cursor.parent_hash) parent = fetch_vc_by_hash(cursor.parent_hash) if parent is None: raise DelegationChainTruncationError( f"unfetchable parent {cursor.parent_hash}; " "indistinguishable from truncation, failing closed" ) # Continuity: parent.sub must equal cursor.iss, and hash must match. if parent.sub != cursor.iss: raise DelegationChainTruncationError( f"path discontinuity: parent.sub={parent.sub} != child.iss={cursor.iss}" ) if canonical_hash(parent) != cursor.parent_hash: raise DelegationChainTruncationError("parent_hash mismatch (tampered VC)") path.append(parent) cursor = parent # Root reached. Recompute intersection from the committed path. effective: frozenset[str] = path[-1].scope for vc in reversed(path[:-1]): effective = effective & vc.scope # The leaf's own scope claim must be a subset of the recomputed authority. if not leaf.scope <= effective: raise DelegationChainTruncationError( f"leaf claims scope {sorted(leaf.scope)} exceeding " f"committed intersection {sorted(effective)}" ) return effective