Recently, we had a security audit on our code, and one of the problem is that our application is subject to the Xml eXternal Entity (XXE) attack.
Basically, the application is a calculator that receives inputs as XML, through a Web-Service.
Here is an example of such an XXE attack on our application:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<foo:calculateStuff>
<!--Optional:-->
<xmlInput><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE currency [
<!ENTITY include SYSTEM "file:///d:/" >]>
<calcinput>...</calcinput>
]]></xmlInput>
</foo:calculateStuff>
</soapenv:Body>
</soapenv:Envelope>
As you can see, we can refer to an entity that points to an external file ("file:///d:/"
).
Regarding the XML input itself (the <calcinput>...</calcinput>
part) is unmarshalled with JAXB (v2.1). The web-service part is based on jaxws-rt (2.1).
What do I need to do to secure my web-service?
JAXB
You can prevent the Xml eXternal Entity (XXE) attack by unmarshalling from an
XMLStreamReader
that has theIS_SUPPORTING_EXTERNAL_ENTITIES
and/orXMLInputFactory.SUPPORT_DTD
properties set tofalse
.JAX-WS
A JAX-WS implementation should take care of this for you. If it doesn't I would recommend opening a bug against the specific implmententation.
EXAMPLE
Demo
input.xml
This XML document contains an entity that has been setup to get the listing of files I used to create this example.
Customer
Output - Default Configuration
By default the entity will be resolved.
Output when
XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES
property is set tofalse
When this property is set the entity is not resolved.
Output when
XMLInputFactory.SUPPORT_DTD
property is set tofalse
When this property is set an exception is thrown trying to resolve the entity.