12
12
import javax .xml .bind .Marshaller ;
13
13
import javax .xml .bind .Unmarshaller ;
14
14
import javax .xml .bind .annotation .XmlRootElement ;
15
+ import javax .xml .parsers .ParserConfigurationException ;
16
+ import javax .xml .parsers .SAXParserFactory ;
17
+ import javax .xml .transform .Source ;
18
+ import javax .xml .transform .sax .SAXSource ;
15
19
16
20
import org .apache .commons .logging .Log ;
17
21
import org .apache .commons .logging .LogFactory ;
22
+ import org .xml .sax .InputSource ;
23
+ import org .xml .sax .SAXException ;
24
+ import org .xml .sax .SAXNotRecognizedException ;
25
+ import org .xml .sax .SAXNotSupportedException ;
18
26
19
27
/**
20
28
* Helper methods for serializing and de-serializing to XML using JAXB
@@ -80,11 +88,25 @@ public static synchronized <T extends Serializable> String getXml(T entity) thro
80
88
* @param <T> class that implements Serializable
81
89
* @return T De-serialized object
82
90
* @throws JAXBException if errors during de-serialization
91
+ * @throws ParserConfigurationException
92
+ * @throws SAXException
83
93
*/
84
94
@ SuppressWarnings ("unchecked" )
85
- public static synchronized <T extends Serializable > T create (String xml , Class <T > classType ) throws JAXBException
95
+ public static synchronized <T extends Serializable > T create (String xml , Class <T > classType ) throws JAXBException , ParserConfigurationException , SAXException
86
96
{
87
97
T entity = null ;
98
+
99
+ //Disable XXE
100
+ SAXParserFactory spf = SAXParserFactory .newInstance ();
101
+ spf .setNamespaceAware (true );
102
+ spf .setValidating (true );
103
+ spf .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
104
+ spf .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
105
+ spf .setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd" , false );
106
+
107
+ //Do unmarshall operation
108
+ Source xmlSource = new SAXSource (spf .newSAXParser ().getXMLReader (), new InputSource (new StringReader (xml )));
109
+
88
110
//make sure we have not null and not-empty string to de-serialize
89
111
if ( null != xml && !xml .trim ().isEmpty ())
90
112
{
@@ -102,7 +124,7 @@ public static synchronized <T extends Serializable> T create(String xml, Class<T
102
124
{
103
125
Unmarshaller um = response_ctx .createUnmarshaller ();
104
126
try {
105
- Object unmarshaled = um .unmarshal (new StringReader ( xml ) );
127
+ Object unmarshaled = um .unmarshal (xmlSource );
106
128
if ( null != unmarshaled )
107
129
{
108
130
try {
0 commit comments