Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/a2a/utils/signing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import binascii
import json

from collections.abc import Callable
Expand Down Expand Up @@ -144,6 +145,13 @@ def signature_verifier(
agent_card_signature.protected.encode('utf-8')
).decode('utf-8')
protected_header = json.loads(protected_header_json)
except (binascii.Error, UnicodeDecodeError, json.JSONDecodeError):
continue

if not isinstance(protected_header, dict):
continue

try:
kid = protected_header.get('kid')
jku = protected_header.get('jku')
verification_key = key_provider(kid, jku)
Expand Down
26 changes: 26 additions & 0 deletions tests/utils/test_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ def test_signer_and_verifier_symmetric_multiple_signatures(
verifier_wrong_key(signed_card)


def test_verifier_skips_malformed_protected_header(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extend the test to also cover json.JSONDecodeError, UnicodeDecodeError, and the not isinstance(protected_header, dict) guard

sample_agent_card: AgentCard,
):
key = 'key12345'
signer = signing.create_agent_card_signer(
signing_key=key,
protected_header={'alg': 'HS384', 'kid': 'key1', 'jku': None},
)
verifier = signing.create_signature_verifier(
create_key_provider(key), ['HS256', 'HS384', 'ES256', 'RS256']
)
malformed = AgentCardSignature(
protected='!!!not-base64url!!!', signature='invalid'
)

invalid_card = AgentCard()
invalid_card.CopyFrom(sample_agent_card)
invalid_card.signatures.append(malformed)
with pytest.raises(signing.InvalidSignaturesError):
verifier(invalid_card)

valid_card = signer(sample_agent_card)
valid_card.signatures.insert(0, malformed)
verifier(valid_card)


def test_signer_and_verifier_asymmetric(sample_agent_card: AgentCard):
"""Test the agent card signing and verification process with an asymmetric key encryption."""
private_key = ec.generate_private_key(ec.SECP256R1())
Expand Down
Loading