Fix `.partition(":")` usage on potential IPv6 addresses (#6096)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents 514fc6b, 7203fea · Browse files · 4 files·+23 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+23 added−5 removed
- tests:+15 added−0 removed
- other:+8 added−5 removed
Provenance
- Commit
05e9c6b- Tree
4eb1131- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
514fc6b- Parent 2
7203fea
4 files changed+23 added−5 removed
modified:src/flask/app.py+7 added-4 removed
10 unmodified lines11 from inspect import iscoroutinefunction12 from itertools import chain13 from types import TracebackType14 from urllib.parse import quote as _url_quote15+from urllib.parse import urlsplit16 17 import click18 from werkzeug.datastructures import Headers19 from werkzeug.datastructures import ImmutableDict701 unmodified lines721 server_name = self.config.get("SERVER_NAME")722 sn_host = sn_port = None723 724 if server_name:724- sn_host, _, sn_port = server_name.partition(":")725+ server_url = urlsplit(f"//{server_name}")726+ sn_host = server_url.hostname727+ sn_port = server_url.port728 729 if not host:730 if sn_host:731 host = sn_host1 unmodified line733 host = "127.0.0.1"734 735 if port or port == 0:736 port = int(port)734- elif sn_port:735- port = int(sn_port)737+ elif sn_port is not None:738+ port = sn_port739 else:740 port = 5000741 742 options.setdefault("use_reloader", self.debug)4 unmodified lines747 748 from werkzeug.serving import run_simple749 750 try:748- run_simple(t.cast(str, host), port, self, **options)751+ run_simple(host, port, self, **options)752 finally:753 # reset the first request information if the development server754 # reset normally. This makes it possible to restart the server755 # without reloader and that stuff from an interactive shell.873 unmodified linesmodified:src/flask/testing.py+1 added-1 removed
175 unmodified lines176 with ctx:177 app.session_interface.save_session(app, sess, resp)178 179 self._update_cookies_from_response(180- ctx.request.host.partition(":")[0],180+ urlsplit(ctx.request.host_url).hostname or "localhost",181 ctx.request.path,182 resp.headers.getlist("Set-Cookie"),183 )184 114 unmodified linesmodified:tests/test_basic.py+1 added
1908 unmodified lines1909 ("localhost", 80, "pocoo.org:8080", "localhost", 80),1910 ("localhost", 0, "localhost:8080", "localhost", 0),1911 (None, None, "localhost:8080", "localhost", 8080),1912 (None, None, "localhost:0", "localhost", 0),1913+ (None, None, "[::1]:8080", "::1", 8080),1914 ),1915 )1916 def test_run_from_config(1917 monkeypatch, host, port, server_name, expect_host, expect_port, app54 unmodified linesmodified:tests/test_testing.py+14 added
169 unmodified lines170 assert len(sess) == 1171 assert sess["foo"] == [42]172 173 174+def test_session_transaction_ipv6(app):175+ base_url = "http://[::1]:8000/"176+ client = app.test_client()177+178+ @app.get("/")179+ def index():180+ return str(flask.session.get("value"))181+182+ with client.session_transaction(base_url=base_url) as sess:183+ sess["value"] = 42184+185+ assert client.get("/", base_url=base_url).text == "42"186+187+188 def test_session_transactions_no_null_sessions():189 app = flask.Flask(__name__)190 191 with app.test_client() as c:207 unmodified lines