java - 如何修复 Java 中的“禁用 XML 外部实体 (XXE) 处理”漏洞
问题描述
我针对 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,任何帮助表示赞赏。谢谢
解决方案
我最终添加了以下所有属性,以避免 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();
推荐阅读
- amazon-web-services - 无法让 AWS API Gateway 覆盖响应代码 - FitBit 端点验证
- asp.net-core - ASP.Net Core 3.0 UserManager UpdateSecurityStampAsync 乐观并发失败,对象已修改
- python - 如何 SUMIF 在 Pandas 中包含部分字符串
- r - 警告消息:在 read.table(file = file, header = header, sep = sep, quote = quote, : readTableHeader 在 'hola.csv' 上找到的不完整的最后一行
- mediawiki - 维基/页面排名器的内部链接结构
- reactjs - 如何在 react-table V7 中禁用对一列的分组
- java - Oracle Eclipse IDE for Java Dev 2019-09 Windowbuilder 崩溃 IDE
- sanity - 按对象数组中的字段值过滤查询结果 [Sanity.io & GROQ]
- json - 如何使用 rest 模板将 application/atom+xml 响应转换为 json
- java - IBM 云存储客户端