Veracode XML外部实体引用(XXE)unmarshaling org.w3c.dom.Element

时间:2017-10-16 15:56:20

标签: 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')?

1 个答案:

答案 0 :(得分:2)

根据您的示例,您可以尝试以下代码:

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外部实体参考有关的问题