Repository navigation
[Cosmos Encryption] Encrypt each array element independently for mixed-type arrays - #51247
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 2 pipeline(s). 32 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Fabian Meiswinkel (FabianMeiswinkel)
left a comment
There was a problem hiding this comment.
LGTM
There was a problem hiding this comment.
🟡 Changes recommended
The new decryption branch throws a NullPointerException when reading mixed arrays written by the previous implementation, requiring legacy-data compatibility handling or a migration path and an old-writer/new-reader regression test.
1 open issue
What changed in this PR
This PR updates Cosmos client-side encryption to process mixed-type arrays element by element rather than choosing behavior from the first element.
Changes:
- Recurses into objects and nested arrays, processes non-null scalars, and preserves null entries during encryption and decryption.
- Adds mixed-array regression coverage for patch and full-document round trips.
- Documents the fix in the package changelog.
| File | Description |
|---|---|
sdk/cosmos/azure-cosmos-encryption/src/test/java/com/azure/cosmos/encryption/implementation/EncryptionProcessorAndSettingsTest.java |
Adds ciphertext checks and mixed-array round-trip tests. |
sdk/cosmos/azure-cosmos-encryption/src/main/java/com/azure/cosmos/encryption/implementation/EncryptionProcessor.java |
Replaces first-element-based array traversal with per-element handling. |
sdk/cosmos/azure-cosmos-encryption/CHANGELOG.md |
Records the mixed-array encryption and decryption fix. |
Review effort: Balanced
Fail array decryption with actionable migration guidance while preserving the original cause. Document the upgrade break and cover legacy scalar representations and authentication failures. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use an absolute URL to satisfy the Verify Links check. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Match azure-cosmos changelog formatting for the mixed-array fix and upgrade guidance. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Support JVM and environment opt-out with a shared client configuration snapshot. Preserve legacy traversal for coordinated migration and identify explicit opt-out in the user agent. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Resolve the user agent from the shared container when gateway and thin-client requests are serialized so feature flags evaluated after store-model construction reach the wire. Add coverage for database-account and thin-client HTTP headers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit be7befe)
Avoid overridable default-header construction in the gateway base constructor. Keep transport defaults static and resolve only the mutable user-agent value when each request is serialized. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 9b2ee77)
Preserve the existing default-header construction and merge paths. Replace only the serialized User-Agent value immediately before gateway and thin-client HTTP requests are created. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 234ba13)
Document that Gateway and thin-client request headers now include feature flags evaluated after transport initialization. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 43e221c)
Keep the entry focused on the externally visible header behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 04364a7)
Publish suffix updates atomically, preserve case-insensitive request overrides, and evaluate configuration-known feature flags before the bootstrap account request. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit aedf49c)
Publish complete user-agent snapshots through a volatile reference while retaining a writer lock for compound updates. Avoid scanning request headers by using the merged case-insensitive header value to detect SDK defaults. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 44fe92a)
Apply the current SDK user agent before request overrides are merged. This removes value-equality inference while preserving explicit overrides through the existing case-insensitive HTTP header handling. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit c7fa98e)
Verify Gateway and thin-client wire headers contain both the CosmosClientBuilder application suffix and feature flags added later by UserAgentContainer. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit b8b6c8f)
Allow the bootstrap DatabaseAccount request to precede feature evaluation and keep coverage focused on normal post-initialization Gateway requests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 5d8cf1c)
Keep the original feature-suffix expression while publishing the complete volatile user-agent value in one assignment. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit c655610)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Verify late user-agent suffix updates reach Gateway and GatewayV2 HTTP headers alongside feature flags. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace the textual mixed-array opt-out marker with flag 0x80. Preserve explicit configuration flags across feature refreshes and suffix updates, with Gateway and encryption regression coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use CosmosAsyncClient internal accessors instead of CosmosBridgeInternal, update forwarding tests, and remove user-agent details from the encryption README. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/azp run java - cosmos - tests |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Route array encryption and decryption through explicit mode-selection helpers, keeping traversal config-free and sharing the existing decryption error guidance. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the property/index, migration and opt-out settings, under-encryption warning, and original cause while removing redundant wording. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Live end-to-end validation: service telemetryAll six cases passed create/PATCH, raw-storage encryption checks, and decrypted reads. Below are representative Create rows from the service aggregates for run GatewayV1 / HTTP/1.1 —
GatewayV2 / HTTP/2 —
Direct —
The explicit opt-out adds exactly |
Restore main-style legacy iteration and rebuilding, make corrected recursion explicit, and add empty-array and live opt-in/out coverage across GatewayV1, GatewayV2 and Direct. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Retain core user-agent and live E2E coverage without the encryption-side reflection tests or extra test JVM flag. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restore the scalar helper fallback from main so the mixed-array fix does not include an unrelated behavior change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/azp run java - cosmos - tests |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Fabian Meiswinkel (FabianMeiswinkel)
left a comment
There was a problem hiding this comment.
LGTM - Thanks!
f7ec207
into
Azure:main

Summary
Fix mixed-type array encryption/decryption for full items and PATCH operations. Keep legacy traversal behind an explicit compatibility opt-out; standalone scalar PATCH behavior remains unchanged.
Before / after
For encrypted path
/sensitive, given[{"label":"x"},42,null](ciphertext shown schematically):[{"label":"<ciphertext>"},42,null][{"label":"<ciphertext>"},"<ciphertext>",null]Previously, the first element determined handling for the whole array. The corrected path handles each element independently, recurses into objects/arrays, and preserves order, nulls, and decrypted value types.
Upgrade impact / opt-out
Legacy partially encrypted arrays require migration. Read them with the old SDK or an opted-out reader, then rewrite the complete decrypted item with the fixed writer; see migration guidance.
To temporarily restore legacy reads and writes, set either before creating encryption clients:
-DCOSMOS.ENCRYPTION_MIXED_ARRAYS_ENABLED=falseCOSMOS_ENCRYPTION_MIXED_ARRAYS_ENABLED=falseThe fix is enabled by default; the JVM property takes precedence. Opt-out retains the under-encryption bug and cannot correctly read fixed-format mixed arrays, so coordinate migration and client cutover. Decryption failures preserve their original causes; authentication failures are never treated as plaintext.
Includes the core Gateway/GatewayV2 user-agent propagation fix from #50580 and a compact opt-out feature bit. Release requires a matching core SDK.
Example error
For the legacy
42above, wrapped for readability:Validation