Fix #865: RFC: Security middleware layer for uAgent message handlers ... - #872
Open
JiwaniZakir wants to merge 1 commit into
Open
Conversation
JiwaniZakir
requested review from
Alejandro-Morales,
Archento,
jrriehl,
lrahmani and
qati
as code owners
April 16, 2026 06:07
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.
Closes #865
Proposed Changes
Adds an
allowed_sendersparameter toProtocol.on_message(and the correspondingAgent.on_messagewrapper) that restricts handler execution to a declared set of agent addresses.python/src/uagents/protocol.pyProtocol.on_messageandProtocol._add_message_handleraccept a newallowed_senders: set[str] | Noneparameter.allowed_sendersis provided,_add_message_handlerwraps the original callback in_guarded, an async closure that checkssenderagainst afrozensetof permitted addresses. Unauthorized senders produce alogger.warningand return without invoking the handler.python/src/uagents/agent.pyAgent.on_messagepasses the newallowed_sendersargument through toself._protocol.on_message.python/tests/test_protocol.pytest_protocol_allowed_senders_registered: confirms the handler digest is stored in_signed_message_handlerswhenallowed_sendersis set.test_protocol_allowed_senders_blocks_unauthorized: exercises the_guardedwrapper end-to-end — verifies an unauthorized sender is dropped and an authorized sender reaches the callback.Linked Issues
Addresses the authorization gap described in #865: any signed message from any registered agent could trigger any handler regardless of whether the receiving agent should accept commands from that sender.
Types of changes
Checklist
If applicable
python/scripts/generate_api_docs.py)Further comments
This is a minimal, in-framework authorization primitive that doesn't require an external policy gateway. It operates at the handler registration site, so the allow-list is co-located with the handler definition and remains visible in code review. The
frozensetconversion in_add_message_handlerensures the provided set is not mutated after registration.The
_guardedwrapper only applies whenallowed_senders is not None; omitting the parameter preserves existing behaviour exactly. No existing handler registration paths are affected.This approach is intentionally narrower in scope than the full SINT middleware proposed in #865 — it addresses the most common case (static allow-lists known at registration time) without introducing a runtime policy dependency. Dynamic or tier-based authorization (T0–T3 approval flows, rate limiting, CSML drift detection) remains out of scope for this PR and would require a separate middleware hook point in the dispatch layer.
This PR was created with AI assistance (Claude). The changes were reviewed by quality gates and a critic model before submission.