Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed: Handle whitelist of serializable classes from properties
(OFBIZ-11261)

There was a bug regarding the way the ‘ListOfSafeObjectsForInputStream’ value
defined in the “SafeObjectInputStream.properties” file was handled.  Mistakenly
only one class identifier was allowed.

Some unit tests have been added to check that the identified bug is fixed.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1869001 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 26, 2019
1 parent b1aaad3 commit 197d7a5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Expand Up @@ -23,9 +23,11 @@
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

import org.apache.ofbiz.base.lang.Factory;
import org.apache.ofbiz.base.lang.SourceMonitored;
Expand Down Expand Up @@ -90,7 +92,10 @@ public static Object getObjectException(byte[] bytes) throws ClassNotFoundExcept
"ListOfSafeObjectsForInputStream");
List<String> listOfSafeObjects = null;
if (UtilValidate.isNotEmpty(listOfSafeObjectsForInputStream)) {
listOfSafeObjects = java.util.Arrays.asList(listOfSafeObjectsForInputStream);
listOfSafeObjects = Arrays.stream(listOfSafeObjectsForInputStream.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
} else {
listOfSafeObjects = java.util.Arrays.asList("byte\\[\\]", "foo", "SerializationInjector",
"\\[Z","\\[B","\\[S","\\[I","\\[J","\\[F","\\[D","\\[C",
Expand Down
@@ -0,0 +1,72 @@
/*
* 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.ofbiz.base.util;

import static org.apache.ofbiz.base.util.UtilObject.getObjectException;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class UtilObjectUnitTest {
// Test reading a basic list of string object.
@Test
public void testGetObjectExceptionSafe() throws IOException, ClassNotFoundException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
List<String> allowedObject = Arrays.asList("foo", "bar", "baz");
oos.writeObject(allowedObject);
List<String> readObject = UtilGenerics.cast(getObjectException(bos.toByteArray()));
assertThat(readObject, contains("foo", "bar", "baz"));
}
}

// Test reading a valid customized list of string object.
@Test
public void testGetObjectExceptionCustomized() throws IOException, ClassNotFoundException {
UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream",
"java.util.Arrays.ArrayList,java.lang.String");
testGetObjectExceptionSafe();

// With extra whitespace
UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream",
"java.util.Arrays.ArrayList, java.lang.String");
testGetObjectExceptionSafe();
}

// Test reading a basic list of string object after forbidding such kind of objects.
@Test(expected = ClassCastException.class)
public void testGetObjectExceptionUnsafe() throws IOException, ClassNotFoundException {
// Only allow object of type where the package prefix is 'org.apache.ofbiz'
UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream",
"org.apache.ofbiz..*");
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
List<String> forbiddenObject = Arrays.asList("foo", "bar", "baz");
oos.writeObject(forbiddenObject);
getObjectException(bos.toByteArray());
}
}
}

0 comments on commit 197d7a5

Please sign in to comment.