XXE修复方案参考

XXE不同的库修复代码,略有差别,但都是通过:

1、禁止加载外部实体;
2、不允许XML中含有任何自己声明的DTD。可以解决


    例1: //DOM Read XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();     
            
            /*以下为修复代码*/ 
            //https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#Java
        
            //禁用DTDs (doctypes),几乎可以防御所有xml实体攻击
            dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); //首选
            
            //如果不能禁用DTDs,可以使用下两项,必须两项同时存在
            dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);        //防止外部实体POC 
            dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);   //防止参数实体POC
            /*以上为修复代码*/    
            
            DocumentBuilder db = dbf.newDocumentBuilder();        
            Document doc = db.parse(request.getInputStream());
例2:    //DOM4J Read XML
            SAXReader saxReader = new SAXReader();
                    
            /*以下为修复代码*/ 
            //https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#Java
        
            //禁用DTDs (doctypes),几乎可以防御所有xml实体攻击
            saxReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); //首选
            
            //如果不能禁用DTDs,可以使用下两项,必须两项同时存在
            saxReader.setFeature("http://xml.org/sax/features/external-general-entities", false);        //防止外部实体POC 
            saxReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);   //防止参数实体POC
            /*以上为修复代码*/    
            
            Document document = saxReader.read(request.getInputStream());


例3:   //JDOM2 Read XML    
            SAXBuilder builder = new SAXBuilder();    
            
            /*以下为修复代码*/ 
            //https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#Java
        
            //禁用DTDs (doctypes),几乎可以防御所有xml实体攻击
            builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); //首选
            
            //如果不能禁用DTDs,可以使用下两项,必须两项同时存在
            builder.setFeature("http://xml.org/sax/features/external-general-entities", false);        //防止外部实体POC 
            builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);   //防止参数实体POC
            /*以上为修复代码*/    
            
            
            Document document = builder.build(request.getInputStream());
例4:        //SAX Read XML
            SAXParserFactory factory  = SAXParserFactory.newInstance(); 
                    
            /*以下为修复代码*/ 
            //https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#Java
        
            //禁用DTDs (doctypes),几乎可以防御所有xml实体攻击
            factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); //首选
            
            //如果不能禁用DTDs,可以使用下两项,必须两项同时存在
            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);        //防止外部实体POC 
            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);   //防止参数实体POC
            /*以上为修复代码*/    
            
            
            SAXParser saxparser = factory.newSAXParser();  
            SAXHandler handler = new SAXHandler();      
            saxparser.parse(request.getInputStream(), handler); 

你可能感兴趣的:(安全测试)