如何使 Sonar 符合 XMLInputFactory 和 woodstox 库的注册实现?
Posted
技术标签:
【中文标题】如何使 Sonar 符合 XMLInputFactory 和 woodstox 库的注册实现?【英文标题】:How to be Sonar compliant with the XMLInputFactory and woodstox library registered implementation? 【发布时间】:2021-07-23 22:58:24 【问题描述】:我正在尝试遵守以下声纳拦截器规则:
XML 解析器不应受到 XXE 攻击 (java:S2755)
XML 规范允许使用可以是内部或 外部(文件系统/网络访问......),这可能导致 机密文件泄露或 s-s-rF 等漏洞。
在 Sonar 规则描述中,他们给出了如何合规的示例:
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // Compliant
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); // compliant
我的问题是,在应用程序的类路径中,有 2 个库 woodstox-core-asl 和 wstx-asl 在 /META-INF/services/javax 中注册了自己的实现 com.ctc.wstx.stax.WstxInputFactory .xml.stream.XMLInputFactory。此实现 com.ctc.wstx.stax.WstxInputFactory 不支持 accessExternalDTD,因此它失败并显示以下错误消息:
无法识别的属性 'http://javax.xml.XMLConstants/property/accessExternalDTD'
我尝试成功地直接做一个新的 com.sun.xml.internal.stream.XMLInputFactoryImpl() 或 Class.forName("com.sun.xml.internal.stream.XMLInputFactoryImpl").newInstance() 但它是只是摆脱另一个警告,即受限 API 上的警告。
有没有好的解决方案?
下面是一个最小的可重现示例,其中的解决方法是注释行:
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.util.StreamReaderDelegate;
public class XMLReader extends StreamReaderDelegate implements AutoCloseable
private final Reader reader;
public XMLReader(Reader reader) throws XMLStreamException, InstantiationException, IllegalAccessException, ClassNotFoundException
this.reader = reader;
//XMLInputFactory factory = (XMLInputFactory) Class.forName("com.sun.xml.internal.stream.XMLInputFactoryImpl").newInstance();
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
setParent(factory.createXMLStreamReader(reader));
@Override
public void close() throws XMLStreamException
try
super.close();
reader.close();
catch (IOException e)
throw new XMLStreamException(e.getMessage(), e);
public static void main(String[] args) throws XMLStreamException, InstantiationException, IllegalAccessException, ClassNotFoundException
try (XMLReader xmlReader = new XMLReader(new StringReader("</test>")))
您还可以在以下 Maven POM 文件中找到以下依赖项:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-xml</groupId>
<artifactId>test-xml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
</dependencies>
</project>
【问题讨论】:
【参考方案1】:显然,即使它不在规则描述中,Sonar 也可以识别属性 XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,它是具有相同作用的 Stax 标准属性:
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, 假);
另见:
https://github.com/FasterXML/woodstox/issues/50
【讨论】:
以上是关于如何使 Sonar 符合 XMLInputFactory 和 woodstox 库的注册实现?的主要内容,如果未能解决你的问题,请参考以下文章
Sonar建议Reorder the modifiers to comply with the Java Language Specification重新排序修饰符以符合Java语言规范
Sonar建议Reorder the modifiers to comply with the Java Language Specification重新排序修饰符以符合Java语言规范
使 Sonar Runner Gradle 任务依赖于我的一项任务