|
| 1 | +package org.codefx.demo.java9.api.deserialization_filter; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InvalidClassException; |
| 7 | +import java.io.ObjectInputFilter; |
| 8 | +import java.io.ObjectInputStream; |
| 9 | +import java.io.ObjectOutputStream; |
| 10 | + |
| 11 | +import static org.codefx.demo.java9.api.deserialization_filter.LinkedListNode.createList; |
| 12 | + |
| 13 | +public class SerializeThenFilter { |
| 14 | + |
| 15 | + /* |
| 16 | + * CAREFUL: (De)serialization is a security-sensitive topic, but these examples don't take |
| 17 | + * that into account. They just demonstrate how some parts of the API work without |
| 18 | + * looking at the security implications. If you use this APIs to secure your application, |
| 19 | + * make sure to NOT USE THESE EXAMPLES. Instead, make sure to master the topic |
| 20 | + * and API first, e.g. by starting here: |
| 21 | + * - https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/ObjectInputFilter.html |
| 22 | + * - https://docs.oracle.com/pls/topic/lookup?ctx=javase17&id=secure_coding_guidelines_javase |
| 23 | + * - https://docs.oracle.com/pls/topic/lookup?ctx=javase17&id=serialization_filter_guide |
| 24 | + */ |
| 25 | + |
| 26 | + // launch with -Djdk.serialFilter=maxdepth=5 to configure deserialization filter to reject |
| 27 | + // instance graphs with a depth of 6 or more - observe effect in `staticDeserializationFilter` |
| 28 | + public static void main(String[] args) throws IOException, ClassNotFoundException { |
| 29 | + staticDeserializationFilter(); |
| 30 | + dynamicDeserializationFilter(); |
| 31 | + } |
| 32 | + |
| 33 | + private static void staticDeserializationFilter() throws IOException, ClassNotFoundException { |
| 34 | + LinkedListNode list = createList("A", "B", "C", "D", "E", "F", "G", "H"); |
| 35 | + System.out.println("List to serialize: " + list); |
| 36 | + |
| 37 | + byte[] serializedList = serialize(list); |
| 38 | + try { |
| 39 | + LinkedListNode deserializedList = deserialize(serializedList); |
| 40 | + System.out.println("Deserialization succeeded unexpectedly: " + deserializedList); |
| 41 | + } catch (InvalidClassException ex) { |
| 42 | + System.out.println("Deserialization failed as expected: " + ex.getMessage()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void dynamicDeserializationFilter() throws IOException, ClassNotFoundException { |
| 47 | + LinkedListNode list = createList("AAA", "BBB", "CCC"); |
| 48 | + System.out.println("List to serialize: " + list); |
| 49 | + |
| 50 | + byte[] serializedList = serialize(list); |
| 51 | + ObjectInputFilter filter = ObjectInputFilter.Config.createFilter("maxbytes=128"); |
| 52 | + |
| 53 | + try { |
| 54 | + LinkedListNode deserializedList = deserializeWithFilter(serializedList, filter); |
| 55 | + System.out.println("Deserialization succeeded unexpectedly: " + deserializedList); |
| 56 | + } catch (InvalidClassException ex) { |
| 57 | + System.out.println("Deserialization failed as expected: " + ex.getMessage()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static byte[] serialize(LinkedListNode list) throws IOException { |
| 62 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 63 | + new ObjectOutputStream(byteArrayOutputStream).writeObject(list); |
| 64 | + return byteArrayOutputStream.toByteArray(); |
| 65 | + } |
| 66 | + |
| 67 | + private static LinkedListNode deserialize(byte[] serializedList) throws IOException, ClassNotFoundException { |
| 68 | + ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(serializedList)); |
| 69 | + return (LinkedListNode) inputStream.readObject(); |
| 70 | + } |
| 71 | + |
| 72 | + private static LinkedListNode deserializeWithFilter(byte[] serializedList, ObjectInputFilter filter) throws IOException, ClassNotFoundException { |
| 73 | + ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(serializedList)); |
| 74 | + inputStream.setObjectInputFilter(filter); |
| 75 | + return (LinkedListNode) inputStream.readObject(); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments