Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5fe1bc5
Fix mixed-array client-side encryption
jeet1995 Nov 12, 2026
e3f6230
Report legacy mixed-array decryption failures
jeet1995 Nov 18, 2026
a561f5c
Fix encryption changelog migration link
jeet1995 Nov 18, 2026
4b8cf09
Align encryption changelog PR references
jeet1995 Nov 18, 2026
eb0f6b2
Add mixed-array encryption compatibility opt-out
jeet1995 Nov 19, 2026
f45ac08
Fix Cosmos gateway user agent propagation
jeet1995 Sep 24, 2026
4f22182
Simplify Cosmos gateway default headers
jeet1995 Sep 24, 2026
3481211
Refresh Cosmos user agent at serialization boundary
jeet1995 Sep 24, 2026
258767b
Update Cosmos changelog for user agent propagation
jeet1995 Sep 24, 2026
e7cac69
Refine Cosmos user agent changelog entry
jeet1995 Sep 24, 2026
2f1446c
Harden Cosmos user agent propagation
jeet1995 Sep 29, 2026
a68e427
Remove user agent hot-path locking
jeet1995 Sep 29, 2026
29456f6
Simplify Gateway user agent refresh
jeet1995 Sep 30, 2026
602b152
Test combined Cosmos user agent suffixes
jeet1995 Sep 30, 2026
fd02f5e
Limit Cosmos user agent refresh scope
jeet1995 Sep 30, 2026
f3564d7
Minimize Cosmos feature suffix publication
jeet1995 Sep 30, 2026
826b85e
Merge Azure main into encryption mixed-array fix
jeet1995 Nov 19, 2026
a3a6d01
Test encryption opt-out marker in Gateway user agents
jeet1995 Nov 19, 2026
9379934
Encode encryption opt-out as a user-agent feature bit
jeet1995 Nov 19, 2026
6e2e75a
Route encryption user-agent updates through wrapped client
jeet1995 Nov 19, 2026
f35f2a5
Separate legacy and corrected encryption array traversal
jeet1995 Nov 24, 2026
6664f5f
Shorten mixed-array decryption error guidance
jeet1995 Nov 24, 2026
3341498
Keep encryption array traversal close to main
jeet1995 Nov 24, 2026
62d18d8
Remove encryption user-agent unit tests and module opening
jeet1995 Nov 24, 2026
6be7d27
Keep standalone scalar PATCH behavior unchanged
jeet1995 Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/cosmos/azure-cosmos-encryption/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#### Features Added

