Skip to content

Commit 16e838a

Browse files
rraystpredic8
andauthored
Fixes #1904. (#1946)
* Fixes #1904. * renamed property --------- Co-authored-by: Thomas Bayer <[email protected]>
1 parent 4abf59a commit 16e838a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

core/src/main/java/com/predic8/membrane/core/interceptor/json/JsonProtectionInterceptor.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class JsonProtectionInterceptor extends AbstractInterceptor {
5757
private int maxKeyLength = 256;
5858
private int maxObjectSize = 1000;
5959
private int maxArraySize = 1000;
60-
60+
private boolean blockProto = true;
6161

6262
public JsonProtectionInterceptor() {
6363
name = "json protection";
@@ -93,6 +93,10 @@ public void check(JsonToken jsonToken, JsonParser parser) throws JsonProtectionE
9393
throw new JsonProtectionException("Exceeded maxObjectSize.",
9494
parser.currentLocation().getLineNr(),
9595
parser.currentLocation().getColumnNr());
96+
if (blockProto && "__proto__".equals(parser.currentName()))
97+
throw new JsonProtectionException("__proto__ found as key.",
98+
parser.currentLocation().getLineNr(),
99+
parser.currentLocation().getColumnNr());
96100
if (parser.currentName().length() > maxKeyLength) {
97101
throw new JsonProtectionException("Exceeded maxKeyLength.",
98102
parser.currentLocation().getLineNr(),
@@ -366,6 +370,20 @@ public void setMaxArraySize(int maxArraySize) {
366370
this.maxArraySize = maxArraySize;
367371
}
368372

373+
public boolean isBlockProto() {
374+
return blockProto;
375+
}
376+
377+
/**
378+
* @description Blocks JSON properties with a key of "__proto__" to avoid prototype pollution in Javascript backends.
379+
* @default true
380+
* @param blockProto
381+
*/
382+
@MCAttribute
383+
public void setBlockProto(boolean blockProto) {
384+
this.blockProto = blockProto;
385+
}
386+
369387
@Override
370388
public String getShortDescription() {
371389
return "Protects against several JSON attack classes.";

core/src/test/java/com/predic8/membrane/core/interceptor/json/JsonProtectionInterceptorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private static JsonProtectionInterceptor buildJPI(boolean prod) {
4242
jpi.setMaxKeyLength(10);
4343
jpi.setMaxObjectSize(10);
4444
jpi.setMaxArraySize(2048);
45+
jpi.setBlockProto(true);
4546

4647
jpi.init(router);
4748
return jpi;
@@ -229,6 +230,15 @@ public void justNotTooManyTokens() throws Exception {
229230
CONTINUE);
230231
}
231232

233+
@Test
234+
public void protoBlocked() throws Exception {
235+
send("{\"__proto__\": {}}",
236+
RETURN,
237+
1,
238+
16,
239+
"__proto__ found as key.");
240+
}
241+
232242
private void send(String body, Outcome expectOut, Object ...parameters) throws Exception {
233243
Exchange exc = new Request.Builder()
234244
.post("/")

0 commit comments

Comments
 (0)