From f32fe95179ad5dd58da54eade15ac61b029bb4a8 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 4 Dec 2026 15:28:20 -0500 Subject: [PATCH] android: certificate failures found behind OkHttp 5's fast fallback; release-key check reads apksigner 37 (#8533) Run 11511 failed in two places. ServerProbe: OkHttp 5 tries a host's addresses in parallel. When all of them fail it throws one failure and attaches the others as suppressed exceptions. For localhost, the refused ::1 connection was thrown, and the certificate error from 127.0.0.1 was only attached, so the probe said "Couldn't reach" instead of naming the certificate. A real server with both IPv4 and IPv6 addresses would hit the same. isCertificateFailure now searches suppressed exceptions as well as causes, up to 16 in all. Release-key check: apksigner from build-tools 37 labels signers by signature scheme ("V3.0 Signer: certificate SHA-256 digest: ...") instead of "Signer #1 certificate ...". The step's grep matched nothing, and pipefail ended it with no message. The step now collects every certificate SHA-256 digest whatever the label, and still requires exactly one distinct digest, equal to the release key's. I checked the parsing on both label formats, on two keys and on no output: it passes our key in either format and fails the other two cases. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/release.yml | 10 ++++--- .../fabledsword/minstrel/auth/ServerProbe.kt | 26 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) 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 } }