|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.io.InvalidClassException; |
21 | 21 | import java.io.ObjectOutputStream; |
| 22 | +import java.io.Serializable; |
| 23 | +import java.lang.reflect.InvocationHandler; |
| 24 | +import java.lang.reflect.Method; |
| 25 | +import java.lang.reflect.Proxy; |
22 | 26 | import java.util.HashSet; |
23 | 27 | import java.util.Set; |
24 | 28 |
|
@@ -109,4 +113,61 @@ private Set buildEvilHashset() { |
109 | 113 | } |
110 | 114 | return root; |
111 | 115 | } |
| 116 | + |
| 117 | + /** |
| 118 | + * Demonstrates that HardenedObjectInputStream.resolveProxyClass works correctly |
| 119 | + * by rejecting deserialization of dynamic proxy classes, even when the interfaces |
| 120 | + * they implement are whitelisted. |
| 121 | + */ |
| 122 | + @Test |
| 123 | + public void resolveProxyClassRejectsDynamicProxies() throws Exception { |
| 124 | + ProxyInterface proxy = (ProxyInterface) Proxy.newProxyInstance( |
| 125 | + getClass().getClassLoader(), |
| 126 | + new Class<?>[]{ProxyInterface.class}, |
| 127 | + new TestInvocationHandler() |
| 128 | + ); |
| 129 | + |
| 130 | + // Serialize the proxy instance |
| 131 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 132 | + try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { |
| 133 | + oos.writeObject(proxy); |
| 134 | + } |
| 135 | + |
| 136 | + // Attempt to deserialize using HardenedObjectInputStream. |
| 137 | + // We deliberately whitelist both the interface and the invocation handler. |
| 138 | + // Despite this, deserialization must fail because resolveProxyClass always |
| 139 | + // throws InvalidClassException for proxy classes. |
| 140 | + String[] whitelist = new String[]{ |
| 141 | + ProxyInterface.class.getName(), |
| 142 | + TestInvocationHandler.class.getName() |
| 143 | + }; |
| 144 | + |
| 145 | + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
| 146 | + HardenedObjectInputStream hardenedOis = new HardenedObjectInputStream(context, bis, whitelist); |
| 147 | + |
| 148 | + assertThrows(InvalidClassException.class, hardenedOis::readObject); |
| 149 | + hardenedOis.close(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * A marker interface for the dynamic proxy used in the resolveProxyClass test. |
| 154 | + */ |
| 155 | + interface ProxyInterface extends Serializable { |
| 156 | + String getMessage(); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * A serializable InvocationHandler used to create the test dynamic proxy. |
| 161 | + */ |
| 162 | + static class TestInvocationHandler implements InvocationHandler, Serializable { |
| 163 | + private static final long serialVersionUID = 1L; |
| 164 | + |
| 165 | + @Override |
| 166 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 167 | + if ("getMessage".equals(method.getName())) { |
| 168 | + return "hello from proxy"; |
| 169 | + } |
| 170 | + return null; |
| 171 | + } |
| 172 | + } |
112 | 173 | } |
0 commit comments