Skip to content

Return REQUIRED_SECOND_FACTOR regardless of the password when the TOTP code is missing - #125823

Merged
vdimir merged 3 commits into
masterfrom
vdimir/totp-second-factor-no-password-leak
Oct 21, 2026
Merged

vdimir merged 3 commits into
masterfrom
vdimir/totp-second-factor-no-password-leak

Conversation

@vdimir

@vdimir vdimir commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Motivation

For an account with a time-based one-time password, the server replied with REQUIRED_SECOND_FACTOR (767) only when the password was right and the code was missing; a wrong password got AUTHENTICATION_FAILED (516). Over HTTP the two were also told apart by the status: 500 for the former, 403 for the latter. An attacker could therefore brute-force the password without knowing a code, because 767 confirmed a guess. Found by the internal security review of the TOTP implementation.

Changes

  • The missing code is reported before the password is looked at (checkBasicAuthentication): a right, a wrong and an empty password without a code now give the same error, and the password is verified only together with the code. When a code is present, both factors are evaluated unconditionally, so the timing does not tell which one failed. A code suffix of a wrong length counts as no code, as before.
  • REQUIRED_SECOND_FACTOR maps to HTTP 403 like the other authentication errors, instead of falling through to 500.
  • clickhouse-client gets REQUIRED_SECOND_FACTOR already for the first attempt without any password, so on that error it asks for the password alone first (it may contain the code as password+code) and asks for the code only if the server asks again. Users with a password see the same prompts as before. Users with no_password plus TOTP now get a password prompt (press Enter) before the code prompt, because the client cannot tell the two kinds of accounts apart anymore.
  • The integration test asserts byte-identical errors over TCP and identical 403/767 responses over HTTP for a right, a wrong and an empty password without a code, and that a wrong code still gives AUTHENTICATION_FAILED.

The error still reveals that the account exists and has a second factor configured; it no longer says anything about the password.

Related: #125664
Related: #125785

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):

For users with a time-based one-time password, the error REQUIRED_SECOND_FACTOR is now returned whenever the code is missing, regardless of whether the password is correct. Previously a wrong password without a code returned AUTHENTICATION_FAILED, which allowed guessing the password without the second factor. The password is verified only together with the code. The error maps to HTTP status 403 instead of 500. clickhouse-client prompts for the password before the code when neither is given, so users with no_password and TOTP now press Enter at the password prompt.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.430-master (included in 26.11 and later)
  • Backported to: 26.10.2.63, 26.9.24.14, 26.8.34.5, 26.3.61.14

…P code is missing

For an account with a time-based one-time password, the server returned
`REQUIRED_SECOND_FACTOR` only when the password was right and the code was
missing, and `AUTHENTICATION_FAILED` otherwise. The two responses let an
attacker brute-force the password without ever knowing a code.

Now the missing code is reported before the password is looked at, so a
right and a wrong password without a code get the same error, and the
password is verified only together with the code. When a code is present
both factors are evaluated unconditionally, so the timing does not tell
which one failed. `REQUIRED_SECOND_FACTOR` maps to HTTP 403 like the other
authentication errors instead of 500.

`clickhouse-client` now gets `REQUIRED_SECOND_FACTOR` already for the first
attempt without any password, so on that error it asks for the password
alone first (it may contain the code as `password+code`) and asks for the
code only if the server asks again. Users with a password see the same
prompts as before; users with `no_password` plus TOTP get a password prompt
(press Enter) before the code prompt.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mintlify

mintlify Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
ClickHouse-docs 🟢 Ready View Preview Oct 20, 2026, 12:56 PM

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [710913f]

Summary: ✅


AI Review

Summary

The PR makes missing TOTP codes independent of password correctness for basic authentication, maps REQUIRED_SECOND_FACTOR to HTTP 403, and adjusts client prompts. The security fix remains incomplete when the MySQL listener is enabled: its native password exchange can still authenticate TOTP accounts using only the primary password.

Findings

❌ Blockers

  • [P1] MySQL native password proofs bypass the missing-code protection — src/Access/Authentication.cpp:275–277. The new guard only covers BasicCredentials. For a configured plaintext_password or double_sha1_password account with TOTP, MySQLProtocol::Authentication::Native41::authenticate creates MySQLNative41Credentials. The dispatch at Authentication.cpp:401–404 calls checkMySQLAuthentication, which validates only the password; consumeOneTimePassword also accepts non-basic credentials without checking a code. Thus the existing totuser fixture can authenticate over MySQL with aa+bb alone. This path predates the PR but violates the security guarantee being established: password guesses remain distinguishable, and a correct guess opens a session without the second factor. Reject native MySQL proofs for authentication methods with TOTP, mirroring the SCRAM guard, or use an exchange that verifies both factors.
