Skip to content

Commit

Permalink
[COLLECTIONS-580] Add fix for PrototypeFactory as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Nov 11, 2015
1 parent bce4d02 commit d9a0013
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public class PrototypeFactory {
* <li>public copy constructor
* <li>serialization clone
* <ul>
* <p>
* <b>WARNING:</b> from v3.2.2 onwards this method will return a {@code Factory}
* that will throw an {@link UnsupportedOperationException} when trying to serialize
* or de-serialize it to prevent potential remote code execution exploits.
* <p>
* In order to re-enable serialization support the following system property
* can be used (via -Dproperty=true):
* <pre>
* org.apache.commons.collections.enableUnsafeSerialization
* </pre>
*
* @param prototype the object to clone each time in the factory
* @return the <code>prototype</code> factory
Expand Down Expand Up @@ -144,6 +154,24 @@ public Object create() {
throw new FunctorException("PrototypeCloneFactory: Clone method threw an exception", ex);
}
}

/**
* Overrides the default writeObject implementation to prevent
* serialization (see COLLECTIONS-580).
*/
private void writeObject(ObjectOutputStream os) throws IOException {
FunctorUtils.checkUnsafeSerialization(PrototypeCloneFactory.class);
os.defaultWriteObject();
}

/**
* Overrides the default readObject implementation to prevent
* de-serialization (see COLLECTIONS-580).
*/
private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException {
FunctorUtils.checkUnsafeSerialization(PrototypeCloneFactory.class);
is.defaultReadObject();
}
}

// PrototypeSerializationFactory
Expand Down Expand Up @@ -204,6 +232,24 @@ public Object create() {
}
}
}

/**
* Overrides the default writeObject implementation to prevent
* serialization (see COLLECTIONS-580).
*/
private void writeObject(ObjectOutputStream os) throws IOException {
FunctorUtils.checkUnsafeSerialization(PrototypeSerializationFactory.class);
os.defaultWriteObject();
}

/**
* Overrides the default readObject implementation to prevent
* de-serialization (see COLLECTIONS-580).
*/
private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException {
FunctorUtils.checkUnsafeSerialization(PrototypeSerializationFactory.class);
is.defaultReadObject();
}
}

}
2 changes: 2 additions & 0 deletions src/java/org/apache/commons/collections/functors/package.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<li>InstantiateFactory</li>
<li>InstantiateTransformer</li>
<li>InvokerTransformer</li>
<li>PrototypeFactory$PrototypeCloneFactory</li>
<li>PrototypeFactory$PrototypeSerializationFactory</li>
<li>WhileClosure</li>
</ul>
<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static Test suite() {
suite.addTest(TestInstantiateTransformer.suite());
suite.addTest(TestInstantiateFactory.suite());
suite.addTest(TestInvokerTransformer.suite());
suite.addTest(TestPrototypeFactory.suite());
suite.addTest(TestWhileClosure.suite());
return suite;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.commons.collections.functors;

import java.util.ArrayList;

import org.apache.commons.collections.Factory;

import junit.framework.Test;
import junit.framework.TestSuite;

public class TestPrototypeFactory extends AbstractTestSerialization {

// conventional
// ------------------------------------------------------------------------

public TestPrototypeFactory(String testName) {
super(testName);
}

public static Test suite() {
return new TestSuite(TestPrototypeFactory.class);
}

// ------------------------------------------------------------------------

public Object makeObject() {
return PrototypeFactory.getInstance(new ArrayList());
}

public Class getTestClass() {
return Factory.class;
}

}

0 comments on commit d9a0013

Please sign in to comment.