Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

mgaligniana
Copy link
Contributor

Fixes GH-3516

host = host[:-3]
elif scheme in {"https", "wss"} and host.endswith(":443"):
host = host[:-4]

Copy link
Contributor Author

@mgaligniana mgaligniana Sep 12, 2025

Choose a reason for hiding this comment

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

πŸ“ Here werkzeug have a condition for trusted hosts: https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/sansio/utils.py#L98

Why we don't use it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I assume because we're only using the get_host function to reconstruct the request URL and not any actual functionality.

host = f"[{host}]"

if server[1] is not None:
host = f"{host}:{server[1]}" # noqa: E231
Copy link
Contributor Author

Choose a reason for hiding this comment

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

πŸŽ—οΈ I had to define this because of:

E231 missing whitespace after ':'

@mgaligniana mgaligniana force-pushed the GH-3516-werkzeug-vendor-update branch 2 times, most recently from 75b9810 to dab33fb Compare September 12, 2025 16:09
Copy link

codecov bot commented Sep 12, 2025

Codecov Report

❌ Patch coverage is 44.44444% with 15 lines in your changes missing coverage. Please review.
βœ… Project coverage is 83.41%. Comparing base (5a122b5) to head (ba2c350).

Files with missing lines Patch % Lines
sentry_sdk/_werkzeug.py 44.44% 11 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4793      +/-   ##
==========================================
- Coverage   84.47%   83.41%   -1.07%     
==========================================
  Files         158      158              
  Lines       16506    16512       +6     
  Branches     2865     2864       -1     
==========================================
- Hits        13944    13773     -171     
- Misses       1712     1889     +177     
  Partials      850      850              
Files with missing lines Coverage Ξ”
sentry_sdk/_werkzeug.py 54.54% <44.44%> (+6.39%) ⬆️

... and 8 files with indirect coverage changes

@mgaligniana mgaligniana force-pushed the GH-3516-werkzeug-vendor-update branch 5 times, most recently from 30d3040 to ba2c350 Compare September 12, 2025 18:39
@mgaligniana mgaligniana force-pushed the GH-3516-werkzeug-vendor-update branch from ba2c350 to 6238c21 Compare September 12, 2025 18:58
return None

try:
port = int(environ.get("SERVER_PORT", None)) # type: ignore[arg-type]
Copy link
Contributor Author

@mgaligniana mgaligniana Sep 12, 2025

Choose a reason for hiding this comment

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

@mgaligniana mgaligniana marked this pull request as ready for review September 12, 2025 19:14
@mgaligniana mgaligniana requested a review from a team as a code owner September 12, 2025 19:14
environ["wsgi.url_scheme"],
environ.get("HTTP_HOST"),
_get_server(environ),
)
Copy link

Choose a reason for hiding this comment

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

Bug: Host Detection Functionality Broken

The get_host function now ignores the use_x_forwarded_for parameter. This means it no longer checks for HTTP_X_FORWARDED_HOST headers, breaking existing functionality that relies on forwarded host detection.

Fix in CursorΒ Fix in Web

Comment on lines 70 to +75
"""
if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ:
rv = environ["HTTP_X_FORWARDED_HOST"]
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
rv = rv[:-3]
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
rv = rv[:-4]
elif environ.get("HTTP_HOST"):
rv = environ["HTTP_HOST"]
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
rv = rv[:-3]
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
rv = rv[:-4]
elif environ.get("SERVER_NAME"):
rv = environ["SERVER_NAME"]
if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in (
("https", "443"),
("http", "80"),
):
rv += ":" + environ["SERVER_PORT"]
else:
# In spite of the WSGI spec, SERVER_NAME might not be present.
rv = "unknown"

return rv
return _get_host(
environ["wsgi.url_scheme"],
environ.get("HTTP_HOST"),
_get_server(environ),
)

Choose a reason for hiding this comment

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

Potential bug: The new get_host function ignores the use_x_forwarded_for parameter and no longer checks for the HTTP_X_FORWARDED_HOST header, causing a functional regression.
  • Description: The refactored get_host function no longer handles the HTTP_X_FORWARDED_HOST header, even when the use_x_forwarded_for parameter is True. This is a regression from the previous implementation. In a proxied environment where the HTTP_HOST (the proxy's host) differs from HTTP_X_FORWARDED_HOST (the client's original host), this will cause incorrect request URLs to be generated and reported in Sentry events. This affects integrations like Django where settings.USE_X_FORWARDED_HOST is used to control this behavior.

  • Suggested fix: Reinstate the logic to check for environ['HTTP_X_FORWARDED_HOST'] when use_x_forwarded_for is True. If the header is present, its value should be used as the host, preserving the behavior of the previous implementation for proxied environments.
    severity: 0.65, confidence: 0.95

Did we get this right? πŸ‘ / πŸ‘Ž to inform future reviews.

@mgaligniana mgaligniana marked this pull request as draft September 15, 2025 12:54
@sentrivana
Copy link
Contributor

Thanks for the PR @mgaligniana -- I think the HTTP_X_FORWARDED_HOST handling needs to be added manually on top of the vendored in code. As far as I can tell we also did that in the current version as the original upstream 1.0.1 version also doesn't contain it.

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.

Check if we still need _werkzeug.py
2 participants