-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureDeserializer.java
More file actions
36 lines (29 loc) · 972 Bytes
/
Copy pathSecureDeserializer.java
File metadata and controls
36 lines (29 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Insecure Deserialization Remediation
*
* SECURE VERSION
*
* Restricts deserialization using an ObjectInputFilter.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputFilter;
import java.io.ObjectInputStream;
public class SecureDeserializer {
public static Object deserialize(InputStream inputStream)
throws IOException, ClassNotFoundException {
try (ObjectInputStream objectInputStream =
new ObjectInputStream(inputStream)) {
/*
* Only allow objects from the expected application package.
* Reject everything else.
*/
ObjectInputFilter filter =
ObjectInputFilter.Config.createFilter(
"com.myapp.*;java.base/*;!*"
);
objectInputStream.setObjectInputFilter(filter);
return objectInputStream.readObject();
}
}
}