Skip to content

Commit 4a056c0

Browse files
authored
fix(wsgi): Respect HTTP_X_FORWARDED_PROTO in request.url construction (#5963)
#### Issues * resolves: #5955 * resolves: PY-2283
1 parent 9c360eb commit 4a056c0

File tree

3 files changed

+123
-17
lines changed

3 files changed

+123
-17
lines changed

sentry_sdk/_werkzeug.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from typing import Dict
3939
from typing import Iterator
4040
from typing import Tuple
41+
from typing import Optional
4142

4243

4344
#
@@ -62,35 +63,41 @@ def _get_headers(environ: "Dict[str, str]") -> "Iterator[Tuple[str, str]]":
6263
yield key.replace("_", "-").title(), value
6364

6465

65-
#
66+
def _strip_default_port(host: str, scheme: "Optional[str]") -> str:
67+
"""Strip the port from the host if it's the default for the scheme."""
68+
if scheme == "http" and host.endswith(":80"):
69+
return host[:-3]
70+
if scheme == "https" and host.endswith(":443"):
71+
return host[:-4]
72+
return host
73+
74+
6675
# `get_host` comes from `werkzeug.wsgi.get_host`
6776
# https://github.com/pallets/werkzeug/blob/1.0.1/src/werkzeug/wsgi.py#L145
68-
#
77+
78+
6979
def get_host(environ: "Dict[str, str]", use_x_forwarded_for: bool = False) -> str:
7080
"""
7181
Return the host for the given WSGI environment.
7282
"""
83+
scheme = environ.get("wsgi.url_scheme")
84+
if use_x_forwarded_for:
85+
scheme = environ.get("HTTP_X_FORWARDED_PROTO", scheme)
86+
7387
if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ:
74-
rv = environ["HTTP_X_FORWARDED_HOST"]
75-
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
76-
rv = rv[:-3]
77-
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
78-
rv = rv[:-4]
88+
return _strip_default_port(environ["HTTP_X_FORWARDED_HOST"], scheme)
7989
elif environ.get("HTTP_HOST"):
80-
rv = environ["HTTP_HOST"]
81-
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
82-
rv = rv[:-3]
83-
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
84-
rv = rv[:-4]
90+
return _strip_default_port(environ["HTTP_HOST"], scheme)
8591
elif environ.get("SERVER_NAME"):
92+
# SERVER_NAME/SERVER_PORT describe the internal server, so use
93+
# wsgi.url_scheme (not the forwarded scheme) for port decisions.
8694
rv = environ["SERVER_NAME"]
8795
if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in (
8896
("https", "443"),
8997
("http", "80"),
9098
):
9199
rv += ":" + environ["SERVER_PORT"]
100+
return rv
92101
else:
93102
# In spite of the WSGI spec, SERVER_NAME might not be present.
94-
rv = "unknown"
95-
96-
return rv
103+
return "unknown"

sentry_sdk/integrations/wsgi.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ def get_request_url(
5757
path_info = environ.get("PATH_INFO", "").lstrip("/")
5858
path = f"{script_name}/{path_info}"
5959

60+
scheme = environ.get("wsgi.url_scheme")
61+
if use_x_forwarded_for:
62+
scheme = environ.get("HTTP_X_FORWARDED_PROTO", scheme)
63+
6064
return "%s://%s/%s" % (
61-
environ.get("wsgi.url_scheme"),
65+
scheme,
6266
get_host(environ, use_x_forwarded_for),
6367
wsgi_decoding_dance(path).lstrip("/"),
6468
)

tests/integrations/wsgi/test_wsgi.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
import sentry_sdk
88
from sentry_sdk import capture_message
9-
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware, _ScopedResponse
9+
from sentry_sdk.integrations.wsgi import (
10+
SentryWsgiMiddleware,
11+
_ScopedResponse,
12+
get_request_url,
13+
)
1014

1115

1216
@pytest.fixture
@@ -547,3 +551,94 @@ def app(environ, start_response):
547551
assert isinstance(result, _ScopedResponse)
548552
else:
549553
assert result is response_mock
554+
555+
556+
@pytest.mark.parametrize(
557+
"environ,use_x_forwarded_for,expected_url",
558+
[
559+
# Without use_x_forwarded_for, wsgi.url_scheme is used
560+
(
561+
{
562+
"wsgi.url_scheme": "http",
563+
"SERVER_NAME": "example.com",
564+
"SERVER_PORT": "80",
565+
"PATH_INFO": "/test",
566+
"HTTP_X_FORWARDED_PROTO": "https",
567+
},
568+
False,
569+
"http://example.com/test",
570+
),
571+
# With use_x_forwarded_for, HTTP_X_FORWARDED_PROTO is respected
572+
(
573+
{
574+
"wsgi.url_scheme": "http",
575+
"SERVER_NAME": "example.com",
576+
"SERVER_PORT": "80",
577+
"PATH_INFO": "/test",
578+
"HTTP_X_FORWARDED_PROTO": "https",
579+
},
580+
True,
581+
"https://example.com/test",
582+
),
583+
# With use_x_forwarded_for but no forwarded proto, wsgi.url_scheme is used
584+
(
585+
{
586+
"wsgi.url_scheme": "http",
587+
"SERVER_NAME": "example.com",
588+
"SERVER_PORT": "80",
589+
"PATH_INFO": "/test",
590+
},
591+
True,
592+
"http://example.com/test",
593+
),
594+
# Forwarded host with default https port is stripped using forwarded proto
595+
(
596+
{
597+
"wsgi.url_scheme": "http",
598+
"SERVER_NAME": "internal",
599+
"SERVER_PORT": "80",
600+
"PATH_INFO": "/test",
601+
"HTTP_X_FORWARDED_PROTO": "https",
602+
"HTTP_X_FORWARDED_HOST": "example.com:443",
603+
},
604+
True,
605+
"https://example.com/test",
606+
),
607+
# Forwarded host with non-default port is preserved
608+
(
609+
{
610+
"wsgi.url_scheme": "http",
611+
"SERVER_NAME": "internal",
612+
"SERVER_PORT": "80",
613+
"PATH_INFO": "/test",
614+
"HTTP_X_FORWARDED_PROTO": "https",
615+
"HTTP_X_FORWARDED_HOST": "example.com:8443",
616+
},
617+
True,
618+
"https://example.com:8443/test",
619+
),
620+
# Forwarded proto with HTTP_HOST (no forwarded host) strips default port
621+
(
622+
{
623+
"wsgi.url_scheme": "http",
624+
"HTTP_HOST": "example.com:443",
625+
"SERVER_NAME": "internal",
626+
"SERVER_PORT": "80",
627+
"PATH_INFO": "/test",
628+
"HTTP_X_FORWARDED_PROTO": "https",
629+
},
630+
True,
631+
"https://example.com/test",
632+
),
633+
],
634+
ids=[
635+
"ignores_forwarded_proto_when_disabled",
636+
"respects_forwarded_proto_when_enabled",
637+
"falls_back_to_url_scheme_when_no_forwarded_proto",
638+
"strips_default_https_port_from_forwarded_host",
639+
"preserves_non_default_port_on_forwarded_host",
640+
"strips_default_port_from_http_host_with_forwarded_proto",
641+
],
642+
)
643+
def test_get_request_url_x_forwarded_proto(environ, use_x_forwarded_for, expected_url):
644+
assert get_request_url(environ, use_x_forwarded_for) == expected_url

0 commit comments

Comments
 (0)