【问题标题】:Veracode CWE id 611Veracode CWE id 611
【发布时间】:2019-03-06 08:18:18
【问题描述】:

我有一段代码,其中找到了针对 XML 外部实体引用 ('XXE') 攻击的不当限制的 veracode。

代码:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(node);
        transformer.transform(source, result); //CWE ID 611, impacted line.

我用过

transformer.setOutputProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformer.setOutputProperty(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");

但没有运气。

【问题讨论】:

    标签: veracode


    【解决方案1】:

    问题已通过以下代码解决:

            TransformerFactory transformer = TransformerFactory.newInstance();//.newTransformer();
            transformer.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            transformer.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(node);
            transformer.newTransformer().transform(source, result);
    

    【讨论】:

      【解决方案2】:

      建议放置一个 try-catch 块。

      try{
                  transformer.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
                  transformer.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
      
              } catch (IllegalArgumentException e) {
                  //jaxp 1.5 feature not supported
              }
      

      【讨论】:

        【解决方案3】:

        请注意,对于在 JDK5 或更早版本上运行应用程序的任何人,您将无法使用这些 XML 常量:

        transformer.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        transformer.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
        

        相反,您必须使用安全文档构建器解析为文档,然后在转换器中使用 DOM 源。

        private static void example(String xmlDocument, Result result) throws ParserConfigurationException, IOException, SAXException, TransformerException {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setEntityResolver(new EntityResolver() {
                public InputSource resolveEntity(String s, String s1) throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
            Document doc = db.parse(new InputSource(new StringReader(xmlDocument)));
        
            DOMSource domSource = new DOMSource(doc);
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(domSource, result);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-11-06
          • 2021-11-04
          • 2020-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-06
          相关资源
          最近更新 更多