Skip to content

Commit 20f8f53

Browse files
committed
Reverted: commit 6e8d862
The commit 6e8d862, "Fixed: Remove support for serialization and deserialization of custom Java objects in XmlSerializer for security reasons", implemented a defense-in-depth against deserialization attacks by disabling suport for custom objects. While deserializing custom objects, without proper checks, is not recommended, and we should consider to abandon this feature, this commit re-adds it, because of some regressions (e.g. errors scheduling email notifications for sales orders).
1 parent f82bca8 commit 20f8f53

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

framework/entity/src/main/java/org/apache/ofbiz/entity/serialize/XmlSerializer.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.FileNotFoundException;
2222
import java.io.IOException;
23+
import java.io.Serializable;
2324
import java.lang.ref.WeakReference;
2425
import java.math.BigDecimal;
2526
import java.math.RoundingMode;
@@ -47,8 +48,10 @@
4748
import javax.xml.parsers.ParserConfigurationException;
4849

4950
import org.apache.ofbiz.base.util.Debug;
51+
import org.apache.ofbiz.base.util.StringUtil;
5052
import org.apache.ofbiz.base.util.UtilGenerics;
5153
import org.apache.ofbiz.base.util.UtilMisc;
54+
import org.apache.ofbiz.base.util.UtilObject;
5255
import org.apache.ofbiz.base.util.UtilXml;
5356
import org.apache.ofbiz.entity.Delegator;
5457
import org.apache.ofbiz.entity.GenericPK;
@@ -256,11 +259,18 @@ public static Element serializeSingle(Object object, Document document) throws S
256259
}
257260

258261
public static Element serializeCustom(Object object, Document document) throws SerializeException {
259-
Debug.logError("Serialization of custom Java objects (cus-obj) is no longer supported. "
260-
+ "This feature has been removed for security reasons. Object class: "
261-
+ object.getClass().getName(), MODULE);
262-
throw new SerializeException("Serialization of custom Java objects is not supported. "
263-
+ "Object class: " + object.getClass().getName());
262+
if (object instanceof Serializable) {
263+
byte[] objBytes = UtilObject.getBytes(object);
264+
if (objBytes == null) {
265+
throw new SerializeException("Unable to serialize object; null byte array returned");
266+
}
267+
String byteHex = StringUtil.toHexString(objBytes);
268+
Element element = document.createElement("cus-obj");
269+
// this is hex encoded so does not need to be in a CDATA block
270+
element.appendChild(document.createTextNode(byteHex));
271+
return element;
272+
}
273+
throw new SerializeException("Cannot serialize object of class " + object.getClass().getName());
264274
}
265275

266276
public static Element makeElement(String elementName, Object value, Document document) {
@@ -457,9 +467,17 @@ public static Object deserializeSingle(Element element, Delegator delegator) thr
457467
public static Object deserializeCustom(Element element) throws SerializeException {
458468
String tagName = element.getLocalName();
459469
if ("cus-obj".equals(tagName)) {
460-
Debug.logError("Deserialization of cus-obj elements is no longer supported. "
461-
+ "This feature has been removed for security reasons.", MODULE);
462-
throw new SerializeException("Deserialization of cus-obj elements is not supported.");
470+
String value = UtilXml.elementValue(element);
471+
if (value != null) {
472+
byte[] valueBytes = StringUtil.fromHexString(value);
473+
if (valueBytes != null) {
474+
Object obj = UtilObject.getObject(valueBytes);
475+
if (obj != null) {
476+
return obj;
477+
}
478+
}
479+
}
480+
throw new SerializeException("Problem deserializing object from byte array + " + element.getLocalName());
463481
}
464482
throw new SerializeException("Cannot deserialize element named " + element.getLocalName());
465483
}

0 commit comments

Comments
 (0)