Skip to content

Cherry pick #117407 to 26.3: Fix TOTP bypass in MySQL protocol authentication - #127116

Merged
robot-clickhouse-ci-2 merged 10 commits into
backport/26.3/117407from
cherrypick/26.3/117407
Oct 21, 2026
Merged

robot-clickhouse-ci-2 merged 10 commits into
backport/26.3/117407from
cherrypick/26.3/117407

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #117407

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

thevar1able and others added 8 commits August 31, 2026 23:15
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]>
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]>
…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
# 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]>
Fix TOTP bypass in MySQL protocol authentication
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 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 pr-critical-bugfix labels Oct 21, 2026
alexey-milovidov and others added 2 commits October 21, 2026 11:06
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.
On master it is included already; on 26.3 it is not.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 merged commit 7fd4874 into backport/26.3/117407 Oct 21, 2026
1 check passed
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 deleted the cherrypick/26.3/117407 branch October 21, 2026 11:43
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! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants