Skip to content

Record the forwarded client address for every interserver authentication failure - #125326

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/interserver-auth-failure-forwarded-address
Oct 12, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/interserver-auth-failure-forwarded-address

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123188

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Interserver authentication failures on a listener that uses the PROXY protocol now record the client address from the PROXY header in system.session_log when auth_use_forwarded_address is enabled, instead of the address of the proxy.

Description

Follow-up to #123188, as requested in #123188 (comment).

On a PROXY listener with auth_use_forwarded_address, six of the seven interserver onAuthenticationFailure calls in TCPHandler.cpp passed socket().peerAddress(), so their LoginFailure rows in system.session_log named the proxy instead of the client: the unknown-cluster / no-secret reject in receiveHello and the five processQuery failures (cluster lookup, no salt, no nonce, hash mismatch, non-SSL build). Only the site added in #123188 resolved the forwarded address, inline.

The change moves that resolution into TCPHandler::getClientAddressNoThrow and calls it at all seven sites. It reads the connection's own ClientInfo (session->getClientInfo(), filled from the PROXY header or the protocols stack), never the one from the Query packet: at these sites the hash is not verified yet, so that packet's forwarded_for is chosen by the peer. Unlike getClientAddress, it does not throw on a malformed forwarded address, so the reject stays a silent AUTHENTICATION_FAILED close with a session_log row. The non-interserver rejects keep getClientAddress, which rejects a malformed forwarded address by design.

Validation: test_tcp_with_proxy_interserver_mode gets a third instance that allows interserver mode on the proxy port, and three cases that send a PROXY header and a raw Hello (plus a Query at protocol revision 54449) to reach the receiveHello unknown-cluster reject, the no-salt reject and the hash mismatch; the last one carries a decoy X-Forwarded-For inside the Query packet. Without the change the three cases fail (the rows carry the runner's address); with it, the whole module passes 10 times in a row. Making the helper call getClientAddress, or the hash-mismatch site read the packet's ClientInfo, fails the existing malformed-address test and the decoy case respectively.


Workflow [PR]
Sync PR [sync-upstream/pr/125326]

Version info

  • Merged into: 26.10.1.2225-master (included in 26.10 and later)

…ion failure

On a PROXY listener with `auth_use_forwarded_address`, six of the seven
interserver `onAuthenticationFailure` calls in `TCPHandler` passed
`socket().peerAddress()`, so their `system.session_log` `LoginFailure` rows
named the proxy instead of the client. Only the site added in ClickHouse#123188
resolved the forwarded address, through an inline block that must not throw:
`getClientAddress` throws `INCORRECT_DATA` on a malformed forwarded value,
which would turn the silent `AUTHENTICATION_FAILED` close into a serialized
exception and drop the row.

Move that block into `getClientAddressNoThrow` and call it at all seven
sites (the `receiveHello` proxy-port and unknown-cluster / no-secret rejects,
and the five `processQuery` failures). The helper takes no `ClientInfo`
argument on purpose: in `processQuery` the local `client_info` has been
overwritten from the still unauthenticated peer's Query packet, whose
`forwarded_for` is chosen by that peer, so it must read the session's own
`ClientInfo`, which carries only the connection-level value from the PROXY
header or the `protocols` stack.

The non-interserver rejects keep `getClientAddress`, which rejects a
malformed forwarded address by design.

Test: a third instance in `test_tcp_with_proxy_interserver_mode` that allows
interserver mode on the proxy port, and three raw-protocol cases reaching the
`receiveHello` unknown-cluster reject, the no-salt reject and the hash
mismatch (the last with a decoy `X-Forwarded-For` in the Query packet, sent
at protocol revision 54449 so that no addendum or nonce is involved).

Requested in ClickHouse#123188 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review: my own cold review plus an independent model (engine: codex; 2 findings; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ⚠️ The third test instance node_forwarded is redundant; reuse node_default DISAGREE node_default is the instance requested on #123188 to set only tcp_with_proxy_port (review comment); node rejects interserver mode before these sites. The whole module runs in about 12 s locally.
2 💡 Changelog category should be Bug Fix DISAGREE No query result, accepted input or authentication decision changes, only the address in a LoginFailure row. #123188 filed the same change at its own site as Improvement.
3 💡 Three of the seven sites have no test noted Same one-line change as the tested sites. The no-nonce site is unreachable (a nonce is always sent at revisions >= V2), the non-SSL site exists only without SSL, and the processQuery cluster-lookup site needs the cluster removed between Hello and Query.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261012-062600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The three new test_tcp_with_proxy_interserver_mode cases fail 15/15 on the unchanged base build: the LoginFailure rows carry the runner's address and an ephemeral port instead of the PROXY source 192.0.2.1:<port>.
b Root cause explained? Six of the seven interserver onAuthenticationFailure calls passed socket().peerAddress(). The forwarded-address-aware getClientAddress throws on a malformed forwarded value, so only the #123188 site had an inline non-throwing resolution.
c Fix matches root cause? Yes. That resolution is now one helper, getClientAddressNoThrow, used at all seven sites. It reads the session's ClientInfo, never the unauthenticated Query packet's.
d Test intent preserved / new tests added? The 4 existing tests are unchanged and pass. 3 new cases cover the reachable sites (receiveHello unknown cluster, no salt, hash mismatch with a decoy X-Forwarded-For in the Query packet).
e Both directions demonstrated? Base: 15/15 fail. Fix: 30/30 for the new cases, 70/70 for the whole module. A mutant helper that calls getClientAddress fails the existing malformed-address test, and a hash-mismatch site that reads the packet's ClientInfo fails the decoy case.
f Fix is general across code paths? All 7 interserver sites use the helper (3 are wire-reachable and tested; one needs a config reload between Hello and Query, one is unreachable because a nonce is always sent, one exists only in non-SSL builds). The non-interserver rejects keep the throwing getClientAddress by design.
g Fix generalizes across inputs (params/datatypes/wrappers)? Forwarded address absent (peer address, as before), well-formed (used), malformed (peer address, no throw), and a peer-supplied value in the Query packet (ignored). Both forwarded sources (PROXY header, protocols stack) reach the session the same way.
h Backward compatible? (maintainer-approved exception only) Yes. Only the address in failure rows changes, and only with auth_use_forwarded_address enabled on a PROXY connection. There is no setting, protocol or format change.
i Invariants and contracts preserved? The reject stays a silent AUTHENTICATION_FAILED close (nothing is serialized to the unauthenticated peer), session is non-null at every site, and no authentication decision changes.

Session id: cron:clickhouse-impl-slot-4:20261012-055100

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [196d9c7]

Summary: ✅


AI Review

Summary

This PR factors the non-throwing forwarded-address resolution for interserver login-failure logging into a helper and applies it to the remaining interserver authentication-failure sites, with focused integration coverage for the reachable paths. I did not find a correctness, safety, compatibility, or evidence gap in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 12, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... label Oct 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 196d9c7cd with master 371b23a4b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

9 translation units recompiled, 49 s compile time in total, 9 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Oct 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 12, 2026
Merged via the queue into ClickHouse:master with commit bae927f Oct 12, 2026
177 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants