Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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
Expand Down
Loading