Skip to content

Cherry pick #118588 to 26.3: Do not deserialize a Data packet that arrives before any query - #119693

Merged
robot-ch-test-poll3 merged 13 commits into
backport/26.3/118588from
cherrypick/26.3/118588
Sep 13, 2026
Merged

robot-ch-test-poll3 merged 13 commits into
backport/26.3/118588from
cherrypick/26.3/118588

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #118588

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 12 commits September 7, 2026 12:38
TCPHandler::receivePacketsExpectQuery() handles the packets that arrive
before any Query has been received. Its Data/Scalar case called
processUnexpectedData() and only then threw UNEXPECTED_PACKET_FROM_CLIENT.
That call builds a headerless NativeReader and reads a block, so the
column type name comes off the wire and is handed to DataTypeFactory.

Entering interserver mode requires only the name of a cluster that has a
non-empty <secret>, never the secret itself, and the secret is verified
only inside processQuery(), which this loop never reaches. So an
unauthenticated peer decided which type the server constructed and which
deserializer ran.

The read had no consumer for any peer either: its return value was
discarded, and the exception thrown right after is caught where
"if (!query_state) return;" exits runImpl, so the exception is never
sent, the connection is closed, and no further byte is read from the
socket. runImpl's exception handler already documents that policy ("the
server should not try to skip (parse, decompress) the remaining packets
sent by the client, as it will lead to additional work and unneeded
exposure to unauthenticated connections"); the parse it forbids had
already happened one frame earlier. The adjacent case of the same
switch, processObsoleteIgnoredPartUUIDs(), was given exactly this shape
in #99398.

