如何使声纳符合 XMLInputFactory 和 woodstox 库注册实现?

问题描述

我正在努力遵守以下声纳拦截器规则:

XML 解析器不应该容易受到 XXE 攻击 (java:S2755)

XML 规范允许使用可以是内部或 外部(文件系统/网络访问...)可能导致 机密文件泄露或 SSRF 等漏洞。

在 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,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> 

解决方法

显然,即使它不在规则描述中,Sonar 也识别属性 XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,这是执行相同操作的 Stax 标准属性:

factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,假);

另见:

https://github.com/FasterXML/woodstox/issues/50

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...