Skip to content

Commit dab33fb

Browse files
committed
ref(wsgi): Update _werkzeug vendor to newer version
Fixes GH-3516
1 parent dcefe38 commit dab33fb

File tree

2 files changed

+93
-31
lines changed

2 files changed

+93
-31
lines changed

sentry_sdk/_werkzeug.py

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,60 +39,100 @@
3939
from typing import Iterator
4040
from typing import Tuple
4141

42+
text_type = str
43+
iteritems = lambda d, *args, **kwargs: iter(d.items(*args, **kwargs))
44+
4245

4346
#
44-
# `get_headers` comes from `werkzeug.datastructures.EnvironHeaders`
45-
# https://github.com/pallets/werkzeug/blob/0.14.1/werkzeug/datastructures.py#L1361
47+
# `get_headers` comes from `werkzeug.datastructures.headers.__iter__`
48+
# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/datastructures/headers.py#L644
4649
#
4750
# We need this function because Django does not give us a "pure" http header
4851
# dict. So we might as well use it for all WSGI integrations.
4952
#
5053
def _get_headers(environ):
5154
# type: (Dict[str, str]) -> Iterator[Tuple[str, str]]
52-
"""
53-
Returns only proper HTTP headers.
54-
"""
5555
for key, value in environ.items():
56-
key = str(key)
57-
if key.startswith("HTTP_") and key not in (
56+
if key.startswith("HTTP_") and key not in {
5857
"HTTP_CONTENT_TYPE",
5958
"HTTP_CONTENT_LENGTH",
60-
):
59+
}:
6160
yield key[5:].replace("_", "-").title(), value
62-
elif key in ("CONTENT_TYPE", "CONTENT_LENGTH"):
61+
elif key in {"CONTENT_TYPE", "CONTENT_LENGTH"} and value:
6362
yield key.replace("_", "-").title(), value
6463

6564

6665
#
6766
# `get_host` comes from `werkzeug.wsgi.get_host`
68-
# https://github.com/pallets/werkzeug/blob/1.0.1/src/werkzeug/wsgi.py#L145
67+
# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/wsgi.py#L86
6968
#
7069
def get_host(environ, use_x_forwarded_for=False):
7170
# type: (Dict[str, str], bool) -> str
7271
"""
7372
Return the host for the given WSGI environment.
7473
"""
7574
if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ:
76-
rv = environ["HTTP_X_FORWARDED_HOST"]
77-
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
78-
rv = rv[:-3]
79-
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
80-
rv = rv[:-4]
81-
elif environ.get("HTTP_HOST"):
82-
rv = environ["HTTP_HOST"]
83-
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
84-
rv = rv[:-3]
85-
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
86-
rv = rv[:-4]
87-
elif environ.get("SERVER_NAME"):
88-
rv = environ["SERVER_NAME"]
89-
if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in (
90-
("https", "443"),
91-
("http", "80"),
92-
):
93-
rv += ":" + environ["SERVER_PORT"]
75+
host = environ["HTTP_X_FORWARDED_HOST"]
76+
if environ.get("wsgi.url_scheme") == "http" and host.endswith(":80"):
77+
host = host[:-3]
78+
elif environ.get("wsgi.url_scheme") == "https" and host.endswith(":443"):
79+
host = host[:-4]
9480
else:
95-
# In spite of the WSGI spec, SERVER_NAME might not be present.
96-
rv = "unknown"
81+
host = _get_host(
82+
environ["wsgi.url_scheme"],
83+
environ.get("HTTP_HOST"),
84+
_get_server(environ),
85+
)
86+
87+
return host
88+
89+
90+
# `_get_host` comes from `werkzeug.sansio.utils`
91+
# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/sansio/utils.py#L49
92+
def _get_host(
93+
scheme: str,
94+
host_header: str | None,
95+
server: tuple[str, int | None] | None = None,
96+
) -> str:
97+
"""
98+
Return the host for the given parameters.
99+
"""
100+
101+
host = ""
102+
103+
if host_header is not None:
104+
host = host_header
105+
elif server is not None:
106+
host = server[0]
107+
108+
# If SERVER_NAME is IPv6, wrap it in [] to match Host header.
109+
# Check for : because domain or IPv4 can't have that.
110+
if ":" in host and host[0] != "[":
111+
host = f"[{host}]"
112+
113+
if server[1] is not None:
114+
host = f"{host}:{server[1]}" # noqa: E231
115+
116+
if scheme in {"http", "ws"} and host.endswith(":80"):
117+
host = host[:-3]
118+
elif scheme in {"https", "wss"} and host.endswith(":443"):
119+
host = host[:-4]
120+
121+
return host
122+
123+
124+
def _get_server(
125+
environ, # type: Dict[str, str]
126+
) -> tuple[str, int | None] | None:
127+
name = environ.get("SERVER_NAME")
128+
129+
if name is None:
130+
return None
131+
132+
try:
133+
port: int | None = int(environ.get("SERVER_PORT", None)) # type: ignore[arg-type]
134+
except (TypeError, ValueError):
135+
# unix socket
136+
port = None
97137

98-
return rv
138+
return name, port

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)