|
| 1 | +"""The sample manifest was issued by Provenza (an MCP-served agent with one |
| 2 | +approved tool). The tests run offline against it.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import sys |
| 8 | +from datetime import datetime, timezone |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import agent_manifest._verify as sdk_verify |
| 12 | +import cbor2 |
| 13 | +import pytest |
| 14 | + |
| 15 | +SRC = Path(__file__).resolve().parents[1] / "src" |
| 16 | +sys.path.insert(0, str(SRC)) |
| 17 | + |
| 18 | +import verify_provenza_manifest as v # noqa: E402 |
| 19 | + |
| 20 | +HERE = Path(__file__).resolve().parent |
| 21 | +ENVELOPE = (HERE / "agent-1-manifest.cose").read_bytes() |
| 22 | +KEYS = json.loads((HERE / "trusted_keys.json").read_text()) |
| 23 | + |
| 24 | +# The sample's validity window: issued 2026-11-02T05:12:23Z, expires 2027-01-31T05:12:23Z. |
| 25 | +WITHIN = datetime(2026, 12, 1, tzinfo=timezone.utc) |
| 26 | +AFTER = datetime(2027, 2, 1, tzinfo=timezone.utc) |
| 27 | + |
| 28 | +ED25519, ML_DSA_65 = -19, -49 |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def clock(monkeypatch): |
| 33 | + """Fix the time the SDK's verification engine checks the validity window |
| 34 | + against, so a test does not change meaning when the sample expires.""" |
| 35 | + |
| 36 | + def set_now(now: datetime) -> None: |
| 37 | + class Frozen(datetime): |
| 38 | + @classmethod |
| 39 | + def now(cls, tz=None): |
| 40 | + return now if tz is None else now.astimezone(tz) |
| 41 | + |
| 42 | + monkeypatch.setattr(sdk_verify, "datetime", Frozen) |
| 43 | + |
| 44 | + return set_now |
| 45 | + |
| 46 | + |
| 47 | +def _keeping(algorithm: int) -> bytes: |
| 48 | + """The sample envelope with every signer but ``algorithm`` removed. The |
| 49 | + remaining signature is untouched and still verifies on its own: COSE_Sign |
| 50 | + signers do not cover each other.""" |
| 51 | + tag = cbor2.loads(ENVELOPE) |
| 52 | + protected, unprotected, payload, signatures = tag.value |
| 53 | + kept = [s for s in signatures if cbor2.loads(s[0])[1] == algorithm] |
| 54 | + assert len(kept) == 1 |
| 55 | + return cbor2.dumps(cbor2.CBORTag(tag.tag, [protected, unprotected, payload, kept]), canonical=True) |
| 56 | + |
| 57 | + |
| 58 | +def _run(tmp_path, envelope: bytes, capsys) -> tuple[int, dict]: |
| 59 | + path = tmp_path / "manifest.cose" |
| 60 | + path.write_bytes(envelope) |
| 61 | + keys = tmp_path / "keys.json" |
| 62 | + keys.write_text(json.dumps(KEYS)) |
| 63 | + code = v.main([str(path), "--keys", str(keys)]) |
| 64 | + return code, json.loads(capsys.readouterr().out) |
| 65 | + |
| 66 | + |
| 67 | +def test_signatures_catalog_and_profile(): |
| 68 | + report = v.check(ENVELOPE, KEYS) |
| 69 | + assert report["signatures_verified"] and report["signers"] == 2 |
| 70 | + assert report["algorithms"] == [ML_DSA_65, ED25519] and report["hybrid"] is True |
| 71 | + assert report["missing_signers"] == [] |
| 72 | + assert report["catalog_hash_matches"] and report["tools"] == ["files.read"] |
| 73 | + assert report["profile"] == "composition-only" |
| 74 | + assert "system_prompt" in report["unbound_artifacts"] and "policy_bundle" in report["unbound_artifacts"] |
| 75 | + |
| 76 | + |
| 77 | +def test_verdict_is_incomplete_not_valid(clock): |
| 78 | + # a composition-only manifest authenticates; it never claims to be complete |
| 79 | + clock(WITHIN) |
| 80 | + assert v.check(ENVELOPE, KEYS)["verdict"] == "INCOMPLETE" |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("kept, missing", [(ED25519, "ml-dsa-65"), (ML_DSA_65, "ed25519")]) |
| 84 | +def test_one_signer_is_not_the_hybrid_signature(tmp_path, capsys, clock, kept, missing): |
| 85 | + # The remaining signature verifies against the supplied key and the verdict |
| 86 | + # is unchanged; the envelope is still refused, whichever half is left. |
| 87 | + clock(WITHIN) |
| 88 | + code, report = _run(tmp_path, _keeping(kept), capsys) |
| 89 | + assert report["signatures_verified"] is True and report["signers"] == 1 |
| 90 | + assert report["algorithms"] == [kept] and report["verdict"] == "INCOMPLETE" |
| 91 | + assert report["hybrid"] is False and report["missing_signers"] == [missing] |
| 92 | + assert code == 1 |
| 93 | + |
| 94 | + |
| 95 | +def test_a_changed_payload_fails(): |
| 96 | + tag = cbor2.loads(ENVELOPE) |
| 97 | + parts = list(tag.value) |
| 98 | + parts[2] = parts[2].replace(b"files.read", b"files.rm!!") |
| 99 | + forged = cbor2.dumps(cbor2.CBORTag(tag.tag, parts), canonical=True) |
| 100 | + with pytest.raises(Exception): |
| 101 | + v.check(forged, KEYS) |
| 102 | + |
| 103 | + |
| 104 | +def test_unknown_keys_fail(): |
| 105 | + with pytest.raises(Exception): |
| 106 | + v.check(ENVELOPE, {"00" * 32: "AAAA"}) |
| 107 | + |
| 108 | + |
| 109 | +def test_cli_succeeds_within_the_validity_window(tmp_path, capsys, clock): |
| 110 | + clock(WITHIN) |
| 111 | + code, report = _run(tmp_path, ENVELOPE, capsys) |
| 112 | + assert report["hybrid"] is True and report["verdict"] == "INCOMPLETE" |
| 113 | + assert code == 0 |
| 114 | + |
| 115 | + |
| 116 | +def test_cli_fails_once_expired(tmp_path, capsys, clock): |
| 117 | + clock(AFTER) |
| 118 | + code, report = _run(tmp_path, ENVELOPE, capsys) |
| 119 | + assert report["hybrid"] is True and report["verdict"] == "EXPIRED" |
| 120 | + assert code == 1 |
0 commit comments