How to Prevent XML External Entity Injection on TransformerFactory

后端 未结 2 996
醉梦人生
醉梦人生 2021-02-04 17:40

My problem:

Fortify 4.2.1 is marking below code as susceptible for XML External Entities attack.

TransformerFactory factory = Transform         


        
2条回答
  •  野的像风
    2021-02-04 17:53

    Because of lot of xml parsing engines in the market, each of it has its own mechanism to disable External entity injection. Please refer to the documentation of your engine. Below is an example to prevent it when using a SAX parser.

    The funda is to disallow DOCTYPE declaration. However if it is required disabling external general entities and external parameter entities will not trick the underlying SAX parser to XXE injection.

    public class MyDocumentBuilderFactory{
    
        public static DocumentBuilderFactory newDocumentBuilderFactory(){
    
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    
            try{
    
                documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
                documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities",false);
                documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities",false);
    
            }catch(ParserConfigurationException exp){
                exp.printStackTrace();
            }
    
            return documentBuilderFactory;
        }
    }
    

提交回复
热议问题