Skip to content

fix(http): fail at once on a refused server certificate instead of retrying - #632

Merged
linroid merged 2 commits into
mainfrom
fix/http-certificate-failures
Oct 23, 2026
Merged

linroid merged 2 commits into
mainfrom
fix/http-certificate-failures

Conversation

@linroid

@linroid linroid commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Problem

Downloading from a server whose certificate the platform refuses (expired, self-signed, issued for another host, unknown root) failed with a retryable KetchError.Network. Every download retried it three times, about 15 seconds, before giving up:

Download failed: Network: Network error occurred <- FailedRequest: GET https://expired.badssl.com/ <- SSLHandshakeException: (certificate_expired) PKIX path validation failed: ...

The certificate is the same on every attempt, so the retries only delay the failure. The apps then say "Connection lost" and suggest checking the connection, which doesn't help either.

Fix

  • KtorHttpEngine classifies a refused certificate (certificateProblemOf, expect/actual) and fails the request with a non-retryable KetchError.SourceError saying which problem it found:
    • JVM and Android read JSSE, Conscrypt and OkHttp exceptions: CertificateExpiredException, CertificateNotYetValidException, CertificateRevokedException, the CertPathValidatorException reasons, OkHttp's Hostname … not verified and any other CertificateException.
    • iOS reads the NSURLErrorServerCertificate* codes.
  • The descriptions are "The server's certificate has expired", "… is not valid yet", "… has been revoked", "… is for another host" and "… is not trusted".
  • Handshakes that are cut short and other transport failures stay retryable Network errors.
  • Metalink's MirrorPool drops a mirror at once on such a SourceError instead of backing off and retrying it five times.
  • Removed a stale duplicate KDoc on requestHeaders.

After this change, against badssl.com:

Download failed: The server's certificate has expired <- FailedRequest: HEAD https://expired.badssl.com/ <- ...

The download now fails in about 1 s instead of 16 s, and the HEAD probe no longer falls back to a ranged GET.

Tests

  • CertificateProblemTest (jvmTest):
    • Classifier cases for expired, untrusted and wrong host, plus handshake failures that must stay transient.
    • A loopback HttpsServer with a self-signed certificate (keytool): the engine fails at once with the detail, without retrying, and the server sees no request.
  • MirrorPoolTest: a refused certificate drops the mirror, a network error keeps it. This test fails without the fix.
  • Checked by hand with the CLI against expired, self-signed, wrong.host and untrusted-root.badssl.com, plus badssl.com itself, which still downloads.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T18:23:34.283356Z 5b79bc7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5b79bc7802

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 2 205 files  + 5   2 233 suites  +5   57m 8s ⏱️ +41s
15 395 tests +15  15 127 ✅ +15  268 💤 ±0  0 ❌ ±0 
18 762 runs  +15  18 494 ✅ +15  268 💤 ±0  0 ❌ ±0 

Results for commit 92d0028. ± Comparison against base commit 73c60d3.

♻️ This comment has been updated with latest results.

…trying

An expired, self-signed or wrong-host certificate was reported as a
retryable network error, so a download retried it three times, about
15 seconds, before failing with "Network error occurred" and a cause
chain that repeats itself. The certificate is refused the same way on
every attempt.

KtorHttpEngine now reads the refusal from JSSE, Conscrypt and OkHttp
exceptions (JVM, Android) or NSURLErrorServerCertificate codes (iOS)
and fails with a SourceError saying which: expired, not valid yet,
revoked, for another host or not trusted. Metalink mirrors drop a mirror
whose certificate is refused at once rather than backing off.
@linroid
linroid force-pushed the fix/http-certificate-failures branch from 5b79bc7 to 92d0028 Compare October 23, 2026 19:17
@linroid
linroid merged commit 55e8722 into main Oct 23, 2026
14 checks passed
@linroid
linroid deleted the fix/http-certificate-failures branch October 23, 2026 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant