Skip to content

Commit b21a144

Browse files
sean0407SeanIwaniukooo11
authored
fix: skip malformed agent card signature headers (a2aproject#1335)
## Summary ```diff for each AgentCard signature: + skip malformed base64, UTF-8, or JSON protected headers attempt verification of remaining signatures ``` The malformed-header failure now reaches the existing `InvalidSignaturesError` result when no signature verifies, while a later valid signature can still verify. Fixes a2aproject#1332. ## Evidence - **Before:** The issue reproduction raised raw `binascii.Error` for `protected='!!!not-base64url!!!'` and stopped before later signatures. - **After:** The regression test confirms malformed-only input raises `InvalidSignaturesError` and malformed-then-valid input verifies. `./scripts/lint.sh` passed; `uv run pytest --cov=src --cov-report=term-missing` reported 2,270 passed, 183 skipped, 3 xfailed, 1 xpassed, and 93% total coverage. ## Merge Danger **Door:** Two-way **Blast Radius:** Low Only malformed protected-header parsing is skipped; key-provider and signature-verification handling remain unchanged. --------- Co-authored-by: Sean <sean@SeandeMac-mini-2.local> Co-authored-by: Sean Li <22557931+sean0407@users.noreply.github.com> Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent ca3ef0d commit b21a144

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎src/a2a/utils/signing.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import binascii
12
import json
23

34
from collections.abc import Callable
@@ -144,6 +145,13 @@ def signature_verifier(
144145
agent_card_signature.protected.encode('utf-8')
145146
).decode('utf-8')
146147
protected_header = json.loads(protected_header_json)
148+
except (binascii.Error, UnicodeDecodeError, json.JSONDecodeError):
149+
continue
150+
151+
if not isinstance(protected_header, dict):
152+
continue
153+
154+
try:
147155
kid = protected_header.get('kid')
148156
jku = protected_header.get('jku')
149157
verification_key = key_provider(kid, jku)

‎tests/utils/test_signing.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,42 @@ def test_signer_and_verifier_symmetric_multiple_signatures(
143143
verifier_wrong_key(signed_card)
144144

145145

146+
@pytest.mark.parametrize(
147+
'protected',
148+
[
149+
pytest.param('!!!not-base64url!!!', id='invalid-base64url'),
150+
pytest.param(
151+
base64url_encode(b'\xff').decode('utf-8'), id='invalid-utf8'
152+
),
153+
pytest.param(base64url_encode(b'{').decode('utf-8'), id='invalid-json'),
154+
pytest.param(base64url_encode(b'[]').decode('utf-8'), id='non-object'),
155+
],
156+
)
157+
def test_verifier_skips_malformed_protected_header(
158+
sample_agent_card: AgentCard,
159+
protected: str,
160+
):
161+
key = 'key12345'
162+
signer = signing.create_agent_card_signer(
163+
signing_key=key,
164+
protected_header={'alg': 'HS384', 'kid': 'key1', 'jku': None},
165+
)
166+
verifier = signing.create_signature_verifier(
167+
create_key_provider(key), ['HS256', 'HS384', 'ES256', 'RS256']
168+
)
169+
malformed = AgentCardSignature(protected=protected, signature='invalid')
170+
171+
invalid_card = AgentCard()
172+
invalid_card.CopyFrom(sample_agent_card)
173+
invalid_card.signatures.append(malformed)
174+
with pytest.raises(signing.InvalidSignaturesError):
175+
verifier(invalid_card)
176+
177+
valid_card = signer(sample_agent_card)
178+
valid_card.signatures.insert(0, malformed)
179+
verifier(valid_card)
180+
181+
146182
def test_signer_and_verifier_asymmetric(sample_agent_card: AgentCard):
147183
"""Test the agent card signing and verification process with an asymmetric key encryption."""
148184
private_key = ec.generate_private_key(ec.SECP256R1())

0 commit comments

Comments
 (0)