Repository navigation
feat(wsgi): Add sentry.is_localhost for inbound filters
#7780
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,9 @@ | |
| be affected by this limit if they have a custom recursion limit. | ||
| """ | ||
|
|
||
| _LOCAL_IPS = frozenset({"127.0.0.1", "::1"}) | ||
|
sentrivana marked this conversation as resolved.
Resolved
|
||
| _LOCAL_DOMAINS = frozenset({"localhost", "127.0.0.1"}) | ||
|
Check warning on line 104 in sentry_sdk/utils.py
|
||
|
sentrivana marked this conversation as resolved.
Resolved
sentrivana marked this conversation as resolved.
Resolved
sentrivana marked this conversation as resolved.
Resolved
Comment on lines
+103
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IPv6 loopback Host headers are not detected as localhost
Evidence
Also found at 3 additional locations
Identified by Warden · find-bugs · HAM-7BK |
||
|
|
||
|
|
||
| def env_to_bool(value: "Any", *, strict: "Optional[bool]" = False) -> "bool | None": | ||
| """Casts an ENV variable value to boolean using the constants defined above. | ||
|
|
@@ -2205,6 +2208,43 @@ | |
| return {"value": safe_repr(val), "type": "string"} | ||
|
|
||
|
|
||
| def _host_matches_local_domain(host: str) -> bool: | ||
| """Check if host matches a local domain, including subdomains like foo.localhost.""" | ||
| if host in _LOCAL_DOMAINS: | ||
| return True | ||
| for domain in _LOCAL_DOMAINS: | ||
| if host.endswith("." + domain): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def _is_localhost( | ||
| client_ip: "Optional[str]" = None, | ||
| url_host: "Optional[str]" = None, | ||
| host_header: "Optional[str]" = None, | ||
| forwarded_host_header: "Optional[str]" = None, | ||
| ) -> bool: | ||
| """ | ||
| Determine if a request originates from localhost. | ||
|
|
||
| Based on the logic in relay's localhost filter. | ||
| """ | ||
| if client_ip is not None and client_ip in _LOCAL_IPS: | ||
| return True | ||
|
sentrivana marked this conversation as resolved.
Resolved
|
||
|
|
||
| # URL host uses subdomain-aware matching | ||
| 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] | ||
| if _host_matches_local_domain(domain): | ||
| return True | ||
|
Check warning on line 2243 in sentry_sdk/utils.py
|
||
|
sentrivana marked this conversation as resolved.
Resolved
Comment on lines
+2241
to
+2243
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IPv6 loopback Host headers are not classified as localhost
Evidence
Identified by Warden · code-review · WMM-RCU |
||
|
|
||
| return False | ||
|
|
||
|
|
||
| # This noop context manager can be replaced with "from contextlib import nullcontext" when we drop Python 3.6 support | ||
| @contextmanager | ||
| def nullcontext() -> "Iterator[None]": | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.