Skip to content

Commit e0737bb

Browse files
committed
TRUNK-6218: Defer setup of xstream security until needed
1 parent d37011a commit e0737bb

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

api/src/main/java/org/openmrs/module/reporting/serializer/ReportingSerializer.java

+45-35
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@
99
*/
1010
package org.openmrs.module.reporting.serializer;
1111

12-
import java.io.OutputStream;
13-
import java.io.OutputStreamWriter;
14-
import java.io.UnsupportedEncodingException;
15-
import java.lang.reflect.Method;
16-
17-
import org.openmrs.api.APIException;
18-
import org.openmrs.api.context.Context;
19-
import org.openmrs.module.VersionComparator;
20-
import org.openmrs.module.serialization.xstream.XStreamShortSerializer;
21-
import org.openmrs.module.serialization.xstream.mapper.CGLibMapper;
22-
import org.openmrs.module.serialization.xstream.mapper.HibernateCollectionMapper;
23-
import org.openmrs.module.serialization.xstream.mapper.JavassistMapper;
24-
import org.openmrs.module.serialization.xstream.mapper.NullValueMapper;
25-
import org.openmrs.serialization.SerializationException;
26-
import org.openmrs.serialization.SimpleXStreamSerializer;
27-
2812
import com.thoughtworks.xstream.XStream;
2913
import com.thoughtworks.xstream.converters.ConverterLookup;
3014
import com.thoughtworks.xstream.converters.DataHolder;
@@ -33,12 +17,29 @@
3317
import com.thoughtworks.xstream.io.xml.DomDriver;
3418
import com.thoughtworks.xstream.mapper.Mapper;
3519
import com.thoughtworks.xstream.mapper.MapperWrapper;
36-
import org.openmrs.util.OpenmrsConstants;
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
22+
import org.openmrs.api.context.Context;
23+
import org.openmrs.module.serialization.xstream.XStreamShortSerializer;
24+
import org.openmrs.module.serialization.xstream.mapper.CGLibMapper;
25+
import org.openmrs.module.serialization.xstream.mapper.HibernateCollectionMapper;
26+
import org.openmrs.module.serialization.xstream.mapper.JavassistMapper;
27+
import org.openmrs.module.serialization.xstream.mapper.NullValueMapper;
28+
import org.openmrs.serialization.SerializationException;
29+
import org.openmrs.serialization.SimpleXStreamSerializer;
3730

31+
import java.io.OutputStream;
32+
import java.io.OutputStreamWriter;
33+
import java.io.UnsupportedEncodingException;
34+
import java.lang.reflect.Method;
3835

3936
public class ReportingSerializer extends XStreamShortSerializer {
4037

4138
private static ThreadLocal<DataHolder> cache = new ThreadLocal<DataHolder>();
39+
40+
private final Log log = LogFactory.getLog(this.getClass());
41+
42+
private boolean xstreamSecuritySetup = false;
4243

4344
/**
4445
* @throws SerializationException
@@ -87,15 +88,14 @@ public Object unmarshal(HierarchicalStreamReader reader, Object root) {
8788
xstream.registerConverter(new IndicatorConverter(mapper, converterLookup));
8889

8990
xstream.registerConverter(new ReportDefinitionConverter(mapper, converterLookup));
90-
91-
// Only setup XStreamSecurity only on versions that are after 2.7.0
92-
if (new VersionComparator().compare(OpenmrsConstants.OPENMRS_VERSION, "2.7.0") >= 0) {
93-
setupXStreamSecurity(xstream);
94-
}
9591
}
9692

9793
@Override
9894
synchronized public <T> T deserialize(String serializedObject, Class<? extends T> clazz) throws SerializationException {
95+
if (!xstreamSecuritySetup) {
96+
setupXStreamSecurity();
97+
xstreamSecuritySetup = true;
98+
}
9999
boolean cacheOwner = cache.get() == null;
100100
if (cacheOwner) {
101101
cache.set(new MapBackedDataHolder());
@@ -123,21 +123,31 @@ public void serializeToStream(Object object, OutputStream out) {
123123
}
124124
}
125125

126-
private void setupXStreamSecurity(XStream xstream) throws SerializationException {
126+
/**
127+
* Sets up xstream security on the Reporting Serializer to match the OpenMRS core security configuration
128+
*/
129+
public void setupXStreamSecurity() throws SerializationException {
130+
log.debug("Setting up xstream security on ReportingSerializer");
131+
SimpleXStreamSerializer serializer = null;
127132
try {
128-
SimpleXStreamSerializer serializer = Context.getRegisteredComponent("simpleXStreamSerializer", SimpleXStreamSerializer.class);
129-
if (serializer != null) {
130-
try {
131-
Method method = serializer.getClass().getMethod("initXStream", XStream.class);
132-
method.invoke(serializer, xstream);
133-
}
134-
catch (Exception ex) {
135-
throw new SerializationException("Failed to set up XStream Security", ex);
136-
}
137-
}
133+
serializer = Context.getRegisteredComponent("simpleXStreamSerializer", SimpleXStreamSerializer.class);
134+
}
135+
catch (Exception ignored) {
136+
}
137+
if (serializer == null) {
138+
log.debug("Not setting up XStream security as no simpleXStreamSerializer component is found");
139+
return;
140+
}
141+
try {
142+
Method method = serializer.getClass().getMethod("initXStream", XStream.class);
143+
method.invoke(serializer, xstream);
144+
log.info("XStream security initialized on ReportingSerializer");
145+
}
146+
catch (NoSuchMethodException ignored) {
147+
log.debug("Not setting up XStream Security as no initXStream method found on SimpleXStreamSerializer");
138148
}
139-
catch (APIException ex) {
140-
//Ignore APIException("Error during getting registered component) for platform versions below 2.7.0
149+
catch (Exception e) {
150+
throw new SerializationException("Failed to set up XStream Security on Reporting Serializer", e);
141151
}
142152
}
143153
}

omod/src/main/resources/config.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@
155155
</globalProperty>
156156
<globalProperty>
157157
<property>reporting.serializer.whitelist.types</property>
158-
<defaultValue>org.openmrs.module.reporting.evaluation.parameter.Parameter,org.openmrs.module.reporting.data.converter.PropertyConverter,org.openmrs.module.reporting.data.converter.ObjectFormatter,org.openmrs.module.reporting.data.converter.DateConverter,org.openmrs.module.reporting.data.converter.ChainedConverter,org.openmrs.module.reporting.common.SortCriteria$SortElement,org.openmrs.module.reporting.evaluation.parameter.Mapped</defaultValue>
159-
<description></description>
158+
<defaultValue>org.openmrs.module.reporting.**</defaultValue>
159+
<description>White-list of classes that are allowed for deserialization by the reporting module</description>
160160
</globalProperty>
161161

162162
<!-- Message Properties -->

0 commit comments

Comments
 (0)