metsw24-max commented on code in PR #16138:
URL: https://github.com/apache/lucene/pull/16138#discussion_r3328242123


##########
lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java:
##########
@@ -216,7 +216,13 @@ static Class<?> readClass(final InputStream inputStream)
       return StandardObjects.CODE_REGISTRY.get(index);
     } else {
       String className = readString(inputStream);
-      return Class.forName(className);
+      // Load without initializing and confirm the named class is actually a 
SerializableObject
+      // before it can be instantiated, so a crafted stream cannot load 
arbitrary classes.
+      Class<?> clazz = Class.forName(className, false, 
SerializableObject.class.getClassLoader());
+      if (!SerializableObject.class.isAssignableFrom(clazz)) {

Review Comment:
   Done, switched to == false.



##########
lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/TestSerializableObjectClassFilter.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.spatial3d.geom;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import org.apache.lucene.tests.util.LuceneTestCase;
+
+public class TestSerializableObjectClassFilter extends LuceneTestCase {
+
+  static volatile boolean nonSerializableConstructed = false;
+
+  /** A SerializableObject that is not registered in {@link StandardObjects}. 
*/
+  public static class CustomShape implements SerializableObject {
+    final int value;
+
+    public CustomShape(int value) {
+      this.value = value;
+    }
+
+    public CustomShape(InputStream inputStream) throws IOException {
+      this.value = SerializableObject.readInt(inputStream);
+    }
+
+    @Override
+    public void write(OutputStream outputStream) throws IOException {
+      SerializableObject.writeInt(outputStream, value);
+    }
+  }
+
+  /** Not a SerializableObject, but instantiable from an InputStream. */
+  public static class NotASerializableObject {
+    public NotASerializableObject(InputStream inputStream) {
+      nonSerializableConstructed = true;
+    }
+  }
+
+  public void testCustomClassRoundTrips() throws IOException {
+    CustomShape shape = new CustomShape(42);
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    SerializableObject.writeObject(out, shape);
+    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+    SerializableObject copy = SerializableObject.readObject(in);
+    assertTrue(copy instanceof CustomShape);
+    assertEquals(42, ((CustomShape) copy).value);
+  }
+
+  public void testRejectsNonSerializableObjectClass() throws IOException {
+    nonSerializableConstructed = false;
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    SerializableObject.writeBoolean(out, false);
+    SerializableObject.writeString(out, 
NotASerializableObject.class.getName());
+    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+    expectThrows(IOException.class, () -> SerializableObject.readObject(in));

Review Comment:
   Added an assert that the message contains the class name.



##########
lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java:
##########
@@ -216,7 +216,13 @@ static Class<?> readClass(final InputStream inputStream)
       return StandardObjects.CODE_REGISTRY.get(index);
     } else {
       String className = readString(inputStream);
-      return Class.forName(className);
+      // Load without initializing and confirm the named class is actually a 
SerializableObject
+      // before it can be instantiated, so a crafted stream cannot load 
arbitrary classes.
+      Class<?> clazz = Class.forName(className, false, 
SerializableObject.class.getClassLoader());

Review Comment:
   Class.forName doesn't return null, it throws ClassNotFoundException for an 
unknown name, so that path is the same as before (readClass already declares 
it). The new check only kicks in once the class resolves.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to