Skip to content

Fail close on TOTP in SCRAM SHA-256 authentication - #117408

Merged
vdimir merged 6 commits into
masterfrom
fix-scram-totp-fail-close
Oct 16, 2026
Merged

vdimir merged 6 commits into
masterfrom
fix-scram-totp-fail-close

Conversation

@thevar1able

@thevar1able thevar1able commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

The SCRAM client proof is derived from the password alone, so it cannot carry or verify a one-time password. checkScramSHA256Authentication ignored the configured TOTP secret and verified only the proof. The PostgreSQL frontend already refuses to run the SCRAM exchange for a scram_sha256_password method with a second factor (see getScramSalt), so this was not reachable, but the check itself must not depend on every producer of ScramSHA256Credentials pre-filtering such users. It now fails closed when a one-time password is configured.

Accordingly, the ambiguous-credentials pass in getScramSalt no longer exempts a same-salt scram_sha256_password method that carries a second factor (only an expired one can reach that point): the proof can no longer be checked against it, so it would silently drop out of the fail-close GRANTS/VALID UNTIL combination instead of shortening the session lifetime. Such a configuration is refused as unsupported instead.

Added an integration test for TOTP over the PostgreSQL protocol: a scram_sha256_password user with TOTP is refused as an unsupported configuration, and cleartext password authentication enforces the second factor (password+TOTP).

Related: #117407

Changelog category (leave one):

  • Improvement

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

SCRAM SHA-256 authentication now fails closed for users with a time-based one-time password, because a SCRAM proof cannot carry a second factor.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.86-master (included in 26.11 and later)

The SCRAM client proof is derived from the password alone, so it cannot
carry or verify a one-time password. `checkScramSHA256Authentication`
ignored the configured TOTP secret and verified only the proof. The
PostgreSQL frontend already refuses to run the SCRAM exchange for a
`scram_sha256_password` method with a second factor (see
`getScramSalt`), so the bypass was not reachable, but the check itself
must not depend on every producer of `ScramSHA256Credentials`
pre-filtering such users.

Now `checkScramSHA256Authentication` fails closed when a one-time
password is configured. Accordingly, the ambiguous-credentials
combination in `getScramSalt` no longer exempts a same-salt
`scram_sha256_password` method that carries a second factor: the proof
can no longer be checked against it, so it would silently drop out of
the fail-close combination instead of shortening the session lifetime;
such a configuration is refused as unsupported instead.

Also added an integration test for TOTP over the PostgreSQL protocol:
a SCRAM user with TOTP is refused, and cleartext password
authentication 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 [0ee378b]

Summary: ✅


AI Review

Summary

Adds a guard rejecting SCRAM proofs for authentication methods configured with TOTP. Request changes because the added PostgreSQL test never reaches the new guard, leaving the central security invariant without regression coverage.

Findings

⚠️ Majors

  • tests/integration/test_totp_auth/test_totp.py:226: The SCRAM attempts fail in the existing getScramSalt pre-check before ScramSHA256Credentials are constructed; the cleartext attempts use BasicCredentials. Removing the new guard in checkScramSHA256Authentication therefore leaves every added assertion passing. The shared authenticator's promised TOTP rejection is not covered.
Tests
  • ⚠️ Add a focused test invoking Authentication::areCredentialsValid with a valid ScramSHA256Credentials proof. Assert Success without TOTP and Fail with TOTP for the same stored SCRAM password, and confirm that removing the new guard makes the test fail.
Final Verdict

⚠️ Request changes — add regression coverage that reaches the shared SCRAM authenticator and detects removal of the TOTP guard.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 31, 2026
Comment thread src/Core/PostgreSQLProtocol.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 0ee378b72d5409f0809954773bde37aa87e8fb6e was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception; stopped retrying after the 1250 s read budget).

See the job log for details.

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]>
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]>
@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 4 commits September 18, 2026 20:30
…pre-check

With `BasicCredentials`, a method with a second factor yields
`NeedSecondFactor` for the password alone, so `IAccessStorage::authenticateImpl`
never folds it into the session limits. Treating an expired same-salt
`scram_sha256_password` method with TOTP as one that must participate in the
combination made the PostgreSQL frontend return `NOT_IMPLEMENTED` where the
native protocol accepts the login through the live method. Revert that part
of `getScramSalt` so both protocols behave the same; the fail-close check in
`checkScramSHA256Authentication` stays.

Addresses the review comment on #117408

# A SCRAM user with TOTP must be refused: the exchange cannot verify the second factor.
scram_user = "totuser_postgresql_scram"
with pytest.raises(psycopg2.OperationalError, match="not supported"):

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.

[P2] Exercise the new SCRAM guard in a regression test

Both SCRAM attempts are rejected by the existing ScrambleSHA256Auth::getScramSalt pre-check: it detects TOTP, and authenticate throws NOT_IMPLEMENTED before constructing any ScramSHA256Credentials. The cleartext attempts exercise BasicCredentials. Consequently, this test still passes if the new getOneTimePassword guard in checkScramSHA256Authentication is removed, leaving the shared authenticator's second-factor enforcement unprotected against regressions.

Please add a focused test passing a valid ScramSHA256Credentials proof directly to Authentication::areCredentialsValid: the same stored SCRAM password should produce Success without TOTP and Fail with TOTP. That test should fail when the new guard is removed.

@thevar1able
thevar1able marked this pull request as ready for review October 14, 2026 17:34
@vdimir vdimir self-assigned this Oct 16, 2026
@vdimir
vdimir added this pull request to the merge queue Oct 16, 2026
Merged via the queue into master with commit 73b284b Oct 16, 2026
176 checks passed
@vdimir
vdimir deleted the fix-scram-totp-fail-close branch October 16, 2026 14:32
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 2026
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-improvement Pull request with some product improvements 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.

3 participants