为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复 java 中的“禁用 XML 外部实体 (XXE) 处理”漏洞

如何修复 java 中的“禁用 XML 外部实体 (XXE) 处理”漏洞

ABOUTYOU 2023-02-23 10:55:11
我针对 sonarqube 运行我的 java 代码,我得到了“禁用 XML 外部实体 (XXE) 处理”作为漏洞。我花了一些时间在谷歌上解决这个问题。我一直在尝试很多方法,但没有任何方法适合我。我不知道我错过了什么我的代码:        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();        docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);        docFactory.setFeature(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);        docFactory.setFeature(XMLInputFactory.SUPPORT_DTD, false);        docFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);        docFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);        docFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);        docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();        final Document doc = docBuilder.parse(filepath);我正在使用 java 1.8,感谢任何帮助。谢谢
查看完整描述

4 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

我最终添加了以下所有属性,以避免 Sonar 抱怨此漏洞:


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();


        //REDHAT

        //https://www.blackhat.com/docs/us-15/materials/us-15-Wang-FileCry-The-New-Age-Of-XXE-java-wp.pdf

        factory.setAttribute(XMLConstants.FEATURE_SECURE_PROCESSING, true);

        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");

        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");


        //OWASP

        //https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html

        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

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

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

        // Disable external DTDs as well

        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        // and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"

        factory.setXIncludeAware(false);

        factory.setExpandEntityReferences(false);


        DocumentBuilder builder = factory.newDocumentBuilder();


查看完整回答
反对 回复 2023-02-23
?
繁星coding

TA贡献1797条经验 获得超4个赞

Java 9+ 解决方案:

对我来说,更改DocumentBuilderFactory.newInstance()DocumentBuilderFactory.newDefaultInstance()足以消除此警告。


查看完整回答
反对 回复 2023-02-23
?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

只需设置这两个属性就足够了:

factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);


查看完整回答
反对 回复 2023-02-23
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

我通过添加以下代码片段解决了这个问题:


saxParserFactory = SAXParserFactory.newInstance();

saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

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

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

saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

saxParserFactory.setXIncludeAware(false);


查看完整回答
反对 回复 2023-02-23
  • 4 回答
  • 0 关注
  • 345 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号