我已经在这里得到了一些帮助,但我遇到了一个稍微不同的问题。我正在寻找DocumentBuilderFactory
创建 a 但没有限制ExpandEntityReferences
. 我有以下正则表达式:
(?x)
# finds DocumentBuilderFactory creation and pulls out the variable name
# of the form DocumentBuilderFactory VARNAME = DocumentBuilderFactory.newInstance
# then checks if that variable name has one of three acceptable ways to stop XXE attacks
# matches any instance where the variable is initialized, but not restricted
(?:
# This is for DocumentBuilderFactory VARNAME = DocumentBuilderFactory.newInstance with many possible alternates
DocumentBuilderFactory
[\s]+?
(\w+)
[\s]*?
=
[\s]*?
(?:.*?DocumentBuilderFactory)
[.\s]+
newInstance.*
# checks that the var name is NOT (using ?!) using one of the acceptable rejection methods
(?!\1[.\s]+
(?:setFeature\s*\(\s*"http://xml.org/sax/features/external-general-entities"\s*,\s*false\s*\)
|setFeature\s*\(\s*"http://apache.org/xml/features/disallow-doctype-decl"\s*,\s*false\s*\)
|setExpandEntityReferences\s*\(\s*false\s*\))
)
)
测试文件可能如下所示:
// Set the parser properties
javax.xml.parsers.DocumentBuilderFactory factory =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
factory.setExpandEntityReferences(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setCoalescing(true);
javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
有没有办法让这个正则表达式在这个文件上运行并且正则表达式失败(因为它正确设置factory.setExpandEntityReferences(false);
?
更新:
(?:
DocumentBuilderFactory
\s+
(\w+)
\s*
=
\s*
(?:.*?DocumentBuilderFactory)
\s*.\s*
newInstance.*
(?:[\s\S](?!
\1\s*.\s*
(?:setFeature\s*\(\s*"http://xml.org/sax/features/external-general-entities"\s*,\s*false\s*\)
|setFeature\s*\(\s*"http://apache.org/xml/features/disallow-doctype-decl"\s*,\s*false\s*\)
|setExpandEntityReferences\s*\(\s*false\s*\))
))*$
)
正如预期的那样,它没有成功地找到();但是,如果我将 factory.setExpandEntityReferences(false) 拼错为 factory.setExpandEntity##References(false),我希望找到正则表达式,但事实并非如此。有没有办法让这个功能发挥作用?