No setting gates the rejection, deliberately. The TablesStatusRequest
path needed an operator opt-out when it was hardened in #99854 because
it was a served path, and rejecting it broke rolling upgrades (#113602).
Data before Query has always thrown, so there is no working flow to
preserve, and nothing a client can observe changes: the connection
closes with nothing written back, before and after.

The regression test completes an accepted interserver handshake over a
raw socket and then sends a Data packet declaring a column type that
does not exist. Without this change the server logs "Unknown data type
family: NoSuchTypeGroeneAI", which is the proof that a wire-supplied
type name reached DataTypeFactory before authentication. It is extended
into the existing pre-authentication interserver test module rather than
added as a new one, because that module already provides the cluster
with a <secret> and a handshake pinned below the chunked-packets
revision, which is what makes a hand-built packet feasible.

Requested by @ nikitamikhaylov, relayed via OranjeAI.

Co-Authored-By: Claude Opus 5 <[email protected]>
The regression test sent the complete Data packet and then asserted only
that no data type had been constructed. That is a subset of what the
change claims: a handler that read the external table name and only then
threw would touch peer-supplied payload bytes and still pass the test.

The test function now opens a second connection that sends the Data
packet type alone, one byte, and shuts down its write side. On a server
without the fix the external-table-name length varint inside
processUnexpectedData() hits end of stream, so UNEXPECTED_PACKET_FROM_CLIENT
is never constructed and the rejection is never logged; measured, that
arm fails with "Attempt to read after eof" raised from readStringBinary
one frame below processUnexpectedData(). With the fix the rejection is
immediate, because no payload byte is required.

The handshake and the bounded log barrier are factored into helpers so
the second connection reuses them; both are used by the new test only,
and the two pre-existing tests keep their own inlined handshake
unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>
The native protocol spec describes READY as the state where the client
may send Ping, Query or close, and, unlike the handshake, ping and query
phases, it gives no outcome for anything else. A client author reading it
cannot learn what happens if they send a Data packet there, which is
precisely the packet this change stops the server from decoding.

State the three facts: it is a protocol violation and the connection is
terminated, no Exception packet is sent first so the client observes a
silent close, and the body is not decoded. The silent close is general
rather than interserver-specific: query_state is always null in the loop
that reads pre-query packets, so runImpl's handler returns before any
send for every peer.

Scalar is named alongside Data because both share the same case label.
The sentence is deliberately not written as "anything other than Ping or
Query", which would be wrong: TablesStatusRequest is served from READY
and Cancel is accepted and ignored.

Co-Authored-By: Claude Opus 5 <[email protected]>
The module comment stated that a packet arriving before the Query must
neither disclose anything nor have its payload deserialized. Two
pre-query paths contradict that by design, not by omission:
processTablesStatusRequest reads the request body before validating the
hash, because the hash is computed over that body, and Ping is answered
with Pong. A reader who takes the sentence as the module's invariant
concludes the hardened TablesStatusRequest path is buggy.

Scope the claim to the packets the server rejects. The legitimately
served pre-query packets are not enumerated here; the protocol spec
carries that.

Co-Authored-By: Claude Opus 5 <[email protected]>
The module comment claimed that a packet the server rejects before the
Query must disclose nothing and must not have its payload deserialized.
The second half is false for two pre-query paths, one of which this very
comment enumerates: processTablesStatusRequest() deserializes the request
body at TCPHandler.cpp:1711 before comparing digests at :1738, because
the digest binds request.getAuthDigest(), so a wrong-secret request is
rejected only after its payload has been read. processUnexpectedHello()
likewise drains seven bounded fields and then throws. A reader who checks
the module's second bullet, a TablesStatusRequest signed with the wrong
cluster secret, against the sentence above it finds them contradictory.

Only a rejected Data or Scalar packet leaves its payload untouched, so
the payload clause now names Data. The disclosure clause holds for every
rejected packet and is unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>
The READY row of the protocol spec said the client "observes a silent
close". The server rejects the packet without reading its body, so
unconsumed bytes can still be queued when the socket closes and the client
can see a TCP reset instead. The sibling reject-before-read path behaves
the same way on master, which is why its test accepts ConnectionResetError.

Stating the reset outcome keeps a client author from treating it as a
protocol bug.
Both comments said the cluster secret is only verified while the Query
packet is processed. That is not true: a new-protocol TablesStatusRequest
validates a secret-derived hash of its own before any Query, which the
module's wrong-secret test exercises. What stays unauthenticated until the
Query packet is the connection, which is what is_interserver_authenticated
tracks and what makes an early Data packet peer-controlled input.
…ation failure

Not reading the payload is the point of the previous commit, but throwing
`UNEXPECTED_PACKET_FROM_CLIENT` for it also renames the case: an interserver
connection that has not reached its `Query` packet is unauthenticated, and
`runImpl` has a dedicated answer for `AUTHENTICATION_FAILED` in interserver
mode. Throw that instead, and keep `UNEXPECTED_PACKET_FROM_CLIENT` for an
ordinary client.

`CH Inc sync` found this: a test there asserts the guard's log line for exactly
this shape, and it stopped appearing once the packet was rejected earlier.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A Data packet arriving before any Query on an unauthenticated interserver
connection is now reported as AUTHENTICATION_FAILED, so "Unexpected packet
Data received from client" is no longer logged on the path this test drives:
both log barriers waited out their 60s timeout and the test failed even with
the guard working. Measured here, the old needle never appears at all
(assert 0 > 0 after 84s); the new one appears within the barrier, and the
test passes 50/50.

Matching the interserver wording rather than a signal shared by both
exception codes keeps the assertion tied to the unauthenticated case: a
connection that stopped reaching interserver mode would otherwise still
satisfy it. With src/Server/TCPHandler.cpp reverted to master the test still
reddens on the deserialization assertion (assert 1 == 0), so the oracle is
no weaker than before.

Co-Authored-By: Claude Opus 5 <[email protected]>
…ket-unread

Do not deserialize a Data packet that arrives before any query
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Sep 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

I authored #118588, so here is what I measured on this cherry-pick. I cannot resolve it myself: hand-authoring a backport or pushing to a robot branch is off-limits for me under the standing rule from @ pufit, and I hold triage, not push. The disposition is @ alexey-milovidov's, who applied pr-must-backport and merged the source PR.

The conflict is structural, not textual. The diff touches 3 paths and 2 of them do not exist on 26.3 (probed at ref=<branch>):

path 26.3 26.6 26.7 26.8
src/Server/TCPHandler.cpp 115076 B 124770 B 133392 B 140713 B
docs/reference/interfaces/specs/NativeProtocol.mdx no NativeProtocol page anywhere in the tree only at the old path docs/en/interfaces/specs/NativeProtocol.md 93846 B 98916 B
tests/integration/test_interserver_tables_status_auth/ absent, whole module absent, whole module present present

The module arrived on master in efe33a693543 (2026-06-18) and the spec page moved under docs/reference/ in 584a98097122 (2026-07-03), both after 26.3 was cut. That is why #119695 (26.7) and #119696 (26.8) are git-clean while this one is modify/delete against missing pre-images, so there is no side to pick.

The source hunk itself would apply. Its pre-image is byte-identical on every branch (26.3:1105-1108, 26.6:1162-1165, 26.7:1189-1192, 26.8:1227-1230), and what it relies on is present on 26.3: is_interserver_mode and is_interserver_authenticated at src/Server/TCPHandler.h:248-249, and the pre-authentication early exit in runImpl at TCPHandler.cpp:958-966, reached from the receivePacketsExpectQuery call at :541. The one construct that is master/26.8-only is the connect-time is_interserver_mode && e.code() == AUTHENTICATION_FAILED catch (master:524, 26.8:520), and it wraps the handshake, not this path. I have not built 26.3, so this is a source-level check and not a runtime one.

One timing fact before this lapses. tests/ci/cherry_pick.py pings at 3 days and closes at 7 (:788-789); a closed cherry-pick counts as backported (:300-307, "is discarded"), so once both of these close, #118588 gets pr-backports-created (:1359-1361) while 26.3 and 26.6 are still unfixed, and that label comes off again only if a cherry-pick is reopened (:1406-1410), which makes a fresh must-backport label a no-op. For a pr-critical-bugfix that end state looks worth choosing deliberately rather than by timer.

The dispositions I see are: resolve these two by hand, or close them and accept the fix on 26.7 and 26.8 only, or commission a branch-local variant for 26.3 and 26.6, which would be source-only since the test module does not exist there. I will not push to the branch or change this PR's state.

Resolve the two modify/delete conflicts by dropping the paths that do not
exist on 26.3: the `NativeProtocol` spec page (no such page in the 26.3
tree) and the `test_interserver_tables_status_auth` integration test module
(added on master after 26.3 was cut). The backport is source-only: the
`TCPHandler.cpp` hunk applied cleanly and everything it relies on
(`is_interserver_mode`, `is_interserver_authenticated`,
`ErrorCodes::AUTHENTICATION_FAILED`) is present on 26.3.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Resolved by hand as a source-only backport, as you laid out: the two modify/delete conflicts (the NativeProtocol spec page and the test_interserver_tables_status_auth module, neither of which exists on 26.3) are dropped, and the TCPHandler.cpp hunk merged cleanly. Verified on the 26.3 tree that is_interserver_mode, is_interserver_authenticated, and the ErrorCodes::AUTHENTICATION_FAILED declaration are all present, so the net diff against the backport branch is only the 9-line handler change. Not built locally (the build directory here is configured for master), so CI on this PR is the runtime check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants