Skip to content

Cherry pick #116724 to 26.3: Verify the server certificate hostname on outbound TLS connections - #121185

Merged
PedroTadim merged 3 commits into
backport/26.3/116724from
cherrypick/26.3/116724
Sep 20, 2026
Merged

PedroTadim merged 3 commits into
backport/26.3/116724from
cherrypick/26.3/116724

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #116724

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 3 commits August 27, 2026 11:51
Outbound TLS validated the certificate chain but never checked that the
certificate identified the host being connected to, so any CA-signed
certificate for any name was accepted for any host. Reproduced against a
server presenting a certificate for `not-localhost` and reached at a
non-loopback address under a default client configuration: `SELECT 1`
returned `1` before this change and now fails with `Code: 210 ...
Unacceptable certificate from <host>: application verification failure`.

`SSLManager::initDefaultContext(bool server)` picks its prefix at
`SSLManager.cpp:241` and runs once per default context, so the single
`false` default at `:345` reached the default client context as well as
the server one. With extended verification off,
`SecureSocketImpl::verifyPeerCertificateImpl` returns `X509_V_OK` at
`SecureSocketImpl.cpp:465-467` before reaching `X509_check_host` or
`X509_check_ip_asc`, and nothing calls `SSL_set1_host`. That `false` was
the only place in the layer turning the check off: all three `Context`
constructors default `_extendedCertificateVerification(true)`.

The default is now `!server`. It must not flip for the server context.
On an accepted socket `_peerHostName` is empty, so the check runs against
the client's address, while a client certificate names a user. Measured:
with `openSSL.server.extendedVerification` set to `true`, which produces
the same context state a bare `true` would, a client presenting a
`CN=client1` certificate is rejected with `Unacceptable certificate from
10.33.45.204` under `relaxed` and additionally from `127.0.0.1` under
`strict`. That is `ssl_certificate` authentication broken. With `!server`
and the key absent, the same client connects in both modes.

Correcting the boundary covers every caller of the default client
context: pooled HTTPS, the native secure protocol, JWT, and the Silk
socket factory all set a peer host name already. One carrier could not
benefit, so it is fixed here too. `ZooKeeper::connect` built a bare
`SecureStreamSocket` and never called `setPeerHostName`, then connected
to a pre-resolved address, so `verifyPeerCertificate()` fell back to
`peerAddress().host().toString()` and verified the resolved IP. Measured
against a local Keeper over TLS whose certificate names the host by DNS
name only: the same configuration failed with `Unacceptable certificate
from 127.0.1.1` and now succeeds. Carrying the name is also what puts
the host into the SNI extension, which that path did not send.

`node.host` is `"<host>:<port>"`, so the name has to be extracted.
`DB::parseAddress` is not used: it requires a numeric port, and its IPv6
branch keeps the brackets, which `X509_check_host` would never match. The
helper mirrors Poco's own lexical split and never inspects the port, so
no host:port shape that resolves today changes. Verified against
`Poco::Net::SocketAddress` for IPv4, bracketed IPv6, a DNS name and a
port spelled as a service name by reconstructing the input from the
helper's answer and requiring Poco's `port()` to equal an independent
resolution of the remaining suffix. `SocketAddress::host()` is not a
valid oracle here because it returns the resolved address.

`openssl_client_extended_verification` in `ServerSettings.cpp` moves with
the literal. Nothing reads it, but it is the source of the default shown
in `system.server_settings` and in the generated reference page, so
leaving it would publish `false` for behaviour that is `true`.
`SettingsChangesHistory.cpp` holds query settings only and contains no
server settings, so no entry there is possible.

Four CI sites verify a certificate that does not name the address they
dial, out of 71 `openSSL/client` blocks in the tree, 56 of which are
immune because they set `verificationMode` to `none`. Note that
`AcceptCertificateHandler` is not an off switch: it is consulted from
OpenSSL's chain-verify callback, while extended verification throws
directly from `SecureSocketImpl`. `test_ssl_cert_authentication` sets
`tls-sni-override` to the host it already declares in `SSL_HOST`, which
makes its native path agree with the hostname checking it already forces
on its Python path. `test_bind_host` and both `test_acme_tls` configs opt
out, the latter because Pebble's serving certificate names `localhost`,
`pebble` and `127.0.0.1` while the test dials a pinned `10.5.11.2`.

