Skip to content

Fix TOTP bypass in MySQL protocol authentication - #117407

Merged
alexey-milovidov merged 7 commits into
masterfrom
fix-mysql-totp-bypass
Oct 21, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
fix-mysql-totp-bypass

Conversation

@thevar1able

@thevar1able thevar1able commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fix a second-factor bypass in the MySQL protocol: a user configured with plaintext_password or double_sha1_password plus a time-based one-time password could authenticate with the password alone, because the mysql_native_password auth response was verified without checking the TOTP. Such users are now switched to the sha256_password plugin and must append the one-time password to the password, like password+123456.

The mysql_native_password auth response is a hash of the password alone, so it cannot carry a one-time password. checkMySQLAuthentication verified only the password scramble and ignored the configured TOTP secret, so the second factor was silently skipped over the MySQL protocol.

Now checkMySQLAuthentication fails closed when a one-time password is configured, and MySQLHandler switches such users to the sha256_password plugin, which transmits the actual password string; the one-time password is appended to it and enforced via the BasicCredentials path, the same as for the native and HTTP protocols.

Added an integration test covering both plaintext_password and double_sha1_password users with TOTP over the MySQL protocol (password alone → REQUIRED_SECOND_FACTOR, wrong TOTP → authentication failure, password+TOTP → success).


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

Version info

  • Merged into: 26.11.1.390-master (included in 26.11 and later)
  • Backported to: 26.10.2.58, 26.9.24.16, 26.8.34.7, 26.7.37.10, 26.3.59.8

The `mysql_native_password` auth response is a hash of the password
alone, so it cannot carry a one-time password. `checkMySQLAuthentication`
verified only the password scramble and ignored the configured TOTP
secret, so a user with `plaintext_password` or `double_sha1_password`
plus TOTP could log in over the MySQL protocol with the password alone,
skipping the second factor.

Now `checkMySQLAuthentication` fails closed when a one-time password is
configured, and `MySQLHandler` switches such users to the
`sha256_password` plugin, which transmits the actual password string:
the client appends the one-time password to the password, like
'password+123456', and the `BasicCredentials` path enforces the second
factor.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4f4fe3f]

Summary: ✅


AI Review

Summary

The PR closes the MySQL TOTP bypass by choosing sha256_password when mysql_native_password cannot enforce the user's authentication methods, and rejects incompatible credentials against the same user snapshot used for authentication. No actionable findings remain at 4f4fe3fef9f51f38df608eac823d2b7e8f18b9be; both earlier review findings are addressed.

Missing context / blind spots
  • ⚠️ The head's integration jobs were pending in the CI report. The available Fast test binary lacks SSL and bcrypt support, preventing an independent run of the original MySQL tests. A local pause/alter/resume regression passed with an expired sha256_hash method substituted for bcrypt_password. Completing test_totp_auth on a full build would close this validation gap; the author separately reports that the original race test passes and fails without the snapshot check.
Final Verdict

Status: ✅ Approve. No additional code changes requested; the pending integration checks should pass before merging.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Aug 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4f4fe3fef with master 2a4ae1408 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 02fd3427a; 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

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

Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +7.0 s (+9%) in total before that adjustment.

Job report

The flaky check runs the test file on several pytest-xdist workers in
parallel, and `create_config` writes the shared `config/users.xml`.
With a random module-level `TOTP_SECRET` the last writer wins, and the
other workers' servers reject codes generated for their own secrets
with `WRONG_PASSWORD`, failing every test that uses the random secret:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117407&sha=a9ca343fa63aa8299c12e5cb9cd68c96372ff03e&name_0=PR
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117408&sha=c88676cc0bc8587b70dfbb9fba92482587b74245&name_0=PR
#117407
#117408

Also fix operator precedence in `get_one_time_password`: the intent is
`int((timepoint or time.time()) / interval)`, not dividing only
`time.time()` by the interval.

