Skip to content

Fix remote_url_allow_hosts checking the wrong port for MySQL dictionary replicas that inherit the top-level PORT - #109736

Merged
tiandiwonder merged 5 commits into
masterfrom
fix-mysql-dict-replica-port
Oct 28, 2026
Merged

tiandiwonder merged 5 commits into
masterfrom
fix-mysql-dict-replica-port

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Related: #99720
Related: #109304

Problem

The remote_url_allow_hosts check for DDL-created MySQL dictionary sources (added in #99720) validates the wrong endpoint when a REPLICA(...) clause has no PORT of its own. For SOURCE(MYSQL(PORT 3307 ... REPLICA(HOST 'h'))):

  • the filter checks h:3306 (the hardcoded default),
  • the connection pool dials h:3307 (the top-level PORT).

So an allow-list entry pinned to a specific port (<host>h:3306</host>) can be bypassed: the dictionary passes validation but connects to a different, disallowed port. Found during the review of the 25.8 backport #109304.

Root cause

The check and the connection resolve the replica port from the same configuration with two different rules.

The check side, registerDictionarySourceMysql in src/Dictionaries/MySQLDictionarySource.cpp (before this fix):

global_context->getRemoteHostFilter().checkHostAndPort(
    config.getString(replica_prefix + ".host"),
    toString(config.getInt(replica_prefix + ".port", 3306)));
    //       ^ replica's own PORT, else the hardcoded default 3306

The connection side, mysqlxx::Pool constructor in src/Common/mysqlxx/Pool.cpp (invoked with the parent prefix by PoolWithFailover.cpp, right after the check):

port = cfg.has(config_name + ".port")
    ? cfg.getInt(config_name + ".port")              // replica's own PORT...
    : cfg.getInt(parent_config_name + ".port", 0);   // ...else the top-level PORT

The two rules agree when the replica declares its own PORT and diverge exactly when it inherits the parent one: the check validates 3306 while mysql_real_connect dials the top-level PORT. The host has no such gap — Pool reads the replica's host with no parent fallback, matching the check.

Fix

Make the check resolve the port the same way mysqlxx::Pool does — the replica's own port, falling back to the top-level port:

toString(config.getInt(replica_prefix + ".port",
    config.getInt(settings_config_prefix + ".port", 3306)))

The pre-existing 3306 default is kept for the case when neither is set (the pool constructor then throws expected port or socket before dialing anyway). One expression changed.

Regression test test_allowed_url_from_config/test.py::test_mysql_dictionary_replica_inherited_port_host_filter on a new node whose allow-list permits exactly 127.0.0.1:3306: a replica inheriting the denied top-level PORT 3307 must be rejected with not allowed (before the fix, the filter passed and the pool dialed 127.0.0.1:3307, verified locally against the unfixed binary), and a replica with its own allowed PORT 3306 must still pass the filter (its own port takes precedence). The existing #99720 tests keep passing.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix the remote_url_allow_hosts check for MySQL dictionary sources validating the wrong port when a REPLICA(...) clause has no PORT of its own: the check used the default port 3306 while the connection used the top-level PORT, so an allow-list entry pinned to a specific port could be bypassed.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

Version info

  • Merged into: 26.11.1.868-master (included in 26.11 and later)

…riting the top-level `PORT`

The DDL host-filter check added in #99720 validated a `REPLICA(...)` entry
without its own `PORT` against the default port 3306, while `mysqlxx::Pool`
resolves the effective replica port with fallback to the top-level `PORT`.
So `SOURCE(MYSQL(PORT 3307 ... REPLICA(HOST 'h')))` was checked as `h:3306`
but dialed `h:3307`, bypassing a port-pinning `remote_url_allow_hosts`
entry. Validate the effective port instead.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e511a48]

Summary: ✅


AI Review

Summary

The PR fixes inherited TCP port validation for MySQL dictionary replicas and adds regression cases for parent-port inheritance and replica-port precedence. The port change matches mysqlxx::Pool, but the previously reported socket bypass remains in the current code and has been deferred by the author. No new inline findings were identified.

Findings

