【问题标题】:Prevent XXE Attack with XStream使用 XStream 防止 XXE 攻击
【发布时间】:2016-10-12 14:15:39
【问题描述】:

想知道我们如何使用 Xstream API 修复 Xml 外部实体 (XXE) 漏洞。

我们可以做的

// 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
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
String FEATURE = null;
FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
dbf.setFeature(FEATURE, true);

使用 DocumentBuilderFactory。更多详情 - https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Prevention_Cheat_Sheet

我的代码类似于 -

public static Class<?>[] myAnnotatedClasses = { Test1.class, Test2.class };

public static Object parseStr(String str) throws XStreamException
{
    XStream xstream = new XStream(new StaxDriver());
    xstream.processAnnotations(myAnnotatedClasses);
    Object obj =xstream.fromXML(str);
    return obj;
}

【问题讨论】:

    标签: java security xstream xxe


    【解决方案1】:

    根据XStream FAQs

    StaxDriver 尝试关闭对标准 StaX 解析器的外部实体的支持。但是,最终使用的 StAX 实现是在外部定义的(参见 JDK 文档),应在目标平台上进行测试以确保解析器遵守设置。

    这就是说StaxDriver 试图告诉StAX 实现做正确的事情,但您正在使用的StAX 实现可能会忽略这一点。如果它确实忽略它,简单的答案是使用常见问题解答中列出的没有问题的替代驱动程序之一。

    【讨论】:

    • 有道理。将尝试与其他驱动程序。报告说StaxDriver 对我来说很脆弱。
    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 2017-03-31
    • 2015-10-10
    • 2012-12-23
    • 1970-01-01
    • 2015-04-03
    • 2018-01-19
    • 2018-07-16
    相关资源
    最近更新 更多