Skip to content

Commit

Permalink
Client Encryption : Fixes issue when passing null parameter value in …
Browse files Browse the repository at this point in the history
…AddParameterAsync (Azure#2507)

This PR fixes issue where null parameter value when passed in AddParameterAsync throws ArgumentNullException. Null value is a valid value that can be passed as part of parameter value when executing query.
  • Loading branch information
kr-santosh authored Jun 1, 2021
1 parent 7f7ec04 commit 30b3d4e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public static async Task<QueryDefinition> AddParameterAsync(
throw new ArgumentNullException(nameof(name));
}

// if null use as-is
if (value == null)
{
throw new ArgumentNullException(nameof(value));
queryDefinition.WithParameter(name, value);
return queryDefinition;
}

QueryDefinition queryDefinitionwithEncryptedValues = queryDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public async Task EncryptionCreateItemWithNullProperty()

testDoc.Sensitive_ArrayFormat = null;
testDoc.Sensitive_StringFormat = null;
testDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_StringFormatL2 = null;

ItemResponse<TestDoc> createResponse = await encryptionContainer.CreateItemAsync(
testDoc,
Expand All @@ -356,6 +357,44 @@ public async Task EncryptionCreateItemWithNullProperty()

VerifyExpectedDocResponse(testDoc, createResponse.Resource);

// run query on document with null property value.
QueryDefinition withEncryptedParameter = encryptionContainer.CreateQueryDefinition(
"SELECT * FROM c where c.Sensitive_StringFormat = @Sensitive_StringFormat AND c.Sensitive_ArrayFormat = @Sensitive_ArrayFormat" +
" AND c.Sensitive_IntFormat = @Sensitive_IntFormat" +
" AND c.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_StringFormatL2 = @Sensitive_StringFormatL2" +
" AND c.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_DecimalFormatL2 = @Sensitive_DecimalFormatL2");

await withEncryptedParameter.AddParameterAsync(
"@Sensitive_StringFormat",
testDoc.Sensitive_StringFormat,
"/Sensitive_StringFormat");

await withEncryptedParameter.AddParameterAsync(
"@Sensitive_ArrayFormat",
testDoc.Sensitive_ArrayFormat,
"/Sensitive_ArrayFormat");

await withEncryptedParameter.AddParameterAsync(
"@Sensitive_IntFormat",
testDoc.Sensitive_IntFormat,
"/Sensitive_IntFormat");

await withEncryptedParameter.AddParameterAsync(
"@Sensitive_StringFormatL2",
testDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_StringFormatL2,
"/Sensitive_NestedObjectFormatL1");

await withEncryptedParameter.AddParameterAsync(
"@Sensitive_DecimalFormatL2",
testDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_DecimalFormatL2,
"/Sensitive_NestedObjectFormatL1");

TestDoc expectedDoc = new TestDoc(testDoc);
await MdeEncryptionTests.ValidateQueryResultsAsync(
encryptionContainer,
queryDefinition: withEncryptedParameter,
expectedDoc: expectedDoc);

// no access to key.
testEncryptionKeyStoreProvider.RevokeAccessSet = true;

Expand Down Expand Up @@ -521,7 +560,7 @@ public async Task QueryOnEncryptedProperties()
TestDoc testDoc1 = await MdeEncryptionTests.MdeCreateItemAsync(MdeEncryptionTests.encryptionContainer);

// string/int
string[] arrayofStringValues = new string[] { testDoc1.Sensitive_StringFormat, "randomValue" };
string[] arrayofStringValues = new string[] { testDoc1.Sensitive_StringFormat, "randomValue", null };

QueryDefinition withEncryptedParameter = MdeEncryptionTests.encryptionContainer.CreateQueryDefinition(
"SELECT * FROM c where array_contains(@Sensitive_StringFormat, c.Sensitive_StringFormat) " +
Expand Down Expand Up @@ -2369,8 +2408,8 @@ private static void VerifyExpectedDocResponse(TestDoc expectedDoc, TestDoc verif
else
{
Assert.AreEqual(
expectedDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_IntFormatL2,
verifyDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_IntFormatL2);
expectedDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_StringFormatL2,
verifyDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_StringFormatL2);

Assert.AreEqual(
expectedDoc.Sensitive_NestedObjectFormatL1.Sensitive_NestedObjectFormatL2.Sensitive_NestedObjectFormatL3.Sensitive_IntFormatL3,
Expand Down Expand Up @@ -2497,7 +2536,7 @@ public class Sensitive_NestedObjectL1

public class Sensitive_NestedObjectL2
{
public int Sensitive_IntFormatL2 { get; set; }
public string Sensitive_StringFormatL2 { get; set; }
public decimal Sensitive_DecimalFormatL2 { get; set; }
public Sensitive_ArrayData[] Sensitive_ArrayFormatL2 { get; set; }
public Sensitive_NestedObjectL3 Sensitive_NestedObjectFormatL3 { get; set; }
Expand Down Expand Up @@ -2622,7 +2661,7 @@ public static TestDoc Create(string partitionKey = null)
Sensitive_IntFormatL0 = 888,
Sensitive_DecimalFormatL0 = 888.1m,
},
Sensitive_StringArrayMultiType = new string[2] { "sensitivedata1a", "verysensitivedata1a"},
Sensitive_StringArrayMultiType = new string[3] { "sensitivedata1a", "verysensitivedata1a", null},
Sensitive_ArrayMultiTypeDecimalFormat = 10.2m,
Sensitive_IntArrayMultiType = new int[2] { 999, 1000 }
},
Expand Down Expand Up @@ -2683,7 +2722,7 @@ public static TestDoc Create(string partitionKey = null)
},
Sensitive_NestedObjectFormatL2 = new Sensitive_NestedObjectL2()
{
Sensitive_IntFormatL2 = 2000,
Sensitive_StringFormatL2 = "sensitiveData",
Sensitive_DecimalFormatL2 = 2000.1m,
Sensitive_ArrayFormatL2 = new Sensitive_ArrayData[]
{
Expand Down

0 comments on commit 30b3d4e

Please sign in to comment.