reject hosts that fail IP canonicalization in verifyIpAddress - #9621
Merged
swankjesse merged 1 commit intoJul 30, 2026
Merged
Conversation
Collaborator
|
So is the threat that if you can get a certificate with an invalid ip address, and construct a url with an invalid host ip address, then that would match as null == null? It seems like a bug, but I can't work out how to exploit it in practice as you only get the certificate back once you have connected, and presumably that IP address isn't usable to connect as it doesn't parse? |
yschimke
approved these changes
Jul 29, 2026
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.
OkHostnameVerifier sends any host that looks like an IP address to verifyIpAddress, which canonicalizes the host and compares it against the canonicalized form of each iPAddress SAN in the peer certificate. Both sides of that comparison come from toCanonicalHost, which returns null for anything it can't parse, and a null on the left is never treated as a failure. A host like
1::2::3gets past the loose canParseAsIpAddress regex but has two::so it canonicalizes to null, and an iPAddress SAN that carries an address plus a netmask comes back from the JDK as1.2.3.4/255.255.255.0, which also canonicalizes to null because of the slash. The two nulls compare equal, so verification succeeds against a certificate that has nothing to do with the host. I found it while reading the other toCanonicalHost call sites: Cookie, CertificatePinner, Dns and HttpUrl all reject null right where they get it, and this was the only one that let it through. Bailing out when the host doesn't canonicalize keeps the comparison to addresses we actually understand, and the hosts OkHttp passes in itself already come from HttpUrl in canonical form, so nothing changes for them.