|
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;
|
| 13 | +import org.junit.jupiter.api.Disabled; |
10 | 14 | import org.junit.jupiter.api.Test;
|
11 | 15 |
|
12 | 16 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 17 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
13 | 19 |
|
14 | 20 | public class HardenedObjectInputStreamTest {
|
15 | 21 |
|
@@ -53,39 +59,38 @@ private void writeObject(ObjectOutputStream oos, Object o) throws IOException {
|
53 | 59 | oos.close();
|
54 | 60 | }
|
55 | 61 |
|
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 |
| -// } |
| 62 | + @Test |
| 63 | + public void denialOfService() throws ClassNotFoundException, IOException { |
| 64 | + ByteArrayInputStream bis = new ByteArrayInputStream(payload()); |
| 65 | + inputStream = new HardenedObjectInputStream(bis, whitelist); |
| 66 | + try { |
| 67 | + assertThrows(InvalidClassException.class, () -> inputStream.readObject()); |
| 68 | + } finally { |
| 69 | + inputStream.close(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private byte[] payload() throws IOException { |
| 74 | + Set root = buildEvilHashset(); |
| 75 | + writeObject(oos, root); |
| 76 | + return bos.toByteArray(); |
| 77 | + } |
| 78 | + |
| 79 | + private Set buildEvilHashset() { |
| 80 | + Set root = new HashSet(); |
| 81 | + Set s1 = root; |
| 82 | + Set s2 = new HashSet(); |
| 83 | + for (int i = 0; i < 100; i++) { |
| 84 | + Set t1 = new HashSet(); |
| 85 | + Set t2 = new HashSet(); |
| 86 | + t1.add("foo"); // make it not equal to t2 |
| 87 | + s1.add(t1); |
| 88 | + s1.add(t2); |
| 89 | + s2.add(t1); |
| 90 | + s2.add(t2); |
| 91 | + s1 = t1; |
| 92 | + s2 = t2; |
| 93 | + } |
| 94 | + return root; |
| 95 | + } |
91 | 96 | }
|
0 commit comments