Skip to content

Commit 47d563c

Browse files
committed
ref(wsgi): Delete custom _werkzeug and use new Werkzeug version
Fixes: GH-3516
1 parent e012ff1 commit 47d563c

File tree

3 files changed

+35
-101
lines changed

3 files changed

+35
-101
lines changed

sentry_sdk/_werkzeug.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

sentry_sdk/integrations/wsgi.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from functools import partial
33

44
import sentry_sdk
5-
from sentry_sdk._werkzeug import get_host, _get_headers
5+
from werkzeug.datastructures import EnvironHeaders
6+
from werkzeug.wsgi import get_host
67
from sentry_sdk.api import continue_trace
78
from sentry_sdk.consts import OP
89
from sentry_sdk.scope import should_send_default_pii
@@ -62,9 +63,18 @@ def get_request_url(environ, use_x_forwarded_for=False):
6263
path_info = environ.get("PATH_INFO", "").lstrip("/")
6364
path = f"{script_name}/{path_info}"
6465

66+
if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ:
67+
host = environ["HTTP_X_FORWARDED_HOST"]
68+
if environ.get("wsgi.url_scheme") == "http" and host.endswith(":80"):
69+
host = host[:-3]
70+
elif environ.get("wsgi.url_scheme") == "https" and host.endswith(":443"):
71+
host = host[:-4]
72+
else:
73+
host = get_host(environ)
74+
6575
return "%s://%s/%s" % (
6676
environ.get("wsgi.url_scheme"),
67-
get_host(environ, use_x_forwarded_for),
77+
host,
6878
wsgi_decoding_dance(path).lstrip("/"),
6979
)
7080

@@ -286,7 +296,7 @@ def _make_wsgi_event_processor(environ, use_x_forwarded_for):
286296
query_string = environ.get("QUERY_STRING")
287297
method = environ.get("REQUEST_METHOD")
288298
env = dict(_get_environ(environ))
289-
headers = _filter_headers(dict(_get_headers(environ)))
299+
headers = _filter_headers(dict(EnvironHeaders(environ)))
290300

291301
def event_processor(event, hint):
292302
# type: (Event, Dict[str, Any]) -> Event

tests/integrations/wsgi/test_wsgi.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ def test_basic(sentry_init, crashing_app, capture_events):
6161
}
6262

6363

64+
def test_basic_django(sentry_init, crashing_app, capture_events):
65+
sentry_init(send_default_pii=True)
66+
app = SentryWsgiMiddleware(crashing_app, use_x_forwarded_for=True)
67+
client = Client(app)
68+
events = capture_events()
69+
70+
with pytest.raises(ZeroDivisionError):
71+
client.get("/", environ_overrides={"HTTP_X_FORWARDED_HOST": "localhost:80"})
72+
73+
(event,) = events
74+
75+
assert event["transaction"] == "generic WSGI request"
76+
77+
assert event["request"] == {
78+
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
79+
"headers": {"Host": "localhost", "X-Forwarded-Host": "localhost:80"},
80+
"method": "GET",
81+
"query_string": "",
82+
"url": "http://localhost/",
83+
}
84+
85+
6486
@pytest.mark.parametrize("path_info", ("bark/", "/bark/"))
6587
@pytest.mark.parametrize("script_name", ("woof/woof", "woof/woof/"))
6688
def test_script_name_is_respected(

0 commit comments

Comments
 (0)