← Back to Pull requests

Check attribute reads and reset the salt in alter_user_scram_credentials() #2442

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 14, 2026 09:03 UTC
Author

What
----
Problems in the C alter_user_scram_credentials():

1. An upsertion without a salt got the salt of the previous upsertion in the same call. c_salt and c_salt_size were only set when the alteration had a salt, and librdkafka copies the salt it is given, so it only generates a random salt when it gets none. With an LD_PRELOAD shim logging the arguments of rd_kafka_UserScramCredentialUpsertion_new(), AdminClient.alter_user_scram_credentials([UserScramCredentialUpsertion("alice", info, b"password-a", b"0123456789abcdef"), UserScramCredentialUpsertion("bob", info, b"password-b")]) passes the salt 0123456789abcdef for bob too on master and #2439, on 3.11 and 3.14t. With this branch, bob's upsertion gets no salt, so librdkafka generates a random one, as UserScramCredentialUpsertion documents.
2. The function ignored the result of every attribute read. AdminClient.alter_user_scram_credentials() validates most of the alterations first, but the C method, when called directly or given alterations changed after that validation, crashed on a missing attribute:
- Most reads were optional, so a missing attribute came back as NULL with the AttributeError still set, and went to PyObject_IsInstance() or Py_TYPE(). This happened for scram_credential_info, password, salt, the mechanism of a ScramCredentialInfo, and the mechanism of a deletion.
- A missing user crashed in Py_DECREF() on the first alteration. On a later alteration, it reused the previous alteration's user name and released it a second time.
- A missing or non-int iterations left the C value uninitialized, and the call went on with the exception set.
- An alteration that was neither an upsertion nor a deletion left its librdkafka alteration uninitialized: free(): invalid size.
- An error from PyObject_IsInstance() counted as a match.
3. AdminClient's check of the iteration count is iterations < 1, so a non-int one such as 4096.0, an IntEnum member or True reaches the C method through the public API. It left the count uninitialized and passed it to librdkafka. The call then raised SystemError on 3.14t, and returned normally on 3.11, where the next attribute read cleared the pending TypeError (PyErr_Clear() in PyObject_GenericGetAttr()). Depending on what the count held, librdkafka rejected the request (Non-positive iterations at index 0) or went on to send it: in a run under gdb, it waited for the controller. cfl_PyObject_GetInt() also truncated a value that fits in a C long but not in an int (232 + 4096 became 4096, 231 became negative), and for one that does not fit in a long, such as 2100, returned success with an OverflowError set.

The attributes are now read as required ones, and each read and each PyObject_IsInstance() result is checked. An alteration that is neither an upsertion nor a deletion raises TypeError, and the user is released if a check on it fails. A non-int iteration count raises TypeError, and cfl_PyObject_GetInt() raises OverflowError for a value outside the range of a C int. The error messages use Py_TYPE(), since PyObject_Type() leaked a reference to the type, as in #2440.

Checklist
------------------

  • Contains customer facing changes? Including API/behavior changes

  • - The salt fix, and TypeError/OverflowError for iteration counts that AdminClient lets through. CHANGELOG entries under Unreleased.
  • Did you add sufficient unit test and/or integration test coverage for this PR?

  • - New tests in tests/test_Admin.py call AdminClient.alter_user_scram_credentials() with the iteration counts its checks let through: 4096.0, an IntEnum member and True raise TypeError, 2
    31 and 2100 raise OverflowError, and 231 - 1 is accepted.
    - New tests/test_admin_scram_alterations.py calls the C method directly with 14 alterations: 3 valid ones, one of them with an iteration count of -231, the lowest that fits, and 11 that AdminClient rejects itself, with an attribute missing, of the wrong type or out of range (-231 - 1), or of the wrong class. Each runs in a subprocess, since most of them crashed, and closes its client before the interpreter exits.
    - The salt fix has no unit test: only the broker sees the salt, and the mock cluster of librdkafka 2.16.0 doesn't implement AlterUserScramCredentials. I checked it with the shim described above.

    References
    ----------

  • Stacked on #2439, rebased on its updated commit, so no longer on #2436 either. #2439 clears the user name of each alteration at the end of the loop, and the error paths added here rely on that. Until #2439 is merged, this PR shows its commit too.

  • Follows up on the open question of #2439.

  • #2443 fixes the same kind of problem in list_consumer_group_offsets() and alter_consumer_group_offsets().
  • Test & Review
    ------------

  • CPython 3.11.16 and 3.14.7t, librdkafka 2.16.0, Linux x86_64.

  • tests/test_admin_scram_alterations.py: on #2439, 11 of the 14 cases fail on both: 7 by segfault, 1 by abort (free(): invalid size), the 2 iterations attribute cases are accepted on 3.11 and raise SystemError on 3.14t, and -231 - 1 is accepted. With this branch, all 14 pass on both.

  • The new tests/test_Admin.py tests: on master, the 5 rejection cases fail on both. On 3.11, all 5 calls return normally. On 3.14t, 4096.0, the IntEnum member, True and 2100 raise SystemError, and 231 is accepted. Before the cfl_PyObject_GetInt() change, 231 and 2100 still failed (on 3.14t, 2100 raised TypeError: Required attribute .value missing). With this branch, all 6 tests pass on both.

  • tests/test_*.py and tests/concurrency: 697 passed and 5 skipped on 3.11; 603 passed and 7 skipped on 3.14t. On 3.14t I deselected test_kafkaError_unknown_error, whose KafkaError escapes pytest with the pluggy 1.7.0 of my environment and ends the session (#2444 fixes that test).

  • tools/style-format.sh and flake8 pass on the changed test files. clang-format 18.1.3 and 20.1.8 report no new differences in Admin.c and confluent_kafka.c.

  • Merges without conflicts with master and the other open PRs: #2425, #2427, #2429, #2432 to #2436, #2438, #2440, #2441, #2443 and #2444.
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: pylaterreur Review duration: 10212m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2442"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%