Skip to content

feat: also block DNS resolution - #482

Merged
miketheman merged 2 commits into
mainfrom
feat/disable-dns-resolution
May 21, 2026
Merged

miketheman merged 2 commits into
mainfrom
feat/disable-dns-resolution

Conversation

@miketheman

Copy link
Copy Markdown
Owner

--disable-socket (and the marker/fixture) now guards socket.getaddrinfo and socket.gethostbyname too. Before this, a test could quietly resolve a hostname with sockets blocked, so "no network" wasn't really no network.

The error names the call that got caught (socket.socket, socket.getaddrinfo, socket.gethostbyname) so failures point at the right one.

Restoration is identity-checked: we only swap a function back if our guard is still the one in place. The resolution-cache tests monkeypatch socket.getaddrinfo themselves, and without that check each inner pytester teardown would stomp the outer monkeypatch.

Closes #43. Supersedes #64.

@miketheman miketheman added the enhancement New feature or request label May 20, 2026
@miketheman
miketheman requested a review from Copilot May 20, 2026 01:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens pytest-socket’s “no network” guarantee by blocking DNS resolution APIs (socket.getaddrinfo, socket.gethostbyname) when sockets are disabled, and updates tests to cover the new behavior and messaging.

Changes:

  • Guard socket.getaddrinfo and socket.gethostbyname under --disable-socket (and marker/fixture), with clearer per-entrypoint error messages.
  • Restore the DNS functions only when the currently-installed function is the plugin’s guard (identity-checked restoration).
  • Add a dedicated DNS test module and loosen a shared assertion helper to match the expanded error surface.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/pytest_socket/__init__.py Adds DNS guards/restoration and updates SocketBlockedError messaging support.
tests/test_dns.py Introduces coverage for DNS blocking/restoration and error message clarity.
tests/common.py Broadens the helper assertion to accept the new blocked-call messages.
Comments suppressed due to low confidence (2)

src/pytest_socket/init.py:146

  • enable_socket() restores socket.getaddrinfo/socket.gethostbyname via assignment, but these lines may also trip strict mypy (CI runs uv run mypy src). Consider adding the same kind of typing workaround as other monkeypatch points in this file (e.g., # type: ignore[...]/cast) so type checking remains green.
    socket.socket = _true_socket  # type: ignore[misc]
    if socket.getaddrinfo is _guarded_getaddrinfo:
        socket.getaddrinfo = _true_getaddrinfo
    if socket.gethostbyname is _guarded_gethostbyname:
        socket.gethostbyname = _true_gethostbyname

src/pytest_socket/init.py:368

  • _remove_restrictions() now conditionally restores socket.getaddrinfo/socket.gethostbyname via assignment. As with the other new monkeypatch assignments, this may need an explicit # type: ignore[...] or cast to satisfy strict mypy (CI runs uv run mypy src).
    socket.socket.connect = _true_connect  # type: ignore[method-assign]
    if socket.getaddrinfo is _guarded_getaddrinfo:
        socket.getaddrinfo = _true_getaddrinfo
    if socket.gethostbyname is _guarded_gethostbyname:
        socket.gethostbyname = _true_gethostbyname

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pytest_socket/__init__.py Resolved
Comment thread tests/test_dns.py Resolved
`--disable-socket` (and the marker/fixture) now guards
`socket.getaddrinfo` and `socket.gethostbyname` too. Before this,
a test could quietly resolve a hostname with sockets blocked, so
"no network" wasn't really no network.

The error names the call that got caught (`socket.socket`,
`socket.getaddrinfo`, `socket.gethostbyname`) so failures point at
the right one.

Restoration is identity-checked: we only swap a function back if
our guard is still the one in place. The resolution-cache tests
monkeypatch `socket.getaddrinfo` themselves, and without that check
each inner pytester teardown would stomp the outer monkeypatch.

Closes #43. Supersedes #64.

Signed-off-by: Mike Fiedler <[email protected]>
@miketheman
miketheman force-pushed the feat/disable-dns-resolution branch from 310bf32 to 57591e0 Compare May 20, 2026 03:43
@miketheman
miketheman merged commit aae7f44 into main May 21, 2026
27 checks passed
@miketheman
miketheman deleted the feat/disable-dns-resolution branch May 21, 2026 13:18
miketheman added a commit that referenced this pull request Oct 12, 2026
Since DNS blocking landed in #482, resolve_hostnames called the patched
socket.getaddrinfo. With the README's conftest recipe (disable_socket()
in pytest_runtest_setup, which runs before the plugin's hook) and a
hostname in --allow-hosts, every test errored in setup with
SocketBlockedError, and the resolution cache never filled.

Call the saved _true_getaddrinfo instead, and point the test fixture
that fakes DNS at it.

Signed-off-by: Mike Fiedler <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Also block DNS lookups (socket.getaddrinfo)

2 participants