Skip to content

refactor(core): all errors in signature verification now throw an exception #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
39 changes: 21 additions & 18 deletions python/src/uagents/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,29 +261,32 @@ async def __call__(self, scope, receive, send): # pylint: disable=too-many-bra
return

expects_response = headers.get(b"x-uagents-connection") == b"sync"
do_verify = not is_user_address(env.sender)

if expects_response:
# Add a future that will be resolved once the query is answered
self._queries[env.sender] = asyncio.Future()

if do_verify and env.verify() is False:
await send(
{
"type": "http.response.start",
"status": 400,
"headers": [
[b"content-type", b"application/json"],
],
}
)
await send(
{
"type": "http.response.body",
"body": b'{"error": "signature verification failed"}',
}
)
return
if not is_user_address(env.sender): # verify signature if sent from agent
try:
env.verify()
except Exception as err:
self._logger.warning(f"Failed to verify envelope: {err}")
await send(
{
"type": "http.response.start",
"status": 400,
"headers": [
[b"content-type", b"application/json"],
],
}
)
await send(
{
"type": "http.response.body",
"body": b'{"error": "signature verification failed"}',
}
)
return

if not dispatcher.contains(env.target):
await send(
Expand Down
7 changes: 1 addition & 6 deletions python/src/uagents/crypto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,4 @@ def verify_digest(address: str, digest: bytes, signature: str) -> bool:
# build the verifying key
verifying_key = ecdsa.VerifyingKey.from_string(pk_data, curve=ecdsa.SECP256k1)

try:
result = verifying_key.verify_digest(sig_data, digest)
except ecdsa.keys.BadSignatureError:
return False

return result
return verifying_key.verify_digest(sig_data, digest)
3 changes: 1 addition & 2 deletions python/src/uagents/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def verify(self) -> bool:
bool: True if the signature is valid, False otherwise.
"""
if self.signature is None:
return False

raise ValueError("Envelope signature is missing")
return Identity.verify_digest(self.sender, self._digest(), self.signature)

def _digest(self) -> bytes:
Expand Down
13 changes: 8 additions & 5 deletions python/src/uagents/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ async def _handle_envelope(self, payload: dict):
self._logger.warning("Received invalid envelope")
return

do_verify = not is_user_address(env.sender)

if do_verify and env.verify() is False:
self._logger.warning("Received envelope that failed verification")
return
if not is_user_address(env.sender): # verify signature if sent from agent
try:
env.verify()
except Exception as err:
self._logger.warning(
"Received envelope that failed verification: %s", err
)
return

if not dispatcher.contains(env.target):
self._logger.warning("Received envelope for unrecognized address")
Expand Down
2 changes: 1 addition & 1 deletion python/src/uagents/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def sign(self, identity: Identity):

def verify(self) -> bool:
if self.signature is None:
return False
raise ValueError("Attestation signature is missing")
return Identity.verify_digest(
self.agent_address, self._build_digest(), self.signature
)
Expand Down
Loading