XML外部实体参考(CWE ID 611)的限制不当(6个缺陷)

时间:2019-09-19 11:45:58

标签: java xml jaxb veracode

该产品处理XML文档,该XML文档可以包含带有URL的XML实体,这些URL可以解析为外部文档 预期的控制范围,导致产品将错误的文档嵌入其输出中。
默认情况下,XML实体解析器将尝试解析和检索外部引用。如果攻击者控制的XML可以 提交给这些功能之一,攻击者就可以访问有关内部网络的信息 文件系统或其他敏感数据。这就是XML外部实体(XXE)攻击。

没事

package com.integratingstuff.jaxb;

import java.io.ByteArrayInputStream;

import java.io.InputStream;
import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.integratingstuff.pojo.Item;

public class DoUnmarshall {
    public static void main(String[] args) {
        try 
{
            JAXBContext jaxbContext= JAXBContext.newInstance(Item.class);

            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            String xml = "<?xml version="1.0" encoding="UTF-8"?><item
 price="" description="Test description" catalog-number="10"/>";

            InputStream inputStream = new 
ByteArrayInputStream(xml.getBytes());
            Item item = (Item) unmarshaller.unmarshal(inputStream);

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

1 个答案:

答案 0 :(得分:0)

这是获取解决方案的良好参考: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 Item item = (Item) unmarshaller.unmarshal(nsIgnoringXmlReader);

这也应该通过Veracode扫描,而没有任何XXE问题。

希望有帮助