gpt4 book ai didi

java - 使用 XStream 防止 XXE 攻击

转载 作者:行者123 更新时间:2023-11-30 07:06:39 28 4
gpt4 key购买 nike

想知道如何使用 Xstream API 修复 Xml EXternal Entity (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;
}

最佳答案

根据XStream FAQs :

StaxDriver tries to turns off support for external entities for the standard StaX parser. However, the finally used StAX implementation is defined externally (see JDK documentation) and a test should be made on the target platform to ensure that the parser respects the setting.

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

关于java - 使用 XStream 防止 XXE 攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40000957/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com