Skip to content

fix(asgi): Stop duplicating root_path in URLs#6579

Draft
alexander-alderman-webb wants to merge 1 commit into
masterfrom
webb/asgi/double-mount-prefix
Draft

fix(asgi): Stop duplicating root_path in URLs#6579
alexander-alderman-webb wants to merge 1 commit into
masterfrom
webb/asgi/double-mount-prefix

Conversation

@alexander-alderman-webb

Copy link
Copy Markdown
Contributor

Description

Issues

Closes #6577

Reminders

Comment on lines +35 to +46
path_includes_root_path: "bool" = True,
) -> str:
"""
Extract URL from the ASGI scope, without also including the querystring.
"""
scheme = asgi_scope.get("scheme", default_scheme)

server = asgi_scope.get("server", None)
path = asgi_scope.get("root_path", "") + asgi_scope.get("path", "")
path = (
asgi_scope.get("path", "")
if path_includes_root_path
else asgi_scope.get("root_path", "") + asgi_scope.get("path", "")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Default path_includes_root_path=True silently drops root_path from request URLs in _get_request_data and _get_request_attributes

The new default path_includes_root_path=True causes every unparameterised _get_url() call to omit root_path from the URL. Both _get_request_data (line 103) and _get_request_attributes (line 132) call _get_url without this argument, so the request.url and url.full fields in Sentry events will now be missing the root path prefix for Starlette versions < 0.33 (and any plain-ASGI app) where path does not already embed root_path.

Evidence
  • Before this change, path was always built as root_path + path; now with path_includes_root_path=True (the new default) only asgi_scope.get("path", "") is used.
  • _get_request_data (line 103) and _get_request_attributes (line 132) call _get_url with no path_includes_root_path argument, inheriting the new default True.
  • SentryAsgiMiddleware correctly threads self.path_includes_root_path through its own _get_url calls, but _get_request_data/_get_request_attributes have no access to that flag.
  • For Starlette < 0.33, patch_asgi_app passes path_includes_root_path=False to the middleware, meaning the framework itself knows path doesn't contain root_path, yet the event-level URL functions still assume it does.
  • The result is that event["request"]["url"] and span.attributes["url.full"] will be missing the mount prefix for any deployment that sets a non-empty root_path on Starlette < 0.33 or a bare ASGI server.
Also found at 2 additional locations
  • sentry_sdk/integrations/starlette.py:144-147
  • sentry_sdk/integrations/starlette.py:452-455

Identified by Warden code-review · YAE-94S

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

90848 passed | ⏭️ 6037 skipped | Total: 96885 | Pass Rate: 93.77% | Execution Time: 315m 51s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 2421 uncovered lines.
✅ Project coverage is 89.76%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.75%    89.76%    +0.01%
==========================================
  Files          192       192         —
  Lines        23630     23632        +2
  Branches      8134      8134         —
==========================================
+ Hits         21208     21211        +3
- Misses        2422      2421        -1
- Partials      1341      1340        -1

Generated by Codecov Action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASGI integration doubles the mount prefix in request.url for Starlette Mounted sub-apps (root_path + path)

1 participant