java - Veracode XML 外部实体引用 (XXE) 解码 org.w3c.dom.Element

标签 java xml xml-parsing jaxb xxe

我在解码 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/

相关文章:

Java & GUI : Builder, 框架、技术——最先进的?

java - 在 Java 中使用 Stream 和 BinaryOperator 的 Fibonacci

xml - XSLT XML : highlight a search word in search results

xml-parsing - "Error attempting to parse XML file"使用 XInclude 解析时

java - 如何使用java从xml文件中的CDATA获取测试值

java - 如何在 Java 中将字符串转换为 XML 对象

java - 矩阵乘法错误 : incompatible types (float to int)

java - 如何在 stub 生成期间重命名 WSDL 中的复杂类型

保留属性顺序的 Java XML 库

xml - 使用带有 foreach 循环的 LINQ to XML 创建 XML 文档