Skip to content

Commit

Permalink
Merge pull request #92 from Mastercard/fix/CVE-2023-51074andCVE-2023-…
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-neeraj authored Apr 15, 2024
2 parents f96fbb3 + 265773d commit d347e61
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.6.0</version>
<version>2.9.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -148,7 +148,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
<version>20240303</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private static Object readAndDeleteJsonKey(DocumentContext context, String objec
}
JsonProvider jsonProvider = JsonParser.jsonPathConfig.jsonProvider();
Object value = jsonProvider.getMapValue(object, key);
context.delete(objectPath + "." + key);
JsonParser.deleteIfExists(context, objectPath + "." + key);
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void addDecryptedDataToPayload(DocumentContext payloadContext, String dec
int length = jsonProvider.length(decryptedValueJsonElement);
Collection<String> propertyKeys = (0 == length) ? Collections.emptyList() : jsonProvider.getPropertyKeys(decryptedValueJsonElement);
for (String key : propertyKeys) {
payloadContext.delete(jsonPathOut + "." + key);
deleteIfExists( payloadContext, jsonPathOut + "." + key);
payloadContext.put(jsonPathOut, key, jsonProvider.getMapValue(decryptedValueJsonElement, key));
}
}
Expand Down Expand Up @@ -86,4 +86,11 @@ static Object readJsonObject(DocumentContext context, String jsonPathString) {
}
return jsonElement;
}

static void deleteIfExists(DocumentContext context, String jsonPathString){
Object value = context.read(jsonPathString);
if(value != null){
context.delete(jsonPathString);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static DocumentContext encryptPayloadPath(DocumentContext payloadContext

// Delete data in clear
if (!"$".equals(jsonPathIn)) {
payloadContext.delete(jsonPathIn);
JsonParser.deleteIfExists(payloadContext, jsonPathIn);
} else {
// We can't reuse the same DocumentContext. We have to create a new DocumentContext
// with the appropriate internal representation (JSON object).
Expand Down Expand Up @@ -135,12 +135,12 @@ private static DocumentContext decryptPayloadPath(DocumentContext payloadContext
}

// Remove the input
payloadContext.delete(jsonPathIn);
JsonParser.deleteIfExists(payloadContext, jsonPathIn);
return payloadContext;
}

private static Object readAndDeleteJsonKey(DocumentContext context, Object object, String key) {
context.delete(key);
JsonParser.deleteIfExists(context, key);
return object;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.mastercard.developer.encryption;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;

public class JsonParserTest {

@Test
public void testDeleteIfExists_shouldDeleteIfElementExists() {
final String key = "dummyKey";
JsonObject dummyObject = new JsonObject();
dummyObject.addProperty(key, "dummyValue");

DocumentContext context = JsonPath.parse(new Gson().toJson(dummyObject), JsonParser.jsonPathConfig);

JsonParser.deleteIfExists(context, key);

Object value = context.read(key);

assertNull(value);
}

@Test
public void testDeleteIfExists_doNothingIfElementDoesNotExist() {
final String key = "dummyKey";
JsonObject dummyObject = new JsonObject();
dummyObject.addProperty(key, "dummyValue");

DocumentContext context = JsonPath.parse(new Gson().toJson(dummyObject), JsonParser.jsonPathConfig);

JsonParser.deleteIfExists(context, "keyWhichDoesNotExist");

Object value = context.read(key);
assertNotNull(value);
}
}

0 comments on commit d347e61

Please sign in to comment.