Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1802] ensure we trap XML errors while applying XSLT stylesheet #1860

Merged
merged 1 commit into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/nokogiri/xslt_stylesheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static VALUE transform(int argc, VALUE* argv, VALUE self)

errstr = rb_str_new(0, 0);
xsltSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler);
xmlSetGenericErrorFunc(NULL, (xmlGenericErrorFunc)&swallow_superfluous_xml_errors);
xmlSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler);

result = xsltApplyStylesheet(wrapper->ss, xml, params);
free(params);
Expand Down
47 changes: 47 additions & 0 deletions test/test_xslt_transforms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,51 @@ def test_non_html_xslt_transform
result = xsl.transform xml
assert !result.html?
end

it "should not crash when given XPath 2.0 features" do
#
# https://github.com/sparklemotion/nokogiri/issues/1802
#
# note that here the XPath 2.0 feature is `decimal`.
# this test case is taken from the example provided in the original issue.
#
xml = <<~EOXML
<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cac:TaxTotal>
<cbc:TaxAmount currencyID="EUR">48.00</cbc:TaxAmount>
</cac:TaxTotal>
</Invoice>
EOXML

xsl = <<~EOXSL
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xsl:template match="/">
<xsl:apply-templates select="/" mode="qwerty"/>
</xsl:template>
<xsl:template match="/ubl:Invoice/cac:TaxTotal" priority="1001" mode="qwerty">
<xsl:choose>
<xsl:when test="(round(xs:decimal(child::cbc:TaxAmount)))"/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
EOXSL

doc = Nokogiri::XML(xml)
xslt = Nokogiri::XSLT(xsl)
exception = assert_raise(RuntimeError) do
xslt.transform(doc)
end
assert_match(/decimal/, exception.message)
end
end