奇怪的实体扩展案例

时间:2014-12-25 08:54:43

标签: java xml saxparser expansion

我目前正处理一个相当奇怪的实体扩张问题。

SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    InputSource inputSource = new InputSource(new FileReader("input.xml"));
    SAXSource source = new SAXSource(xmlReader, inputSource);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Foo foo = (Foo) unmarshaller.unmarshal(source);

代码取自http://blog.bdoughan.com/2011/03/preventing-entity-expansion-attacks-in.html。我在我的应用程序中使用类似的代码。现在当我像

那样提供xml时
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>

我得到了#34;解析器遇到了超过&#34; 64,000&#34;本文件中的实体扩展;这是应用程序强加的限制。 线程&#34; main&#34;中的例外情况javax.xml.bind.UnmarshalException&#34; 这是预期的行为。我抓住它并优雅地处理。但是,我们的测试团队制作了这个例子

    <!DOCTYPE lolz [
<!ENTITY lul "lulllullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullullul">
  ]>

 <someTag>&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;&lul;  </someTag>

原来的线路要长得多:)无论如何,当我提供这种类型的xml时,应用程序不会崩溃,但CPU使用率变得高得令人无法接受,并且不会抛出任何异常。 有什么办法可以阻止这个吗?设定一定限度?

0 个答案:

没有答案
相关问题