|
16 | 16 | import java.io.IOException;
|
17 | 17 | import java.io.InputStream;
|
18 | 18 | import java.io.InvalidClassException;
|
| 19 | +import java.io.ObjectInputFilter; |
19 | 20 | import java.io.ObjectInputStream;
|
20 | 21 | import java.io.ObjectStreamClass;
|
21 | 22 | import java.util.ArrayList;
|
|
36 | 37 | */
|
37 | 38 | public class HardenedObjectInputStream extends ObjectInputStream {
|
38 | 39 |
|
39 |
| - final List<String> whitelistedClassNames; |
40 |
| - final static String[] JAVA_PACKAGES = new String[] { "java.lang", "java.util" }; |
| 40 | + final private List<String> whitelistedClassNames; |
| 41 | + final private static String[] JAVA_PACKAGES = new String[] { "java.lang", "java.util" }; |
| 42 | + final private static int DEPTH_LIMIT = 16; |
| 43 | + final private static int ARRAY_LIMIT = 10000; |
41 | 44 |
|
42 |
| - public HardenedObjectInputStream(InputStream in, String[] whilelist) throws IOException { |
| 45 | + public HardenedObjectInputStream(InputStream in, String[] whitelist) throws IOException { |
43 | 46 | super(in);
|
44 |
| - |
| 47 | + this.initObjectFilter(); |
45 | 48 | this.whitelistedClassNames = new ArrayList<String>();
|
46 |
| - if (whilelist != null) { |
47 |
| - for (int i = 0; i < whilelist.length; i++) { |
48 |
| - this.whitelistedClassNames.add(whilelist[i]); |
| 49 | + if (whitelist != null) { |
| 50 | + for (int i = 0; i < whitelist.length; i++) { |
| 51 | + this.whitelistedClassNames.add(whitelist[i]); |
49 | 52 | }
|
50 | 53 | }
|
51 | 54 | }
|
52 | 55 |
|
| 56 | + private void initObjectFilter() { |
| 57 | + this.setObjectInputFilter(ObjectInputFilter.Config.createFilter( |
| 58 | + "maxarray=" + ARRAY_LIMIT + ";maxdepth=" + DEPTH_LIMIT + ";" |
| 59 | + )); |
| 60 | + } |
53 | 61 | public HardenedObjectInputStream(InputStream in, List<String> whitelist) throws IOException {
|
54 | 62 | super(in);
|
55 | 63 |
|
|
0 commit comments