Repository navigation
Fix remote_url_allow_hosts checking the wrong port for MySQL dictionary replicas that inherit the top-level PORT - #109736
Conversation
…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]>
|
Workflow [PR], commit [e511a48] Summary: ✅
AI ReviewSummaryThe 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 Findings❌ Blockers
Missing context / blind spots
Final VerdictStatus: ❌ 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. |
Co-Authored-By: Claude Fable 5 <[email protected]>
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.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
| 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( |
There was a problem hiding this comment.
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.
Build profile diff (arm_release)Commit See the job log for details. |
…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]>
|
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 |
| 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)))); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@groeneai would you please fix it as a followup?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Related: #99720
Related: #109304
Problem
The
remote_url_allow_hostscheck for DDL-created MySQL dictionary sources (added in #99720) validates the wrong endpoint when aREPLICA(...)clause has noPORTof its own. ForSOURCE(MYSQL(PORT 3307 ... REPLICA(HOST 'h'))):h:3306(the hardcoded default),h:3307(the top-levelPORT).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,
registerDictionarySourceMysqlinsrc/Dictionaries/MySQLDictionarySource.cpp(before this fix):The connection side,
mysqlxx::Poolconstructor insrc/Common/mysqlxx/Pool.cpp(invoked with the parent prefix byPoolWithFailover.cpp, right after the check):The two rules agree when the replica declares its own
PORTand diverge exactly when it inherits the parent one: the check validates3306whilemysql_real_connectdials the top-levelPORT. The host has no such gap —Poolreads the replica'shostwith no parent fallback, matching the check.Fix
Make the check resolve the port the same way
mysqlxx::Pooldoes — the replica's ownport, falling back to the top-levelport:The pre-existing 3306 default is kept for the case when neither is set (the pool constructor then throws
expected port or socketbefore dialing anyway). One expression changed.Regression test
test_allowed_url_from_config/test.py::test_mysql_dictionary_replica_inherited_port_host_filteron a new node whose allow-list permits exactly127.0.0.1:3306: a replica inheriting the denied top-levelPORT 3307must be rejected withnot allowed(before the fix, the filter passed and the pool dialed127.0.0.1:3307, verified locally against the unfixed binary), and a replica with its own allowedPORT 3306must still pass the filter (its own port takes precedence). The existing #99720 tests keep passing.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the
remote_url_allow_hostscheck for MySQL dictionary sources validating the wrong port when aREPLICA(...)clause has noPORTof its own: the check used the default port 3306 while the connection used the top-levelPORT, so an allow-list entry pinned to a specific port could be bypassed.Documentation entry for user-facing changes
Workflow [PR]
Sync PR [sync-upstream/pr/109736]
Version info
26.11.1.868-master(included in26.11and later)