代码之家  ›  专栏  ›  技术社区  ›  siddharthdg

Veracode XML外部实体引用(XXE)解组组织。w3c。dom。要素

  •  2
  • siddharthdg  · 技术社区  · 7 年前

    我从代码扫描审计(Veracode)中获得了一个XML外部实体引用(XXE)漏洞,同时对 Element .

        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 回复  |  直到 7 年前
        1
  •  3
  •   Greg    5 年前

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

    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外部实体引用相关的问题