❌ Blockers

  • src/Dictionaries/MySQLDictionarySource.cpp:223–235: The validated endpoint can still differ from the connection endpoint. [dismissed by author -- https://github.com/Fix remote_url_allow_hosts checking the wrong port for MySQL dictionary replicas that inherit the top-level PORT #109736#discussion_r4354892587] The author requested a follow-up; the bypass remains real because SOCKET is still accepted without validation. With localhost:3306 allowed, SOURCE(MYSQL(PORT 3306 SOCKET '/var/run/other.sock' USER 'root' PASSWORD 'pass' DB 'test' TABLE 'tbl' REPLICA(HOST 'localhost'))) passes the host filter, while mysqlxx::Pool inherits the socket at src/Common/mysqlxx/Pool.cpp:78–80 and mysqlxx::Connection::connect passes it to mysql_real_connect. The non-replica path has the same gap. Reject user-supplied socket paths for DDL-created dictionaries, or validate the actual socket endpoint before pool construction; cover both source-level inheritance and replica-level overrides. This existing finding was retained without duplicating or replying to its inline threads.
Missing context / blind spots
  • ⚠️ The fetched current-head and earlier targeted integration reports exposed no per-test results, and no local integration suite was run. A result for test_mysql_dictionary_replica_inherited_port_host_filter would independently confirm execution of the regression cases.
Final Verdict

Status: ❌ Block — the existing endpoint-filter security bypass remains unresolved. Minimum action: close the socket bypass and add focused rejection coverage, whether here or in the requested follow-up before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 8, 2026
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Jul 8, 2026
With the replica's own `PORT` equal to the hardcoded 3306 default, the case could
not distinguish the replica-port lookup from the old default-port fallback. Use
3308 (differs from both the denied parent 3307 and the 3306 default) and
allow-list it, so a regression validating either wrong port fails the case.
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the comp-mysql MySQL-specific integration (table engine/function/protocol mapping). label Sep 4, 2026
checkNoSSLPaths(config, replica_prefix);
/// Resolve the port the same way mysqlxx::Pool does, so the checked port
/// always matches the port the connection actually dials.
global_context->getRemoteHostFilter().checkHostAndPort(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysqlxx::Pool does not just inherit the replica or parent PORT here: it also inherits socket (src/Common/mysqlxx/Pool.cpp:71-80), and mysqlxx::Connection prefers that socket over TCP whenever host is localhost (src/Common/mysqlxx/mysqlxx/Connection.h:69-70). So this still does not validate the endpoint the connector will actually use: SOURCE(MYSQL(HOST 'localhost' SOCKET '/var/run/other.sock' ... REPLICA(HOST 'localhost'))) is checked as localhost:3306 or localhost:<parent PORT>, but the pool connects to the socket instead.

That means a remote_url_allow_hosts entry pinned to localhost:3306 can still authorize an arbitrary local MySQL socket. Please either reject socket for DDL-created MySQL dictionaries, or add socket-aware validation here (and in the non-replica branch below) plus a regression test.

@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit e511a486e73995cf2570d3a9778c07689b55abbd was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "commit_sha", "type": "String" } ], "…).

See the job log for details.

pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Sep 22, 2026
…guration file

ClickHouse#112070 established that the source configuration of a dictionary created with a
DDL query comes from the query itself, so it may not name files for the server to
open: the server reads them with its own privileges. It implemented that as
checkNoSSLPaths, keyed on the three ssl_ca / ssl_cert / ssl_key key names rather
than on the invariant itself.

enable_local_infile is in the same dictionary_allowed_keys and satisfies the same
premise by a second, indirect route: it sets MYSQL_OPT_LOCAL_INFILE on the
outgoing connection, which lets the MySQL endpoint the dictionary points at ask
the client for the contents of a file of its choosing. The option is off by
default for exactly this reason, and has been since 33205bb ("Disable LOAD
DATA LOCAL INFILE because it is insecure", still visible at
mysqlxx/Connection.h:16); 26a951e re-exposed it as an argument and
bfc705c put it in the dictionary source's allowed keys, which is how a DDL
query reaches it. It was never considered in ClickHouse#112070, so this completes that
guard for the sibling key rather than duplicating it.

The guard is applied at the two configuration prefixes the connection pool
actually reads for a DDL-created dictionary: each <replica> prefix and the
single-endpoint prefix, both inside the existing created_from_ddl block of the
non-named-collection branch.

