|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2020. Nitrite author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.dizitart.no2.mvstore.compat.v1; |
| 18 | + |
| 19 | +import com.evil.EvilGadget; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.ObjectOutputStream; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | + |
| 26 | +import static org.junit.Assert.*; |
| 27 | + |
| 28 | +/** |
| 29 | + * Regression test for GHSA-9297-g93h-86gg: the legacy v1 migration deserializer |
| 30 | + * must reject arbitrary (potentially gadget) classes on the classpath. |
| 31 | + */ |
| 32 | +public class NitriteObjectInputStreamTest { |
| 33 | + |
| 34 | + private static byte[] serialize(Object obj) throws Exception { |
| 35 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 36 | + try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { |
| 37 | + oos.writeObject(obj); |
| 38 | + } |
| 39 | + return bos.toByteArray(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void rejectsUnknownClass() throws Exception { |
| 44 | + EvilGadget.readObjectCalled = false; |
| 45 | + byte[] payload = serialize(new EvilGadget()); |
| 46 | + |
| 47 | + try { |
| 48 | + NitriteDataType.deserialize(payload); |
| 49 | + fail("expected deserialization of a non-allowlisted class to be rejected"); |
| 50 | + } catch (IllegalArgumentException expected) { |
| 51 | + // filter rejected the class before its readObject() could run |
| 52 | + } |
| 53 | + assertFalse("gadget readObject() must not be invoked", EvilGadget.readObjectCalled); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void allowsStandardTypes() throws Exception { |
| 58 | + assertEquals("hello", NitriteDataType.deserialize(serialize("hello"))); |
| 59 | + |
| 60 | + LinkedHashMap<String, Object> doc = new LinkedHashMap<>(); |
| 61 | + doc.put("k", 42L); |
| 62 | + assertEquals(doc, NitriteDataType.deserialize(serialize(doc))); |
| 63 | + } |
| 64 | +} |
0 commit comments