Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Format: [Semantic Versioning](https://semver.org/). Spec versions follow `MAJOR.

## [Unreleased]

- Compare challenge nonces as UTF-8 bytes so non-ASCII mismatches raise the documented ValueError instead of TypeError (#381). Matching strings remain accepted without Unicode normalization. Invalid non-string expected_nonce settings raise a clear ValueError. Reported by @chernistry; follow-up review by @lywinged.

- Report revocation bundles without an RFC 8785 form as `unverified_for_revocation`
with cause `bundle_malformed`, while continuing record verification (#382).
The direct bundle-digest API still refuses such input. Reported by @chernistry.
Expand Down
6 changes: 5 additions & 1 deletion src/agentrust_trace/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,12 @@ def verify_record(

# Freshness: bind to a caller-supplied nonce when provided.
if expected_nonce is not None:
if not isinstance(expected_nonce, str):
raise ValueError("expected_nonce must be a string")
actual_nonce = record.get("runtime", {}).get("nonce")
if not isinstance(actual_nonce, str) or not compare_digest(actual_nonce, expected_nonce):
if not isinstance(actual_nonce, str) or not compare_digest(
actual_nonce.encode("utf-8"), expected_nonce.encode("utf-8")
):
raise ValueError("record runtime.nonce does not match expected_nonce")

# Canonical bytes: record without "signature" key
Expand Down
59 changes: 59 additions & 0 deletions tests/test_sign.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for agentrust_trace.sign."""

import base64
import hmac
import json
import time

Expand Down Expand Up @@ -1186,3 +1187,61 @@ def test_the_guards_do_not_refuse_what_they_should_accept():

jwk_thumbprint(key_to_jwk(key))
verify_record(record, key_to_jwk(key))


@pytest.mark.parametrize("nonce", ["abc123", "n\u00f8nce", "\U0001f512"])
def test_verify_record_nonce_unicode_match(nonce):
key = generate_key()
record = _fresh_record()
record["runtime"]["nonce"] = nonce
signed = sign_record(record, key)
verify_record(signed, key_to_jwk(key), expected_nonce=nonce)


@pytest.mark.parametrize(
("actual", "expected"),
[
("wrong", "abc123"),
("n\u00f8nce", "a-nonce-value"),
("a-nonce-value", "n\u00f8nce"),
("\U0001f512", "\U0001f513"),
("\u00e9", "e\u0301"),
],
)
def test_verify_record_nonce_unicode_mismatch(actual, expected):
key = generate_key()
record = _fresh_record()
record["runtime"]["nonce"] = actual
signed = sign_record(record, key)
with pytest.raises(ValueError, match="record runtime.nonce does not match expected_nonce"):
verify_record(signed, key_to_jwk(key), expected_nonce=expected)


@pytest.mark.parametrize("expected", [b"a-nonce-value", 123, [], True, {}])
def test_verify_record_rejects_invalid_expected_nonce_configuration(expected):
key = generate_key()
record = _fresh_record()
record["runtime"]["nonce"] = "a-nonce-value"
signed = sign_record(record, key)
with pytest.raises(ValueError, match="^expected_nonce must be a string$"):
verify_record(signed, key_to_jwk(key), expected_nonce=expected)


def test_nonce_verdict_uses_compare_digest_result(monkeypatch):
key = generate_key()
record = _fresh_record()
record["runtime"]["nonce"] = "n\u00f8nce"
signed = sign_record(record, key)
real_compare = hmac.compare_digest
seen = []

def refuse_nonce(a, b):
if isinstance(a, bytes) and isinstance(b, bytes):
seen.append((a, b))
return False
return real_compare(a, b)

monkeypatch.setattr(hmac, "compare_digest", refuse_nonce)
with pytest.raises(ValueError, match="record runtime.nonce does not match expected_nonce"):
verify_record(signed, key_to_jwk(key), expected_nonce="n\u00f8nce")
assert seen == [("n\u00f8nce".encode("utf-8"), "n\u00f8nce".encode("utf-8"))]
Loading