#### Breaking Changes
* Reading legacy mixed-type arrays with unencrypted scalars now fails by default with `IllegalStateException`; see [migration and opt-out guidance](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/cosmos/azure-cosmos-encryption/README.md#legacy-mixed-type-arrays). - See [PR 51247](https://github.com/Azure/azure-sdk-for-java/pull/51247)

#### Bugs Fixed
* Fixed client-side encryption and decryption of mixed-type arrays for item and PATCH operations. - See [PR 51247](https://github.com/Azure/azure-sdk-for-java/pull/51247)

#### Other Changes

Expand Down
46 changes: 46 additions & 0 deletions sdk/cosmos/azure-cosmos-encryption/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,52 @@ However, because Azure Cosmos DB is accessed via network calls there are client-

- [Troubleshooting Guide][troubleshooting] covers common issues, workarounds, diagnostic steps, and tools when you use Azure Cosmos DB Java SDK v4 with Azure Cosmos DB SQL API accounts.

### Legacy mixed-type arrays
Comment thread
jeet1995 marked this conversation as resolved.
Resolved

Earlier SDK versions could leave scalar array elements unencrypted inside an encrypted property when the first
array element was an object or another array. For example, with `/sensitive` included in the encryption policy,
writing `[{"label":"secret"},42]` encrypted `"secret"` but left `42` in plaintext. This can occur in nested arrays
as well. The old reader skipped the same scalar elements, so reads could succeed despite the missing encryption.

The corrected reader decrypts every non-null scalar array element. If an element cannot be decrypted, it throws
`IllegalStateException` with the encrypted property name, the array-local index, migration guidance, and the original
exception as its cause. It does not silently return the value as plaintext. This failure can also indicate incorrect
keys or damaged ciphertext; it is not proof that the item was written by an older SDK.

#### Temporary compatibility opt-out

The fix is enabled by default. To temporarily restore legacy array traversal for **both reads and writes**
(including PATCH), set either of the following before creating encryption clients:

- JVM system property: `-DCOSMOS.ENCRYPTION_MIXED_ARRAYS_ENABLED=false`
- Environment variable: `COSMOS_ENCRYPTION_MIXED_ARRAYS_ENABLED=false`

A non-empty JVM property takes precedence over the environment variable. Values are case-insensitive `true` or
`false`; invalid values are rejected rather than silently disabling encryption. Each encryption client
captures the setting when it is created and shares it with its container processors; restart the application
to change modes consistently.
The setting is implemented in the encryption package and does not require a new `azure-cosmos` config getter.

**Warning:** legacy mode restores the first-element-based behavior, including the bug that leaves scalar elements
unencrypted after an object or array. It logs a warning once per JVM and is a migration bridge, not a long-term fix.
It also retains legacy failures for unsupported scalar-first mixed arrays. Legacy readers skip scalar elements
after a leading container, so they cannot correctly read mixed arrays already written with the fix enabled.
Do not use the two modes interchangeably on the same data or switch to legacy mode after writing corrected data.
Opt-out is an explicit traversal choice, not a fallback for decoding or authentication failures.

If you confirm that an item contains legacy mixed-type arrays:

1. Coordinate a migration so old clients do not read rewritten items or write more legacy items during the cutover.
2. Read the affected items with the SDK version that wrote them, or with the compatibility opt-out in a separate
migration reader process, to recover their decrypted values.
3. Pass the decrypted items to a separate encryption-enabled writer process with the fix enabled and rewrite the
complete items. Use ETags to prevent overwriting concurrent changes; do not first read the legacy items through
the corrected reader. Do not copy raw encrypted values into the writer.
4. Resume normal access with the fix enabled after the affected items have been migrated.

These items are partially encrypted, not wholly unencrypted: existing encrypted fields still require decryption.
Do not treat authentication failures as plaintext or assume that raw stored items can be rewritten without decryption.

### Enable Client Logging
Azure Cosmos DB Java SDK v4 uses SLF4j as the logging facade that supports logging into popular logging frameworks such as log4j and logback.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
import com.azure.cosmos.CosmosAsyncClientEncryptionKey;
import com.azure.cosmos.CosmosAsyncContainer;
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.CosmosBridgeInternal;
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.CosmosItemSerializer;
import com.azure.cosmos.encryption.implementation.Constants;
import com.azure.cosmos.encryption.implementation.EncryptionConfigs;
import com.azure.cosmos.encryption.implementation.EncryptionImplementationBridgeHelpers;
import com.azure.cosmos.encryption.implementation.keyprovider.EncryptionKeyStoreProviderImpl;
import com.azure.cosmos.implementation.HttpConstants;
import com.azure.cosmos.implementation.ImplementationBridgeHelpers;
import com.azure.cosmos.implementation.RequestOptions;
import com.azure.cosmos.implementation.Utils;
import com.azure.cosmos.implementation.UserAgentFeatureFlags;
import com.azure.cosmos.implementation.apachecommons.lang.StringUtils;
import com.azure.cosmos.implementation.caches.AsyncCache;
import com.azure.cosmos.models.CosmosClientEncryptionKeyProperties;
Expand All @@ -46,6 +47,7 @@ public final class CosmosEncryptionAsyncClient implements Closeable {
private final KeyEncryptionKeyResolver keyEncryptionKeyResolver;
private final String keyEncryptionKeyResolverName;
private final EncryptionKeyStoreProviderImpl encryptionKeyStoreProviderImpl;
private final boolean mixedArrayEncryptionEnabled;
private final static ImplementationBridgeHelpers.CosmosAsyncClientEncryptionKeyHelper.CosmosAsyncClientEncryptionKeyAccessor cosmosAsyncClientEncryptionKeyAccessor = ImplementationBridgeHelpers.CosmosAsyncClientEncryptionKeyHelper.getCosmosAsyncClientEncryptionKeyAccessor();

private final static ImplementationBridgeHelpers.CosmosAsyncClientHelper.CosmosAsyncClientAccessor cosmosAsyncClientAccessor = ImplementationBridgeHelpers.CosmosAsyncClientHelper.getCosmosAsyncClientAccessor();
Expand All @@ -62,6 +64,7 @@ public final class CosmosEncryptionAsyncClient implements Closeable {
if (StringUtils.isEmpty(keyEncryptionKeyResolverName)) {
throw new IllegalArgumentException("keyEncryptionKeyResolverName is null");
}
this.mixedArrayEncryptionEnabled = EncryptionConfigs.isMixedArrayEncryptionEnabled();
this.cosmosAsyncClient = cosmosAsyncClient;
this.keyEncryptionKeyResolver = keyEncryptionKeyResolver;
this.clientEncryptionKeyPropertiesCacheByKeyId = new AsyncCache<>();
Expand Down Expand Up @@ -93,6 +96,10 @@ EncryptionKeyStoreProviderImpl getEncryptionKeyStoreProviderImpl() {
return encryptionKeyStoreProviderImpl;
}

boolean isMixedArrayEncryptionEnabled() {
return this.mixedArrayEncryptionEnabled;
}

Mono<CosmosContainerProperties> getContainerPropertiesAsync(
CosmosAsyncContainer container,
boolean shouldForceRefresh) {
Expand Down Expand Up @@ -195,9 +202,11 @@ public CosmosAsyncClient getCosmosAsyncClient() {

private void appendEncryptionUserAgentSuffix() {
try {
CosmosBridgeInternal
.getAsyncDocumentClient(this.cosmosAsyncClient)
.appendUserAgentSuffix(Constants.USER_AGENT_SUFFIX);
cosmosAsyncClientAccessor.appendUserAgentSuffix(this.cosmosAsyncClient, Constants.USER_AGENT_SUFFIX);
if (!this.mixedArrayEncryptionEnabled) {
cosmosAsyncClientAccessor.addUserAgentFeatureFlag(this.cosmosAsyncClient,
UserAgentFeatureFlags.EncryptionMixedArrayOptOut);
}
} catch (Exception e) {
LOGGER.warn("Failed to append encryption SDK user agent suffix", e);
}
Expand Down Expand Up @@ -273,6 +282,11 @@ public Mono<CosmosContainerProperties> getContainerPropertiesAsync(CosmosEncrypt
public EncryptionKeyStoreProviderImpl getEncryptionKeyStoreProviderImpl(CosmosEncryptionAsyncClient cosmosEncryptionAsyncClient) {
return cosmosEncryptionAsyncClient.getEncryptionKeyStoreProviderImpl();
}

@Override
public boolean isMixedArrayEncryptionEnabled(CosmosEncryptionAsyncClient cosmosEncryptionAsyncClient) {
return cosmosEncryptionAsyncClient.isMixedArrayEncryptionEnabled();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos.encryption.implementation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* Internal configuration for the encryption SDK.
*/
public final class EncryptionConfigs {
static final String MIXED_ARRAYS_ENABLED = "COSMOS.ENCRYPTION_MIXED_ARRAYS_ENABLED";
static final String MIXED_ARRAYS_ENABLED_VARIABLE = "COSMOS_ENCRYPTION_MIXED_ARRAYS_ENABLED";
private static final Logger LOGGER = LoggerFactory.getLogger(EncryptionConfigs.class);
private static final AtomicBoolean LEGACY_MODE_WARNING_LOGGED = new AtomicBoolean(false);

private EncryptionConfigs() {
}

/**
* Resolves the mixed-array encryption setting, giving the JVM property precedence over the environment variable.
*
* @return whether corrected mixed-array traversal is enabled
*/
public static boolean isMixedArrayEncryptionEnabled() {
return isMixedArrayEncryptionEnabled(System.getProperty(MIXED_ARRAYS_ENABLED),
System.getenv(MIXED_ARRAYS_ENABLED_VARIABLE));
}

static boolean isMixedArrayEncryptionEnabled(String systemPropertyValue, String environmentVariableValue) {
String value = systemPropertyValue == null || systemPropertyValue.isEmpty()
? environmentVariableValue : systemPropertyValue;
if (value == null || value.isEmpty() || "true".equalsIgnoreCase(value)) {
return true;
}
if ("false".equalsIgnoreCase(value)) {
if (LEGACY_MODE_WARNING_LOGGED.compareAndSet(false, true)) {
LOGGER.warn("Mixed-array encryption is disabled through {} or {}. Legacy traversal can leave "
+ "scalar array elements unencrypted and cannot correctly read mixed arrays written with "
+ "the fix enabled. Use this temporary compatibility mode only until legacy data is migrated.",
MIXED_ARRAYS_ENABLED, MIXED_ARRAYS_ENABLED_VARIABLE);
}
return false;
}
throw new IllegalArgumentException("Invalid " + MIXED_ARRAYS_ENABLED + " or "
+ MIXED_ARRAYS_ENABLED_VARIABLE + " setting. Only 'true' or 'false' are supported.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ Mono<CosmosContainerProperties> getContainerPropertiesAsync(
boolean shouldForceRefresh);

EncryptionKeyStoreProviderImpl getEncryptionKeyStoreProviderImpl(CosmosEncryptionAsyncClient cosmosEncryptionAsyncClient);

boolean isMixedArrayEncryptionEnabled(CosmosEncryptionAsyncClient cosmosEncryptionAsyncClient);
}
}

Expand Down
Loading
Loading