|
20 | 20 |
|
21 | 21 | import javax.xml.bind.JAXBException; |
22 | 22 | import javax.xml.bind.Unmarshaller; |
| 23 | +import javax.xml.parsers.ParserConfigurationException; |
| 24 | +import javax.xml.parsers.SAXParserFactory; |
| 25 | +import javax.xml.transform.Source; |
| 26 | +import javax.xml.transform.sax.SAXSource; |
23 | 27 |
|
24 | 28 | import feign.Response; |
25 | 29 | import feign.Util; |
26 | 30 | import feign.codec.DecodeException; |
27 | 31 | import feign.codec.Decoder; |
| 32 | +import org.xml.sax.InputSource; |
| 33 | +import org.xml.sax.SAXException; |
28 | 34 |
|
29 | 35 | /** |
30 | 36 | * Decodes responses using JAXB. <br> <p> Basic example with with Feign.Builder: </p> |
@@ -57,11 +63,25 @@ public Object decode(Response response, Type type) throws IOException { |
57 | 63 | throw new UnsupportedOperationException( |
58 | 64 | "JAXB only supports decoding raw types. Found " + type); |
59 | 65 | } |
| 66 | + |
| 67 | + |
60 | 68 | try { |
| 69 | + SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
| 70 | + /* Explicitly control sax configuration to prevent XXE attacks */ |
| 71 | + saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); |
| 72 | + saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); |
| 73 | + saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false); |
| 74 | + saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); |
| 75 | + |
| 76 | + Source source = new SAXSource(saxParserFactory.newSAXParser().getXMLReader(), new InputSource(response.body().asInputStream())); |
61 | 77 | Unmarshaller unmarshaller = jaxbContextFactory.createUnmarshaller((Class) type); |
62 | | - return unmarshaller.unmarshal(response.body().asInputStream()); |
| 78 | + return unmarshaller.unmarshal(source); |
63 | 79 | } catch (JAXBException e) { |
64 | 80 | throw new DecodeException(e.toString(), e); |
| 81 | + } catch (ParserConfigurationException e) { |
| 82 | + throw new DecodeException(e.toString(), e); |
| 83 | + } catch (SAXException e) { |
| 84 | + throw new DecodeException(e.toString(), e); |
65 | 85 | } finally { |
66 | 86 | if (response.body() != null) { |
67 | 87 | response.body().close(); |
|
0 commit comments