fix: surface the real OpenSSL error instead of a generic TLS message

_request()'s ssl.SSLCertVerificationError handler hardcoded "certificate
verification failed" regardless of cause, discarding e.verify_message/
e.reason (standard OpenSSL diagnostic text - never a secret) - exactly
what's needed to tell a wrong CA, expired cert, clock skew, and a
hostname mismatch apart. Found while diagnosing a live addKey TLS
failure on minisforum that doesn't reproduce from this machine (same
CA file, same pinned IP, same SNI hostname - openssl s_client and a
direct repro of get_pinned() both succeed here), so the next attempt
needs the real reason, not a collapsed generic string. Also stopped
discarding detail on the generic SSLError/OSError branches.

Verified: forcing a real verification failure (wrong CA) now reports
"unable to get local issuer certificate" instead of the old generic
text. Full test suite still passes (86/86).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Nik Afiq 2026-09-22 18:30:35 +09:00
parent de5ce7bf52
commit 7b78e4a88e

View File

@ -785,13 +785,22 @@ class HttpClient:
data = resp.read(4_000_000)
return resp.status, data
except ssl.SSLCertVerificationError as e:
raise ApiError("tls", "certificate verification failed") from e
# verify_message/reason are standard OpenSSL diagnostic text
# (e.g. "unable to get local issuer certificate", "certificate
# has expired", a hostname mismatch) — never a secret, and
# exactly what's needed to tell "wrong CA", "expired cert",
# "clock skew", and "hostname mismatch" apart instead of
# collapsing all of them into one opaque string.
detail = e.verify_message or e.reason or str(e) or "certificate verification failed"
raise ApiError("tls", detail) from e
except ssl.SSLError as e:
raise ApiError("tls", e.__class__.__name__) from e
detail = getattr(e, "reason", None) or str(e) or e.__class__.__name__
raise ApiError("tls", f"{e.__class__.__name__}: {detail}") from e
except socket.timeout as e:
raise ApiError("timeout", "request timed out") from e
except (OSError, http.client.HTTPException) as e:
raise ApiError("network", e.__class__.__name__) from e
detail = str(e) or e.__class__.__name__
raise ApiError("network", f"{e.__class__.__name__}: {detail}") from e
finally:
with contextlib.suppress(Exception):
conn.close()