diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 3889a7d15a..79448517e3 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -101,7 +101,7 @@ """ _LOCAL_IPS = frozenset({"127.0.0.1", "::1"}) -_LOCAL_DOMAINS = frozenset({"localhost", "127.0.0.1"}) +_LOCAL_DOMAINS = _LOCAL_IPS.union({"localhost"}) def env_to_bool(value: "Any", *, strict: "Optional[bool]" = False) -> "bool | None": @@ -2236,9 +2236,13 @@ def _is_localhost( if url_host is not None and _host_matches_local_domain(url_host): return True - for header_val in (host_header, forwarded_host_header): - if header_val is not None: - domain = header_val.split(":")[0] + for header in (host_header, forwarded_host_header): + if header is not None: + if header.startswith("["): + # Bracketed IPv6, e.g. [::1]:8080 or [::1] + domain = header.split("]")[0].strip("[]") + else: + domain = header.split(":")[0] if _host_matches_local_domain(domain): return True diff --git a/tests/test_utils.py b/tests/test_utils.py index d3d70026e8..ffe3ae7046 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1425,6 +1425,7 @@ def test_package_version_is_none(): # URL host (subdomain-aware matching) ({"url_host": "localhost"}, True), ({"url_host": "127.0.0.1"}, True), + ({"url_host": "::1"}, True), ({"url_host": "foo.localhost"}, True), ({"url_host": "bar.foo.localhost"}, True), ({"url_host": "foolocalhost"}, False), @@ -1434,6 +1435,8 @@ def test_package_version_is_none(): ({"host_header": "localhost"}, True), ({"host_header": "localhost:3000"}, True), ({"host_header": "127.0.0.1:8080"}, True), + ({"host_header": "[::1]"}, True), + ({"host_header": "[::1]:8080"}, True), ({"host_header": "foo.localhost"}, True), ({"host_header": "foo.localhost:8080"}, True), ({"host_header": "example.com"}, False), @@ -1442,6 +1445,7 @@ def test_package_version_is_none(): ({"forwarded_host_header": "localhost"}, True), ({"forwarded_host_header": "localhost:3000"}, True), ({"forwarded_host_header": "127.0.0.1:8080"}, True), + ({"forwarded_host_header": "[::1]:8080"}, True), ({"forwarded_host_header": "foo.localhost"}, True), ({"forwarded_host_header": "example.com"}, False), # No args