From e33b212df86300f7ccde9583237509e611224cba Mon Sep 17 00:00:00 2001 From: Diego Marcovecchio Date: Fri, 17 Feb 2023 13:01:58 -0300 Subject: [PATCH 1/3] Add whois info to the host string. --- pytest_socket.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pytest_socket.py b/pytest_socket.py index cef3eaa..263ef3c 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -1,5 +1,6 @@ import ipaddress import socket +from ipwhois import IPWhois import pytest @@ -16,9 +17,12 @@ class SocketConnectBlockedError(RuntimeError): def __init__(self, allowed, host, *_args, **_kwargs): if allowed: allowed = ",".join(allowed) + ip_whois = IPWhois(host).lookup_rdap(depth=1) + host_description = ip_whois.get("asn_description", None) + host_text = f"{host} ({host_description})" if host_description else host super().__init__( "A test tried to use socket.socket.connect() " - f'with host "{host}" (allowed: "{allowed}").' + f'with host "{host_text}" (allowed: "{allowed}").' ) From e0849ba4cacbddc98e077ef18ba20bd358958750 Mon Sep 17 00:00:00 2001 From: Diego Marcovecchio Date: Mon, 20 Feb 2023 11:42:02 -0300 Subject: [PATCH 2/3] Make DNS lookup safer. Add ipwhois requirement to pyproject.toml. --- pyproject.toml | 1 + pytest_socket.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b52324f..f732921 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.7" pytest = ">=3.6.3" +ipwhois = "~=1.2.0" [tool.poetry.dev-dependencies] coverage = "^7.0" diff --git a/pytest_socket.py b/pytest_socket.py index 263ef3c..803c835 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -1,6 +1,7 @@ import ipaddress import socket from ipwhois import IPWhois +from ipwhois.exceptions import HTTPLookupError, IPDefinedError import pytest @@ -17,8 +18,11 @@ class SocketConnectBlockedError(RuntimeError): def __init__(self, allowed, host, *_args, **_kwargs): if allowed: allowed = ",".join(allowed) - ip_whois = IPWhois(host).lookup_rdap(depth=1) - host_description = ip_whois.get("asn_description", None) + try: + ip_whois = IPWhois(host).lookup_rdap(depth=1) + host_description = ip_whois.get("asn_description", None) + except (HTTPLookupError, IPDefinedError): + host_description = None host_text = f"{host} ({host_description})" if host_description else host super().__init__( "A test tried to use socket.socket.connect() " From 60fa87b470f02168938d258218fd0c6d48c240d9 Mon Sep 17 00:00:00 2001 From: Diego Marcovecchio Date: Mon, 20 Feb 2023 17:30:29 -0300 Subject: [PATCH 3/3] Relax ipwhois version constraint. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f732921..40a995e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.7" pytest = ">=3.6.3" -ipwhois = "~=1.2.0" +ipwhois = ">= 1.0.0" [tool.poetry.dev-dependencies] coverage = "^7.0"