Repository navigation
Fail the connection when a fatal TLS alert arrives after the handshake - #8490
choksi2212 wants to merge 7 commits into
Conversation
TLS 1.3 servers may reject a client certificate only after the handshake has already completed. The fatal alert then surfaces from recv() as SSL.Error, which the TLS layer logged without failing the connection, so the HTTP layer was left waiting on a connection that would never deliver a response (a TLS 1.2 peer that rejects during the handshake yields a prompt 502 instead). We now record the error on the connection and dispatch ConnectionClosed to the child layer, mirroring the QUIC stream layer's existing error handling. The HTTP layer surfaces conn.error in the 502 it synthesizes, so the response names the actual TLS failure instead of a generic "server closed connection". Fixes mitmproxy#8486 Signed-off-by: choksi2212 <[email protected]>
# Conflicts: # CHANGELOG.md
|
Merged current
|
…-after-handshake # Conflicts: # CHANGELOG.md
|
Merged current
|
…-after-handshake # Conflicts: # CHANGELOG.md
|
Main moved past 12.3.0 again yesterday, so this branch conflicted once more — resolved at 39a8dc3.
Still keeping this current with main as needed; ready for review whenever someone has cycles. |
…erts codecov/patch was the only red check: the diff's one uncovered branch was the debug log naming the close reason. Keep a layer reference in the test, switch debug on right before the alert, and pin the full command sequence including both debug logs, the warning, and the 502-bound close reason. Signed-off-by: choksi2212 <[email protected]>
|
Pushed fefb9bb to chase the codecov/patch red X: the only uncovered diff lines were the debug branch of the close path (the log that names the close reason).
|
|
Coming back to this one - the full matrix is green at fefb9bb (including the coverage jobs after the last round), so it should be merge-ready from CI's side. If anything in the approach needs adjusting, point me at it and I'll rework; otherwise I'll keep it rebased as main moves. |
There was a problem hiding this comment.
VM validation on the frozen base/head exercised a loopback TLS 1.3 server that kept TCP open after certificate_required: base reached the 15.1-second client timeout, while this head returned a reasoned 502 in 0.1s. The two added tests pass on head and fail on base; the recorded full proxy run is 469 passed/1 skipped, with Ruff/mypy clean. The original immediate-close script returned 502 on both revisions in this environment, so the demonstrated improvement is proactive handling independent of prompt transport shutdown. Non-blocking: could the generic SSL.Error debug label say “terminal TLS error” rather than always “fatal alert,” and is surfacing every recorded conn.error in HTTP/1 intentional?
Description
Fixes #8486.
A TLS 1.3 server that rejects the client certificate only after the handshake has completed sends a fatal alert that surfaces from
SSL.Connection.recv()asSSL.Error.TLSLayer.receive_datalogged that alert but never failed the connection, so the child layer (e.g. the HTTP layer waiting for a response) was left waiting on a connection that would never deliver a response — the client hangs. A TLS 1.2 peer that rejects the certificate during the handshake instead fails the connection promptly and yields a502 Bad Gateway, so the TLS 1.3 behavior is an observable regression in failure handling.With this change,
TLSLayer.receive_datarecords the error on the connection (conn.error, the same convention the QUIC stream layer already uses) and dispatchesConnectionClosedto the child layer, so the HTTP layer synthesizes its 502 promptly.Http1Clientnow surfacesconn.errorin theResponseProtocolErrorit emits when the server connection dies with a recorded error, so the 502 names the actual TLS failure instead of the generic "server closed connection".WantReadError(need more data) andZeroReturnError(close_notify) handling is unchanged; the new close path only fires when OpenSSL has permanently failed the record layer.Reproduction
Reporter's exact scenario, TLS 1.3 server requesting a client cert and rejecting it after the handshake:
mitmdumplogsTLS Error: [('SSL routines', '', 'tlsv13 alert certificate required')]and then nothing — the client hangs indefinitely.<< OpenSSL Error([('SSL routines', '', 'tlsv13 alert certificate required')])— same prompt-failure behavior as TLS 1.2, with the actual reason named.Testing
test/mitmproxy/proxy/layers/test_tls.py::TestServerTLS::test_fatal_alert_after_handshake— drives a genuine server-sent fatal alert (undecryptable record → OpenSSL fatal alert) throughServerTLSLayerpost-handshake and asserts the connection is failed (CloseConnection+ConnectionClosedto the child +CAN_READcleared +conn.errorrecorded). Fails onmainwith a playbook mismatch: noCloseConnectionis ever emitted.test/mitmproxy/proxy/layers/http/test_http1.py::TestClient::test_server_closed_with_error— assertsHttp1Clientsurfaces the recorded connection error in theResponseProtocolErrorwhen the server connection dies. Fails onmain, which emits the genericserver closed connectionmessage instead.test_memory_usage_*tests pass standalone on this machine and only hit a missing_cursesmodule under the xdist run — Python curses is not available on Windows).ruff check/ruff formatclean on all changed files;mypyreports no errors for the changed modules.