Bracket IPv6 literals in Host headers and URLs by RitiGrover · Pull Request #8484 · mitmproxy/mitmproxy · GitHub
Skip to content

Bracket IPv6 literals in Host headers and URLs - #8484

Open
RitiGrover wants to merge 2 commits into
mitmproxy:mainfrom
RitiGrover:fix/ipv6-host-brackets
Open

RitiGrover wants to merge 2 commits into
mitmproxy:mainfrom
RitiGrover:fix/ipv6-host-brackets

Conversation

@RitiGrover

Copy link
Copy Markdown

url.hostport() joined the host and port without brackets. For an IPv6 host that gives ::1:8080 which is ambiguous and not a valid Host value or URL authority.

This showed up in a few places:

  • mitmdump --mode reverse:http://[::1]:8000 rewrote the Host header of every forwarded request to ::1:8000.
  • Changing request.host, request.port or request.url in a script rewrote the Host header or :authority the same way.
  • request.url and request.pretty_url returned http://::1:8080/a for IPv6 targets.

hostport() now wraps an unbracketed IPv6 literal in brackets. A host that is already bracketed is left alone. unparse() uses hostport() so it is fixed too.

#8284 has the same change in hostport(). That PR has had no review since June. This one also covers the Request url and Host header paths and the reverse proxy case with tests. I am happy to close this one if you would rather use #8284.

Tests: five new tests across test_url.py, test_http.py and test_modes.py (reverse proxy to [::1]:8000). They fail without the change. ruff and mypy pass. A broader run of the proxy, net, http and addon tests passes except for a few load dependent tests that fail the same way on unmodified main here.

This branch has not been deployed

No deployments
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.

1 participant