想知道我们如何使用 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;
}