Skip to content

Commit 8993045

Browse files
committed
Updates for CVE-2023-51074 and CVE-2023-5072
Updates the org.json.json and com.jayway.jsonpath.json-path libraries which fix CVE-2023-51074 and CVE-2023-5072. Had to make code changes because the json-path library introduced a bug in the updated version which fails a few unit tests in our repo. See this PR to track the issue json-path/JsonPath#871
1 parent f96fbb3 commit 8993045

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<dependency>
5353
<groupId>com.jayway.jsonpath</groupId>
5454
<artifactId>json-path</artifactId>
55-
<version>2.6.0</version>
55+
<version>2.9.0</version>
5656
</dependency>
5757

5858
<dependency>
@@ -148,7 +148,7 @@
148148
<dependency>
149149
<groupId>org.json</groupId>
150150
<artifactId>json</artifactId>
151-
<version>20230227</version>
151+
<version>20240303</version>
152152
<scope>test</scope>
153153
</dependency>
154154

src/main/java/com/mastercard/developer/encryption/FieldLevelEncryption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private static Object readAndDeleteJsonKey(DocumentContext context, String objec
210210
}
211211
JsonProvider jsonProvider = JsonParser.jsonPathConfig.jsonProvider();
212212
Object value = jsonProvider.getMapValue(object, key);
213-
context.delete(objectPath + "." + key);
213+
JsonParser.deleteIfExists(context, objectPath + "." + key);
214214
return value;
215215
}
216216
}

src/main/java/com/mastercard/developer/encryption/JsonParser.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void addDecryptedDataToPayload(DocumentContext payloadContext, String dec
4747
int length = jsonProvider.length(decryptedValueJsonElement);
4848
Collection<String> propertyKeys = (0 == length) ? Collections.emptyList() : jsonProvider.getPropertyKeys(decryptedValueJsonElement);
4949
for (String key : propertyKeys) {
50-
payloadContext.delete(jsonPathOut + "." + key);
50+
deleteIfExists( payloadContext, jsonPathOut + "." + key);
5151
payloadContext.put(jsonPathOut, key, jsonProvider.getMapValue(decryptedValueJsonElement, key));
5252
}
5353
}
@@ -86,4 +86,16 @@ static Object readJsonObject(DocumentContext context, String jsonPathString) {
8686
}
8787
return jsonElement;
8888
}
89+
90+
// Upgrading the json-path lib from 2.6.0 to 2.9.0 introduced a bug where when you
91+
// try to delete a non-existent key in a DocumentContext with the SUPPRESS_EXCEPTIONS flag,
92+
// it would throw a ClassCastException. This method is a workaround for the issue.
93+
// Once this issue is fixed this method's usages can be replaced with a simple DocumentContext.delete(path).
94+
// Track the issue here https://github.com/json-path/JsonPath/issues/870
95+
static void deleteIfExists(DocumentContext context, String jsonPathString){
96+
Object value = context.read(jsonPathString);
97+
if(value != null){
98+
context.delete(jsonPathString);
99+
}
100+
}
89101
}

src/main/java/com/mastercard/developer/encryption/JweEncryption.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static DocumentContext encryptPayloadPath(DocumentContext payloadContext
9292

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

137137
// Remove the input
138-
payloadContext.delete(jsonPathIn);
138+
JsonParser.deleteIfExists(payloadContext, jsonPathIn);
139139
return payloadContext;
140140
}
141141

142142
private static Object readAndDeleteJsonKey(DocumentContext context, Object object, String key) {
143-
context.delete(key);
143+
JsonParser.deleteIfExists(context, key);
144144
return object;
145145
}
146146

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.mastercard.developer.encryption;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
5+
import com.jayway.jsonpath.DocumentContext;
6+
import com.jayway.jsonpath.JsonPath;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertNull;
10+
import static org.junit.Assert.assertNotNull;
11+
12+
public class JsonParserTest {
13+
14+
@Test
15+
public void testDeleteIfExists_shouldDeleteIfElementExists() {
16+
final String key = "dummyKey";
17+
JsonObject dummyObject = new JsonObject();
18+
dummyObject.addProperty(key, "dummyValue");
19+
20+
DocumentContext context = JsonPath.parse(new Gson().toJson(dummyObject), JsonParser.jsonPathConfig);
21+
22+
JsonParser.deleteIfExists(context, key);
23+
24+
Object value = context.read(key);
25+
26+
assertNull(value);
27+
}
28+
29+
@Test
30+
public void testDeleteIfExists_doNothingIfElementDoesNotExist() {
31+
final String key = "dummyKey";
32+
JsonObject dummyObject = new JsonObject();
33+
dummyObject.addProperty(key, "dummyValue");
34+
35+
DocumentContext context = JsonPath.parse(new Gson().toJson(dummyObject), JsonParser.jsonPathConfig);
36+
37+
JsonParser.deleteIfExists(context, "keyWhichDoesNotExist");
38+
39+
Object value = context.read(key);
40+
assertNotNull(value);
41+
}
42+
}

0 commit comments

Comments
 (0)