Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -6,6 +6,7 @@ import android.os.strictmode.DiskWriteViolation
import android.os.strictmode.IncorrectContextUseViolation
import android.os.strictmode.Violation
import androidx.annotation.RequiresApi
import io.homeassistant.companion.android.common.data.HomeAssistantApis
import io.homeassistant.companion.android.common.util.IgnoreViolationRule

val vmPolicyIgnoredViolationRules = listOf(
Expand All @@ -28,6 +29,7 @@ val threadPolicyIgnoredViolationRules = listOf(
IgnoreMiuiTurboSchedMonitorDiskRead,
IgnoreChromiumKeyStoreDiskWrite,
IgnoreAppCompatPersistLocalesDiskReadWrite,
IgnoreConfigureOkHttpClientDiskRead,
)

/**
Expand Down Expand Up @@ -327,3 +329,21 @@ private data object IgnoreChromiumKeyStoreDiskWrite : IgnoreViolationRule {
}
}
}

/**
* Ignores a [DiskReadViolation] raised while [HomeAssistantApis] configures its OkHttpClient:
* building the TLSHelper reads CA keystores from disk on some devices.
*
* Solved by https://github.com/home-assistant/android/pull/7042
*/
private data object IgnoreConfigureOkHttpClientDiskRead : IgnoreViolationRule {
@RequiresApi(Build.VERSION_CODES.P)
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
override fun shouldIgnore(violation: Violation): Boolean {
if (violation !is DiskReadViolation) return false

return violation.stackTrace.any {
it.className == HomeAssistantApis::class.java.name &&
it.methodName == "configureOkHttpClient"
}
}
}
43 changes: 27 additions & 16 deletions automotive/lint-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
errorLine2=" ^">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/frontend/FrontendScreen.kt"
line="288"
line="291"
column="9"/>
</issue>

Expand All @@ -360,7 +360,7 @@
errorLine2=" ^">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/frontend/FrontendScreen.kt"
line="386"
line="405"
column="55"/>
</issue>

Expand All @@ -371,7 +371,7 @@
errorLine2=" ^">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/frontend/FrontendScreen.kt"
line="423"
line="442"
column="9"/>
</issue>

Expand Down Expand Up @@ -778,7 +778,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="104"
line="106"
column="5"/>
</issue>

Expand All @@ -789,7 +789,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="118"
line="120"
column="5"/>
</issue>

Expand All @@ -800,7 +800,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="133"
line="135"
column="5"/>
</issue>

Expand All @@ -811,7 +811,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="149"
line="151"
column="5"/>
</issue>

Expand All @@ -822,7 +822,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="166"
line="168"
column="5"/>
</issue>

Expand All @@ -833,7 +833,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="181"
line="183"
column="5"/>
</issue>

Expand All @@ -844,7 +844,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="201"
line="203"
column="5"/>
</issue>

Expand All @@ -855,7 +855,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="218"
line="220"
column="5"/>
</issue>

Expand All @@ -866,7 +866,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="235"
line="237"
column="5"/>
</issue>

Expand All @@ -877,7 +877,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="252"
line="254"
column="5"/>
</issue>

Expand All @@ -888,7 +888,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="268"
line="270"
column="5"/>
</issue>

Expand All @@ -899,7 +899,18 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="291"
line="293"
column="5"/>
</issue>

<issue
id="ObsoleteSdkInt"
message="Unnecessary; `SDK_INT` is always >= 28"
errorLine1=" @RequiresApi(Build.VERSION_CODES.P)"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="319"
column="5"/>
</issue>

Expand All @@ -910,7 +921,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="${:automotive*fullDebug*MAIN*sourceProvider*0*javaDir*5}/io/homeassistant/companion/android/util/IgnoreViolationRules.kt"
line="317"
line="340"
column="5"/>
</issue>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.homeassistant.companion.android.common.data

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import java.net.Socket
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLEngine
import javax.net.ssl.X509ExtendedTrustManager

/**
* An [X509ExtendedTrustManager] that trusts a server certificate if either the [primary] or the
* [fallback] accepts it. The [primary] is always tried first; the [fallback] is only asked when the
* [primary] rejects the chain with a [CertificateException].
*
* This is deliberately more permissive than the [primary] alone: a certificate the [primary] rejects
* becomes trusted as soon as the [fallback] accepts it. It can never be stricter than the [primary],
* so it only ever adds acceptances and never introduces new rejections.
*
* Client-trust checks ([checkClientTrusted]) and [getAcceptedIssuers] decide which client
* certificates to accept, which is unrelated to widening server-certificate trust, so they delegate
* to the [primary] only.
*
* Requires API 24: [X509ExtendedTrustManager] and its hostname-aware overloads don't exist below it.
*/
@RequiresApi(Build.VERSION_CODES.N)
@SuppressLint("CustomX509TrustManager")
internal class CompositeX509ExtendedTrustManager(
private val primary: X509ExtendedTrustManager,
private val fallback: X509ExtendedTrustManager,
) : X509ExtendedTrustManager() {

/**
* Runs [check] against the [primary], and only if it rejects with a [CertificateException]
* retries against the [fallback]. If both reject, the [primary]'s exception is rethrown (its
* verdict wins) with the [fallback]'s attached via [Throwable.addSuppressed].
*
* Only [CertificateException] triggers the fallback; anything else propagates unchanged.
*/
private inline fun checkServerOrFallback(check: (X509ExtendedTrustManager) -> Unit) {
try {
check(primary)
} catch (primaryError: CertificateException) {
try {
check(fallback)
} catch (fallbackError: CertificateException) {
primaryError.addSuppressed(fallbackError)
throw primaryError
}
}
}

override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {
checkServerOrFallback { it.checkServerTrusted(chain, authType) }
}

override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?, socket: Socket?) {
checkServerOrFallback { it.checkServerTrusted(chain, authType, socket) }
}

override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?, engine: SSLEngine?) {
checkServerOrFallback { it.checkServerTrusted(chain, authType, engine) }
}

/**
* Client trust uses the primary only (see class doc).
*/
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {
primary.checkClientTrusted(chain, authType)
}

/**
* Client trust uses the primary only (see class doc).
*/
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?, socket: Socket?) {
primary.checkClientTrusted(chain, authType, socket)
}

/**
* Client trust uses the primary only (see class doc).
*/
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?, engine: SSLEngine?) {
primary.checkClientTrusted(chain, authType, engine)
}

/** Returns the [primary]'s accepted issuers only; the [fallback] has no role in client authentication. */
override fun getAcceptedIssuers(): Array<X509Certificate> {
return primary.acceptedIssuers
}
}
Loading
Loading