- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在开发一个安卓应用
android:minSdkVersion="14"
在这个需要解析 xml 的应用程序中。为此,我正在使用这样的 DOM 解析器
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
Document doc = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
但是当检查代码的安全性时,我在线上遇到了两个安全问题
dBuilder = dbFactory.newDocumentBuilder();
,它们是
1.XML Entity Expansion Injection (XML Bomb)
2.XML External Entity Injection (XXE attack)
经过一些研究,我添加了这一行dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
但是现在执行这一行时出现异常
javax.xml.parsers.ParserConfigurationException: http://javax.xml.XMLConstants/feature/secure-processing
谁能帮帮我?
最佳答案
您是否尝试过 OWASP page 中的以下代码 fragment ? ?
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features
...
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML entity attacks are prevented
// Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
String FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
dbf.setFeature(FEATURE, true);
// If you can't completely disable DTDs, then at least do the following:
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
FEATURE = "http://xml.org/sax/features/external-general-entities";
dbf.setFeature(FEATURE, false);
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
dbf.setFeature(FEATURE, false);
// and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks" (see reference below)
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
// And, per Timothy Morgan: "If for some reason support for inline DOCTYPEs are a requirement, then
// ensure the entity settings are disabled (as shown above) and beware that SSRF attacks
// (http://cwe.mitre.org/data/definitions/918.html) and denial
// of service attacks (such as billion laughs or decompression bombs via "jar:") are a risk."
// remaining parser logic
...
catch (ParserConfigurationException e) {
// This should catch a failed setFeature feature
logger.info("ParserConfigurationException was thrown. The feature '" +
FEATURE +
"' is probably not supported by your XML processor.");
...
}
catch (SAXException e) {
// On Apache, this should be thrown when disallowing DOCTYPE
logger.warning("A DOCTYPE was passed into the XML document");
...
}
catch (IOException e) {
// XXE that points to a file that doesn't exist
logger.error("IOException occurred, XXE may still possible: " + e.getMessage());
...
}
关于java - 如何防止 XML 注入(inject),如 XML 炸弹和 XXE 攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488319/
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
请原谅这个有趣的标题,我用它来比喻“zip bomb”。是否可以创建一个 Scala 源文件,该文件在编译时会生成大量类文件(或非常大的单个类文件)?有什么办法可以让类文件的大小比源文件的大小线性增长
我玩 Unix 有一段时间了,我刚刚发现了一段可爱的代码,每个人都称之为 fork 炸弹::(){ :|:& };:。我想尝试一下,但我知道它会像疯了一样滞后我的计算机,所以我只是想知道是否有人可以给
如果您已经登录系统,如何阻止 fork 炸弹? 最佳答案 斯雷, 如果您在 shell 中仍然拥有“控制权”,则可以尝试使用 ps 和 grep,以及一些 awk,然后是一个循环,以关闭包括父进程在内
可以使用 fork 炸弹(无限 fork )进行拒绝服务攻击。进程表很快就会满,系统就会崩溃。 在线编译器(如编程竞赛)如何处理此类代码。他们有时间限制吗?如果某些程序有几秒的时间限制,它们的进程表将
This question关于 zip 炸弹自然而然地把我带到了 Wikipedia page关于这个话题。文章提到了一个 45.1 kb 的 zip 文件解压缩到 1.3 艾字节的示例。 首先用于创
我正在学习C,遇到了一个小问题。在维基百科和 StackOverflow 上阅读了有关 fork() 炸弹的内容后。我想实现相同的功能,但使用命令行参数。 我想无休止地调用 firefox/chrom
我正在使用 XercesDOMParser 在 linux (c++) 中读取 xml 文件,我想防止 xml 炸弹(Billion 笑)所以我设置了这些属性: parser->setDoNamesp
`#include #include int main(int argc, char **argv){ int pid = 0; int forever; static c
我写了一些这样的代码: std::vector unzip(std::vector const& compressed) { std::vector decompressed; boost
当我有时使用 Apache POI 创建 xlsx 文件时(当文件很大时),它会创建这样一个文件,该文件无法由同一个 Apache POI 打开,而 MS Excel 或 LibreOffice Ca
我使用 Nodejs 应用程序(expressjs、request 等)在网络主机上托管一个网站。 Web 主机有一个 apache 终端,我可以从其中调用 node app.js & 或 forev
代码如下 XmlDocument xdoc = new XmlDocument(); String xml = @"" + "" +
我是一名优秀的程序员,十分优秀!