Skip to content

Commit 197d7a5

Browse files
committed
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
1 parent b1aaad3 commit 197d7a5

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

framework/base/src/main/java/org/apache/ofbiz/base/util/UtilObject.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import java.io.IOException;
2424
import java.io.ObjectOutputStream;
2525
import java.lang.reflect.Array;
26+
import java.util.Arrays;
2627
import java.util.Iterator;
2728
import java.util.List;
2829
import java.util.ServiceLoader;
30+
import java.util.stream.Collectors;
2931

3032
import org.apache.ofbiz.base.lang.Factory;
3133
import org.apache.ofbiz.base.lang.SourceMonitored;
@@ -90,7 +92,10 @@ public static Object getObjectException(byte[] bytes) throws ClassNotFoundExcept
9092
"ListOfSafeObjectsForInputStream");
9193
List<String> listOfSafeObjects = null;
9294
if (UtilValidate.isNotEmpty(listOfSafeObjectsForInputStream)) {
93-
listOfSafeObjects = java.util.Arrays.asList(listOfSafeObjectsForInputStream);
95+
listOfSafeObjects = Arrays.stream(listOfSafeObjectsForInputStream.split(","))
96+
.map(String::trim)
97+
.filter(s -> !s.isEmpty())
98+
.collect(Collectors.toList());
9499
} else {
95100
listOfSafeObjects = java.util.Arrays.asList("byte\\[\\]", "foo", "SerializationInjector",
96101
"\\[Z","\\[B","\\[S","\\[I","\\[J","\\[F","\\[D","\\[C",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.ofbiz.base.util;
20+
21+
import static org.apache.ofbiz.base.util.UtilObject.getObjectException;
22+
import static org.hamcrest.Matchers.contains;
23+
import static org.junit.Assert.assertThat;
24+
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.ObjectOutputStream;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
31+
import org.junit.Test;
32+
33+
public class UtilObjectUnitTest {
34+
// Test reading a basic list of string object.
35+
@Test
36+
public void testGetObjectExceptionSafe() throws IOException, ClassNotFoundException {
37+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
38+
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
39+
List<String> allowedObject = Arrays.asList("foo", "bar", "baz");
40+
oos.writeObject(allowedObject);
41+
List<String> readObject = UtilGenerics.cast(getObjectException(bos.toByteArray()));
42+
assertThat(readObject, contains("foo", "bar", "baz"));
43+
}
44+
}
45+
46+
// Test reading a valid customized list of string object.
47+
@Test
48+
public void testGetObjectExceptionCustomized() throws IOException, ClassNotFoundException {
49+
UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream",
50+
"java.util.Arrays.ArrayList,java.lang.String");
51+
testGetObjectExceptionSafe();
52+
53+
// With extra whitespace
54+
UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream",
55+
"java.util.Arrays.ArrayList, java.lang.String");
56+
testGetObjectExceptionSafe();
57+
}
58+
59+
// Test reading a basic list of string object after forbidding such kind of objects.
60+
@Test(expected = ClassCastException.class)
61+
public void testGetObjectExceptionUnsafe() throws IOException, ClassNotFoundException {
62+
// Only allow object of type where the package prefix is 'org.apache.ofbiz'
63+
UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream",
64+
"org.apache.ofbiz..*");
65+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
66+
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
67+
List<String> forbiddenObject = Arrays.asList("foo", "bar", "baz");
68+
oos.writeObject(forbiddenObject);
69+
getObjectException(bos.toByteArray());
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)