Skip to content
Open
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
98 changes: 0 additions & 98 deletions sentry_sdk/_werkzeug.py

This file was deleted.

16 changes: 13 additions & 3 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from functools import partial

import sentry_sdk
from sentry_sdk._werkzeug import get_host, _get_headers
from werkzeug.datastructures import EnvironHeaders
from werkzeug.wsgi import get_host
from sentry_sdk.api import continue_trace
from sentry_sdk.consts import OP
from sentry_sdk.scope import should_send_default_pii
Expand Down Expand Up @@ -62,9 +63,18 @@ def get_request_url(environ, use_x_forwarded_for=False):
path_info = environ.get("PATH_INFO", "").lstrip("/")
path = f"{script_name}/{path_info}"

if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ:
host = environ["HTTP_X_FORWARDED_HOST"]
if environ.get("wsgi.url_scheme") == "http" and host.endswith(":80"):
host = host[:-3]
elif environ.get("wsgi.url_scheme") == "https" and host.endswith(":443"):
host = host[:-4]
else:
host = get_host(environ)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: URL Construction Fails Without X-Forwarded-Host

The get_request_url function's URL construction logic changed. Host resolution with use_x_forwarded_for=True no longer correctly falls back to other headers if HTTP_X_FORWARDED_HOST is missing. Additionally, port stripping now silently skips if wsgi.url_scheme is absent from the environment, which can result in URLs retaining default ports.

Fix in Cursor Fix in Web


return "%s://%s/%s" % (
environ.get("wsgi.url_scheme"),
get_host(environ, use_x_forwarded_for),
host,
wsgi_decoding_dance(path).lstrip("/"),
)

Expand Down Expand Up @@ -286,7 +296,7 @@ def _make_wsgi_event_processor(environ, use_x_forwarded_for):
query_string = environ.get("QUERY_STRING")
method = environ.get("REQUEST_METHOD")
env = dict(_get_environ(environ))
headers = _filter_headers(dict(_get_headers(environ)))
headers = _filter_headers(dict(EnvironHeaders(environ)))

def event_processor(event, hint):
# type: (Event, Dict[str, Any]) -> Event
Expand Down
22 changes: 22 additions & 0 deletions tests/integrations/wsgi/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ def test_basic(sentry_init, crashing_app, capture_events):
}


def test_basic_django(sentry_init, crashing_app, capture_events):
sentry_init(send_default_pii=True)
app = SentryWsgiMiddleware(crashing_app, use_x_forwarded_for=True)
client = Client(app)
events = capture_events()

with pytest.raises(ZeroDivisionError):
client.get("/", environ_overrides={"HTTP_X_FORWARDED_HOST": "localhost:80"})

(event,) = events

assert event["transaction"] == "generic WSGI request"

assert event["request"] == {
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
"headers": {"Host": "localhost", "X-Forwarded-Host": "localhost:80"},
"method": "GET",
"query_string": "",
"url": "http://localhost/",
}


@pytest.mark.parametrize("path_info", ("bark/", "/bark/"))
@pytest.mark.parametrize("script_name", ("woof/woof", "woof/woof/"))
def test_script_name_is_respected(
Expand Down
Loading