Repository navigation
fix: honour --allow-hosts for connect_ex, sendto and sendmsg - #575
Open
simin-yuan wants to merge 2 commits into
Open
simin-yuan wants to merge 2 commits into
simin-yuan wants to merge 2 commits into
Conversation
--allow-hosts leaves socket creation to the real class and only patched connect(), so connect_ex(), sendto() and sendmsg() reached any host. Route all of them through the same allow-list check and restore them on teardown. SocketConnectBlockedError grows an optional call name so the message says what was blocked, and pickling keeps it. Signed-off-by: Simin Yuan <281164480+simin-yuan@users.noreply.github.com>
for more information, see https://pre-commit.ci
This branch has not been deployed
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 #574
Summary
--allow-hostskeeps the realsocket.socketclass installed and only patchedconnect(), soconnect_ex(),sendto()andsendmsg()could reach any hostwhile the flag was set. All four entry points now run the same allow-list check
(host name, resolved address, CIDR range, Unix socket family), and all four are
restored on teardown.
Details
host_from_send_args()reads the destination ofsendto()/sendmsg(), whichis the trailing argument rather than the first.
SocketConnectBlockedErrortakes an optionalcallname (default"connect"), so a blockedsendto()no longer reports itself as a blockedconnect(). The name survives pickling._SLOTSand_is_ours()cover the new slots, so the fixture-teardownbracketing keeps treating them as ours rather than as another tool's patch.
Tests
tests/test_allow_hosts_transports.py:connect_ex(),sendto()andsendmsg()outside the list raiseSocketConnectBlockedErrorand warn;connect_ex()andsendto()to an allowed host still work (a real listener,and a real datagram);
_remove_restrictions()puts every guard back;pytesterrun with--disable-socket --allow-hosts=127.0.0.1where a UDP
sendto()to192.0.2.1:53is expected to raise.Against unpatched
main(d45b1cb) the same file gives4 failed, 2 passed, 1 skipped: the blocked cases do not raise, and the restore assertions see theguards still installed.
Checks
uv run mypy srcis clean. Full suite on Windows 11 / CPython 3.14.9:3 failed, 130 passed, 10 skipped; the three failures(
test_parametrize_with_socket_enabled_and_allow_hosts,test_global_disable_and_allow_host,test_single_cli_arg_connect_disabled_hostname_resolved) fail identically onunpatched
mainon that machine. The whole matrix — Linux, macOS and Windowsacross 3.10 to 3.15t, 24 jobs — passes on my fork before opening this. CI is
authoritative.