ramanathan1504 commented on code in PR #4466:
URL: https://github.com/apache/logging-log4j2/pull/4466#discussion_r4660057428
##########
log4j-core-test/pom.xml:
##########
@@ -444,6 +456,40 @@
<profiles>
+ <!-- Extends the `java8-tests` profile in `log4j-parent`, which only
modifies the `default-test` run:
+ the `jdk.serialFilter` of the `test-serial-filter` run must work on
all supported JDKs, including Java 8. -->
+ <profile>
+
+ <id>java8-tests</id>
+
+ <activation>
+ <property>
+ <name>env.CI</name>
+ <value>true</value>
+ </property>
+ </activation>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>test-serial-filter</id>
+ <configuration>
Review Comment:
With this override, CI runs `test-serial-filter` on Java 8 too, so no CI run
is on Java 9 or later. `readWrappedObjectUsesObjectInputFilterOfOuterStream`
and `readWrappedObjectUsesObjectInputFilterOfOuterFilteredStream` are skipped
in all three builds. Can we keep `test-serial-filter` on the build JDK and add
Java 8 as a second execution? #4515 has the same override for `2.x`.
```suggestion
<id>test-serial-filter-java8</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<argLine>-Djdk.serialFilter=java.lang.**;java.time.**;java.util.**;java.math.BigDecimal;java.math.BigInteger;org.apache.logging.log4j.**;!*</argLine>
<groups>serialization</groups>
```
##########
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/WrappedObjects.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.logging.log4j.core.util.internal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.io.StreamCorruptedException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.function.Supplier;
+import org.apache.logging.log4j.core.internal.annotation.SuppressFBWarnings;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.FilteredObjectInputStream;
+
+/**
+ * Wraps serialized objects in byte arrays, so that unserializable or
unavailable classes don't break
+ * the serialization of {@link
org.apache.logging.log4j.core.impl.Log4jLogEvent}.
+ * <p>
+ * Subset of {@code
org.apache.logging.log4j.util.internal.SerializationUtil}, which is not
exported by
+ * {@code log4j-api} on this branch.
+ * </p>
+ */
+public final class WrappedObjects {
+
+ private static final Method setObjectInputFilter;
+ private static final Method getObjectInputFilter;
+
+ static {
+ Method setMethod = null;
+ Method getMethod = null;
+ for (final Method method : ObjectInputStream.class.getMethods()) {
+ if (method.getName().equals("setObjectInputFilter")) {
+ setMethod = method;
+ } else if (method.getName().equals("getObjectInputFilter")) {
+ getMethod = method;
+ }
+ }
+ setObjectInputFilter = setMethod;
+ getObjectInputFilter = getMethod;
+ }
+
+ /**
+ * Writes an object as a byte array, falling back to {@code fallback} if
it cannot be serialized.
+ *
+ * @param obj The object to write.
+ * @param fallback Provides the value to write if {@code obj} cannot be
serialized.
+ * @param out The output stream.
+ */
+ public static void writeWrappedObject(
+ final Object obj, final Supplier<Serializable> fallback, final
ObjectOutputStream out) throws IOException {
+ out.writeObject(wrapObject(obj, fallback));
+ }
+
+ /**
+ * Reads an object written by {@link #writeWrappedObject}.
+ *
+ * @param in The input stream.
+ * @return The object or {@code null}.
+ */
+ public static Object readWrappedObject(final ObjectInputStream in) throws
IOException, ClassNotFoundException {
+ return unwrapObject(in, (byte[]) in.readObject());
+ }
+
+ private static byte[] wrapObject(final Object obj, final
Supplier<Serializable> fallback) throws IOException {
+ if (obj == null || obj instanceof Serializable) {
+ try {
+ return toByteArray((Serializable) obj);
+ } catch (final IOException | RuntimeException e) {
+ StatusLogger.getLogger()
+ .warn("Unable to serialize an object of type {}, using
a fallback value.", obj.getClass(), e);
+ }
+ }
+ return toByteArray(fallback.get());
+ }
+
+ private static byte[] toByteArray(final Serializable obj) throws
IOException {
+ final ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ try (final ObjectOutputStream oos = new ObjectOutputStream(bout)) {
+ oos.writeObject(obj);
+ }
+ return bout.toByteArray();
+ }
+
+ @SuppressFBWarnings(
+ value = "OBJECT_DESERIALIZATION",
+ justification = "The nested stream is filtered in the same way as
the outer stream.")
+ @SuppressWarnings("deprecation")
+ private static Object unwrapObject(final ObjectInputStream in, final
byte[] data) throws IOException {
+ final ByteArrayInputStream bin = new ByteArrayInputStream(data);
+ final ObjectInputStream ois = in instanceof FilteredObjectInputStream
+ ? new FilteredObjectInputStream(bin,
((FilteredObjectInputStream) in).getAllowedClasses())
+ : new ObjectInputStream(bin);
+ copyObjectInputFilter(in, ois);
Review Comment:
Sounds good to me. I will drop the deserialization commits from #4385 and
keep the other 2.26.2 fixes there.
##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/WrappedObjectsTest.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.logging.log4j.core.util.internal;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InvalidClassException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+import java.net.URI;
+import org.apache.logging.log4j.core.test.junit.Tags;
+import org.apache.logging.log4j.util.FilteredObjectInputStream;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledForJreRange;
+import org.junit.jupiter.api.condition.JRE;
+
+/**
+ * Tests that the nested stream of {@link WrappedObjects} applies the same
restrictions as the outer stream.
+ */
+@Tag(Tags.SERIALIZATION)
+class WrappedObjectsTest {
+
+ static final class Payload implements Serializable {
+ private static final long serialVersionUID = 1L;
+ }
+
+ @Test
+ void readWrappedObjectRoundTrip() throws Exception {
+ final byte[] data = writeWrapped(new Payload());
+
+ assertThat(WrappedObjects.readWrappedObject(new ObjectInputStream(new
ByteArrayInputStream(data))))
+ .isInstanceOf(Payload.class);
+ }
+
+ @Test
+ @SuppressWarnings("deprecation")
+ void readWrappedObjectUsesAllowlistOfOuterFilteredStream() throws
Exception {
+ // `Payload` is in `org.apache.logging.log4j.`, so it is only rejected
if the allowlist is used
Review Comment:
`Payload` is accepted in this test. The class the allowlist rejects is
`URI`. Can we drop this line?
```suggestion
```
--
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]