Skip to content

Commit

Permalink
修复CVE-2023-3276漏洞,XmlUtil.readBySax问题
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jun 16, 2023
1 parent 143cd59 commit 2a786af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
* 【extra 】 修复Sftp中exists方法父目录不存在时报错(issue#I7CSQ9@Gitee)
* 【extra 】 修复xml转json再转bean失败问题(issue#3139@Github)
* 【poi 】 修复RowUtil传入参数错误问题(issue#3139@Github)
* 【poi 】 修复XmlUtil.xmlToBean空节点转换失败问题(issue#3136@Github)
* 【core 】 修复XmlUtil.xmlToBean空节点转换失败问题(issue#3136@Github)
* 【core 】 修复CVE-2023-3276漏洞,XmlUtil.readBySax问题(issue#I7DX8W@Gitee)

-------------------------------------------------------------------------------------------------------------
# 5.8.19(2023-05-27)
Expand Down
20 changes: 20 additions & 0 deletions hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ public static void readBySax(InputSource source, ContentHandler contentHandler)
factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(namespaceAware);

// https://blog.spoock.com/2018/10/23/java-xxe/
try{
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);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (final Exception ignore){
// ignore
}
}
// 2.从解析工厂获取解析器
final SAXParser parse;
Expand All @@ -306,6 +316,16 @@ public static void readBySax(InputSource source, ContentHandler contentHandler)

// 3.得到解读器
reader = parse.getXMLReader();
// 防止XEE攻击,见:https://www.jianshu.com/p/1a857905b22c
// https://blog.spoock.com/2018/10/23/java-xxe/
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
// 忽略外部DTD
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
// 不包括外部一般实体。
reader.setFeature("http://xml.org/sax/features/external-general-entities",false);
// 不包含外部参数实体或外部DTD子集。
reader.setFeature("http://xml.org/sax/features/external-parameter-entities",false);

reader.setContentHandler(contentHandler);
reader.parse(source);
} catch (ParserConfigurationException | SAXException e) {
Expand Down

0 comments on commit 2a786af

Please sign in to comment.