Co-Authored-By: Claude Fable 5 <[email protected]>
thevar1able added a commit that referenced this pull request Sep 1, 2026
The flaky check runs the test file on several pytest-xdist workers in
parallel, and `create_config` writes the shared `config/users.xml`.
With a random module-level `TOTP_SECRET` the last writer wins, and the
other workers' servers reject codes generated for their own secrets
with `WRONG_PASSWORD`, failing every test that uses the random secret:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117407&sha=a9ca343fa63aa8299c12e5cb9cd68c96372ff03e&name_0=PR
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117408&sha=c88676cc0bc8587b70dfbb9fba92482587b74245&name_0=PR
#117407
#117408

Also fix operator precedence in `get_one_time_password`: the intent is
`int((timepoint or time.time()) / interval)`, not dividing only
`time.time()` by the interval.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Server/MySQLHandler.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-authentication Identity verification: users, credentials, auth methods, external auth providers. label Sep 4, 2026
thevar1able and others added 3 commits September 18, 2026 20:29
…ully verify

The `mysql_native_password` auth response can be re-checked only against
`plaintext_password` and `double_sha1_password` methods. Besides skipping a
second factor, serving a user with `mysql_native_password` when they also have
a `scram_sha256_password` or `bcrypt_password` method lets that method drop
out of the fail-close combination in `IAccessStorage::authenticateImpl`, so a
shared password regains the broader `GRANTS` or lifetime of the
`double_sha1_password` method over MySQL. Such users, like `sha256_password`
users before, are now switched to the `sha256_password` plugin, which
transmits the actual password string. As a side effect, users with only a
`scram_sha256_password` or `bcrypt_password` method can log in over MySQL.

The decision is made in `MySQLHandler` from the user's authentication methods
instead of a `Session::userHasSecondFactor` helper.

Addresses the review comment on #117407
Comment thread src/Server/MySQLHandler.cpp Resolved
@thevar1able
thevar1able marked this pull request as ready for review October 16, 2026 13:56
@vdimir vdimir self-assigned this Oct 16, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 17, 2026

@alexey-milovidov alexey-milovidov left a comment

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.

Is it relevant to any other of existing protocols?

alexey-milovidov and others added 2 commits October 19, 2026 00:44
# Conflicts:
#	tests/integration/test_totp_auth/config/users.xml
#	tests/integration/test_totp_auth/test_totp.py
…authenticated user snapshot

`MySQLHandler` chose the plugin from a snapshot of the user read before the
client answers the `AuthSwitchRequest`, while `IAccessStorage::authenticateImpl`
reads a fresh one. A user altered in between (e.g. to add a `bcrypt_password`
method with an expired `VALID UNTIL`) could be authenticated with a Native41
response that silently skipped the new method.

Move the predicate to `Authentication::mysqlNative41CannotVerify` and re-check
it in `IAccessStorage::authenticateImpl` on the same snapshot used for the
authentication, rejecting `MySQLNative41Credentials` when any method cannot be
enforced. Add a deterministic pause/alter/resume regression test with a raw
MySQL protocol client.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master and resolved the conflicts in tests/integration/test_totp_auth. Both the MySQL tests from this PR and the PostgreSQL TOTP tests from master are kept, and the node now loads both mysql.xml and postgresql.xml. On the question of other protocols: PostgreSQL had the same class of issue (a SCRAM proof cannot carry a one-time password). It has been fixed separately on master (fail close on TOTP in SCRAM SHA-256 authentication), and its test now runs alongside the MySQL ones here. The native, HTTP and gRPC protocols send the password string itself, so the TOTP suffix is verified there.

The remaining CI failure, 05059_autopr_settings_in_select_settings_clause (flaky in Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel)), is unrelated to this change. A fix is in progress in #125544.

Comment on lines +797 to +805
session->getAuthenticationTypesOrLogInFailure(user_name);

