Skip to content

Start TLS on the control channel right after the AUTH TLS reply - #606

Merged
hannesdejager merged 2 commits into
Cloudinvoke:masterfrom
monurpalan:fix/auth-tls-upgrade-race
Nov 7, 2026
Merged

hannesdejager merged 2 commits into
Cloudinvoke:masterfrom
monurpalan:fix/auth-tls-upgrade-race

Conversation

@monurpalan

@monurpalan monurpalan commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #605.

AUTH TLS spawned a task to send SecureControlChannel and returned 234 at once, so the control loop could read the client's ClientHello as a command line before switching to TLS (or drop the part the codec had already buffered). Details are in the issue.

Change

  • Auth::handle only returns the 234 reply; it no longer spawns the internal message.
  • The control loop starts TLS itself right after sending a 234 reply to AUTH TLS, before reading anything else from the plain connection. If the codec already holds bytes the client sent with the command, they are handed to the TLS acceptor (Framed::into_parts + tokio::io::join) instead of being dropped.
  • The SecureControlChannel event still goes through the middleware chain after the handshake, so session.cmd_tls, logging and metrics behave as before. Client certificates are stored on the session as before.

Test
tests/ftps.rs (new) upgrades 50 control connections and sends NOOP over TLS on each, in two variants: ClientHello right after reading 234, and ClientHello in the same write as AUTH TLS. It uses a test CA and a localhost certificate in tests/resources/ftps/ (test-only key, valid until 2126) and adds rustls as a dev-dependency for the client.

before after
ClientHello after 234, under CPU load, 20 runs 19 runs failed (1–5 of 50 upgrades each) 20 runs passed
ClientHello sent with AUTH TLS 50 of 50 upgrades failed all passed

Without load the first variant rarely fails, so on an idle machine it mainly guards the change; the second one fails deterministically without the fix.

Checked on Linux with Rust 1.92: cargo fmt --check, cargo clippy --all-features --workspace -- -D warnings, cargo test --workspace --exclude 'unftp-sbe-gcs*', doc tests, examples, cargo doc, and the feature combinations from the contributing guide.

The CHANGELOG entry is under Unreleased.

AUTH TLS spawned a task to send the internal SecureControlChannel message
and returned the 234 reply at once. The control loop then went back to
selecting between that message and the next bytes from the client. A
client starts the TLS handshake as soon as it reads the 234 (RFC 4217,
section 4), so when its ClientHello won that race it was decoded as an
FTP command and answered in plain text, and the client failed with
"received corrupt message of type InvalidContentType". When the message
won but the codec had already read part of the ClientHello, those bytes
were dropped by `into_inner()` and the handshake stalled.

The control loop now starts TLS itself right after sending a 234 reply to
AUTH TLS, before reading anything else from the plain connection. Bytes
the codec already holds are handed to the TLS acceptor. The
SecureControlChannel event still goes through the middleware chain
afterwards, so the session state and logging are unchanged.

tests/ftps.rs upgrades 50 control connections, with the ClientHello sent
right after the reply and in the same write as the command. Under CPU
load the first case failed in 19 of 20 runs before this change (1 to 5 of
the 50 upgrades each); the second failed every upgrade. Both pass in 20
of 20 runs with it.
@hannesdejager

Copy link
Copy Markdown
Member

Looks great, thanks @monurpalan

@hannesdejager
hannesdejager merged commit f19fbc1 into Cloudinvoke:master Nov 7, 2026
8 checks passed
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.

AUTH TLS: a fast client's ClientHello can be read as an FTP command (InvalidContentType)

2 participants