fix(uagents): prevent sync-query future leak and cross-talk in ASGI server#910
Open
PrazwalR wants to merge 1 commit into
Open
fix(uagents): prevent sync-query future leak and cross-talk in ASGI server#910PrazwalR wants to merge 1 commit into
PrazwalR wants to merge 1 commit into
Conversation
…erver The ASGI /submit handler registered a response Future in _queries before verifying the envelope signature and confirming the target is routable, and never removed it on the verify-fail, unroutable, or sync-timeout paths. An unauthenticated caller could grow the dict without bound (key = attacker-controlled env.sender) — an unbounded-memory DoS. The map was also keyed by sender only, so concurrent sync queries from the same caller collided. - Register the Future only after signature + routing checks pass. - Key _queries by (sender, session) and clean it up in a finally. - Resolve replies by (sender, session) in context.send_raw. - Update _queries type hints accordingly.
PrazwalR
requested review from
Alejandro-Morales,
Archento,
jrriehl,
lrahmani and
qati
as code owners
July 6, 2026 10:40
There was a problem hiding this comment.
Pull request overview
This PR hardens the ASGI /submit sync-query mechanism by preventing _queries map leaks on failure/timeout paths and eliminating cross-talk between concurrent sync queries from the same sender by keying futures with (sender, session).
Changes:
- Move sync Future registration to occur only after signature verification and routing checks pass.
- Key pending sync-query futures by
(sender, session)and resolve replies using that tuple key. - Ensure
_queriescleanup viafinallyso timeouts/cancellations don’t leak entries.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/src/uagents/asgi.py | Registers sync-query futures later, uses (sender, session) keys, and guarantees cleanup in finally. |
| python/src/uagents/context.py | Updates send_raw typing/docs and resolves sync replies via (sender, session) to avoid cross-talk. |
| python/src/uagents/agent.py | Updates _queries type hints to reflect tuple-keyed futures. |
| python/tests/test_server.py | Updates test helper to locate tuple-keyed query futures. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
27
to
36
| async def mock_process_sync_message(self, sender: str, msg: Model): | ||
| while True: | ||
| if sender in self.agent._server._queries: | ||
| self.agent._server._queries[sender].set_result( | ||
| key = next( | ||
| (k for k in self.agent._server._queries if k[0] == sender), None | ||
| ) | ||
| if key is not None: | ||
| self.agent._server._queries[key].set_result( | ||
| (msg.model_dump_json(), Model.build_schema_digest(msg)) | ||
| ) | ||
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The ASGI
/submithandler leaked entries in the server-side sync-response map(
ASGIServer._queries) and could cross-wire concurrent sync queries._queries[env.sender]was created before the envelope signature was verifiedand before the target was confirmed routable, and it was never removed on the
failure paths:
return(entry leaked)return(entry leaked)Only the happy path (handler replies) removed the entry, via
context.send_raw.Impact
/submitneeds no auth. A callersending sync requests (
x-uagents-connection: sync) with auser...sender(signature check skipped) and any unroutable target adds a permanent dict entry
per request. The key is
env.sender, an arbitrary wire string, so each entry'ssize is attacker-controlled. The dict never shrinks.
_querieswas keyed bysenderonly, so twoin-flight sync queries from the same caller collided (second overwrote the
first's Future).
Fix
_queriesby(sender, session)so concurrent queries never collide.finallyaround the sync wait (happy path alreadydeletes it in
context.send_raw, sopop(..., None)is a safe no-op there).(sender, session)incontext.send_raw; update type hints.Files:
asgi.py,context.py,agent.py(type hints),tests/test_server.py(sync helper updated for the tuple key).
Testing
test_server,test_context,test_agent,test_protocol,test_msg_verify.on_messagehandler →ctx.send→ future resolved), plus failure/timeout/concurrency/load cases:every failure, timeout, and unroutable path leaves
_queriesempty; concurrentsame-sender / different-session queries each receive their own reply with no
cross-talk.
ruff checkclean on changed files.🤖 Generated with Claude Code