pallets / werkzeug

The comprehensive WSGI web application library.
https://werkzeug.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
6.67k stars 1.73k forks source link

Redirecting to ipv6 ip address tries to parse last part as port #2903

Closed reederz closed 6 months ago

reederz commented 6 months ago

I upgraded to Werkzeug 3.0.3 and now I'm no longer able to redirect to ipv6 urls.

Example:

from flask import Flask, redirect

app = Flask(__name__)

@app.route('/')
def index():
    return redirect('http://::1')

app.run()

The redirect produces the following error:

Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/werkzeug/serving.py", line 363, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python3.10/dist-packages/werkzeug/serving.py", line 324, in execute
    application_iter = app(environ, start_response)
  File "/usr/local/lib/python3.10/dist-packages/flask/app.py", line 1498, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.10/dist-packages/flask/app.py", line 1480, in wsgi_app
    return response(environ, start_response)
  File "/usr/local/lib/python3.10/dist-packages/werkzeug/wrappers/response.py", line 576, in __call__
    app_iter, status, headers = self.get_wsgi_response(environ)
  File "/usr/local/lib/python3.10/dist-packages/werkzeug/wrappers/response.py", line 562, in get_wsgi_response
    headers = self.get_wsgi_headers(environ)
  File "/usr/local/lib/python3.10/dist-packages/werkzeug/wrappers/response.py", line 481, in get_wsgi_headers
    location = iri_to_uri(location)
  File "/usr/local/lib/python3.10/dist-packages/werkzeug/urls.py", line 153, in iri_to_uri
    if parts.port:
  File "/usr/lib/python3.10/urllib/parse.py", line 185, in port
    raise ValueError(f"Port could not be cast to integer value as {port!r}")
ValueError: Port could not be cast to integer value as ':1'

I believe the issue was introduced with https://github.com/pallets/werkzeug/commit/97fb2f722297ae4e12e36dab024e0acf8477b3c8 .

Environment:

ThiefMaster commented 6 months ago

raw ipv6 addresses need to be wrapped in brackets.