Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import javax.net.ssl.SSLException
import javax.net.ssl.SSLSession
import okhttp3.internal.canParseAsIpAddress
import okhttp3.internal.toCanonicalHost
import okio.utf8Size

/**
* A HostnameVerifier consistent with [RFC 2818][rfc_2818].
Expand Down Expand Up @@ -96,7 +95,7 @@ object OkHostnameVerifier : HostnameVerifier {
}

/** Returns true if the [String] is ASCII encoded (0-127). */
private fun String.isAscii() = length == utf8Size().toInt()
private fun String.isAscii() = all { it.code <= 127 }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suspect we want to avoid Iterable.all to avoid an allocation.


/**
* Returns true if [hostname] matches the domain name [pattern].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,33 @@ class HostnameVerifierTest {
assertThat(localVerifier.verify("\uD83D\uDCA9.com", session)).isFalse()
}

/**
* Test that malformed surrogates (unpaired high/low surrogates) are correctly
* identified as non-ASCII and rejected.
*
* https://github.com/square/okhttp/issues/6357
*/
@Test fun malformedSurrogatesAreNotAscii() {
val heldCertificate =
HeldCertificate
.Builder()
.commonName("Foo Corp")
.addSubjectAlternativeName("foo.com")
.build()
val session = session(heldCertificate.certificatePem())

// Unpaired high surrogate - should not match any hostname
assertThat(verifier.verify("\uD800.com", session)).isFalse()
assertThat(verifier.verify("foo\uD800.com", session)).isFalse()

// Unpaired low surrogate - should not match any hostname
assertThat(verifier.verify("\uDC00.com", session)).isFalse()
assertThat(verifier.verify("foo\uDC00.com", session)).isFalse()
Comment on lines +821 to +827
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would any of these have matched before the change? is there some case where length == utf8Size().toInt() would match because of offsetting issues, longer lower case, and malformed surrogate.


// Valid hostname should still work
assertThat(verifier.verify("foo.com", session)).isTrue()
}

@Test fun verifyAsIpAddress() {
// IPv4
assertThat("127.0.0.1".canParseAsIpAddress()).isTrue()
Expand Down
Loading