fix(exchange): handle encrypted frame during server key exchange#1777
Merged
Conversation
The server key-exchange read path treated every frame it read as an unencrypted exchange message. When a client reuses an already-established auth key on a connection the server still considers to be performing key exchange, the caller could not tell this apart from a malformed message and would typically reply auth_key_not_found (-404). Clients such as Telegram Desktop treat a -404 on a temporary key as "key destroyed", discard it and re-run key exchange, producing a reconnect/key-exchange storm. Detect a non-zero leading auth_key_id on the server side and return the raw frame as the new exported UnexpectedEncryptedError, so callers can resolve the key and dispatch the frame as a normal encrypted message instead of failing the exchange. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1777 +/- ##
==========================================
- Coverage 71.23% 71.19% -0.04%
==========================================
Files 503 503
Lines 23577 23585 +8
==========================================
- Hits 16795 16792 -3
- Misses 5550 5563 +13
+ Partials 1232 1230 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The server-side key-exchange read path treated every frame it read as an unencrypted exchange message. When a client reuses an already-established auth key on a connection the server still considers to be performing key exchange, the caller could not distinguish this from a malformed message and would typically reply
auth_key_not_found(-404).Clients such as Telegram Desktop treat a
-404on a temporary key as "key destroyed", discard the key and re-run key exchange — producing a reconnect/key-exchange storm (hundreds of connect/exchange/disconnect cycles per second, with the client logging-404 error received ... with temporary key, assuming it was destroyed).Fix
On the server side, detect a non-zero leading
auth_key_idwhile reading an unencrypted exchange message and return the raw frame as a new exported error type,*exchange.UnexpectedEncryptedError, instead of failing the exchange.This lets the caller resolve the key and dispatch the frame as a normal encrypted message (or reply
-404only when the key is genuinely unknown), rather than blindly tearing down a still-valid key.Verification
Wired into a downstream MTProto server (gotd/teled) via
replace, rebuilt and restarted against a live Telegram Desktop client:Client connected/Key exchange failed: EOFper second; client logged 185-404 ... temporary key ... destroyed.0-404sent,0key-exchange failures, ~12 stable connections, normal RPC traffic; client resumed normal sync.Adds
TestServerUnexpectedEncryptedcovering the new behavior.