Skip to content

Parse dial addresses with net.SplitHostPort - #65

Closed
drewr wants to merge 1 commit into
superfly:mainfrom
drewr:ipv6-dial
Closed

drewr wants to merge 1 commit into
superfly:mainfrom
drewr:ipv6-dial

Conversation

@drewr

@drewr drewr commented Nov 17, 2026

Copy link
Copy Markdown

Datum is an IPv6-native platform, so we seem to find subtle IPv6 issues in a lot of places. This is one of them.

What breaks

dialFunc splits the address it is asked to dial at the first ::

hostname, port, _ := strings.Cut(addr, ":")

For an IPv6 literal such as [2001:db8::1]:80 that cuts inside the address: hostname is [ and port is :db8::1]:80. Neither the "80" nor the "443" case matches, so:

  • A client proxying to http://[2001:db8::1]/ (the Transport asks for [2001:db8::1]:80) gets TLS dialed on port 80 instead of 443, and the request fails.
  • [2001:db8::1]:443 is not refused with "proxied request must be HTTP", so the TLS-passthrough guard does not apply to IPv6 literals.
  • An address with no port, [2001:db8::1], fails to dial with a missing-port error instead of defaulting to 443.

Literals on other ports ([::1]:8443) happen to round-trip unchanged and work.

Fix

Split with net.SplitHostPort and rebuild with net.JoinHostPort, so brackets are handled. A missing port is still treated as 443.

An address SplitHostPort cannot parse for another reason (an unbracketed ::1, for example) is refused with ErrBadRequest. Before, it was cut at the first colon and either refused for having no host or failed to dial.

Unchanged

  • Port "" or 80 becomes 443; explicit 443 is refused; an empty host is refused. Same messages.
  • Hostnames and IPv4 addresses behave as before.
  • The private, loopback and link-local checks in the dialer's ControlContext already used net.SplitHostPort, and are untouched.

Tests

  • TestDialFuncParsesAddresses covers names, IPv4 and IPv6 literals. It checks that :443 is refused, that an empty host is refused (:80, []:80, empty string), and that port 80, no port and host: each dial 443 (read from the dial error, which names the address). An unbracketed ::1 is refused. The IPv6 cases fail without the change.
  • TestTokenizerIPv6Upstream sends a request through the proxy to a TLS echo server on [::1], with an allowed_hosts validator on the bracketed host, and checks that the server sees the injected credential. It is skipped if the machine has no IPv6 loopback. It uses an explicit port, so it passes without the change too; it guards the end-to-end path.

dialFunc split the address at the first colon, which cuts an IPv6 literal
such as [2001:db8::1]:80 inside the address. The port then never matched
"80" or "443", so a literal on port 80 was dialed with TLS on port 80
instead of 443, and a literal on port 443 was not refused. Use
net.SplitHostPort and net.JoinHostPort. A missing port still means 443.
@drewr
drewr requested a review from a team as a code owner November 17, 2026 18:21
@timflyio

Copy link
Copy Markdown
Contributor

Addressed in #66 .

@timflyio timflyio closed this Nov 18, 2026
@drewr

drewr commented Nov 18, 2026

Copy link
Copy Markdown
Author

Thanks @timflyio. Curious, what was the problem with my PR?

@timflyio

Copy link
Copy Markdown
Contributor

Thanks @timflyio. Curious, what was the problem with my PR?

It turns out that theres no need to handle the missing port error from SplitHostPort or port defaulting.

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