Don't use IP literals for TLS SNI - #186
Open
dmonagle wants to merge 4 commits into
Open
Conversation
|
|
An authority containing an IP literal (e.g. a .dns target created with an IP address, common when callers resolve addresses themselves) was passed to the TLS handshake as the SNI server hostname. RFC 6066 § 3 only permits DNS hostnames in SNI and NIOSSL enforces this, so such connections failed with cannotUseIPAddressInSNI before the handshake started. SocketAddress.sniHostname already applies this rule; the authority-derived hostname now does too: IP literals (bare v4/v6, with a port, or bracketed v6) yield no SNI instead of a handshake failure.
dmonagle
force-pushed
the
fix/sni-ip-literal
branch
from
July 20, 2026 08:36
90302d3 to
0f1f2c5
Compare
glbrntt
requested changes
Jul 20, 2026
glbrntt
left a comment
Collaborator
There was a problem hiding this comment.
One tiny change but looks good otherwise
| // Undo the bracketing applied to IPv6 addresses in authorities | ||
| // (e.g. "[2001:db8::1]:443"). | ||
| if host.utf8.first == UInt8(ascii: "["), host.utf8.last == UInt8(ascii: "]") { | ||
| host = String(host.dropFirst().dropLast()) |
Collaborator
There was a problem hiding this comment.
Square brackets imply an IPv6 address so we can just return nil here, I think.
Contributor
Author
There was a problem hiding this comment.
Done — brackets now return nil directly (and host became a let).
glbrntt
enabled auto-merge (squash)
July 20, 2026 10:59
Collaborator
|
Apologies for missing this: it looks like the CI is failing because of a Swift issue: swiftlang/swift#85427 We might be able to workaround it with a patch to swift-nio: apple/swift-nio#3696 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When a connection's authority contains an IP literal — e.g. a
.dnstarget created with an IP address, which is common when callers resolve addresses themselves (multi-homed servers, NAT64 synthesis on mobile clients, health checks by address) — the literal is passed to the TLS handshake as the SNI server hostname. RFC 6066 § 3 only permits DNS hostnames in SNI, and NIOSSL enforces this: the connection fails withNIOSSLExtraError.cannotUseIPAddressInSNIbefore the handshake starts, surfacing to callers asunavailable: Could not establish a connection….SocketAddress.sniHostnamealready applies exactly this rule ("Literal IP addresses aren't allowed in the SNI hostname") — but the authority-derived hostname takes precedence over it and doesn't.Modifications
Connection.sanitizeAuthorityForSNInow returnsnilfor IP literals — bare IPv4/IPv6, with a trailing port, or bracketed IPv6 — after its existing port-stripping. The literal check runs before port-stripping too, since that logic would otherwise mangle bare IPv6 literals like::1.sniServerHostnamethen falls back through the existing?? self.address.sniHostnamepath, so such connections are made without SNI instead of failing.Result
Dialling an IP literal over TLS connects (subject to the configured certificate verification) instead of failing with
cannotUseIPAddressInSNI. Hostname authorities are unaffected.Extended the existing
testAuthorityIsSanitizedharness with IP-literal cases (127.0.0.1,127.0.0.1:9443,::1,[2001:db8::1]:443), all expecting no SNI.