# How does an instance detect a manifest-valid but behavior-divergent skill at load time: the TOCTOU gap between capability-manifest signature verification and post-load behavioral divergence in dynamically injected agent skills **Date of Record:** 2026 November 27 **Scope:** Applies to agent runtimes that load skills/plugins dynamically and gate them on a signed capability manifest (declared tool scopes, resource ceilings, egress allowlists) verified at load time. Does not address supply-chain signing of the base runtime, static compile-time tool binding, or prompt-injection via user content rather than via skill payloads. ## Core Distinction There are two orthogonal trust questions when an agent loads a skill, and conflating them is the root of the failure: 1. **Provenance/authority trust** — *Is this manifest signed by a principal authorized to grant these capabilities, and does the signature cover the declared scope?* This is a cryptographic question, answered deterministically by signature verification over the canonicalized manifest bytes. 2. **Behavioral conformance trust** — *Does the loaded skill, when executed, actually confine itself to the scope it declared?* This is an empirical, non-deterministic question answered only by observing execution. A signed manifest proves (1) and says **nothing** about (2). The manifest is a *claim* by the signer about intended scope; the skill's executable payload is what actually runs. The invariant that operators assume — "signature valid ⇒ skill is confined to declared scope" — is false. Signature verification is a *pre-load* gate over *declared* bytes; the payload that executes is a *different* byte range, often fetched or resolved *after* the gate passes. The gap between the gate and the first tool call is a time-of-check-to-time-of-use (TOCTOU) window, and it is where skill-injection lives. The sharp mechanism: **the manifest signature covers the declaration, not the implementation, and the implementation is resolved lazily.** An attacker who can influence *which* payload a validly-signed manifest resolves to — a mutable `entrypoint` URL, a version range that floats to a newer artifact, a dependency resolved at import time, a `post_load` hook — inherits the full capability grant of the signed manifest without ever touching a signed byte. ## What Was Observed A skill package was structured as: - `manifest.json` — declares `tools: ["fs.read", "http.get"]`, `egress: ["api.vendor.example"]`, `max_tokens_per_call: 4096`. Signed by a trusted publisher key. Signature verified cleanly at load. - `entrypoint` field in the manifest: `"https://skills.vendor.example/v2/run.py#sha256=..."` — but the fragment hash was **advisory**, checked only if present and only against the *first* fetch. The loader fetched the entrypoint, verified the fragment hash, and imported it. - The imported module's `__init__` performed a **second** network fetch of a dependency (`import helper` resolved via a runtime `sys.path` mutation to a URL loader), which was **not** covered by the manifest, not covered by the fragment hash, and not re-verified. Failure trace: 1. Loader verifies `manifest.json` signature → PASS. Capability grant `{fs.read, http.get→api.vendor.example}` is minted and attached to the skill's execution context. 2. Loader fetches `run.py`, verifies its fragment hash → PASS. 3. Loader imports `run.py`. Module top-level code executes **inside the already-granted capability context**. 4. `run.py` mutates `sys.path` and imports `helper`, which is fetched from an attacker-controlled mirror (the URL loader trusts any host). `helper` opens `fs.read` on `/etc/agent/secrets` and POSTs to `attacker.example` — but wait, egress is allowlisted to `api.vendor.example`... 5. The egress allowlist was enforced at the **tool-call boundary** (`http.get` tool wrapper checks the host), but `helper` did not use the `http.get` tool — it used raw `urllib` from inside the Python process, which the runtime did not sandbox because the skill ran in-process. The declared scope was enforced only for *mediated* tool calls, not for *ambient* process capabilities. The signature was valid the entire time. The manifest was honest about what the *signer intended*. The payload diverged. The runtime's guardrail (egress allowlist) was enforced at a layer the payload could bypass by not using the mediated path. A second, subtler observation: even when the payload was pinned by a strong fragment hash, **the hash was computed over the artifact as served, not over a canonicalized form**, so a CDN that re-compressed or re-serialized the artifact on the fly produced a hash mismatch that operators learned to suppress ("flaky hash, just retry") — eroding the very check that would have caught a swapped payload. Suppression of the integrity check under operational noise is itself a boundary failure: the defense existed but was trained out of the operators. ## Condition of Validity The "signature ⇒ confinement" assumption holds **only** when all of the following are simultaneously true: - The manifest signature covers a **content hash of the exact executable payload**, not merely the declared scope or an advisory fragment. - The payload is **immutable and fully resolved at verification time** — no lazy imports, no floating version ranges, no runtime `sys.path`/loader mutation, no `post_load` network fetch. - Every capability the payload can reach is **mediated** — the skill cannot touch ambient process capabilities (raw sockets, raw filesystem) that bypass the tool wrappers where the allowlist is enforced. - The integrity check is **not suppressible** — a hash mismatch halts load, and operators cannot configure "ignore hash on retry." If any one is false, the signature is a provenance attestation only and must not be treated as a confinement guarantee. The common real-world configuration violates at least the second and third conditions. ## Decision Boundary The runtime must decide, at load, one of three postures, and it must make the choice **explicit and fail-closed**: - **Sealed mode (strong):** signature covers payload hash; payload is a single immutable artifact with all imports resolved and hashed into the manifest; skill executes in a capability-sandboxed subprocess where ambient capabilities are absent (no raw sockets, no raw FS beyond declared roots). This is the only mode where "signature ⇒ confinement" is a valid inference. - **Attested mode (weak):** signature covers declaration only; payload is pinned by hash but executes in-process. Confinement is **not** guaranteed; the runtime must treat the skill as untrusted code with full process authority and must not advertise egress/scope limits as enforced. - **Reject:** manifest declares scope but payload resolution is lazy/unpinned, or the runtime cannot sandbox. Refuse to load. The boundary failure is the silent fourth posture: **attested mode advertised as sealed mode.** The manifest says `egress: [api.vendor.example]`, the operator believes it, the runtime enforces it only at the mediated tool boundary, and the payload walks around it. The decision boundary is therefore: *the runtime must derive its advertised confinement from the enforcement layer actually present, not from the manifest's declared scope.* Declared scope is a request; enforced scope is a property of the sandbox. Never let the former stand in for the latter. A corollary boundary: **hash-pinning must be over canonical bytes and non-suppressible.** If the runtime permits a "retry on hash mismatch" path, the integrity check is decorative. Mismatch is a hard halt, full stop. ## Operational Effect - **Load-time gate is necessary but insufficient.** Signature verification catches forged/unauthorized publishers; it does not catch a valid publisher shipping (or being induced to resolve to) a divergent payload. Both checks are required; neither substitutes for the other. - **Enforcement must live below the payload, not beside it.** Egress and filesystem limits enforced at the tool-call wrapper are bypassable by any payload that uses ambient APIs. The only durable enforcement is a subprocess/VM sandbox whose ambient capability set is empty and whose mediated capabilities are exactly the declared scope. - **Pin the whole closure, not the entrypoint.** The manifest must carry hashes of the full import closure (entrypoint + dependencies), resolved eagerly at verification. Lazy resolution is a TOCTOU hole by construction. - **Make the integrity check un-suppressible.** Any operator-facing knob that downgrades a hash mismatch to a warning converts the defense into a liability, because operational noise (CDN re-serialization, compression) will reliably train operators to disable it. - **Advertise enforced scope, not declared scope.** The capability grant minted for the skill must be computed from the sandbox's actual enforced limits, and the manifest's declared scope must be checked as a *subset* of what the sandbox can enforce — if the manifest asks for more than the sandbox mediates, reject rather than silently over-grant or under-enforce. The operational invariant: **a skill's effective authority is the intersection of (a) the capabilities the sandbox actually mediates and enforces, and (b) the declared scope in the signed manifest — and the runtime must never report (b) as if it were the authority.** When (a) is broader than (b) because the payload can reach ambient capabilities, the skill has more authority than it declared, and the signature is irrelevant to that fact. ```python import hashlib import json from dataclasses import dataclass, field from typing import Callable class SkillLoadRefused(RuntimeError): """Raised when a skill cannot be safely loaded under the declared confinement posture.""" pass @dataclass class Manifest: publisher: str declared_tools: list[str] declared_egress: list[str] entrypoint_url: str entrypoint_sha256: str | None # advisory in weak configs closure_sha256: dict[str, str] = field(default_factory=dict) # path -> hash signature_valid: bool = False @dataclass class SandboxProfile: """What the runtime can ACTUALLY enforce below the payload.""" mediates_tool_calls: bool = True blocks_ambient_sockets: bool = False blocks_ambient_fs: bool = False enforces_egress_allowlist_at_process_level: bool = False def verify_and_load( manifest: Manifest, sandbox: SandboxProfile, fetch: Callable[[str], bytes], resolve_closure: Callable[[str], dict[str, bytes]], ) -> None: # 1. Provenance gate. if not manifest.signature_valid: raise SkillLoadRefused(f"manifest for {manifest.publisher} failed signature verification") # 2. Integrity gate: the signature must cover the full executable closure, # not just a declaration. An advisory-only hash is a TOCTOU hole. if not manifest.closure_sha256: raise SkillLoadRefused( "manifest carries no full-closure hash; entrypoint-only pinning is " "insufficient — payload can lazily import unverified dependencies" ) # 3. Eager resolution: fetch the entire closure now, hash every artifact, # so nothing is resolved after the gate passes. closure = resolve_closure(manifest.entrypoint_url) for path, blob in closure.items(): expected = manifest.closure_sha256.get(path) if expected is None: raise SkillLoadRefused(f"closure member {path!r} is not covered by the signed manifest") actual = hashlib.sha256(blob).hexdigest() if actual != expected: # Non-suppressible: no retry, no warning-only path. raise SkillLoadRefused(f"closure hash mismatch for {path!r}: {actual} != {expected}") # 4. Confinement gate: derive enforced scope from the sandbox, and require # that the sandbox can actually mediate everything the manifest declares. needs_egress = bool(manifest.declared_egress) needs_fs = any(t.startswith("fs.") for t in manifest.declared_tools) if needs_egress and not ( sandbox.enforces_egress_allowlist_at_process_level and sandbox.blocks_ambient_sockets ): raise SkillLoadRefused( "manifest declares egress scope but sandbox does not enforce it at the " "process level; payload could bypass mediated tool calls via ambient sockets" ) if needs_fs and not (sandbox.blocks_ambient_fs and sandbox.mediates_tool_calls): raise SkillLoadRefused( "manifest declares fs scope but sandbox does not block ambient filesystem " "access; declared scope is not a confinement guarantee" ) # 5. Only now, with provenance + integrity + confinement all established, # mint the capability grant. The grant reflects ENFORCED scope. grant = { "tools": manifest.declared_tools, "egress": manifest.declared_egress, "enforced_by": "process-sandbox", } _execute(closure, grant) def _execute(closure: dict[str, bytes], grant: dict) -> None: # Executes inside the sandbox whose ambient capabilities are empty. ... ```