Skip to content

Commit 855c15f

Browse files
fix(wsgi): WSGI integrations respect SCRIPT_NAME env variable (#2622)
URLs generated using Sentry's WSGI Middleware should include SCRIPT_NAME in the event's url Fixes #2576 --------- Co-authored-by: Daniel Szoke <[email protected]>
1 parent c1c5a95 commit 855c15f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

sentry_sdk/integrations/wsgi.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ def get_request_url(environ, use_x_forwarded_for=False):
5555
# type: (Dict[str, str], bool) -> str
5656
"""Return the absolute URL without query string for the given WSGI
5757
environment."""
58+
script_name = environ.get("SCRIPT_NAME", "").rstrip("/")
59+
path_info = environ.get("PATH_INFO", "").lstrip("/")
60+
path = f"{script_name}/{path_info}"
61+
5862
return "%s://%s/%s" % (
5963
environ.get("wsgi.url_scheme"),
6064
get_host(environ, use_x_forwarded_for),
61-
wsgi_decoding_dance(environ.get("PATH_INFO") or "").lstrip("/"),
65+
wsgi_decoding_dance(path).lstrip("/"),
6266
)
6367

6468

tests/integrations/wsgi/test_wsgi.py

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

6363

64+
@pytest.mark.parametrize("path_info", ("bark/", "/bark/"))
65+
@pytest.mark.parametrize("script_name", ("woof/woof", "woof/woof/"))
66+
def test_script_name_is_respected(
67+
sentry_init, crashing_app, capture_events, script_name, path_info
68+
):
69+
sentry_init(send_default_pii=True)
70+
app = SentryWsgiMiddleware(crashing_app)
71+
client = Client(app)
72+
events = capture_events()
73+
74+
with pytest.raises(ZeroDivisionError):
75+
# setting url with PATH_INFO: bark/, HTTP_HOST: dogs.are.great and SCRIPT_NAME: woof/woof/
76+
client.get(path_info, f"https://dogs.are.great/{script_name}") # noqa: E231
77+
78+
(event,) = events
79+
80+
assert event["request"]["url"] == "https://dogs.are.great/woof/woof/bark/"
81+
82+
6483
@pytest.fixture(params=[0, None])
6584
def test_systemexit_zero_is_ignored(sentry_init, capture_events, request):
6685
zero_code = request.param

0 commit comments

Comments
 (0)