-
-
Notifications
You must be signed in to change notification settings - Fork 16
Fix P2P message validation and host parameter #64
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ def register_handler(self, handler_callback): | |
| raise ValueError("handler_callback must be callable") | ||
| self._handler_callback = handler_callback | ||
|
|
||
| async def start(self, port: int = 9000): | ||
| async def start(self, port: int = 9000, host: str = "127.0.0.1"): | ||
| """Start listening for incoming peer connections on the given port.""" | ||
| self._port = port | ||
| self._server = await asyncio.start_server( | ||
|
|
@@ -206,7 +206,13 @@ def _validate_block_payload(self, payload): | |
| def _validate_message(self, message): | ||
| if not isinstance(message, dict): | ||
| return False | ||
| if set(message) != {"type", "data"}: | ||
| # Allow _peer_addr field added by _listen_to_peer | ||
| required_fields = {"type", "data"} | ||
| if not required_fields.issubset(set(message)): | ||
| return False | ||
| # Reject messages with unexpected fields (except _peer_addr) | ||
| allowed_fields = {"type", "data", "_peer_addr"} | ||
| if not set(message).issubset(allowed_fields): | ||
|
quickquickcode marked this conversation as resolved.
Outdated
|
||
| return False | ||
|
Comment on lines
+213
to
217
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Consider simplifying to a single equality check. The bidirectional subset checks ( This properly addresses the past review feedback to keep 🔧 Proposed simplification def _validate_message(self, message):
if not isinstance(message, dict):
return False
- required_fields = {"type", "data"}
- if not required_fields.issubset(set(message)):
- return False
- if not set(message).issubset(required_fields):
+ if set(message) != {"type", "data"}:
return False🤖 Prompt for AI Agents |
||
|
|
||
| msg_type = message.get("type") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.