Tests
  • ⚠️ Add a focused negative MySQL case for a password-based TOTP account: the correct primary password without a code must not authenticate. Reuse the existing TOTP test cluster with its MySQL listener enabled.
Missing context / blind spots
  • ⚠️ The finding is established by tracing the current credential dispatch and session-authentication path; no local ClickHouse binary or build directory was available for a runtime reproduction. TOTP integration results for head 710913f3767a3f0b48b05f919786d96b21571a79 were not yet available. A completed regression run would close this runtime-validation gap.
Final Verdict

❌ Block — close the password-only MySQL path for TOTP accounts and add the focused regression case.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 14, 2026
@vdimir vdimir added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 14, 2026
Comment thread src/Access/Authentication.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-authentication Identity verification: users, credentials, auth methods, external auth providers. label Oct 14, 2026
@thevar1able thevar1able self-assigned this Oct 16, 2026
Comment thread programs/client/Client.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 710913f3767a3f0b48b05f919786d96b21571a79 was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "file", "type": "LowCardinality(String)" …).

See the job log for details.

Comment on lines +275 to +277
/// for a right and a wrong password: otherwise an attacker could confirm a guessed password without the code.
if (otp_secret && one_time_password.empty())
return Authentication::CredentialsCheckResult::NeedSecondFactor;

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.

[P1] Cover MySQL native password proofs in the missing-code protection

This guard only runs for BasicCredentials. With mysql_port enabled, a user configured with <password> or <password_double_sha1_hex> plus time_based_one_time_password can still authenticate using only the primary password: MySQLProtocol::Authentication::Native41::authenticate constructs MySQLNative41Credentials, and Authentication::areCredentialsValid dispatches those directly to checkMySQLAuthentication, which never checks getOneTimePassword. consumeOneTimePassword then returns true for these non-basic credentials. For example, the existing totuser fixture can authenticate over MySQL with just aa+bb.

This is a pre-existing alternate path left open by the fix: password guesses still produce success versus failure without a code, and a correct guess bypasses TOTP entirely. Please reject native MySQL proofs when the authentication method has a second factor, as checkScramSHA256Authentication already does, or route these accounts through an exchange that verifies both factors. Add a negative MySQL case proving that the correct primary password alone cannot authenticate.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@groeneai is it fixed by #117407 ?

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.

Yes. #117407 (merged as 927e545) closes this path in three places: checkMySQLAuthentication returns false when the method has a one-time password, IAccessStorage::authenticateImpl rejects MySQLNative41Credentials when any of the user's methods has one, and MySQLHandler switches such users to the sha256_password plugin, so the password with the code appended arrives as BasicCredentials. Its test_mysql_protocol_requires_totp is the negative case asked for here (correct password alone gets 767); it passes on master at 927e545.

I also checked a master build at 927e545 with the totuser fixture over the MySQL port: aa+bb alone gets 767, aa+bb+<code> logs in.

This PR's head (710913f) does not include #117407 yet, but the two merge without conflicts. On master a wrong password alone over MySQL still gets 516 instead of 767; after the merge that login goes through checkBasicAuthentication, so the guard in this PR covers it too (from the code, I did not run the merged tree).

@vdimir
vdimir added this pull request to the merge queue Oct 21, 2026
Merged via the queue into master with commit ba38c73 Oct 21, 2026
180 checks passed
@vdimir
vdimir deleted the vdimir/totp-second-factor-no-password-leak branch October 21, 2026 15:18
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 21, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 21, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #125823 to 26.7: Return `REQUIRED_SECOND_FACTOR` regardless of the password when the TOTP code is missing
robot-clickhouse added a commit that referenced this pull request Oct 21, 2026
…of the password when the TOTP code is missing
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #125823 to 26.10: Return `REQUIRED_SECOND_FACTOR` regardless of the password when the TOTP code is missing
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #125823 to 26.9: Return `REQUIRED_SECOND_FACTOR` regardless of the password when the TOTP code is missing
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #125823 to 26.8: Return `REQUIRED_SECOND_FACTOR` regardless of the password when the TOTP code is missing
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
…egardless of the password when the TOTP code is missing
robot-clickhouse added a commit that referenced this pull request Oct 26, 2026
…of the password when the TOTP code is missing
@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 26, 2026
PedroTadim added a commit that referenced this pull request Oct 26, 2026
Backport #125823 to 26.3: Return `REQUIRED_SECOND_FACTOR` regardless of the password when the TOTP code is missing
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…of the password when the TOTP code is missing
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-bugfix Pull request with bugfix, not backported by default 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