SV.XXE.XIF

Possibility of an XML External Entity attack

This error occurs when XML input is processed by a weakly-configured XML parser, XMLInputFactory.

Vulnerability and risk

An XML external entity attack is a type of attack against an application that parses XML input. This attack occurs when XML input containing a reference to an external entity is processed by a weakly-configured XML parser. This attack may lead to the disclosure of confidential data, denial of service, server-side request forgery, port scanning from the perspective of the machine where the parser is located, or other system impacts.

Mitigation and prevention

The safest way to prevent an XML external entity attack is to always completely disable DTDs (external entities). Depending on the parser, the method can be different.

Vulnerable code example

Copy
   import javax.xml.stream.*;
   import java.io.InputStream;
   
   public class Test {
       public void test(InputStream stream) throws XMLStreamException {
           final XMLInputFactory factory = XMLInputFactory.newInstance();
           factory.createXMLStreamReader(stream);
       }    
   }

In this example, Klocwork reports an SV.XXE.XIF error at line 6, indicating that XML input is processed by a weakly configured XML parser 'XMLInputFactory'.

Fixed code example

Copy
   import javax.xml.stream.*;
   import java.io.InputStream;
   
   public class Test {
       public void test(InputStream stream) throws XMLStreamException {
           final XMLInputFactory factory = XMLInputFactory.newInstance();
           factory.setProperty("javax.xml.stream.supportDTD", false);
           factory.setProperty("javax.xml.stream.isSupportingExternalEntities", false);
           factory.createXMLStreamReader(stream);
      }    
  }

Security training

Application security training materials provided by Secure Code Warrior.