/// For compatibility with the JavaScript MySQL client, the `mysql_native_password` plugin (Native41) is used
/// when it can fully authenticate the user. Otherwise the `sha256_password` plugin is used: it transmits the
/// actual password string, which is then checked via the `BasicCredentials` path, the same as for the native
/// and HTTP protocols. See `Authentication::mysqlNative41CannotVerify`.
/// The user can be altered while the client is answering, so this choice is only a hint: `IAccessStorage::authenticateImpl`
/// re-checks it against the snapshot of the user it authenticates with and rejects Native41 credentials if needed.
const auto user = session->globalContext()->getAccessControl().read<User>(user_name);

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.

User is read twice from getAccessControl().read<User>: once inside getAuthenticationTypesOrLogInFailure and again below. Maybe we can read it once and handle the failure with onAuthenticationFailure.

case AuthenticationType::BCRYPT_PASSWORD:
return true;
default:
return false;

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.

It seems this change affects more than just the TOTP case. It was added in commit 584e434, and the changelog entry does not mention it.

As far as I got it covers this case:

Example: double_sha1_password BY 'pw', bcrypt_password BY 'pw' VALID UNTIL '2000-01-01'.

- Native protocol: server has pw, double-SHA1 matches, it also tests bcrypt, bcrypt matches, earliest expiry is 2000, login denied.
- MySQL Native41 before the PR: double-SHA1 matches, the bcrypt test cannot be performed, so the server does not know bcrypt would also accept it, expiry never applies, login allowed.

@alexey-milovidov

Copy link
Copy Markdown
Member

Is it relevant to any other of existing protocols?

@thevar1able

thevar1able commented Oct 20, 2026 •

Copy link
Copy Markdown
Member Author

Is it relevant to any other of existing protocols?

#117407 (comment)

On the question of other protocols: PostgreSQL had the same class of issue (a SCRAM proof cannot carry a one-time password). It has been fixed separately on master (fail close on TOTP in SCRAM SHA-256 authentication), and its test now runs alongside the MySQL ones here. The native, HTTP and gRPC protocols send the password string itself, so the TOTP suffix is verified there.

Fix for postgresql protocol #117408.

@alexey-milovidov alexey-milovidov left a comment

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.

The code is good enough.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2026
Merged via the queue into master with commit 927e545 Oct 21, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-mysql-totp-bypass branch October 21, 2026 06:48
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 21, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 21, 2026
alexey-milovidov added a commit that referenced this pull request Oct 21, 2026
Resolve conflicts in `test_totp_auth`: keep the MySQL protocol tests and users from #117407,
drop the PostgreSQL parts that come from a change not present in 26.3.
alexey-milovidov added a commit that referenced this pull request Oct 21, 2026
Resolve conflicts in `test_totp_auth`: the PostgreSQL TOTP tests are not in 26.9, keep only the MySQL part of #117407.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Oct 21, 2026
Resolve conflicts in the `test_totp_auth` integration test: keep the MySQL
protocol tests and users added by #117407, drop the PostgreSQL protocol tests,
users and `config/postgresql.xml` that come from master and are not in 26.8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #117407 to 26.3: Fix TOTP bypass in MySQL protocol authentication
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #117407 to 26.7: Fix TOTP bypass in MySQL protocol authentication
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #117407 to 26.8: Fix TOTP bypass in MySQL protocol authentication
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #117407 to 26.9: Fix TOTP bypass in MySQL protocol authentication
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #117407 to 26.10: Fix TOTP bypass in MySQL protocol authentication
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 21, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #117407 to 26.10: Fix TOTP bypass in MySQL protocol authentication
PedroTadim added a commit that referenced this pull request Oct 21, 2026
Backport #117407 to 26.9: Fix TOTP bypass in MySQL protocol authentication
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #117407 to 26.8: Fix TOTP bypass in MySQL protocol authentication
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #117407 to 26.7: Fix TOTP bypass in MySQL protocol authentication
alexey-milovidov added a commit that referenced this pull request Oct 21, 2026
Backport #117407 to 26.3: Fix TOTP bypass in MySQL protocol authentication
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-authentication Identity verification: users, credentials, auth methods, external auth providers. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

6 participants