Skip to content

[Cosmos Encryption] Encrypt each array element independently for mixed-type arrays - #51247

Merged
Abhijeet Mohanty (jeet1995) merged 25 commits into
Azure:mainfrom
jeet1995:fix/encryption-patch-mixed-arrays
Nov 24, 2026
Merged

Abhijeet Mohanty (jeet1995) merged 25 commits into
Azure:mainfrom
jeet1995:fix/encryption-patch-mixed-arrays

Conversation

@jeet1995

@jeet1995 Abhijeet Mohanty (jeet1995) commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

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

Stored value
Before [{"label":"<ciphertext>"},42,null]
After (fix enabled) [{"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:

  • JVM: -DCOSMOS.ENCRYPTION_MIXED_ARRAYS_ENABLED=false
  • Environment: COSMOS_ENCRYPTION_MIXED_ARRAYS_ENABLED=false

The 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 42 above, wrapped for readability:

java.lang.IllegalStateException: Unable to decrypt array element at index 1
in encrypted property '/sensitive'. For confirmed legacy mixed arrays, migrate
the item or temporarily set -DCOSMOS.ENCRYPTION_MIXED_ARRAYS_ENABLED=false or
COSMOS_ENCRYPTION_MIXED_ARRAYS_ENABLED=false before creating clients.
Opt-out restores legacy reads/writes and may leave scalars unencrypted.
Check the cause for key or ciphertext errors.

Caused by: java.lang.IllegalArgumentException: Expected a non-empty ciphertext value.

Validation

  • 39 processor tests passed after the final scope correction; Checkstyle passed.
  • 6 live cases passed: opt-in/out across GatewayV1 HTTP/1.1, GatewayV2 HTTP/2, and Direct, covering create/PATCH, raw stored ciphertext, and decrypted reads. Test databases were deleted.
  • Service telemetry confirms the opt-out bit in all three modes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@jeet1995
Abhijeet Mohanty (jeet1995) marked this pull request as ready for review November 17, 2026 15:37
Copilot AI balanced review requested due to automatic review settings November 17, 2026 15:37
@jeet1995 Abhijeet Mohanty (jeet1995) changed the title [Cosmos Encryption] Encrypt each array element independently for mixed-type arrays [WIP][Cosmos Encryption] Encrypt each array element independently for mixed-type arrays Nov 17, 2026

Copilot AI left a comment

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.

🟡 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>
@jeet1995 Abhijeet Mohanty (jeet1995) changed the title [WIP][Cosmos Encryption] Encrypt each array element independently for mixed-type arrays [Cosmos Encryption] Encrypt each array element independently for mixed-type arrays Nov 18, 2026
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>
@jeet1995 Abhijeet Mohanty (jeet1995) changed the title [Cosmos Encryption] Encrypt each array element independently for mixed-type arrays [Do Not Review][Cosmos Encryption] Encrypt each array element independently for mixed-type arrays Nov 19, 2026
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>
@jeet1995

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@jeet1995 Abhijeet Mohanty (jeet1995) changed the title [Do Not Review][Cosmos Encryption] Encrypt each array element independently for mixed-type arrays [Cosmos Encryption] Encrypt each array element independently for mixed-type arrays Nov 23, 2026
@jeet1995 Abhijeet Mohanty (jeet1995) changed the title [Cosmos Encryption] Encrypt each array element independently for mixed-type arrays [Do Not Review][Cosmos Encryption] Encrypt each array element independently for mixed-type arrays Nov 23, 2026
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>
@jeet1995

Copy link
Copy Markdown
Member Author

Live end-to-end validation: service telemetry

All six cases passed create/PATCH, raw-storage encryption checks, and decrypted reads. Below are representative Create rows from the service aggregates for run encma-d1b7c63c (2026-11-24, 15:31:55–15:32:39 UTC). -in means the fix is enabled; -out means explicit opt-out. UserAgent values are copied from the service telemetry.

GatewayV1 / HTTP/1.1 — ComputeRequest5M

OperationType UserAgent StatusCode
Create azsdk-java-cosmos/4.84.0-beta.1 Windows11/10.0 JRE/21.0.13.1 encma-d1b7c63c-GatewayV1-in azure-cosmos-encryption/2.33.0-beta.1 201
Create azsdk-java-cosmos/4.84.0-beta.1 Windows11/10.0 JRE/21.0.13.1 encma-d1b7c63c-GatewayV1-out azure-cosmos-encryption/2.33.0-beta.1|F80 201

GatewayV2 / HTTP/2 — ThinClientProxyRequest5M

OperationType UserAgent StatusCode
Create azsdk-java-cosmos/4.84.0-beta.1 Windows11/10.0 JRE/21.0.13.1 encma-d1b7c63c-GatewayV2-in azure-cosmos-encryption/2.33.0-beta.1|F54 201
Create azsdk-java-cosmos/4.84.0-beta.1 Windows11/10.0 JRE/21.0.13.1 encma-d1b7c63c-GatewayV2-out azure-cosmos-encryption/2.33.0-beta.1|FD4 201

Direct — BackendEndRequest5M (numeric backend OperationType retained)

OperationType UserAgent StatusCode
0 azsdk-java-cosmos/4.84.0-beta.1 Windows11/10.0 JRE/21.0.13.1 encma-d1b7c63c-Direct-in azure-cosmos-encryption/2.33.0-beta.1 201
0 azsdk-java-cosmos/4.84.0-beta.1 Windows11/10.0 JRE/21.0.13.1 encma-d1b7c63c-Direct-out azure-cosmos-encryption/2.33.0-beta.1|F80 201

The explicit opt-out adds exactly 0x80 to the hexadecimal feature mask in all three modes. All isolated test databases were deleted.

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>
@jeet1995

Copy link
Copy Markdown
Member Author

/azp run java - cosmos - tests

@jeet1995 Abhijeet Mohanty (jeet1995) changed the title [Do Not Review][Cosmos Encryption] Encrypt each array element independently for mixed-type arrays [Cosmos Encryption] Encrypt each array element independently for mixed-type arrays Nov 24, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM - Thanks!

@jeet1995
Abhijeet Mohanty (jeet1995) merged commit f7ec207 into Azure:main Nov 24, 2026
130 of 132 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants