Skip to content

Commit c32c3c6

Browse files
Fixed spring data cosmos sql injection risks (Azure#19559)
1 parent 701c5ac commit c32c3c6

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/CosmosTemplate.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.azure.cosmos.models.CosmosQueryRequestOptions;
1414
import com.azure.cosmos.models.FeedResponse;
1515
import com.azure.cosmos.models.PartitionKey;
16+
import com.azure.cosmos.models.SqlParameter;
1617
import com.azure.cosmos.models.SqlQuerySpec;
1718
import com.azure.cosmos.models.ThroughputProperties;
1819
import com.azure.spring.data.cosmos.Constants;
@@ -263,14 +264,15 @@ public <T> T findById(String containerName, Object id, Class<T> domainType) {
263264
Assert.hasText(containerName, "containerName should not be null, empty or only whitespaces");
264265
Assert.notNull(domainType, "domainType should not be null");
265266

266-
final String query = String.format("select * from root where root.id = '%s'",
267-
CosmosUtils.getStringIDValue(id));
267+
final String query = "select * from root where root.id = @ROOT_ID";
268+
final SqlParameter param = new SqlParameter("@ROOT_ID", CosmosUtils.getStringIDValue(id));
269+
final SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(query, param);
268270
final CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
269271
options.setQueryMetricsEnabled(this.queryMetricsEnabled);
270272
return cosmosAsyncClient
271273
.getDatabase(this.databaseName)
272274
.getContainer(containerName)
273-
.queryItems(query, options, JsonNode.class)
275+
.queryItems(sqlQuerySpec, options, JsonNode.class)
274276
.byPage()
275277
.publishOn(Schedulers.parallel())
276278
.flatMap(cosmosItemFeedResponse -> {

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplate.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.azure.cosmos.models.CosmosQueryRequestOptions;
1212
import com.azure.cosmos.models.FeedResponse;
1313
import com.azure.cosmos.models.PartitionKey;
14+
import com.azure.cosmos.models.SqlParameter;
1415
import com.azure.cosmos.models.SqlQuerySpec;
1516
import com.azure.cosmos.models.ThroughputProperties;
1617
import com.azure.spring.data.cosmos.Constants;
@@ -271,14 +272,15 @@ public <T> Mono<T> findById(String containerName, Object id, Class<T> domainType
271272
Assert.hasText(containerName, "containerName should not be null, empty or only whitespaces");
272273
Assert.notNull(domainType, "domainType should not be null");
273274

274-
final String query = String.format("select * from root where root.id = '%s'",
275-
CosmosUtils.getStringIDValue(id));
275+
final String query = "select * from root where root.id = @ROOT_ID";
276+
final SqlParameter param = new SqlParameter("@ROOT_ID", CosmosUtils.getStringIDValue(id));
277+
final SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(query, param);
276278
final CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
277279
options.setQueryMetricsEnabled(this.queryMetricsEnabled);
278280

279281
return cosmosAsyncClient.getDatabase(this.databaseName)
280282
.getContainer(containerName)
281-
.queryItems(query, options, JsonNode.class)
283+
.queryItems(sqlQuerySpec, options, JsonNode.class)
282284
.byPage()
283285
.publishOn(Schedulers.parallel())
284286
.flatMap(cosmosItemFeedResponse -> {

0 commit comments

Comments
 (0)