Skip to content

Commit 7018a36

Browse files
committed
fix missing deseialization filter init call, enable commented out test case
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 8a746eb commit 7018a36

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

logback-core/src/main/java/ch/qos/logback/core/net/HardenedObjectInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void initObjectFilter() {
6060
}
6161
public HardenedObjectInputStream(InputStream in, List<String> whitelist) throws IOException {
6262
super(in);
63-
63+
this.initObjectFilter();
6464
this.whitelistedClassNames = new ArrayList<String>();
6565
this.whitelistedClassNames.addAll(whitelist);
6666
}

logback-core/src/test/java/ch/qos/logback/core/net/HardenedObjectInputStreamTest.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
6+
import java.io.InvalidClassException;
67
import java.io.ObjectOutputStream;
8+
import java.util.HashSet;
9+
import java.util.Set;
710

811
import org.junit.jupiter.api.AfterEach;
912
import org.junit.jupiter.api.BeforeEach;
1013
import org.junit.jupiter.api.Test;
1114

1215
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
17+
import static org.junit.jupiter.api.Assertions.assertThrows;
1318

1419
public class HardenedObjectInputStreamTest {
1520

@@ -53,39 +58,38 @@ private void writeObject(ObjectOutputStream oos, Object o) throws IOException {
5358
oos.close();
5459
}
5560

56-
// @Ignore
57-
// @Test
58-
// public void denialOfService() throws ClassNotFoundException, IOException {
59-
// ByteArrayInputStream bis = new ByteArrayInputStream(payload());
60-
// inputStream = new HardenedObjectInputStream(bis, whitelist);
61-
// try {
62-
// Set set = (Set) inputStream.readObject();
63-
// assertNotNull(set);
64-
// } finally {
65-
// inputStream.close();
66-
// }
67-
// }
68-
//
69-
// private byte[] payload() throws IOException {
70-
// Set root = buildEvilHashset();
71-
// return serialize(root);
72-
// }
73-
//
74-
// private Set buildEvilHashset() {
75-
// Set root = new HashSet();
76-
// Set s1 = root;
77-
// Set s2 = new HashSet();
78-
// for (int i = 0; i < 100; i++) {
79-
// Set t1 = new HashSet();
80-
// Set t2 = new HashSet();
81-
// t1.add("foo"); // make it not equal to t2
82-
// s1.add(t1);
83-
// s1.add(t2);
84-
// s2.add(t1);
85-
// s2.add(t2);
86-
// s1 = t1;
87-
// s2 = t2;
88-
// }
89-
// return root;
90-
// }
61+
@Test
62+
public void denialOfService() throws ClassNotFoundException, IOException {
63+
ByteArrayInputStream bis = new ByteArrayInputStream(payload());
64+
inputStream = new HardenedObjectInputStream(bis, whitelist);
65+
try {
66+
assertThrows(InvalidClassException.class, () -> inputStream.readObject());
67+
} finally {
68+
inputStream.close();
69+
}
70+
}
71+
72+
private byte[] payload() throws IOException {
73+
Set root = buildEvilHashset();
74+
writeObject(oos, root);
75+
return bos.toByteArray();
76+
}
77+
78+
private Set buildEvilHashset() {
79+
Set root = new HashSet();
80+
Set s1 = root;
81+
Set s2 = new HashSet();
82+
for (int i = 0; i < 100; i++) {
83+
Set t1 = new HashSet();
84+
Set t2 = new HashSet();
85+
t1.add("foo"); // make it not equal to t2
86+
s1.add(t1);
87+
s1.add(t2);
88+
s2.add(t1);
89+
s2.add(t2);
90+
s1 = t1;
91+
s2 = t2;
92+
}
93+
return root;
94+
}
9195
}

0 commit comments

Comments
 (0)