Fortify fix for XML External Entity Injection
Asked Answered
U

5

9

When I do scan using fortify tool, I got some issues under "XML External Entity Injection".

TransformerFactory trfactory = TransformerFactory.newInstance(); 

This is the place where it is showing error. I have given the below fix as suggested by fortify

trfactory.setFeature("http://xml.org/sax/features/external-general-entities", false); 
trfactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); 

but still the issues are not fixed. How to fix this issue?

Urolith answered 7/7, 2016 at 13:49 Comment(1)
Please share any suggestions.Urolith
S
4
TransformerFactory trfactory = TransformerFactory.newInstance();
trfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
trfactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
trfactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");

This would be sufficient.

Spirula answered 4/8, 2017 at 9:48 Comment(2)
This worked for me, thanks ): . Before this, I tried the solution suggested by Fortify and that didn't worked.Casi
@AlessandroIudicone Link to the solution suggested by Fortify please!Mammoth
H
1

Sometime it will not work if java version is not compatible.

if (javaVersion > 1.6) {
        dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
        dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
      }
else {
        if (javaVersion > 1.5) {
          dbf.setFeature("http://xerces.apache.org/xerces2-j/features.html#external-general-entities", false);
          dbf.setFeature("http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities", false);
        }
else {
          dbf.setFeature("http://xerces.apache.org/xerces-j/features.html#external-general-entities", false);
          dbf.setFeature("http://xerces.apache.org/xerces-j/features.html#external-parameter-entities", false);
        }
 }

It worked for me :-)

Hypophyge answered 17/9, 2016 at 10:45 Comment(3)
is it working ? do we need to fix for Transformer or Document Builder Factory As part of security fortify issue??Sawn
yes it is working.The providing fixes are wrt the place where it is showing error in fortify tool, in my case I fix it for Document Builder Factory @laxHypophyge
encode for sql from esapi might be the correct fix for this issueStonemason
U
0

I tried with "Xalan" implementation class instead of TransformerFactory.newInstance().It worked for me and fortify issue got fixed

        TransformerFactoryImpl transformerFactoryImpl = new TransformerFactoryImpl();
        Transformer transformer = transformerFactoryImpl.newTransformer();
Urolith answered 14/7, 2016 at 15:43 Comment(0)
M
0

You can also try:

    TransformerFactoryImpl transformerFactoryImpl = new TransformerFactoryImpl();
    Transformer transformer = transformerFactoryImpl.newTransformer();
    transformer.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Manual answered 23/9, 2016 at 10:46 Comment(1)
is it working ? do we need to fix for Transformer or Document Builder Factory As part of security fortify issue??Sawn
W
0

Add this line. It worked for me.

factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
Wittie answered 24/2, 2020 at 7:28 Comment(1)
this will throw an exception instead of processing a request whenever you include doctypeGoodrow

© 2022 - 2024 — McMap. All rights reserved.