Skip to content

Commit

Permalink
ensure we trap XML errors while applying XSLT stylesheet
Browse files Browse the repository at this point in the history
otherwise trivial XML errors will silently result in NULL being
returned by xsltApplyStylesheet() and a subsequent segfault

Fixes #1802
  • Loading branch information
flavorjones committed Jan 13, 2019
1 parent df1bfa0 commit 3ab0c9d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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

0 comments on commit 3ab0c9d

Please sign in to comment.