带有 FOP 的 TransformerFactory:禁用 ACCESS_EXTERNAL_DTD 时出现问题

问题描述 投票:0回答:0

我正在尝试保护一段代码免受 XXE 攻击。 代码使用FOP库,mimeFormat为application/pdf。

原始代码运行良好:

    protected static void transformTo(Result result, Source src, String mimeFormat, String sFileNameXsl)
            throws FOPException {
        try {
            TransformerFactory factory = TransformerFactory.newInstance();

            File myXslFile = new File(sFileNameXsl);
            StreamSource xsltSource = new StreamSource(myXslFile);
            Transformer transformer = factory.newTransformer(xsltSource);
            transformer.setParameter("fop-output-format", mimeFormat);
            transformer.transform(src, result);
        } catch (Exception e) {
            throw new FOPException(e);
        }
    }

该应用程序使用 xalan-2.7.2 的 Apache 实现

org.apache.xalan.processor.TransformerFactoryImpl

当我尝试禁用外部 DTD 和样式表时,由于错误“不支持属性访问ExternalDTD”,我被迫切换实现。

所以我更改了代码以使用JDK8实现

com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
:

TransformerFactory factory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", ClassLoader.getSystemClassLoader());
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");

此时属性已经支持了,但是出现了新的消息:

FATAL ERROR: Cannot convert data type 'int' in 'node-set'.

该消息是意外的,因为我没有更改任何结构或处理,只是尝试使用某些属性来保护变压器。

xslt 是此版本的修改版本 https://www.antennahouse.com/hubfs/uploads/XSL%20Sample/xhtml2fo.xsl?hsLang=en

数据类型错误是由于 | 的误用造成的运算符例如:

<xsl:variable name="numcolumns" select="count(./html:tr/*)|count(./html:TR/*)"/>

<xsl:if test="ancestor::html:table[1]/@rules = 'cols'|ancestor::html:TABLE[1]/@rules = 'cols'">

我设法通过放弃大写匹配器使事情正常进行:

<xsl:variable name="numcolumns" select="count(./html:tr/*)"/>

<xsl:if test="ancestor::html:table[1]/@rules = 'cols'">

所以最终的问题是,如何在这些匹配器中匹配大写和小写?

java xslt apache-fop xalan xxe
© www.soinside.com 2019 - 2024. All rights reserved.