-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Add proper EchTests #9604
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
Merged
+139
−32
Merged
Add proper EchTests #9604
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,52 +15,152 @@ | |
| */ | ||
| package okhttp.android.test | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.net.ssl.EchConfigMismatchException | ||
| import android.os.Build | ||
| import app.cash.burst.Burst | ||
| import assertk.assertThat | ||
| import assertk.assertions.matchesPredicate | ||
| import assertk.assertions.contains | ||
| import assertk.assertions.doesNotContain | ||
| import assertk.assertions.isEqualTo | ||
| import assertk.assertions.isFalse | ||
| import assertk.assertions.isTrue | ||
| import okhttp3.HttpUrl.Companion.toHttpUrl | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.Response | ||
| import okhttp3.android.EchAwareDns | ||
| import okhttp3.dnsoverhttps.DnsOverHttps | ||
| import org.junit.jupiter.api.Assumptions.assumeTrue | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Tag | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.fail | ||
|
|
||
| /** | ||
| * Confirms Encrypted Client Hello (ECH) end to end, with [EchAwareDns]. | ||
| * | ||
| * Test with both [okhttp3.android.AndroidDns] and [DnsOverHttps]. | ||
| * | ||
| * See `res/xml/network_security_config.xml` for overrides. | ||
| */ | ||
| @SuppressLint("NewApi") | ||
| @Tag("Remote") | ||
| class EchTest { | ||
| @Test | ||
| fun testHttpsRequest() { | ||
| val client: OkHttpClient = | ||
| @Burst | ||
| class EchTest( | ||
| private val useDoh: Boolean = false, | ||
| ) { | ||
| private lateinit var client: OkHttpClient | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| // EchAwareDns reads API 37 NetworkSecurityPolicy.getDomainEncryptionMode(). | ||
| assumeTrue(Build.VERSION.SDK_INT >= 37) | ||
|
|
||
| client = | ||
| OkHttpClient | ||
| .Builder() | ||
| .dns(dns()) | ||
| .build() | ||
| } | ||
|
|
||
| val cloudflareEchBody = | ||
| client.sendRequest(Request.Builder().url("https://cloudflare-ech.com/").build()) { | ||
| it.body.string() | ||
| } | ||
| assertThat(cloudflareEchBody).matchesPredicate { it.contains("ECH enabled") } | ||
| @Test | ||
| fun cloudflareUsesEch() { | ||
| assertThat(client.get("https://cloudflare-ech.com/cdn-cgi/trace")).contains("sni=encrypted") | ||
| } | ||
|
|
||
| val cloudflareBody = | ||
| client.sendRequest( | ||
| Request.Builder().url("https://crypto.cloudflare.com/cdn-cgi/trace").build(), | ||
| ) { | ||
| it.body.string() | ||
| } | ||
| assertThat(cloudflareBody).matchesPredicate { it.contains("ECH enabled") } | ||
| @Test | ||
| fun tlsEchDevUsesEch() { | ||
| val body = client.get("https://tls-ech.dev/") | ||
|
|
||
| assertThat(body).contains("You are using ECH") | ||
| assertThat(body).doesNotContain("not using ECH") | ||
| } | ||
|
|
||
| @Test | ||
| fun staleEchConfigIsNotRetried() { | ||
| val rejection = client.echRejectionFrom("https://stale.tls-ech.dev/") | ||
|
|
||
| // TODO retry with these, then assert "You are using ECH" like tlsEchDevUsesEch. | ||
| assertThat(rejection.hasRetryConfigList()).isTrue() | ||
| assertThat(rejection.publicHostname).isEqualTo("public.tls-ech.dev") | ||
| } | ||
|
|
||
| val tlsEchBody = | ||
| client.sendRequest(Request.Builder().url("https://tls-ech.dev/").build()) { | ||
| it.body.string() | ||
| @Test | ||
| fun wrongPublicNameIsNotRetried() { | ||
| val rejection = client.echRejectionFrom("https://wrong.tls-ech.dev/") | ||
|
|
||
| // TODO retry with these, then assert "You are using ECH" like tlsEchDevUsesEch. | ||
| assertThat(rejection.hasRetryConfigList()).isTrue() | ||
| assertThat(rejection.publicHostname).isEqualTo("public.tls-ech.dev") | ||
| } | ||
|
|
||
| /** | ||
| * TLS 1.2 cannot carry ECH. | ||
| */ | ||
| @Test | ||
| fun tls12OffersNothingToRetryWith() { | ||
| assertThat(client.echRejectionFrom("https://tls12.tls-ech.dev/").hasRetryConfigList()).isFalse() | ||
| } | ||
|
|
||
| /** | ||
| * Makes the call at [url] and returns the ECH rejection it fails with. | ||
| * | ||
| * TODO handle EchConfigMismatchException.retry_configs. | ||
| */ | ||
| private fun OkHttpClient.echRejectionFrom(url: String): EchConfigMismatchException { | ||
| val body = | ||
| try { | ||
| get(url) | ||
| } catch (e: EchConfigMismatchException) { | ||
| return e | ||
| } | ||
| assertThat(tlsEchBody).matchesPredicate { it.contains("ECH enabled") } | ||
|
|
||
| fail("expected $url to reject ECH, but it returned: $body") | ||
| } | ||
|
|
||
| @Test | ||
| fun defoUsesEch() { | ||
| assertThat(client.get("https://defo.ie/ech-check.php")).contains("SSL_ECH_STATUS: success") | ||
| } | ||
|
|
||
| private fun <T> OkHttpClient.sendRequest( | ||
| request: Request, | ||
| fn: (Response) -> T, | ||
| ): T { | ||
| val response = newCall(request).execute() | ||
| /** | ||
| * Disabled by policy. | ||
| */ | ||
| @Test | ||
| fun policyDisabledHostDoesNotUseEch() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NEAT |
||
| assertThat(client.get("https://crypto.cloudflare.com/cdn-cgi/trace")).contains("sni=plaintext") | ||
| } | ||
|
|
||
| return response.use { | ||
| fn(it) | ||
| /** | ||
| * [EchAwareDns] over the platform resolver, or over DoH when [useDoh]. Both arms use the same | ||
| * source: the ECH one carries service metadata, the other doesn't. | ||
| */ | ||
| private fun dns(): EchAwareDns = | ||
| when { | ||
| useDoh -> { | ||
| val bootstrapClient = OkHttpClient() | ||
| EchAwareDns( | ||
| echDns = dnsOverHttps(bootstrapClient, includeServiceMetadata = true), | ||
| addressOnlyDns = dnsOverHttps(bootstrapClient, includeServiceMetadata = false), | ||
| ) | ||
| } | ||
| else -> EchAwareDns() | ||
| } | ||
|
|
||
| /** Addressed by IP, so resolving the resolver doesn't need a resolver. */ | ||
| private fun dnsOverHttps( | ||
| bootstrapClient: OkHttpClient, | ||
| includeServiceMetadata: Boolean, | ||
| ): DnsOverHttps = | ||
| DnsOverHttps | ||
| .Builder() | ||
| .client(bootstrapClient) | ||
| .url("https://1.1.1.1/dns-query".toHttpUrl()) | ||
| .includeServiceMetadata(includeServiceMetadata) | ||
| .build() | ||
|
|
||
| private fun OkHttpClient.get(url: String): String = | ||
| newCall(Request.Builder().url(url).build()).execute().use { response -> | ||
| response.body.string() | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
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.
@swankjesse thoughts on handling this?
Uh oh!
There was an error while loading. Please reload this page.
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.
Discussed offline. I want to be extremely judicious at implementing retries, as it exposes us to attacks.