Skip to content

Commit 8e66809

Browse files
authored
C14n raise on failure (#3600)
**What problem is this PR intended to solve?** Canonicalization could fail and an exception would not be raised. This behavior was named as a contributing cause in ruby-saml GHSA-x4h9-gwv3-r4m4 **Have you included adequate test coverage?** Yes. **Does this change affect the behavior of either the C or the Java implementations?** JRuby raised an exception correctly already. This bring the CRuby implementation in line.
2 parents edc5595 + 5b77f3d commit 8e66809

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

ext/nokogiri/xml_document.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,15 +652,19 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
652652
}
653653
}
654654

655-
xmlC14NExecute(c_doc, c_callback_wrapper, rb_callback,
656-
c_mode,
657-
c_namespaces,
658-
(int)RTEST(rb_comments_p),
659-
c_obuf);
655+
int ret = xmlC14NExecute(c_doc, c_callback_wrapper, rb_callback,
656+
c_mode,
657+
c_namespaces,
658+
(int)RTEST(rb_comments_p),
659+
c_obuf);
660660

661661
ruby_xfree(c_namespaces);
662662
xmlOutputBufferClose(c_obuf);
663663

664+
if (ret < 0) {
665+
rb_raise(rb_eRuntimeError, "canonicalization failed");
666+
}
667+
664668
return rb_funcall(rb_io, rb_intern("string"), 0);
665669
}
666670

test/xml/test_c14n.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ def test_c14n_modes
199199
end
200200
end
201201

202+
def test_raise_on_canonicalization_failure
203+
doc = Nokogiri.XML('<root xmlns:a="1"></root>')
204+
assert_raises(RuntimeError) { doc.canonicalize }
205+
end
206+
202207
def test_wrong_params
203208
xml = "<a><b></b></a>"
204209
doc = Nokogiri.XML(xml)

0 commit comments

Comments
 (0)