Skip to content
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

Use generated certificates in unit tests #6346

Merged
merged 6 commits into from
Oct 30, 2020
Merged

Conversation

yschimke
Copy link
Collaborator

@yschimke yschimke commented Oct 28, 2020

We currently embed externally generated certificates in our unit tests, but we can now generate our own certificates so we should.

@yschimke yschimke requested a review from swankjesse October 28, 2020 07:01
@@ -61,7 +60,7 @@ object OkHostnameVerifier : HostnameVerifier {

/** Returns true if [certificate] matches [hostname]. */
private fun verifyHostname(hostname: String, certificate: X509Certificate): Boolean {
val hostname = hostname.toLowerCase(Locale.US)
val hostname = DnsUtils.normalizeIA5String(hostname)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use hostname.toCanonicalHost() instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worse actually, causes us to fail on the tel example also.

okhttp3.internal.tls.HostnameVerifierTest > specialKInHostname FAILED
    org.junit.ComparisonFailure: expected:<[fals]e> but was:<[tru]e>
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at okhttp3.internal.tls.HostnameVerifierTest.specialKInHostname(HostnameVerifierTest.java:586)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is a fix where fix toCanonicalHost logic and then we use it, but maybe that's a good second pass PR once the test is in.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just removing the toLowerCase(Locale.US) call completely? I’m trying to avoid having a bunch of code in here that doesn’t get exercised in practice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still public API



  @Test
  public void thatCatchesErrorsWithBadSession() {
    HostnameVerifier localVerifier = new OkHttpClient().hostnameVerifier();

    // Since this is public API, okhttp3.internal.tls.OkHostnameVerifier.verify is also
    assertThat(verifier).isInstanceOf(OkHostnameVerifier.class);

    SSLSession session = TlsUtil.localhost().sslContext().createSSLEngine().getSession();
    assertThat(localVerifier.verify("\uD83D\uDCA9.com", session)).isFalse();
  }

@@ -61,7 +60,7 @@ object OkHostnameVerifier : HostnameVerifier {

/** Returns true if [certificate] matches [hostname]. */
private fun verifyHostname(hostname: String, certificate: X509Certificate): Boolean {
val hostname = hostname.toLowerCase(Locale.US)
val hostname = DnsUtils.normalizeIA5String(hostname)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just removing the toLowerCase(Locale.US) call completely? I’m trying to avoid having a bunch of code in here that doesn’t get exercised in practice.

}

// https://tools.ietf.org/html/rfc2459#section-4.2.1.11
fun normalizeIA5String(s: String): String {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Okio this function is called ByteString.toAsciiLowercase() (though to use that one we’d need to roundtrip through a ByteString)

@swankjesse
Copy link
Collaborator

@yschimke
Copy link
Collaborator Author

I pushed a commit here:
https://github.com/square/okhttp/compare/master...yschimke-hostnames?expand=1#files_bucket

Nice simplification Picasso :)

I'll land based on your commit.

@yschimke yschimke merged commit bdd55bc into square:master Oct 30, 2020
@yschimke yschimke deleted the hostnames branch November 8, 2020 15:24
yschimke added a commit to yschimke/okhttp that referenced this pull request Jul 4, 2021
* Use generated certificates in unit tests
* Strict to ascii lowercase implementation

Co-authored-by: Jesse Wilson <jwilson@squareup.com>
yschimke added a commit that referenced this pull request Jul 4, 2021
* Use generated certificates in unit tests (#6346)

* Use generated certificates in unit tests
* Strict to ascii lowercase implementation

Co-authored-by: Jesse Wilson <jwilson@squareup.com>

* More restrictive behaviour of OkHostnameVerifier (#6353)

* Test quirks of HostnameVerifier.
* Restrict successful results to ascii input.

Co-authored-by: Jesse Wilson <jwilson@squareup.com>
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.

3 participants