diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 025301f5..d07c7db8 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -560,10 +560,14 @@ jobs: signer="$(ls "$sdk"/build-tools/*/apksigner 2>/dev/null | sort -V | tail -1 || true)" test -n "$signer" || { echo "::error::no apksigner under '$sdk/build-tools'"; exit 1; } certs="$("$signer" verify --print-certs app/build/outputs/apk/release/app-release.apk)" - printf '%s\n' "$certs" | grep -E '^Signer #[0-9]+ certificate (DN|SHA-256 digest)' - # One signer, and it is ours. A second signer would be a lineage or + # apksigner's signer label varies between versions: "Signer #1 + # certificate ..." before build-tools 37, "V3.0 Signer: certificate + # ..." from 37 on. So the digests are read without relying on it. + printf '%s\n' "$certs" | grep -E 'certificate (DN|SHA-256 digest):' || true + # One signer, and it is ours: exactly one distinct SHA-256 across + # every scheme apksigner reports. A second key would be a lineage or # a mistake; either way not something to ship unexamined. - digests="$(printf '%s\n' "$certs" | sed -n 's/^Signer #[0-9]* certificate SHA-256 digest: //p')" + digests="$(printf '%s\n' "$certs" | sed -n 's/^.*certificate SHA-256 digest: //p' | sort -u)" if [ "$digests" != "$RELEASE_CERT_SHA256" ]; then if printf '%s' "$certs" | grep -q 'CN=Android Debug'; then echo "::error::the release APK is signed with a debug key" diff --git a/android/app/src/main/java/com/fabledsword/minstrel/auth/ServerProbe.kt b/android/app/src/main/java/com/fabledsword/minstrel/auth/ServerProbe.kt index dec1e63d..4df70e4c 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/auth/ServerProbe.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/auth/ServerProbe.kt @@ -171,10 +171,26 @@ class ServerProbe internal constructor( // (hostname) or as an SSLHandshakeException caused by a // CertificateException or CertPathValidatorException (trust). A port // with no TLS fails the handshake too, but with no such cause. - private fun isCertificateFailure(e: IOException): Boolean = - generateSequence(e) { it.cause } - .take(MAX_CAUSE_DEPTH) - .any { it is SSLPeerUnverifiedException || it is GeneralSecurityException } + // + // OkHttp tries a host's addresses in parallel (fast fallback). When + // all of them fail it throws one failure and attaches the rest as + // suppressed, so the certificate error can sit behind a refused + // connection to the host's other address. Both causes and suppressed + // exceptions are searched. + private fun isCertificateFailure(e: IOException): Boolean { + val seen = mutableSetOf() + val pending = ArrayDeque().apply { add(e) } + var found = false + while (!found && pending.isNotEmpty() && seen.size < MAX_EXCEPTIONS_SEARCHED) { + val t = pending.removeFirst() + if (seen.add(t)) { + found = t is SSLPeerUnverifiedException || t is GeneralSecurityException + t.cause?.let { pending.addLast(it) } + pending.addAll(t.suppressed) + } + } + return found + } private suspend fun Call.await(): Response = suspendCancellableCoroutine { continuation -> @@ -201,6 +217,6 @@ class ServerProbe internal constructor( private companion object { const val SETUP_STATUS_PATH = "/api/auth/setup-status" const val CALL_TIMEOUT_MILLIS = 8_000L - const val MAX_CAUSE_DEPTH = 8 + const val MAX_EXCEPTIONS_SEARCHED = 16 } }