Skip to content
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

Host names are not working in allowed_hosts #40

Open
Stranger6667 opened this issue Feb 12, 2020 · 2 comments
Open

Host names are not working in allowed_hosts #40

Stranger6667 opened this issue Feb 12, 2020 · 2 comments

Comments

@Stranger6667
Copy link
Collaborator

Example from the README file:

import pytest
import requests

@pytest.mark.block_network(allowed_hosts=["httpbin.*"])
def test_access():
    assert requests.get("http://httpbin.org/get").text == '{"get": true}'
    with pytest.raises(RuntimeError, match=r"^Network is disabled$"):
        requests.get("http://example.com")

It will fail on the first line:

self = <socket.socket fd=10, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 0)>, address = ('100.25.11.135', 80), args = (), kwargs = {}, host = '100.25.11.135'

    def network_guard(self, address, *args, **kwargs):
        host = ""
        if self.family in (socket.AF_INET, socket.AF_INET6):
            host = address[0]
        elif self.family == socket.AF_UNIX:
            host = address
        if is_host_in_allowed_hosts(host, allowed_hosts):
            return original_func(self, address, *args, **kwargs)
>       raise RuntimeError("Network is disabled")
E       RuntimeError: Network is disabled

@Stranger6667
Copy link
Collaborator Author

Not sure if it is possible to do on this level since we don't have the domain name when we connect to a socket. As an alternative - we can patch some known libraries (as VCR does) and then filter there, but it would be a too broad scope change. Maybe we can remove support for domain names and support only IP addresses

@selevit
Copy link

selevit commented Jul 5, 2021

As an option - we can try to resolve the host ip if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants