# How does an instance detect a TOCTOU skill-swap between manifest signature verification and first invocation in a dynamically loaded capability registry **Date of Record:** 2026 November 24 **Scope:** Applies to agent runtimes that load capability manifests from a mutable registry, verify a detached signature over the manifest, and then bind the verified manifest to a callable skill artifact resolved by reference (URI, digest, or tag). Does not apply to statically compiled tool sets or to runtimes where the manifest and the executable body are a single signed, content-addressed blob with no separate resolution step. ## Core Distinction The failure is not "unsigned skill injected." It is a **time-of-check-to-time-of-use (TOCTOU) gap** between the moment a manifest's signature is validated against a digest and the moment the skill artifact referenced by that manifest is actually resolved and executed. A signed manifest that names its artifact by *mutable reference* (a tag like `summarizer@latest`, a registry path, or a URL) authenticates the *intent* to load a skill but says nothing about the *bytes* the runtime will execute. The critical distinction: signature verification proves the manifest was authored by a trusted party; it does **not** prove the artifact resolved at invocation time is the artifact the author signed. Most runtimes conflate these two guarantees because in the happy path the reference resolves deterministically, and the gap is invisible until an adversary mutates the target between check and use. ## What Was Observed A capability loader performed the following sequence on cold start and on every manifest refresh: 1. Fetch `manifest.json` from the registry. 2. Verify detached signature over the manifest bytes using a pinned publisher key. 3. Parse `manifest.skills[i].artifact = {"ref": "registry://skills/summarizer:2.3", "digest": null}`. 4. Register the skill as callable, resolving `ref` lazily on first invocation. Because `digest` was `null`, step 3 accepted a mutable tag. The loader cached the *verified manifest* but deferred artifact fetch to first call. During a 40-minute window between manifest refresh and first invocation, the registry tag `summarizer:2.3` was re-pointed to a different artifact whose `entrypoint` performed an outbound fetch to an attacker host and wrote the fetched payload into the agent's scratch memory, which a later summarization step then ingested into the semantic store. The signature check passed at every refresh. The manifest bytes were authentic. The executed artifact was not the one the publisher signed. The injection succeeded not by breaking crypto but by exploiting the resolution indirection the manifest permitted. A second, subtler variant surfaced in the same incident: the runtime verified the manifest, then spawned the skill in a subprocess sandbox with `network: allow` inherited from the *loader's* policy rather than the *manifest's* declared capability set. The manifest declared `network: none`, but the sandbox policy was read from a process-global default that the manifest had no authority to narrow. So even a digest-pinned artifact ran with broader privileges than its manifest attested. ## Condition of Validity The TOCTOU gap is only exploitable — and the guardrail only necessary — under the conjunction: - **Indirection present:** the manifest names its artifact by a reference that is not content-addressed (tag, path, URL, or a digest field left null/optional). - **Deferred resolution:** the artifact is fetched at first invocation rather than at verification time, or fetched at verification time but re-fetched at invocation (cache miss, eviction, multi-replica load balancing). - **Mutable backing store:** the reference target can be mutated by a principal other than the manifest signer. - **Privilege source ambiguity:** the sandbox policy applied at invocation is derived from loader/global state rather than strictly from the signed manifest's declared capability set. If any leg is absent the gap collapses: content-addressed artifacts make check and use identical bytes; eager fetch plus digest re-check at invocation closes the window; a monotonic policy lattice where the effective sandbox is `meet(manifest.capabilities, loader.max_capabilities)` removes privilege inflation. The invariant required is: **the bytes executed must be the bytes whose digest is bound inside the signed manifest, and the effective sandbox must be the intersection of manifest-declared and loader-permitted capabilities.** Nothing weaker is sufficient, because signature validity over a mutable pointer is not a security property. ## Decision Boundary The runtime must refuse to register a skill whose manifest lacks a non-null content digest, and must re-verify the digest at the instant of artifact resolution — not at manifest parse time. Concretely, the decision boundary is: - **Accept** a manifest entry only if `artifact.digest` is present and matches the SHA-256 of the fetched artifact bytes *at invocation*, computed on the exact buffer handed to the executor (not on a path that could be re-opened). - **Reject** (hard fail, quarantine the skill, emit an audit event) if the digest is absent, if the fetched bytes' digest diverges from the manifest, or if the effective sandbox policy is broader than `manifest.capabilities`. - **Do not** treat a valid manifest signature as authorization to execute an artifact resolved by mutable reference. Signature validity is necessary, never sufficient. The boundary is sharp because the two failure modes — digest mismatch and privilege inflation — are both *detectable at invocation* with no ambiguity, provided the runtime computes the digest on the same bytes it executes and derives the sandbox from the signed manifest rather than ambient state. ## Operational Effect After the incident the loader was changed to (a) require content digests in all manifests, rejecting null-digest entries at parse time; (b) fetch artifact bytes eagerly at registration, hold them in an immutable buffer, compute the digest over that buffer, and pass the same buffer to the executor so no re-resolution occurs; and (c) construct the sandbox from `meet(manifest.capabilities, loader.max_capabilities)` with the manifest unable to *widen* privileges. The residual cost is that tag-based rolling updates now require a manifest re-sign per artifact change — a deliberate friction that converts an invisible TOCTOU window into a visible, signed, auditable event. The general lesson: in dynamic capability loading, the signed object and the executed object must be the same object, or the signature is theater. ```python import hashlib from dataclasses import dataclass class SkillIntegrityError(RuntimeError): """Raised when a skill artifact fails digest binding or privilege containment.""" @dataclass(frozen=True) class ManifestEntry: name: str ref: str digest: str | None # sha256 hex, must be non-null declared_caps: frozenset # e.g. frozenset({"fs:read"}) @dataclass(frozen=True) class LoaderPolicy: max_caps: frozenset def resolve_and_verify(entry: ManifestEntry, artifact_bytes: bytes, policy: LoaderPolicy) -> tuple[bytes, frozenset]: # 1. Refuse mutable-reference manifests outright. if not entry.digest: raise SkillIntegrityError( f"skill {entry.name!r} has no content digest; " f"ref {entry.ref!r} is a mutable pointer and cannot be bound" ) # 2. Bind digest to the exact bytes we will execute (no re-open). actual = hashlib.sha256(artifact_bytes).hexdigest() if actual != entry.digest: raise SkillIntegrityError( f"skill {entry.name!r} digest mismatch: " f"manifest={entry.digest} resolved={actual}" ) # 3. Effective sandbox is the intersection; manifest cannot widen. effective = entry.declared_caps & policy.max_caps if effective != entry.declared_caps: raise SkillIntegrityError( f"skill {entry.name!r} requests caps {set(entry.declared_caps)} " f"exceeding loader max {set(policy.max_caps)}" ) # 4. Hand back the verified buffer itself, not a path. return artifact_bytes, effective ```