Java XML外部实体引用限制不当(CWE ID 611)(6个缺陷)
该产品处理一个XML文档,该文档可以包含URL解析为外部文档的XML实体 超出预期控制范围,导致产品在其输出中嵌入不正确的文档。Java XML外部实体引用限制不当(CWE ID 611)(6个缺陷),java,xml,jaxb,veracode,Java,Xml,Jaxb,Veracode,该产品处理一个XML文档,该文档可以包含URL解析为外部文档的XML实体 超出预期控制范围,导致产品在其输出中嵌入不正确的文档。 默认情况下,XML实体解析器将尝试解析和检索外部引用。如果攻击者控制的XML可以 提交到其中一个函数,则攻击者可以访问有关内部网络(本地)的信息 文件系统或其他敏感数据。这称为XML外部实体(XXE)攻击 没什么 package com.integratingstuff.jaxb; import java.io.ByteArrayInputStream; impo
默认情况下,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();
}
}
}
package com.integratingstuff.jaxb;
导入java.io.ByteArrayInputStream;
导入java.io.InputStream;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Unmarshaller;
导入com.integratingstuff.pojo.Item;
公共班的杜曼霍尔{
公共静态void main(字符串[]args){
尝试
{
JAXBContext JAXBContext=JAXBContext.newInstance(Item.class);
Unmarshaller Unmarshaller=jaxbContext.createUnmarshaller();
字符串xml=”“;
InputStream InputStream=新建
ByteArrayInputStream(xml.getBytes());
Item Item=(Item)解组器。解组器(inputStream);
}捕获(JAXBEException e){
e、 printStackTrace();
}
}
}
这是获得解决方案的一个很好的参考:
例如,在您的示例中,您只需将这两个属性添加到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问题
希望有帮助