You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "lhotari (via GitHub)" <gi...@apache.org> on 2026/03/25 10:42:05 UTC

[PR] [fix][sec] Fix thread-safety issues with shared JCA class instances [pulsar]

lhotari opened a new pull request, #25400:
URL: https://github.com/apache/pulsar/pull/25400

   ### Motivation
   
   Java Cryptography Architecture (JCA) classes like `Cipher`, `MessageDigest`, `KeyGenerator`, `CertificateFactory`, etc. are **not thread-safe** per [Oracle's Java PKI Programmer's Guide](https://docs.oracle.com/en/java/javase/21/security/java-pki-programmers-guide.html). Sharing instances across threads without synchronization can cause corrupted output, `BadPaddingException`, or silently wrong cryptographic results.
   
   This was confirmed by Netty's [PR #16350](https://github.com/netty/netty/pull/16350) for `CertificateFactory`, and is well-documented for `MessageDigest` ([pmd/pmd#1862](https://github.com/pmd/pmd/issues/1862)), `Cipher` ([ekino/hibernate-crypto-types#14](https://github.com/ekino/hibernate-crypto-types/issues/14)), and `KeyGenerator`.
   
   A review of the Pulsar codebase identified the following thread-safety issues:
   
   **MessageCryptoBc (HIGH):**
   - `Cipher cipher` instance field (line 100) — `encrypt()` is `synchronized` but `decrypt()`/`decryptData()` are not, so the shared Cipher can be used concurrently from different threads
   - `MessageDigest digest` instance field (line 101) — used in `decryptDataKey()` and `getKeyAndDecryptData()` without any synchronization
   - `static KeyGenerator keyGenerator` (line 97) — shared across ALL instances, written from constructors on different threads with only per-instance synchronization
   
   **DefaultRoleAnonymizerType (MEDIUM-HIGH):**
   - `MessageDigest digest` fields in `SHA256` and `MD5` enum singletons — `anonymize()` called concurrently without synchronization; `MessageDigest.digest()` resets internal state, so concurrent use produces corrupted hashes
   
   **SecurityUtility (LOW):**
   - `static List<KeyFactory> KEY_FACTORIES` — cached instances shared across threads. While likely safe in practice for standard JDK implementations, the API contract doesn't guarantee thread-safety.
   
   ### Modifications
   
   **MessageCryptoBc:** Replaced shared `Cipher`, `MessageDigest`, and `KeyGenerator` fields with `FastThreadLocal`-cached instances. This is a hot path (per-message encrypt/decrypt), so thread-local caching avoids the cost of creating new instances per call while ensuring thread safety.
   
   **DefaultRoleAnonymizerType:** Replaced `MessageDigest` instance fields in enum singletons with `FastThreadLocal`-cached instances. This is a hot path when enabled (called for every authentication role logged).
   
   **SecurityUtility:** Removed cached static `KeyFactory` instances. New instances are created per call in `loadPrivateKeyFromPemStream()`. This is not a hot path (only used during TLS key loading at connection setup), so caching is unnecessary.
   
   ### Verifying this change
   
   - [ ] Make sure that the change passes the CI checks.
   
   This change is already covered by existing tests, such as:
   - `MessageCryptoBc` unit tests in `pulsar-client-messagecrypto-bc`
   - `DefaultRoleAnonymizerType` tests in `pulsar-broker-common`
   - `SecurityUtility` tests in `pulsar-common`
   - End-to-end encryption integration tests like `SimpleProducerConsumerTest#testCryptoWithChunking`
   
   ### Does this pull request potentially affect one of the following parts:
   
   - [ ] Dependencies (add or upgrade a dependency)
   - [ ] The public API
   - [ ] The schema
   - [ ] The default values of configurations
   - [x] The threading model
   - [ ] The binary protocol
   - [ ] The REST endpoints
   - [ ] The admin CLI options
   - [ ] The metrics
   - [ ] Anything that affects deployment
   
   ### Documentation
   
   - [x] `doc-not-needed`
   
   ### Matching PR in forked repository
   
   PR in forked repository: <!-- ENTER URL HERE -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][misc] Fix thread-safety issues with shared JCA class instances [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #25400:
URL: https://github.com/apache/pulsar/pull/25400#issuecomment-4125492546

   /pulsarbot rerun-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][misc] Fix thread-safety issues with shared JCA class instances and data-race in MessageCryptoBc [pulsar]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #25400:
URL: https://github.com/apache/pulsar/pull/25400#issuecomment-4126004784

   ## [Codecov](https://app.codecov.io/gh/apache/pulsar/pull/25400?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `75.67568%` with `9 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 72.79%. Comparing base ([`0ec3883`](https://app.codecov.io/gh/apache/pulsar/commit/0ec3883ed1425527ce28ac63e6969c99b6a97f17?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`25e5bf1`](https://app.codecov.io/gh/apache/pulsar/commit/25e5bf1c6cfc8f249effb80fbcd5b53a953775fd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 1 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/25400?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...guration/anonymizer/DefaultRoleAnonymizerType.java](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&filepath=pulsar-broker-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fcommon%2Fconfiguration%2Fanonymizer%2FDefaultRoleAnonymizerType.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWJyb2tlci1jb21tb24vc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3B1bHNhci9jb21tb24vY29uZmlndXJhdGlvbi9hbm9ueW1pemVyL0RlZmF1bHRSb2xlQW5vbnltaXplclR5cGUuamF2YQ==) | 33.33% | [4 Missing :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...che/pulsar/client/impl/crypto/MessageCryptoBc.java](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&filepath=pulsar-client-messagecrypto-bc%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fclient%2Fimpl%2Fcrypto%2FMessageCryptoBc.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNsaWVudC1tZXNzYWdlY3J5cHRvLWJjL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvY2xpZW50L2ltcGwvY3J5cHRvL01lc3NhZ2VDcnlwdG9CYy5qYXZh) | 84.00% | [3 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...org/apache/pulsar/common/util/SecurityUtility.java](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&filepath=pulsar-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fcommon%2Futil%2FSecurityUtility.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNvbW1vbi9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NvbW1vbi91dGlsL1NlY3VyaXR5VXRpbGl0eS5qYXZh) | 83.33% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   [![Impacted file tree graph](https://app.codecov.io/gh/apache/pulsar/pull/25400/graphs/tree.svg?width=650&height=150&src=pr&token=acYqCpsK9J&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@             Coverage Diff              @@
   ##             master   #25400      +/-   ##
   ============================================
   + Coverage     72.77%   72.79%   +0.02%     
   - Complexity    34215    34225      +10     
   ============================================
     Files          1952     1952              
     Lines        154546   154536      -10     
     Branches      17700    17700              
   ============================================
   + Hits         112464   112491      +27     
   + Misses        33050    33028      -22     
   + Partials       9032     9017      -15     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/pulsar/pull/25400/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [inttests](https://app.codecov.io/gh/apache/pulsar/pull/25400/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `25.82% <13.51%> (+0.20%)` | :arrow_up: |
   | [systests](https://app.codecov.io/gh/apache/pulsar/pull/25400/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `22.53% <5.40%> (+0.04%)` | :arrow_up: |
   | [unittests](https://app.codecov.io/gh/apache/pulsar/pull/25400/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `73.78% <75.67%> (+0.03%)` | :arrow_up: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/25400?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...org/apache/pulsar/common/util/SecurityUtility.java](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&filepath=pulsar-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fcommon%2Futil%2FSecurityUtility.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNvbW1vbi9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NvbW1vbi91dGlsL1NlY3VyaXR5VXRpbGl0eS5qYXZh) | `71.61% <83.33%> (+0.24%)` | :arrow_up: |
   | [...guration/anonymizer/DefaultRoleAnonymizerType.java](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&filepath=pulsar-broker-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fcommon%2Fconfiguration%2Fanonymizer%2FDefaultRoleAnonymizerType.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWJyb2tlci1jb21tb24vc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3B1bHNhci9jb21tb24vY29uZmlndXJhdGlvbi9hbm9ueW1pemVyL0RlZmF1bHRSb2xlQW5vbnltaXplclR5cGUuamF2YQ==) | `53.33% <33.33%> (-3.81%)` | :arrow_down: |
   | [...che/pulsar/client/impl/crypto/MessageCryptoBc.java](https://app.codecov.io/gh/apache/pulsar/pull/25400?src=pr&el=tree&filepath=pulsar-client-messagecrypto-bc%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fclient%2Fimpl%2Fcrypto%2FMessageCryptoBc.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNsaWVudC1tZXNzYWdlY3J5cHRvLWJjL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvY2xpZW50L2ltcGwvY3J5cHRvL01lc3NhZ2VDcnlwdG9CYy5qYXZh) | `70.54% <84.00%> (+1.28%)` | :arrow_up: |
   
   ... and [91 files with indirect coverage changes](https://app.codecov.io/gh/apache/pulsar/pull/25400/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][client] Fix thread-safety and refactor MessageCryptoBc key management [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari merged PR #25400:
URL: https://github.com/apache/pulsar/pull/25400


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org