java:S2755: XML parsers should not be vulnerable to XXE attacks

Our code is triggering java:S2755 but I think it’s a false positive.

Here is the relevant piece of code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( new InputSource( new StringReader( cr.getEntity(String.class) ) ) ); 

The issue is reported on the first line (creating the factory).

Notice how we’re setting the secure feature XMLConstants.FEATURE_SECURE_PROCESSING.
I found this feature described in this Java 13 security doc: Java API for XML Processing (JAXP) Security Guide.
The doc says that setting this feature explicitly is equivalent to setting “external access restrictions … to empty string”, which appears to be equivalent to the suggestion by SonarQube.
It’s also mentioned in the Java 8 Javadoc, although there’s no mention of external access restrictions…
However, in the SonarQube suggestions for java:S2755, there is no mention of the FEATURE_SECURE_PROCESSING feature anywhere. Neither is it mentioned in the OSWAP cheatsheet doc that the latest SonarQube java scanner was basing its suggestions off of (from this post).

I’m confused – is there something wrong with just using the FEATURE_SECURE_PROCESSING feature? Or is this a False Positive?


SonarQube server version: 9.6.1 (build 59531)
Gradle plugin version: 3.4.0.2513
Gradle Java version: 11
Source code JDK version: 1.8

I’ve found mention of this in the OWASP cheatsheet now:

Since the JDK 6, the flag FEATURE_SECURE_PROCESSING can be used to instruct the implementation of the parser to process XML securely. Its behaviour is implementation dependent. Even if it can help tackling resource exhaustion, it may not always mitigate entity expansion. More details on this flag can be found here.

And then again, in the example of the alternative secure configuration:

    // As stated in the documentation "Feature for Secure Processing (FSP)" is the central mechanism to 
    // help safeguard XML processing. It instructs XML processors, such as parsers, validators, 
    // and transformers, to try and process XML securely. This can be used as an alternative to
    // dbf.setExpandEntityReferences(false); to allow some safe level of Entity Expansion
    // Exists from JDK6.
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);