【问题标题】:Improper Restriction of XML External Entity Reference (CWE ID 611)(6 flaws)XML 外部实体引用的不当限制(CWE ID 611)(6 个缺陷)
【发布时间】:2019-09-19 11:45:58
【问题描述】:

产品处理一个 XML 文档,该文档可以包含 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();
        }
    }
}

【问题讨论】:

    标签: java xml jaxb 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 Item item = (Item) unmarshaller.unmarshal(nsIgnoringXmlReader);
    

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

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-28
      • 2019-10-07
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多