加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何从XML外部攻击中保护javax.xml.transform.TransformerFactor

发布时间:2020-12-15 23:59:37 所属栏目:百科 来源:网络整理
导读:我已就此主题进行了研究,但未找到相关的相关信息 我们是否需要采取任何安全措施来保护javax.xml.transform.Transformer免受XML外部实体攻击? 我做了以下,似乎扩大了dtd. String fileData = "!DOCTYPE acunetix [ !ENTITY sampleVal SYSTEM "file:///media/
我已就此主题进行了研究,但未找到相关的相关信息

我们是否需要采取任何安全措施来保护javax.xml.transform.Transformer免受XML外部实体攻击?

我做了以下,似乎扩大了dtd.

String fileData = "<!DOCTYPE acunetix [  <!ENTITY sampleVal SYSTEM "file:///media/sample">]><username>&sampleVal;</username>";
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,true);
Transformer transformer = transformerFactory.newTransformer();
StringWriter buff = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
transformer.transform(new StreamSource(new StringReader(fileData)),new StreamResult(buff));
System.out.println(buff.toString());

output包含文件中的值

<username>test</username>
你的代码似乎是对的.当我运行这个稍微修改过的JUnit测试用例时:
@Test
public void test() throws TransformerException,URISyntaxException {
  File testFile = new File(getClass().getResource("test.txt").toURI());
  assertTrue(testFile.exists());
  String fileData = "<!DOCTYPE acunetix [  <!ENTITY foo SYSTEM "file://" + 
                    testFile.toString() +
                    "">]><xxe>&foo;</xxe>";
  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  System.out.println(transformerFactory.getClass().getName());
  transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,true);
  Transformer transformer = transformerFactory.newTransformer();
  StringWriter buff = new StringWriter();
  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  transformer.transform(new StreamSource(new StringReader(fileData)),new StreamResult(buff));
  assertEquals("<xxe>&foo;</xxe>",buff.toString());
}

我得到以下输出:

com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
[Fatal Error] :1:182: External Entity: Failed to read external document 'test.txt',because 'file' access is not allowed due to restriction set by the accessExternalDTD property.
ERROR:  'External Entity: Failed to read external document 'test.txt',because 'file' access is not allowed due to restriction set by the accessExternalDTD property.'

从setFeature JavaDocs:

All implementations are required to support the XMLConstants.FEATURE_SECURE_PROCESSING feature. When the feature is:

  • true: the implementation will limit XML processing to conform to implementation limits and behave in a secure fashion as defined by the implementation. Examples include resolving user defined style sheets and functions. If XML processing is limited for security reasons,it will be reported via a call to the registered ErrorListener.fatalError(TransformerException exception). See setErrorListener(ErrorListener listener).

如果我注释掉transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,true),那个错误就会消失;然后测试失败,因为实体已解决.

尝试将ErrorListener添加到TransformerFactory和Transformer:

transformerFactory.setErrorListener(new ErrorListener() {

  @Override
  public void warning(TransformerException exception) throws TransformerException {
    System.out.println("In Warning: " + exception.toString());
  }

  @Override
  public void error(TransformerException exception) throws TransformerException {
    System.out.println("In Error: " + exception.toString());
  }

  @Override
  public void fatalError(TransformerException exception) throws TransformerException {
    System.out.println("In Fatal: " + exception.toString());
  }
});

Transformer transformer = transformerFactory.newTransformer();
transformer.setErrorListener(transformerFactory.getErrorListener());

我现在看到以下新的控制台输出:

In Error: javax.xml.transform.TransformerException: External Entity: Failed to read external document 'test.txt',because 'file' access is not allowed due to restriction set by the accessExternalDTD property.

也许您的实施将其视为警告?否则,也许这是你正在使用的实现?看起来JavaDoc规范并不精确,因此一个实现可能会做一些与另一个不同的实现.我有兴趣知道错误的实现!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读