Skip to content

Updates for CVE-2023-51074 and CVE-2023-5072 #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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);
}
}
Loading