diff --git a/src/cmcp_verify/verify.py b/src/cmcp_verify/verify.py index b7a754f8..b04797e7 100644 --- a/src/cmcp_verify/verify.py +++ b/src/cmcp_verify/verify.py @@ -769,8 +769,7 @@ def verify_audit_bundle( ensure_ascii=True, ).encode() sig_b64 = ev.get("signature", "") - pad = 4 - (len(sig_b64) % 4) - sig = base64.urlsafe_b64decode(sig_b64 + ("=" * pad if pad != 4 else "")) + sig = _canonical_b64url_decode(sig_b64) pub.verify(sig, signing_input) except InvalidSignature: failures.append( @@ -880,12 +879,10 @@ def verify_audit_bundle( failures.append("claim has no confirmation key to check bundle_signature against") else: try: - pad = 4 - (len(x_b64) % 4) pub = Ed25519PublicKey.from_public_bytes( - base64.urlsafe_b64decode(x_b64 + ("=" * pad if pad != 4 else "")) + _canonical_b64url_decode(x_b64) ) - pad = 4 - (len(sig_b64) % 4) - sig = base64.urlsafe_b64decode(sig_b64 + ("=" * pad if pad != 4 else "")) + sig = _canonical_b64url_decode(sig_b64) digest = hashlib.sha256( json.dumps( entries, sort_keys=True, separators=(",", ":"), ensure_ascii=True diff --git a/tests/unit/test_audit_bundle_canonical_b64url.py b/tests/unit/test_audit_bundle_canonical_b64url.py new file mode 100644 index 00000000..a0cecc56 --- /dev/null +++ b/tests/unit/test_audit_bundle_canonical_b64url.py @@ -0,0 +1,58 @@ +"""Audit-bundle signatures must have exactly one accepted spelling. + +Follow-up to #804 / #808, which made claim verification canonical. +""" + +import base64 +import copy + +import pytest + +from cmcp_verify import verify_audit_bundle +from tests.unit.test_audit_bundle_session_epochs import _close, _session_with_a_call + +_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" + + +def _decode(value: str) -> bytes: + return base64.urlsafe_b64decode(value + "=" * (-len(value) % 4)) + + +def _signed_session(): + mgr, state, chain = _session_with_a_call() + return _close(mgr, state, chain) + + +def _verify_with_signature(claim, bundle, signature): + mutated = copy.deepcopy(bundle) + mutated["bundle_signature"] = signature + return verify_audit_bundle(mutated, claim) + + +def test_canonical_bundle_signature_verifies(): + claim, bundle = _signed_session() + result = verify_audit_bundle(bundle, claim) + assert result.verified, result.failures + + +def test_same_byte_alias_of_bundle_signature_is_rejected(): + claim, bundle = _signed_session() + signature = bundle["bundle_signature"] + index = _ALPHABET.index(signature[-1]) + # A canonical encoder leaves the 4 unused bits zero. + assert index % 16 == 0 + alias = signature[:-1] + _ALPHABET[index + 1] + assert alias != signature + assert _decode(alias) == _decode(signature) # same bytes, different spelling + assert not _verify_with_signature(claim, bundle, alias).verified + + +@pytest.mark.parametrize("junk", ["!!!!", "\n\n\n\n"]) +def test_bundle_signature_with_ignored_characters_is_rejected(junk): + claim, bundle = _signed_session() + signature = bundle["bundle_signature"] + # Four characters keep the padding arithmetic valid, so only a strict + # decoder can be what rejects this. + padded = signature[:10] + junk + signature[10:] + assert _decode(padded) == _decode(signature) + assert not _verify_with_signature(claim, bundle, padded).verified