It resolves the value in Pool::Pool's own lookup order, a replica value winning
over the parent as its fallback (Pool.cpp:94-95), so what is rejected is the
value the connection would really use. That matters in both directions: a
<replica> inheriting a truthy parent is rejected, while a truthy parent whose
every replica sets 0 is not, because PoolWithFailover builds only per-replica
pools when replicas exist and so the parent alone decides nothing. An
independent parent check would reject that safe configuration; open ClickHouse#109736 is a
bug fix for the same class of mistake two lines away.

The guard is deliberately not placed beside checkNoSSLPaths, which sits before
the branch because for the SSL keys that breadth is necessary. It is not needed
there for this key: the named-collection route reaches
createMySQLPoolWithFailover, which passes the literal
MYSQLXX_DEFAULT_ENABLE_LOCAL_INFILE (PoolWithFailover.cpp:164), and MySQLSettings
carries no local_infile, so a collection-defined or NAME-overriding key is
accepted and then never read. Guarding it there would break a configuration that
cannot enable the option. That inertness is a precondition rather than a
property, so it is stated in the PR body: adding such a setting, as ClickHouse#103229 did
for enable_compression, would make that branch a live carrier.

Resolution is by value rather than by presence, unlike checkNoSSLPaths: for a
path key any value is dangerous, but enable_local_infile 0 is bit-for-bit the
default and harmless, and this change is already backward incompatible without
breaking configurations that never reach MYSQL_OPT_LOCAL_INFILE = 1.

socket is the one other key in that list naming a path a DDL query can choose. It
is deliberately not mirrored: it is a connect(2) to a path rather than a read of
a file's contents, and pointing a dictionary at a local mysqld.sock is a
legitimate, widely used configuration.

Tests: a stateless test covering the four shapes that enable the option (source,
an explicit <replica>, a <replica> that inherits it, a later <replica> when the
first disables it) and the two that do not (0 at the source, 0 at every <replica>
of a truthy source), which needs no MySQL server
because the rejection precedes any connection, and an integration arm in
test_dictionaries_mysql proving a configuration-file dictionary still loads and
serves data with the option on while the same option from a DDL query against
that endpoint is rejected.

Co-Authored-By: Claude Opus 5 <[email protected]>
@tavplubix

Copy link
Copy Markdown
Member

@tavplubix
tavplubix requested a review from serxa October 6, 2026 17:04
@serxa serxa self-assigned this Oct 23, 2026
@serxa

serxa commented Oct 23, 2026

Copy link
Copy Markdown
Member

hm, why do we check this setting for MySQL? https://clickhouse.com/docs/reference/settings/server-settings/settings/remote#remote_url_allow_hosts

in practice remote_url_allow_hosts covers every outbound connector. The docs page only mentions URL engines, we should fix the stale docs.

config.getString(replica_prefix + ".host"),
toString(config.getInt(replica_prefix + ".port", 3306)));
toString(config.getInt(replica_prefix + ".port",
config.getInt(settings_config_prefix + ".port", 3306))));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the port, but mysqlxx::Pool also inherits socket from the replica or the parent, and for localhost it connects to the socket and ignores the port. So localhost:3306 in the allow-list still lets a dictionary use any local socket.

Could we reject socket for DDL-created dictionaries, the same way we do for the SSL paths? A follow-up PR is fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@groeneai would you please fix it as a followup?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged. I'm investigating the socket allow-list bypass for DDL-created MySQL dictionaries (including the value inherited by replicas); I'll follow up here with a PR (or my findings) shortly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up PR: #127853. It rejects socket in a DDL-created MySQL dictionary (at the source, which also covers the value a replica inherits, and at each replica) with BAD_ARGUMENTS, the same way the SSL paths are rejected; dictionaries from a server configuration file keep socket. It is marked Backward Incompatible Change, since the embedded docs had a DDL socket example.

@tiandiwonder
tiandiwonder added this pull request to the merge queue Oct 28, 2026
Merged via the queue into master with commit 051b2cc Oct 28, 2026
179 checks passed
@tiandiwonder
tiandiwonder deleted the fix-mysql-dict-replica-port branch October 28, 2026 17:35
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 28, 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-mysql MySQL-specific integration (table engine/function/protocol mapping). pr-bugfix Pull request with bugfix, not backported by default 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.

5 participants