diff --git a/CHANGELOG.md b/CHANGELOG.md index f6b95d16..78e073f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- **Breaking:** an Agent Manifest binding now requires issuer key + authorization (Agent Manifest spec §5.3.3). The trust anchor at + `agent_manifest.trust_anchor_path` carries an `issuers` list of SPIFFE URIs + per key, and the gateway passes it to the SDK as `trusted_key_issuers`. + Before, cMCP passed no issuer policy and agent-manifest 0.15.0 skipped the + check, so any trusted key could sign a manifest naming any issuer. Startup + now stops with `AGENT_MANIFEST_BINDING_FAILED` when no key in the trust + anchor lists `issuers`, and when the signing key is not authorized for the + manifest's `issuer`. `cmcp verify` and `verify_trace_claim` read the same + policy (new `trusted_agent_manifest_key_issuers` argument) and report the + binding unverified without it. The trust anchor is parsed once for keys and + policy, and loading it fails on a malformed SPIFFE ID in `issuers` or a + repeated `key_id`. Found by @litzki-systems on + agentrust-io/agent-manifest#493. - Test fixtures and docs use vendor-neutral example model names. - LICENSE and NOTICE name the copyright holder as AgenTrust Contributors. The previous LICENSE line credited Agentic AI Foundation contributors, but the diff --git a/docs/configuration.md b/docs/configuration.md index ccb82da6..a7ee6466 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -99,12 +99,12 @@ policy_reload_interval_seconds: 0 An Agent Manifest is a signed document that declares which agent this is and which policy and tool catalog it was approved with. Setting this block makes the gateway check that manifest before it accepts any session. -All fields are optional as a group. If `path` is set, `trust_anchor_path` must also be set. When the block is configured, cMCP fails closed on manifest signature failure, subject mismatch, policy hash drift, or catalog hash drift. +All fields are optional as a group. If `path` is set, `trust_anchor_path` must also be set. When the block is configured, cMCP fails closed on manifest signature failure, a signing key not authorized for the manifest's issuer, subject mismatch, policy hash drift, or catalog hash drift. | Field | Type | Default | Description | |-------|------|---------|-------------| | `path` | string | none | Path to the signed Agent Manifest JSON document. Path traversal (`..` components) is rejected. | -| `trust_anchor_path` | string | none | Path to a JSON trust anchor containing the issuer Ed25519 public key, either as `{ "key_id": "...", "public_key_base64url": "..." }` or `{ "keys": [...] }`. | +| `trust_anchor_path` | string | none | Path to a JSON trust anchor containing the issuer Ed25519 public key and the issuers it may sign for, either as `{ "key_id": "...", "public_key_base64url": "...", "issuers": ["spiffe://..."] }` or `{ "keys": [...] }` with the same fields per key. `issuers` is required (Agent Manifest spec §5.3.3): a trust anchor in which no key lists `issuers` stops startup with `AGENT_MANIFEST_BINDING_FAILED`, and a key that does not list the manifest's `issuer` is not authorized to sign it. Each issuer must be a well-formed SPIFFE ID (no userinfo, port, query or fragment), and a repeated `key_id` is refused. | | `authenticated_subject` | string | none | SPIFFE URI for the authenticated agent subject. This must equal `manifest.agent_id`. In production this should come from the agent SVID/mTLS identity; the config field is the current runtime input for that subject. | | `revocation_list_path` | string | none | Path to a JSON Lines revocation list, one record per line with `manifest_id`, `revoked_at`, `reason` and `revoked_by` (the format `manifest revoke` writes and `manifest verify --crl-path` reads). A manifest listed there fails startup. A missing, unreadable or malformed file also fails startup rather than being read as empty. Requires `path`. Without it, revocation is not checked and startup logs a warning. The list is read at startup only, so a revocation added later takes effect on the next restart. | diff --git a/src/cmcp_runtime/agent_manifest.py b/src/cmcp_runtime/agent_manifest.py index 5ac46120..7f57e279 100644 --- a/src/cmcp_runtime/agent_manifest.py +++ b/src/cmcp_runtime/agent_manifest.py @@ -178,31 +178,99 @@ def load_agent_manifest(path: str) -> dict[str, Any]: return load_agent_manifest_document(path).manifest -def load_agent_manifest_trust_anchor(path: str) -> dict[str, bytes]: - """Load issuer public keys from a JSON trust anchor file.""" +# SPIFFE ID grammar (SPIFFE ID specification, section 2): a non-empty trust +# domain of lowercase letters, digits, ".", "-" and "_", with no userinfo, port, +# query or fragment, then optional path segments of letters, digits, ".", "-" +# and "_", none empty, "." or "..". +_SPIFFE_SCHEME = "spiffe://" +_SPIFFE_TRUST_DOMAIN = re.compile(r"[a-z0-9._-]{1,255}") +_SPIFFE_PATH_SEGMENT = re.compile(r"[A-Za-z0-9._-]+") +_MAX_SPIFFE_ID_BYTES = 2048 + + +def _is_spiffe_id(value: object) -> bool: + """Whether *value* is a well-formed SPIFFE ID.""" + if not isinstance(value, str) or not value.startswith(_SPIFFE_SCHEME): + return False + try: + encoded = value.encode("utf-8") + except UnicodeEncodeError: + # A lone surrogate survives json.loads but is not a SPIFFE ID. + return False + if len(encoded) > _MAX_SPIFFE_ID_BYTES: + return False + trust_domain, has_path, path = value[len(_SPIFFE_SCHEME) :].partition("/") + if not _SPIFFE_TRUST_DOMAIN.fullmatch(trust_domain): + return False + if not has_path: + return True + return all( + _SPIFFE_PATH_SEGMENT.fullmatch(segment) and segment not in {".", ".."} + for segment in path.split("/") + ) + + +def load_agent_manifest_trust_policy( + path: str, +) -> tuple[dict[str, bytes], dict[str, list[str]] | None]: + """Load a trust anchor's keys and its issuer policy (spec 5.3.3) in one parse. + + Each key is ``public_key_base64url`` with an optional ``key_id`` and an + optional ``issuers`` list of SPIFFE IDs that key may sign for, in the + single-key form or on each entry of ``keys[]``. A key with no ``issuers`` + is authorized for no issuer. + + The policy is None when no key in the file declares ``issuers``, which is + a trust anchor written before issuer authorization existed. The binding + refuses that rather than reading it as "any issuer". A repeated key_id or + a malformed issuer is a ConfigError, so neither can enter the policy. + """ try: with Path(path).open() as f: raw = json.load(f) except (OSError, json.JSONDecodeError) as exc: raise ConfigError(f"Cannot read Agent Manifest trust anchor: {exc}") from exc - anchors: dict[str, bytes] = {} if isinstance(raw, dict) and "public_key_base64url" in raw: - public_key = _b64url_decode(str(raw["public_key_base64url"])) - key_id = str(raw.get("key_id") or _key_id(public_key)) + entries = [raw] + elif isinstance(raw, dict) and isinstance(raw.get("keys"), list): + entries = raw["keys"] + else: + raise ConfigError( + "Agent Manifest trust anchor must contain public_key_base64url or keys[]" + ) + + anchors: dict[str, bytes] = {} + policy: dict[str, list[str]] = {} + declared = False + for item in entries: + if not isinstance(item, dict): + raise ConfigError("Agent Manifest trust anchor keys must be objects") + public_key = _b64url_decode(str(item.get("public_key_base64url", ""))) + key_id = str(item.get("key_id") or _key_id(public_key)) + if key_id in anchors: + raise ConfigError(f"Agent Manifest trust anchor repeats key_id {key_id!r}") anchors[key_id] = public_key - return anchors - if isinstance(raw, dict) and "keys" in raw and isinstance(raw["keys"], list): - for item in raw["keys"]: - if not isinstance(item, dict): - raise ConfigError("Agent Manifest trust anchor keys must be objects") - public_key = _b64url_decode(str(item.get("public_key_base64url", ""))) - key_id = str(item.get("key_id") or _key_id(public_key)) - anchors[key_id] = public_key - return anchors - raise ConfigError( - "Agent Manifest trust anchor must contain public_key_base64url or keys[]" - ) + if "issuers" not in item: + continue + declared = True + issuers = item["issuers"] + if not isinstance(issuers, list) or not all(_is_spiffe_id(i) for i in issuers): + raise ConfigError( + "Agent Manifest trust anchor issuers must be a list of SPIFFE IDs" + ) + policy[key_id] = list(issuers) + return anchors, (policy if declared else None) + + +def load_agent_manifest_trust_anchor(path: str) -> dict[str, bytes]: + """Load issuer public keys from a JSON trust anchor file.""" + return load_agent_manifest_trust_policy(path)[0] + + +def load_agent_manifest_issuer_policy(path: str) -> dict[str, list[str]] | None: + """Load which issuers each trust-anchor key may sign for (spec 5.3.3).""" + return load_agent_manifest_trust_policy(path)[1] # A revocation list is operator-supplied, so it is bounded the same way the @@ -318,6 +386,11 @@ def _raise_for_sdk_result(result: Any, *, require_runtime_artifacts: bool) -> No # as "tool_manifest.catalog_hash"; to an operator it is the same failure. if mismatch_fields & {"tool_manifest", "tool_manifest.catalog_hash"}: raise ConfigError("Agent Manifest tool catalog hash does not match runtime catalog") + if "signature.issuer" in mismatch_fields: + raise ConfigError( + "Agent Manifest signing key is not authorized for the manifest issuer " + "(spec 5.3.3)" + ) if "signature" in mismatch_fields: raise ConfigError("Agent Manifest signature verification failed") if result.result == agent_manifest_sdk.OverallResult.REVOKED: @@ -354,6 +427,7 @@ def _verify_with_sdk( envelope: bytes | None = None, revocations: Any = None, runtime_catalog: ToolCatalog | None = None, + trusted_key_issuers: dict[str, list[str]] | None = None, ) -> str | None: """Appraise the manifest; return the COSE signer's key id when there is exactly one.""" sealed_catalog_hash = tool_catalog_hash @@ -400,6 +474,7 @@ def _verify_with_sdk( else None ), trusted_keys=_trusted_keys_for_sdk(trusted_keys), + trusted_key_issuers=trusted_key_issuers, # The gateway performs a deliberately scoped appraisal. It holds the # running policy bundle and tool catalog and verifies those two # bindings; it does not hold the agent's system prompt or model @@ -591,6 +666,7 @@ def verify_agent_manifest_binding( envelope: bytes | None = None, revocations: Any = None, runtime_catalog: ToolCatalog | None = None, + trusted_key_issuers: dict[str, list[str]] | None = None, ) -> AgentManifestBinding: """Verify manifest signature and bind it to runtime session inputs. @@ -611,7 +687,19 @@ def verify_agent_manifest_binding( manifest does not bind. The experimental evidence-requirements profile always binds this way and must also sign the sealed digest. + + *trusted_key_issuers* maps each trusted key_id to the issuers it may sign + for (load_agent_manifest_trust_policy). It is required: spec 5.3.3 forbids + a VALID manifest whose signing key has not been authorized for its declared + issuer, so a binding without the policy is refused rather than accepted on + the signature alone. """ + if trusted_key_issuers is None: + raise ConfigError( + "Agent Manifest trust anchor authorizes no issuer: add an issuers list " + "to each key so the signing key can be authorized for the manifest " + "issuer (spec 5.3.3)" + ) manifest_id, agent_id, issuer, key_id, manifest_policy, manifest_catalog = ( _manifest_binding_fields(manifest) ) @@ -633,6 +721,7 @@ def verify_agent_manifest_binding( envelope=envelope, revocations=revocations, runtime_catalog=runtime_catalog, + trusted_key_issuers=trusted_key_issuers, ) # A v0.2 manifest has no detached signature block; its signer is the key # that verified the COSE envelope. diff --git a/src/cmcp_runtime/cli.py b/src/cmcp_runtime/cli.py index e0952705..deef4890 100644 --- a/src/cmcp_runtime/cli.py +++ b/src/cmcp_runtime/cli.py @@ -254,7 +254,7 @@ def verify( from cmcp_runtime.agent_manifest import ( load_agent_manifest, - load_agent_manifest_trust_anchor, + load_agent_manifest_trust_policy, ) from cmcp_verify import ApprovedHashes, verify_audit_bundle, verify_trace_claim @@ -273,10 +273,10 @@ def verify( ) manifest_json = load_agent_manifest(agent_manifest) if agent_manifest is not None else None - manifest_keys = ( - load_agent_manifest_trust_anchor(agent_manifest_trust_anchor) + manifest_keys, manifest_key_issuers = ( + load_agent_manifest_trust_policy(agent_manifest_trust_anchor) if agent_manifest_trust_anchor is not None - else None + else (None, None) ) if agent_manifest is not None and agent_manifest_trust_anchor is None: click.echo( @@ -293,6 +293,7 @@ def verify( trusted_public_key_hex=trusted_key, agent_manifest=manifest_json, trusted_agent_manifest_keys=manifest_keys, + trusted_agent_manifest_key_issuers=manifest_key_issuers, trusted_tpm_ca_pem=tpm_ca_bundle, expected_launch_measurements=launch_measurements or None, ) diff --git a/src/cmcp_runtime/startup.py b/src/cmcp_runtime/startup.py index 0a9917d6..3e6a09ba 100644 --- a/src/cmcp_runtime/startup.py +++ b/src/cmcp_runtime/startup.py @@ -17,7 +17,7 @@ AgentManifestBinding, load_agent_manifest_document, load_agent_manifest_revocations, - load_agent_manifest_trust_anchor, + load_agent_manifest_trust_policy, verify_agent_manifest_binding, ) from cmcp_runtime.audit.keys import SigningKey @@ -733,7 +733,7 @@ def run_startup(config_path: str) -> RuntimeContext: if config.agent_manifest.path is not None and config.agent_manifest.trust_anchor_path is not None: try: loaded = load_agent_manifest_document(config.agent_manifest.path) - trusted_keys = load_agent_manifest_trust_anchor( + trusted_keys, trusted_key_issuers = load_agent_manifest_trust_policy( config.agent_manifest.trust_anchor_path ) revocations = ( @@ -757,6 +757,7 @@ def run_startup(config_path: str) -> RuntimeContext: enforcement_mode=config.attestation.enforcement_mode, allow_dev_subject_from_manifest=config.dev_mode, revocations=revocations, + trusted_key_issuers=trusted_key_issuers, ) except ConfigError as exc: _fatal("AGENT_MANIFEST_BINDING_FAILED", str(exc), action="startup_aborted") diff --git a/src/cmcp_verify/verify.py b/src/cmcp_verify/verify.py index acb71260..38eac917 100644 --- a/src/cmcp_verify/verify.py +++ b/src/cmcp_verify/verify.py @@ -896,6 +896,7 @@ def verify_trace_claim( trusted_public_key_hex: str | None = None, agent_manifest: dict[str, Any] | None = None, trusted_agent_manifest_keys: dict[str, bytes] | None = None, + trusted_agent_manifest_key_issuers: dict[str, list[str]] | None = None, trusted_ark_pem: bytes | None = None, trusted_intel_root_pem: bytes | None = None, trusted_tpm_ca_pem: bytes | None = None, @@ -1068,6 +1069,7 @@ def verify_trace_claim( allow_dev_subject_from_manifest=( agent_identity.get("subject_source") == "manifest-dev" ), + trusted_key_issuers=trusted_agent_manifest_key_issuers, ) expected_identity = { "manifest_id": binding.manifest_id, diff --git a/tests/unit/test_agent_manifest.py b/tests/unit/test_agent_manifest.py index af22137d..10bd5bb7 100644 --- a/tests/unit/test_agent_manifest.py +++ b/tests/unit/test_agent_manifest.py @@ -15,7 +15,9 @@ from cmcp_runtime import agent_manifest as cmcp_agent_manifest from cmcp_runtime.agent_manifest import ( SIGNED_FIELDS, + load_agent_manifest_issuer_policy, load_agent_manifest_trust_anchor, + load_agent_manifest_trust_policy, signing_pre_image, verify_agent_manifest_binding, ) @@ -25,6 +27,7 @@ POLICY_HASH = "sha256:" + "a" * 64 CATALOG_HASH = "sha256:" + "b" * 64 AGENT_ID = "spiffe://factory.example/agent/material-movement/dev" +ISSUER = "spiffe://factory.example/signing-authority/development" def _b64url(data: bytes) -> str: @@ -54,7 +57,7 @@ def _signed_manifest( "version": "0.1", "issued_at": "2026-06-12T00:00:00Z", "expires_at": expires_at, - "issuer": "spiffe://factory.example/signing-authority/development", + "issuer": ISSUER, "crypto_profile": "standard", "artifacts": { # agent-manifest 0.12.0 (GHSA-6hjj-gh3c-r6wv) enforces the @@ -95,6 +98,7 @@ def test_valid_manifest_binds_subject_policy_and_catalog() -> None: binding = verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -115,6 +119,7 @@ def fake_verify_manifest(manifest_arg, context, revocation_store): captured["trusted_keys"] = context.trusted_keys captured["policy_bundle_hash"] = context.policy_bundle_hash captured["tool_catalog_hash"] = context.tool_catalog_hash + captured["trusted_key_issuers"] = context.trusted_key_issuers assert isinstance(revocation_store, cmcp_agent_manifest.agent_manifest_sdk.RevocationStore) return cmcp_agent_manifest.agent_manifest_sdk.VerificationResult( manifest_id=manifest_arg["manifest_id"], @@ -135,6 +140,7 @@ def fake_verify_manifest(manifest_arg, context, revocation_store): binding = verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -147,6 +153,7 @@ def fake_verify_manifest(manifest_arg, context, revocation_store): "trusted_keys": {key_id: _b64url(pub)}, "policy_bundle_hash": POLICY_HASH, "tool_catalog_hash": CATALOG_HASH, + "trusted_key_issuers": {key_id: [ISSUER]}, } @@ -156,6 +163,7 @@ def test_dev_subject_fallback_is_marked_as_manifest_dev() -> None: binding = verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=None, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -173,6 +181,7 @@ def test_subject_mismatch_fails_closed() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject="spiffe://factory.example/agent/other/dev", policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -191,6 +200,7 @@ def test_enforcement_mode_mismatch_fails_closed() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -208,6 +218,7 @@ def test_enforcement_mode_not_provided_fails_closed() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -222,6 +233,7 @@ def test_tampered_manifest_signature_fails_closed() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -236,6 +248,7 @@ def test_policy_hash_drift_fails_closed() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash="sha256:" + "0" * 64, tool_catalog_hash=CATALOG_HASH, @@ -250,6 +263,7 @@ def test_catalog_hash_drift_fails_closed() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash="sha256:" + "0" * 64, @@ -264,6 +278,7 @@ def test_expired_manifest_fails_closed() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -285,6 +300,158 @@ def test_trust_anchor_loader_accepts_single_public_key(tmp_path: Path) -> None: assert load_agent_manifest_trust_anchor(str(path)) == {key_id: pub} +def test_issuer_policy_is_read_from_the_trust_anchor(tmp_path: Path) -> None: + _, pub, key_id = _keypair() + _, other_pub, other_id = _keypair() + path = tmp_path / "anchor.json" + path.write_text(json.dumps({"keys": [ + {"key_id": key_id, "public_key_base64url": _b64url(pub), "issuers": [ISSUER]}, + {"key_id": other_id, "public_key_base64url": _b64url(other_pub)}, + ]})) + assert load_agent_manifest_issuer_policy(str(path)) == {key_id: [ISSUER]} + + +def test_trust_anchor_without_issuers_yields_no_policy(tmp_path: Path) -> None: + _, pub, key_id = _keypair() + path = tmp_path / "anchor.json" + path.write_text(json.dumps({"key_id": key_id, "public_key_base64url": _b64url(pub)})) + assert load_agent_manifest_issuer_policy(str(path)) is None + + +def test_trust_anchor_issuers_must_be_spiffe_uris(tmp_path: Path) -> None: + _, pub, key_id = _keypair() + path = tmp_path / "anchor.json" + path.write_text(json.dumps({ + "key_id": key_id, "public_key_base64url": _b64url(pub), "issuers": "not-a-list", + })) + with pytest.raises(ConfigError, match="SPIFFE"): + load_agent_manifest_issuer_policy(str(path)) + + +def _write_anchor(tmp_path: Path, keys: list[dict]) -> str: + path = tmp_path / "anchor.json" + path.write_text(json.dumps({"keys": keys})) + return str(path) + + +@pytest.mark.parametrize( + "issuer", + [ + "spiffe://", + "spiffe:///signing-authority", + "spiffe://factory.example/signing-authority?env=dev", + "spiffe://factory.example/signing-authority#dev", + "spiffe://admin@factory.example/signing-authority", + "spiffe://factory.example:443/signing-authority", + "spiffe://Factory.example/signing-authority", + "spiffe://factory.example/", + "spiffe://factory.example//signing-authority", + "spiffe://factory.example/signing-authority/../other", + "https://factory.example/signing-authority", + "spiffe://factory.example/" + "a" * 2040, + "spiffe://factory.example/\ud800", + ], +) +def test_trust_anchor_refuses_a_malformed_spiffe_issuer(tmp_path: Path, issuer: str) -> None: + _, pub, key_id = _keypair() + path = _write_anchor( + tmp_path, [{"key_id": key_id, "public_key_base64url": _b64url(pub), "issuers": [issuer]}] + ) + with pytest.raises(ConfigError, match="SPIFFE"): + load_agent_manifest_trust_policy(path) + + +@pytest.mark.parametrize("issuer", [ISSUER, "spiffe://factory.example"]) +def test_trust_anchor_accepts_a_well_formed_spiffe_issuer(tmp_path: Path, issuer: str) -> None: + _, pub, key_id = _keypair() + path = _write_anchor( + tmp_path, [{"key_id": key_id, "public_key_base64url": _b64url(pub), "issuers": [issuer]}] + ) + assert load_agent_manifest_trust_policy(path) == ({key_id: pub}, {key_id: [issuer]}) + + +def test_trust_anchor_refuses_a_repeated_key_id(tmp_path: Path) -> None: + _, pub, key_id = _keypair() + _, other_pub, _ = _keypair() + path = _write_anchor( + tmp_path, + [ + {"key_id": key_id, "public_key_base64url": _b64url(pub), "issuers": [ISSUER]}, + {"key_id": key_id, "public_key_base64url": _b64url(other_pub), "issuers": [ISSUER]}, + ], + ) + with pytest.raises(ConfigError, match="repeats key_id"): + load_agent_manifest_trust_policy(path) + + +@pytest.mark.parametrize( + ("content", "message"), + [ + (None, "Cannot read"), + ("{not json", "Cannot read"), + ('{"keys": "not-a-list"}', "public_key_base64url or keys"), + ('{"keys": ["not-an-object"]}', "must be objects"), + ], +) +def test_trust_anchor_refuses_an_unreadable_or_misshapen_file( + tmp_path: Path, content: str | None, message: str +) -> None: + path = tmp_path / "anchor.json" + if content is not None: + path.write_text(content) + with pytest.raises(ConfigError, match=message): + load_agent_manifest_trust_policy(str(path)) + + +def _bind(manifest: dict, keys: dict[str, bytes], issuers: dict[str, list[str]] | None): + return verify_agent_manifest_binding( + manifest, + keys, + trusted_key_issuers=issuers, + authenticated_subject=AGENT_ID, + policy_bundle_hash=POLICY_HASH, + tool_catalog_hash=CATALOG_HASH, + enforcement_mode=EnforcementMode.ENFORCING, + ) + + +def test_a_key_may_be_authorized_for_several_issuers() -> None: + priv, pub, key_id = _keypair() + binding = _bind( + _signed_manifest(priv, key_id), + {key_id: pub}, + {key_id: ["spiffe://factory.example/other-authority", ISSUER]}, + ) + assert binding.manifest_id == "0197739a-8c00-7000-8000-000000000001" + + +def test_an_empty_issuers_list_authorizes_nothing(tmp_path: Path) -> None: + priv, pub, key_id = _keypair() + keys, issuers = load_agent_manifest_trust_policy( + _write_anchor(tmp_path, [{"key_id": key_id, "public_key_base64url": _b64url(pub), "issuers": []}]) + ) + assert issuers == {key_id: []} + with pytest.raises(ConfigError, match="not authorized for the manifest issuer"): + _bind(_signed_manifest(priv, key_id), keys, issuers) + + +def test_an_empty_issuer_policy_authorizes_nothing() -> None: + priv, pub, key_id = _keypair() + with pytest.raises(ConfigError, match="not authorized for the manifest issuer"): + _bind(_signed_manifest(priv, key_id), {key_id: pub}, {}) + + +def test_another_keys_authorization_does_not_cover_the_signer() -> None: + _, pub, key_id = _keypair() + signer_priv, signer_pub, signer_id = _keypair() + with pytest.raises(ConfigError, match="not authorized for the manifest issuer"): + _bind( + _signed_manifest(signer_priv, signer_id), + {key_id: pub, signer_id: signer_pub}, + {key_id: [ISSUER], signer_id: ["spiffe://factory.example/other-authority"]}, + ) + + def test_signing_pre_image_delegates_to_agent_manifest_sdk(monkeypatch) -> None: manifest = {"manifest_id": "0197739a-8c00-7000-8000-000000000001"} @@ -329,6 +496,7 @@ def test_a_mislabelled_post_quantum_manifest_fails_closed_cleanly() -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -360,6 +528,7 @@ def test_manifest_listed_in_revocation_list_is_rejected(tmp_path: Path) -> None: verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, @@ -379,6 +548,7 @@ def test_revocation_list_naming_another_manifest_still_binds(tmp_path: Path) -> binding = verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [ISSUER]}, authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, diff --git a/tests/unit/test_agent_manifest_cose.py b/tests/unit/test_agent_manifest_cose.py index ab25baba..63387b05 100644 --- a/tests/unit/test_agent_manifest_cose.py +++ b/tests/unit/test_agent_manifest_cose.py @@ -97,6 +97,7 @@ def _bind(loaded, trusted_keys): tool_catalog_hash=CATALOG_HASH, enforcement_mode=EnforcementMode.ENFORCING, envelope=loaded.envelope, + trusted_key_issuers={key_id: [ISSUER] for key_id in trusted_keys}, ) @@ -290,4 +291,5 @@ def test_experimental_profile_requires_the_served_catalog(catalog: str) -> None: policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, runtime_catalog=runtime, + trusted_key_issuers={}, ) diff --git a/tests/unit/test_intent_binding.py b/tests/unit/test_intent_binding.py index 9bf28b74..2eeac2ac 100644 --- a/tests/unit/test_intent_binding.py +++ b/tests/unit/test_intent_binding.py @@ -87,6 +87,7 @@ def _bind(manifest: dict, keys: dict[str, bytes]): authenticated_subject=AGENT_ID, policy_bundle_hash=POLICY_HASH, tool_catalog_hash=CATALOG_HASH, + trusted_key_issuers={key_id: [manifest["issuer"]] for key_id in keys}, ) diff --git a/tests/unit/test_manifest_tools_binding.py b/tests/unit/test_manifest_tools_binding.py index ae26d789..7e35b3da 100644 --- a/tests/unit/test_manifest_tools_binding.py +++ b/tests/unit/test_manifest_tools_binding.py @@ -114,6 +114,7 @@ def _bind( runtime_catalog=runtime, enforcement_mode=EnforcementMode.ENFORCING, envelope=envelope, + trusted_key_issuers={keypair.key_id: [ISSUER]}, ) diff --git a/tests/unit/test_startup.py b/tests/unit/test_startup.py index 31996975..5e3b256a 100644 --- a/tests/unit/test_startup.py +++ b/tests/unit/test_startup.py @@ -99,6 +99,7 @@ def _write_agent_manifest_files( "algorithm": "Ed25519", "key_id": key_id, "public_key_base64url": _b64url(pub), + "issuers": [manifest["issuer"]], }) ) return manifest_path, key_path @@ -282,6 +283,60 @@ def test_startup_fails_on_agent_manifest_subject_mismatch(complete_setup): assert exc_info.value.code == 1 +def _bind_with_anchor_issuers(complete_setup, issuers): + """Bind a valid manifest whose trust anchor declares *issuers* (None: omit).""" + config_path = Path(complete_setup) + tmp_path = config_path.parent + policy_hash = load_policy_bundle(str(tmp_path / "policy")).bundle_hash + catalog_hash = load_catalog(str(tmp_path / "catalog.json")).catalog_hash + manifest_path, key_path = _write_agent_manifest_files( + tmp_path, + policy_hash=policy_hash, + catalog_hash=catalog_hash, + ) + anchor = json.loads(key_path.read_text()) + if issuers is None: + del anchor["issuers"] + else: + anchor["issuers"] = issuers + key_path.write_text(json.dumps(anchor)) + config_path.write_text( + config_path.read_text() + + "\nagent_manifest:\n" + + f" path: {manifest_path}\n" + + f" trust_anchor_path: {key_path}\n" + + f" authenticated_subject: {AGENT_ID}\n" + ) + return run_startup(str(config_path)) + + +def test_startup_refuses_a_trust_anchor_that_authorizes_no_issuer(complete_setup, caplog): + """Spec 5.3.3: a signature from a trusted key is not enough. A trust anchor + written before issuer authorization existed names no issuer, so the gateway + cannot authorize the signing key for the manifest's issuer and refuses to + start rather than binding on the signature alone.""" + with caplog.at_level("CRITICAL", logger="cmcp_runtime.startup"), pytest.raises( + SystemExit + ) as exc_info: + _bind_with_anchor_issuers(complete_setup, None) + assert exc_info.value.code == 1 + assert any("5.3.3" in record.getMessage() for record in caplog.records) + + +def test_startup_refuses_a_key_not_authorized_for_the_manifest_issuer( + complete_setup, caplog +): + with caplog.at_level("CRITICAL", logger="cmcp_runtime.startup"), pytest.raises( + SystemExit + ) as exc_info: + _bind_with_anchor_issuers(complete_setup, ["spiffe://factory.example/other-authority"]) + assert exc_info.value.code == 1 + assert any( + "not authorized for the manifest issuer" in record.getMessage() + for record in caplog.records + ) + + def test_startup_fails_on_missing_config(tmp_path): """Conformance: startup exits on invalid config.""" with pytest.raises(SystemExit) as exc_info: diff --git a/tests/unit/test_verify.py b/tests/unit/test_verify.py index a43a012e..4a0974d5 100644 --- a/tests/unit/test_verify.py +++ b/tests/unit/test_verify.py @@ -8,6 +8,7 @@ import secrets from datetime import UTC, datetime, timedelta +import pytest from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat @@ -39,6 +40,7 @@ POLICY_HASH = "sha256:" + "a" * 64 CATALOG_HASH = "sha256:" + "b" * 64 AGENT_ID = "spiffe://factory.example/agent/material-movement/dev" +MANIFEST_ISSUER = "spiffe://factory.example/signing-authority/development" MANIFEST_ID = "0197739a-8c00-7000-8000-000000000001" @@ -158,7 +160,7 @@ def _signed_manifest( "version": "0.1", "issued_at": "2026-06-12T00:00:00Z", "expires_at": "2099-09-10T00:00:00Z", - "issuer": "spiffe://factory.example/signing-authority/development", + "issuer": MANIFEST_ISSUER, "crypto_profile": "standard", "artifacts": { # agent-manifest 0.12.0 (GHSA-6hjj-gh3c-r6wv) enforces the @@ -281,10 +283,38 @@ def test_agent_manifest_binding_is_verified(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.verified_fields +@pytest.mark.parametrize( + "issuers", + [None, {"other": ["spiffe://factory.example/other-authority"]}], + ids=["no-issuer-policy", "key-not-authorized"], +) +def test_agent_manifest_binding_requires_issuer_authorization(issuers): + """Spec 5.3.3: the manifest signature under a trusted key does not bind the + claim unless the key is authorized for the manifest's issuer.""" + priv, pub, key_id = _manifest_keypair() + manifest = _signed_manifest(priv, key_id) + identity = _agent_identity() + identity.issuer_key_id = key_id + claim_dict, _ = _make_signed_claim(agent_identity=identity) + if issuers is not None: + issuers = {key_id: issuers["other"]} + result = verify_trace_claim( + claim_dict, + _approved(), + agent_manifest=manifest, + trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers=issuers, + ) + assert "agent_manifest.binding" in result.unverified_fields + assert result.failure_reason == VerificationError.AGENT_MANIFEST_MISMATCH + assert "5.3.3" in result.details["agent_manifest"] + + def test_agent_manifest_binding_mismatch_fails(): priv, pub, key_id = _manifest_keypair() manifest = _signed_manifest(priv, key_id) @@ -296,6 +326,7 @@ def test_agent_manifest_binding_mismatch_fails(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.unverified_fields assert result.failure_reason == VerificationError.AGENT_MANIFEST_MISMATCH @@ -331,6 +362,7 @@ def test_agent_manifest_binding_intent_hash_mismatch_fails(): real_binding = verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [MANIFEST_ISSUER]}, authenticated_subject=AGENT_ID, authenticated_subject_source="config", policy_bundle_hash=POLICY_HASH, @@ -350,6 +382,7 @@ def test_agent_manifest_binding_intent_hash_mismatch_fails(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.unverified_fields assert result.failure_reason == VerificationError.AGENT_MANIFEST_MISMATCH @@ -368,6 +401,7 @@ def test_agent_manifest_binding_enforcement_mode_reaches_the_claim(): real_binding = verify_agent_manifest_binding( manifest, {key_id: pub}, + trusted_key_issuers={key_id: [MANIFEST_ISSUER]}, authenticated_subject=AGENT_ID, authenticated_subject_source="config", policy_bundle_hash=POLICY_HASH, @@ -386,6 +420,7 @@ def test_agent_manifest_binding_enforcement_mode_reaches_the_claim(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.verified_fields @@ -408,6 +443,7 @@ def test_agent_manifest_binding_enforcement_mode_mismatch_fails(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.unverified_fields assert result.failure_reason == VerificationError.AGENT_MANIFEST_MISMATCH @@ -428,6 +464,7 @@ def test_agent_manifest_binding_declared_mode_missing_from_claim_fails(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.unverified_fields @@ -451,6 +488,7 @@ def test_agent_manifest_binding_without_enforcement_mode_still_works(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.verified_fields @@ -472,6 +510,7 @@ def test_agent_manifest_binding_garbled_enforcement_mode_fails_closed(): _approved(), agent_manifest=manifest, trusted_agent_manifest_keys={key_id: pub}, + trusted_agent_manifest_key_issuers={key_id: [MANIFEST_ISSUER]}, ) assert "agent_manifest.binding" in result.unverified_fields assert result.failure_reason == VerificationError.AGENT_MANIFEST_MISMATCH