【问题标题】:XML Data Injection in the response XML from web service来自 Web 服务的响应 XML 中的 XML 数据注入
【发布时间】:2012-07-03 13:14:13
【问题描述】:

我需要为 XML 数据注入修复下面的代码。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlFromWebService));
Document doc = db.parse(inStream);   // reported at this line by a code audit tool
doc.getDocumentElement().normalize();

如何解决?有没有人有什么建议。

【问题讨论】:

  • 是的,我让你担心了。此外,在我的模块中找到并实施解决方案并对其进行测试后,我已针对所提出的每个问题发布了正确的解决方案。我不知道它。
  • 嘿,你现在能给这个解决方案吗!!!!!

标签: java xml owasp esapi


【解决方案1】:

我猜这与针对给定 XSD 验证您的 XML 以防止 XML 数据注入有关。我建议像这样修改您的代码:

try {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  factory.setNamespaceAware( true);
  factory.setValidating( true);

  factory.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
                       "http://www.w3.org/2001/XMLSchema");
  factory.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource", 
                       "file:<your_xsd_file>");

  DocumentBuilder builder = factory.newDocumentBuilder();
  InputSource inStream = new InputSource();
  inStream.setCharacterStream(new StringReader(xmlFromWebService));
  Document document = builder.parse(inStream);

} catch ( ParserConfigurationException e) {
  e.printStackTrace();
} catch ( SAXException e) {
  e.printStackTrace();
} catch ( IOException e) {
  e.printStackTrace();
}

希望你能得到提示!

【讨论】:

  • 是的,我明白了。我也在这方面做了一些研发。你能告诉我这个 setProperty 设置了什么属性吗?我将 setAttribute 作为实际名称。它会以同样的方式工作吗?这些属性的值应该是多少?还有所有的东西将如何让 documentBuilderFactory 完成工作?
  • @R.K.R: 1. SchemaLanguage & Schema Source(这就是为什么我故意将第二个属性的值保留为 )。 2. setAttribute 也可以。阅读我的代码中的这两行,您就可以理解该值的用途。 3. 我不太明白你问题的最后一部分是什么意思。
  • 我的意思是 documentBuilderfactory 将如何验证所有的东西 xml?我完成的任何方式都找到了问题的所有解决方案,还实施了一个演示解决方案,并得到客户的批准。非常感谢!!!!!!1
  • 不客气!乐意效劳。哦,如果你发现我提出的解决方案可以接受,你可能想接受它:)
  • 没有DocumentBuilderFactory.setProperty() 方法。只有setAttribute()setFeature()
猜你喜欢
  • 2016-02-18
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 2010-12-28
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
相关资源
最近更新 更多