New coverage: three arms in `test_accept_invalid_certificate` using the
existing CA-signed `client-cert.pem`, for a name mismatch being rejected,
`tls-sni-override` being accepted, and `extendedVerification=false`
still being honoured; and one arm in `test_keeper_secure_client` whose
client actually verifies. That suite's `server.crt` had to be regenerated
because it was `CN=localhost` with no SAN while the test connects to
`node1`; the new pair keeps `CN=localhost`, self-signed, `CA:TRUE` and
RSA-2048, adds `subjectAltName = DNS:localhost, DNS:node1`, and
`generate_certs.sh` records how.

Closes: #35504
The shared "Keys for server/client settings" table is a hand-maintained
generator input (`autogenerate_docs.py:185-187` lists it under `deps` and
`global-server-settings.sql:11` reads it with `file(..., RawBLOB)`), so it
carries no autogenerated region and nothing rewrites this cell. Left at
`false` it would contradict the generated per-setting page, which the
nightly derives from the `DECLARE` and will render as `true` for the client
setting, and it would misinform the operators the backward incompatible
changelog entry is aimed at.

The server default is unchanged, so the cell names both.
…-hostname

Verify the server certificate hostname on outbound TLS connections
@robot-clickhouse robot-clickhouse 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 labels Sep 20, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

The conflict is one file, src/Common/ZooKeeper/ZooKeeperImpl.cpp, and one add-versus-empty hunk. The cherry-picked change puts its peerHostName() helper into an anonymous namespace inside namespace Coordination that exists on master only because it holds triggerWatchCallback(), a watch-callback refactor 26.3 does not have, so the 26.3 side of that hunk is empty. The resolution is master's block minus triggerWatchCallback().

merge -X ours is not a faster route to the same tree here: keeping that function gives 6 compile errors on 26.3, every one of them API the branch lacks, starting with no member named 'getTriggeredEvent' in 'Coordination::WatchCallbackPtrOrEventPtr'.

Everything else auto-merges, including the docs row, which lands on 26.3's docs/en/operations/server-configuration-parameters/_server_settings_outside_source.md by rename detection. Against backport/26.3/116724 the merge changes exactly the 15 files the cherry-pick carries, and moves no submodule.

I verified it on the 26.3 tree rather than just making it merge. It builds clean with clang-21, the toolset 26.3 pins, and the ported tests pass there: test_keeper_secure_client 2/2, test_accept_invalid_certificate 9/9, test_ssl_cert_authentication 14/14, test_bind_host 3/3, test_acme_tls 3/3. Both directions hold as well, each server's build id read back from its own log. Dropping only the ZooKeeperImpl.cpp hunk turns test_connection_verifying_certificate red:

Certificate validation error: Unacceptable certificate from 172.18.0.3: application verification failure (version 26.3.33.1)

and on pristine 26.3 test_hostname_mismatch_rejected_by_default fails with DID NOT RAISE, which is what the new client default is for.

@alexey-milovidov, I hold pull and triage on this repository but not push, so I cannot move cherrypick/26.3/116724 myself. The merge is on my fork at aa200f5c053ebac6fc246bf2b64b2083e07bb102, and its first parent is c67f0b1829486bfa55e30e0ff050cfdcbb493e73, this PR's head, so applying it is a fast-forward and resolves nothing by hand:

git fetch https://github.com/groeneai/ClickHouse.git groeneai/resolve-26.3-cherrypick-116724
git push origin FETCH_HEAD:cherrypick/26.3/116724

cherry_pick.py merges this PR once it reports mergeable and opens the 26.3 backport PR itself. The 26.7, 26.8 and 26.9 arms are clean and need nothing from me.

@PedroTadim
PedroTadim merged commit 7deead6 into backport/26.3/116724 Sep 20, 2026
177 checks passed
@PedroTadim
PedroTadim deleted the cherrypick/26.3/116724 branch September 20, 2026 18:57
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!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants