【问题标题】:How to resolve 'Improper Restriction of XML External Entity Reference ('XXE')'如何解决“XML 外部实体引用 ('XXE') 的不当限制”
【发布时间】:2019-10-07 18:09:21
【问题描述】:

我正在尝试修复 veracode 在我的 Web 应用程序中列出的所有漏洞。我被困在这个我实际上不知道的特殊漏洞上。 'XML 外部实体限制不当 参考'。 Cal any 请帮我解释一下代码的问题以及我们可以解决这个问题的方法?

    Object objec = null;

    try {
        JAXBContext jContext = JAXBContext.newInstance(context);
        Unmarshaller unmarshaller = jContext.createUnmarshaller();
        InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
        objec = unmarshaller.unmarshal(inputStream);  //Vulnerability reported in this line

    } catch (JAXBException e) {
        e.printStackTrace();
    }

    return objec;
}

【问题讨论】:

标签: java security xml-parsing unmarshalling veracode


【解决方案1】:

这是获得解决方案的一个很好的参考:https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#java

例如,在您的情况下,您只需将这两个属性添加到 XMLInputFactory 和流阅读器:

        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        // These 2 properties are the key
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        // Your stream reader for the xml string
        final XMLStreamReader xmlStreamReader = xmlInputFactory
                .createXMLStreamReader(new StringReader(yourXMLStringGoesHere));
        final NsIgnoringXmlReader nsIgnoringXmlReader = new NsIgnoringXmlReader(xmlStreamReader);
        // Done with unmarshalling the XML safely
        final YourObject obj = (YourObject) unmarshaller.unmarshal(nsIgnoringXmlReader);

这应该有助于 Veracode 扫描

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2014-03-23
    • 1970-01-01
    • 2016-05-01
    • 2017-09-09
    • 2017-04-04
    相关资源
    最近更新 更多