From 842c71d6b52b77e37441bb6afc4aa3d7b72aac75 Mon Sep 17 00:00:00 2001 From: James Riehl Date: Fri, 16 Aug 2024 08:49:42 +0100 Subject: [PATCH 1/5] chore: verify sync envelope if signed --- python/src/uagents/communication.py | 7 ++++--- python/tests/test_server.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/python/src/uagents/communication.py b/python/src/uagents/communication.py index b2433285..42a263e2 100644 --- a/python/src/uagents/communication.py +++ b/python/src/uagents/communication.py @@ -163,10 +163,11 @@ async def send_exchange_envelope( success = resp.status == 200 if success: if sync: - # If the message is synchronous but not verified, return the envelope env = Envelope.model_validate(await resp.json()) - if env.signature is None: - return env + if env.signature and not env.verify(): + errors.append( + "Received response envelope that failed verification" + ) return await dispatch_sync_response_envelope(env) return MsgStatus( status=DeliveryStatus.DELIVERED, diff --git a/python/tests/test_server.py b/python/tests/test_server.py index 7d600ec1..7a819f41 100644 --- a/python/tests/test_server.py +++ b/python/tests/test_server.py @@ -116,7 +116,7 @@ async def test_message_success_unsigned(self): ] ) - async def test_message_success_sync(self): + async def test_message_success_sync_unsigned(self): message = Message(message="hello") reply = Message(message="hey") user = generate_user_address() From ec0615a649ae981b57985f00b35d3b1b94859fa2 Mon Sep 17 00:00:00 2001 From: James Riehl Date: Fri, 16 Aug 2024 08:59:09 +0100 Subject: [PATCH 2/5] chore: move almanac api resolver warning to debug logging --- python/src/uagents/resolver.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/src/uagents/resolver.py b/python/src/uagents/resolver.py index 271ca526..3acad9bd 100644 --- a/python/src/uagents/resolver.py +++ b/python/src/uagents/resolver.py @@ -281,9 +281,9 @@ async def _api_resolve(self, destination: str) -> Tuple[Optional[str], List[str] if response.status_code != 200: if response.status_code != 404: - LOGGER.warning( - f"Failed to resolve agent {address} from {self._almanac_api_url}," - f"querying Almanac contract..." + LOGGER.debug( + f"Failed to resolve agent {address} from {self._almanac_api_url}, " + f"resolvong via Almanac contract..." ) return None, [] From 89992329e22634d39c8b2bb2bbecdce20c0f8403 Mon Sep 17 00:00:00 2001 From: James Riehl Date: Fri, 16 Aug 2024 09:00:01 +0100 Subject: [PATCH 3/5] fix: typo --- python/src/uagents/resolver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/src/uagents/resolver.py b/python/src/uagents/resolver.py index 3acad9bd..6247adcd 100644 --- a/python/src/uagents/resolver.py +++ b/python/src/uagents/resolver.py @@ -283,7 +283,7 @@ async def _api_resolve(self, destination: str) -> Tuple[Optional[str], List[str] if response.status_code != 404: LOGGER.debug( f"Failed to resolve agent {address} from {self._almanac_api_url}, " - f"resolvong via Almanac contract..." + f"resolving via Almanac contract..." ) return None, [] From 3bc046b580202c3d7729ed85159721311f3f3493 Mon Sep 17 00:00:00 2001 From: James Riehl Date: Fri, 16 Aug 2024 09:27:28 +0100 Subject: [PATCH 4/5] fix: continue to next endpoint on failed verification --- python/src/uagents/communication.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/src/uagents/communication.py b/python/src/uagents/communication.py index 42a263e2..adc95493 100644 --- a/python/src/uagents/communication.py +++ b/python/src/uagents/communication.py @@ -168,6 +168,7 @@ async def send_exchange_envelope( errors.append( "Received response envelope that failed verification" ) + continue return await dispatch_sync_response_envelope(env) return MsgStatus( status=DeliveryStatus.DELIVERED, @@ -366,7 +367,7 @@ async def send_sync_message( def enclose_response( message: Model, sender: str, session: UUID4, target: str = "" -) -> str: +) -> JsonStr: """ Enclose a response message within an envelope. @@ -391,7 +392,7 @@ def enclose_response_raw( sender: str, session: UUID4, target: str = "", -) -> str: +) -> JsonStr: """ Enclose a raw response message within an envelope. From fcaeecb5cbe12cbb0888d78ac4473b0428e28962 Mon Sep 17 00:00:00 2001 From: James Riehl Date: Fri, 16 Aug 2024 13:56:35 +0100 Subject: [PATCH 5/5] chore: catch failed verification exceptions --- python/src/uagents/communication.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/python/src/uagents/communication.py b/python/src/uagents/communication.py index adc95493..a1f7cc64 100644 --- a/python/src/uagents/communication.py +++ b/python/src/uagents/communication.py @@ -164,11 +164,16 @@ async def send_exchange_envelope( if success: if sync: env = Envelope.model_validate(await resp.json()) - if env.signature and not env.verify(): - errors.append( - "Received response envelope that failed verification" - ) - continue + if env.signature: + verified = False + try: + verified = env.verify() + except Exception as ex: + errors.append( + f"Received response envelope that failed verification: {ex}" + ) + if not verified: + continue return await dispatch_sync_response_envelope(env) return MsgStatus( status=DeliveryStatus.DELIVERED,