我在解码 Element 时从代码扫描审核 (Veracode) 中发现 XML 外部实体引用 (XXE) 漏洞.
public static <T> T unMarshal(org.w3c.dom.Element content, Class<T> clazz) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (T) unmarshaller.unmarshal(content, clazz).getValue();
}
如何修复上述代码中 XML 外部实体引用(“XXE”)的不正确限制?
最佳答案
根据您的示例,您可以尝试以下代码:
public static <T> T unMarshal(org.w3c.dom.Element content, Class<T> clazz) throws JAXBException, XMLStreamException {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
XMLInputFactory xmlif = XMLInputFactory.newFactory();
xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xmlif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xmlif.createXMLStreamReader(content);
return (T) unmarshaller.unmarshal(xsr, clazz).getValue();
}
我认为上述解决方案可以解决与 (CWE 611) XML 外部实体引用相关的问题
关于java - Veracode XML 外部实体引用 (XXE) 解码 org.w3c.dom.Element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46774377/