Skip to content
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

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

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,16 @@ static Object readJsonObject(DocumentContext context, String jsonPathString) {
}
return jsonElement;
}

// Upgrading the json-path lib from 2.6.0 to 2.9.0 introduced a bug where when you
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant : remove all of this: // try to delete a non-existent key in a DocumentContext with the SUPPRESS_EXCEPTIONS flag,
// it would throw a ClassCastException. This method is a workaround for the issue.
// Once this issue is fixed this method's usages can be replaced with a simple DocumentContext.delete(path).
// Track the issue here json-path/JsonPath#870

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could leave this comment in until this issue is fixed. It's a good way to make sure we revisit it in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, folks, I would insist that we remove this comment. There will be no reason to update this library, unless another security issue. let keep it code clean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed it now.

// try to delete a non-existent key in a DocumentContext with the SUPPRESS_EXCEPTIONS flag,
// it would throw a ClassCastException. This method is a workaround for the issue.
// Once this issue is fixed this method's usages can be replaced with a simple DocumentContext.delete(path).
// Track the issue here https://github.com/json-path/JsonPath/issues/870
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