Repository navigation
Fix TOTP bypass in MySQL protocol authentication - #117407
Conversation
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]>
|
Workflow [PR], commit [4f4fe3f] Summary: ✅
AI ReviewSummaryThe PR closes the MySQL TOTP bypass by choosing Missing context / blind spots
Final VerdictStatus: ✅ Approve. No additional code changes requested; the pending integration checks should pass before merging. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units14 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 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]>
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
alexey-milovidov
left a comment
There was a problem hiding this comment.
Is it relevant to any other of existing protocols?
# 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]>
|
🕵 Merged The remaining CI failure, |
| 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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
|
Fix for postgresql protocol #117408. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The code is good enough.
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.
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]>
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]>
Cherry pick #117407 to 26.3: Fix TOTP bypass in MySQL protocol authentication
Cherry pick #117407 to 26.7: Fix TOTP bypass in MySQL protocol authentication
Cherry pick #117407 to 26.8: Fix TOTP bypass in MySQL protocol authentication
Cherry pick #117407 to 26.9: Fix TOTP bypass in MySQL protocol authentication
Cherry pick #117407 to 26.10: Fix TOTP bypass in MySQL protocol authentication
Backport #117407 to 26.10: Fix TOTP bypass in MySQL protocol authentication
Backport #117407 to 26.9: Fix TOTP bypass in MySQL protocol authentication
Backport #117407 to 26.8: Fix TOTP bypass in MySQL protocol authentication
Backport #117407 to 26.7: Fix TOTP bypass in MySQL protocol authentication
Backport #117407 to 26.3: Fix TOTP bypass in MySQL protocol authentication
Changelog category (leave one):
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_passwordordouble_sha1_passwordplus a time-based one-time password could authenticate with the password alone, because themysql_native_passwordauth response was verified without checking the TOTP. Such users are now switched to thesha256_passwordplugin and must append the one-time password to the password, likepassword+123456.The
mysql_native_passwordauth response is a hash of the password alone, so it cannot carry a one-time password.checkMySQLAuthenticationverified only the password scramble and ignored the configured TOTP secret, so the second factor was silently skipped over the MySQL protocol.Now
checkMySQLAuthenticationfails closed when a one-time password is configured, andMySQLHandlerswitches such users to thesha256_passwordplugin, which transmits the actual password string; the one-time password is appended to it and enforced via theBasicCredentialspath, the same as for the native and HTTP protocols.Added an integration test covering both
plaintext_passwordanddouble_sha1_passwordusers 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
26.11.1.390-master(included in26.11and later)26.10.2.58,26.9.24.16,26.8.34.7,26.7.37.10,26.3.59.8