Skip to content

AI junk - #6115

Closed
DijieDeng wants to merge 1 commit into
pallets:mainfrom
DijieDeng:fix-ipv6-partition
Closed

AI junk#6115
DijieDeng wants to merge 1 commit into
pallets:mainfrom
DijieDeng:fix-ipv6-partition

Conversation

@DijieDeng

Copy link
Copy Markdown

Fix IPv6 address parsing in session_transaction

Fixes part of #6093.

Problem

Two places in Flask use .partition(":") to parse host:port strings, but IPv6 addresses like [::1]:8000 contain multiple colons, so .partition(":") splits at the wrong position:

  1. session_transaction in src/flask/testing.pyctx.request.host.partition(":")[0] returns "[" instead of "[::1]" for IPv6 hosts. This breaks cookie domain extraction during session transactions with IPv6 base URLs.
  2. Flask.run() in src/flask/app.pyserver_name.partition(":") incorrectly parses SERVER_NAME when it contains an IPv6 address (e.g., [::1]:8080).

Fix applied in this PR (testing.py)

Replaced ctx.request.host.partition(":")[0] with urlsplit(ctx.request.host_url).hostname or "localhost". The host_url property includes the scheme (e.g., http://[::1]:8000), so urlsplit can correctly parse the hostname from IPv6 addresses. The urlsplit import already exists in testing.py.

Fix still needed (app.py)

The run() method in src/flask/app.py also needs the same fix. The change would be:

# In imports, add:
from urllib.parse import urlsplit

# In run() method, replace:
sn_host, _, sn_port = server_name.partition(":")
# With:
sn_url = urlsplit(f"//{server_name}")
sn_host = sn_url.hostname
sn_port = sn_url.port

# And change:
port = int(sn_port)
# To:
port = sn_port  # sn_port is already int | None from urlsplit

This follows the same approach used in the Werkzeug fix for the same issue.

Test added

Added test_session_transaction_ipv6 to tests/test_testing.py which verifies that session transactions work correctly with IPv6 base URLs:

def test_session_transaction_ipv6(app):
    base_url = "http://[::1]:8000/"
    client = app.test_client()

    @app.get("/")
    def index():
        return str(flask.session.get("value"))

    with client.session_transaction(base_url=base_url) as sess:
        sess["value"] = 42

    assert client.get("/", base_url=base_url).text == "42"

This test fails without the fix (raises an error or returns incorrect results) and passes with the fix.

Replace `.partition(":")[0]` with `urlsplit(ctx.request.host_url).hostname`
to correctly handle IPv6 addresses which contain multiple colons.

Fixes pallets#6093 (testing.py part)
@ThiefMaster

Copy link
Copy Markdown
Member

This appears LLM-generated.

https://palletsprojects.com/contributing/llm-ai

@ThiefMaster ThiefMaster closed this Aug 1, 2026
@davidism davidism changed the title Fix IPv6 address parsing in session_transaction (#6093) AI junk Aug 1, 2026
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.

2 participants