|
3 | 3 | import java.io.ByteArrayInputStream; |
4 | 4 | import java.io.ByteArrayOutputStream; |
5 | 5 | import java.io.IOException; |
| 6 | +import java.io.InvalidClassException; |
6 | 7 | import java.io.ObjectOutputStream; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Set; |
7 | 10 |
|
8 | 11 | import org.junit.jupiter.api.AfterEach; |
9 | 12 | import org.junit.jupiter.api.BeforeEach; |
10 | 13 | import org.junit.jupiter.api.Test; |
11 | 14 |
|
12 | 15 | 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; |
13 | 18 |
|
14 | 19 | public class HardenedObjectInputStreamTest { |
15 | 20 |
|
@@ -53,39 +58,38 @@ private void writeObject(ObjectOutputStream oos, Object o) throws IOException { |
53 | 58 | oos.close(); |
54 | 59 | } |
55 | 60 |
|
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 | + } |
91 | 95 | } |
0 commit comments