-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
okhttp: properly verify IPv6 address hosts #4292
okhttp: properly verify IPv6 address hosts #4292
Conversation
Address mismatch between IPv6 address hosts derived from URIs and X509 subjectAltName extensions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix! A few small comments to address, then we can get this in
* | ||
* @return {@param host} in a form consistent with X509 certificates | ||
*/ | ||
static String canonicalizeHost(String host) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is package-private with @VisibleForTesting
(ala TLS_PROTOCOLS
) acceptable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, my mistake - yes, the visibility is fine, but the @VisibleForTesting
annotation would be helpful.
* @return {@param host} in a form consistent with X509 certificates | ||
*/ | ||
static String canonicalizeHost(String host) { | ||
return host.replaceAll("[\\[\\]]", ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be safe as-is, but we should be more conservative here: if the first and last characters are [
and ]
, strip only those two characters out. Otherwise, just return the string.
This would mean that we can add a test like assertEquals("[someIp]", canonicalizeHost("[[someIp]]"));
. This address would, as in #4278, result in OkHostnameVerifier
failing to match the host - but that's good, because having two brackets is malformed anyways, and probably indicates a bug somewhere else that we don't want to mask here.
assertEquals(canonicalizeHost("0:0:0:0:0:0:13.1.68.3"), "0:0:0:0:0:0:13.1.68.3"); | ||
assertEquals(canonicalizeHost("[0:0:0:0:0:0:13.1.68.3]"), "0:0:0:0:0:0:13.1.68.3"); | ||
assertEquals(canonicalizeHost("0:0:0:0:0:FFFF:129.144.52.38"), "0:0:0:0:0:FFFF:129.144.52.38"); | ||
assertEquals(canonicalizeHost("[0:0:0:0:0:FFFF:129.144.52.38]"), "0:0:0:0:0:FFFF:129.144.52.38"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this fails checkstyle (100 char limit per line)
|
||
@Test public void upgrade_canonicalizeHost() { | ||
// IPv4 | ||
assertEquals(canonicalizeHost("127.0.0.1"), "127.0.0.1"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The argument order for assertEquals
is reversed. These should be in the form: assertEquals("ip", canonicalizeHost("ip"))
. (this results in better output when a test fails, as error message will label the first argument as the expected value and the second argument as the actual value)
@@ -32,4 +34,44 @@ | |||
|| OkHttpTlsUpgrader.TLS_PROTOCOLS.indexOf(Protocol.GRPC_EXP) | |||
< OkHttpTlsUpgrader.TLS_PROTOCOLS.indexOf(Protocol.HTTP_2)); | |||
} | |||
|
|||
@Test public void upgrade_canonicalizeHost() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests coverage look good to me (with the addition of a case to ensure we don't strip out additional brackets for malformed URIs). Out of curiosity, what methodology went into choosing the test hosts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will update to ensure we don't strip out additional brackets.
I lifted these from okhttp's HostnameVerifierTest, so I'll add some attribution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's simplify the tests somewhat here, which will also remove the need for attribution - we actually don't care about all of the edge cases that the linked test case covers, because we don't need to distinguish between IP addresses and hostnames. Probably just the following are enough:
assertEquals("::1", canonicalizeHost("::1"));
assertEquals("::1", canonicalizeHost("[::1]"));
assertEquals("127.0.0.1", canonicalizeHost("127.0.0.1"));
assertEquals("some.long.url.com", canonicalizeHost("some.long.url.com"));
// Extra square brackets in a malformed URI are retained
assertEquals("[::1]", canonicalizeHost("[[::1]]"));
Also, does it make sense to rename the test method to just canonicalizeHost
, as this isn't testing any other part of the upgrade logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Is canonicalizeHosts
ok to avoid clash with the static import of canonicalizeHost
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good to me
…ses and hostnames. Test exactly one pair of '[' + ']' is removed from host
c0b7381
to
1065eb0
Compare
fixed whitespace issue to appease travis. One more time from the top 🥁 |
Thanks! |
Address mismatch between IPv6 address hosts derived from URIs and X509 subjectAltName extensions as discussed in #4278