Skip to content

Fail the connection when a fatal TLS alert arrives after the handshake - #8490

Open
choksi2212 wants to merge 7 commits into
mitmproxy:mainfrom
choksi2212:fix/tls13-fatal-alert-after-handshake
Open

choksi2212 wants to merge 7 commits into
mitmproxy:mainfrom
choksi2212:fix/tls13-fatal-alert-after-handshake

Conversation

@choksi2212

Copy link
Copy Markdown

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() as SSL.Error. TLSLayer.receive_data logged 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 a 502 Bad Gateway, so the TLS 1.3 behavior is an observable regression in failure handling.

With this change, TLSLayer.receive_data records the error on the connection (conn.error, the same convention the QUIC stream layer already uses) and dispatches ConnectionClosed to the child layer, so the HTTP layer synthesizes its 502 promptly. Http1Client now surfaces conn.error in the ResponseProtocolError it 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) and ZeroReturnError (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:

  • Before: mitmdump logs TLS Error: [('SSL routines', '', 'tlsv13 alert certificate required')] and then nothing — the client hangs indefinitely.
  • After: the flow fails immediately with << 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) through ServerTLSLayer post-handshake and asserts the connection is failed (CloseConnection + ConnectionClosed to the child + CAN_READ cleared + conn.error recorded). Fails on main with a playbook mismatch: no CloseConnection is ever emitted.
  • test/mitmproxy/proxy/layers/http/test_http1.py::TestClient::test_server_closed_with_error — asserts Http1Client surfaces the recorded connection error in the ResponseProtocolError when the server connection dies. Fails on main, which emits the generic server closed connection message instead.
  • Full local suite: 2005 passed, 3 skipped (Windows sandbox; the two deselected test_memory_usage_* tests pass standalone on this machine and only hit a missing _curses module under the xdist run — Python curses is not available on Windows).
  • ruff check / ruff format clean on all changed files; mypy reports no errors for the changed modules.

choksi2212 and others added 3 commits October 18, 2026 09:42
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]>
@choksi2212

Copy link
Copy Markdown
Author

Merged current main into this branch at b39a278 to clear the conflict.

  • The only conflicting file was CHANGELOG.md (both sides added Unreleased entries); both sides are kept, no entries lost.
  • mitmproxy/proxy/layers/tls.py auto-merged; the only main-side change to that file is the docstring typo fix from Fix typos in option help, docstrings and docs #8494 (negotation → negotiation), orthogonal to the alert-handling change here.
  • Re-verified on the merged tree: pytest test/mitmproxy/proxy/ 469 passed, 1 skipped, including test_tls.py; the merge partner's regression suite test/mitmproxy/net/http/test_cookies.py (Fix Set-Cookie attribute parsing for short Expires values #8489) also passes.

…-after-handshake

# Conflicts:
#	CHANGELOG.md
@choksi2212

Copy link
Copy Markdown
Author

Merged current main (through 1210303, the 12.3.0 release) into this branch at e6d16c7 to clear the conflict.

  • The only conflicting file was CHANGELOG.md: the branch's bullet now sits in the fresh ## Unreleased: mitmproxy next section above the new ## 22 October 2026: mitmproxy 12.3.0 section, everything else is main's verbatim.
  • main has not touched mitmproxy/proxy/layers/tls.py (or any proxy-layer test) since the previous merge at b39a278, so the fix hunks are unchanged.
  • Re-verified on the merged head: pytest test/mitmproxy/proxy/ → 469 passed, 1 skipped — identical to the pre-merge result on b39a278.

@choksi2212

Copy link
Copy Markdown
Author

Main moved past 12.3.0 again yesterday, so this branch conflicted once more — resolved at 39a8dc3.

  • The conflicting file was again only CHANGELOG.md: the branch bullet now sits alongside the Format two exception messages that used %s with separate args #8509 bullet in the fresh ## Unreleased: mitmproxy next section, no entries lost.
  • #8509 touches bidi.py/optmanager.py; mitmproxy/proxy/layers/tls.py and the proxy tests are untouched by it, so the fix hunks are unchanged since the last merge.
  • Re-verified on the merged head: pytest test/mitmproxy/proxy/ → 469 passed, 1 skipped — identical to the pre-merge result.

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]>
@choksi2212

Copy link
Copy Markdown
Author

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). test_fatal_alert_after_handshake now keeps a reference to the TLS layer, switches debug on right before the alert arrives, and pins the full sequence: the event echo log, the TLS Error warning, the fatal ... alert debug line, the CloseConnection echo, and the ConnectionClosed swallow.

test/mitmproxy/proxy/ is still 469 passed, 1 skipped, ruff check and format clean, so the patch should be at full diff coverage once CI reports on the new head.

@choksi2212

Copy link
Copy Markdown
Author

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.

@mjatin2007 mjatin2007 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.

@choksi2212

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?

This branch has not been deployed

No deployments
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.

Request hangs when a TLS 1.3 server rejects the client certificate after the handshake

2 participants