Skip to content

Commit 5b77f3d

Browse files
committed
Raise RuntimeError when canonicalization fails
The return value of xmlC14NExecute was not being checked, so canonicalization failures (e.g., relative namespace URIs) silently returned an empty string. This could allow downstream libraries to accept invalid canonicalized output. Check the return value and raise RuntimeError on failure, matching JRuby's existing behavior. This behavior was named as a contributing cause to GHSA-x4h9-gwv3-r4m4
1 parent edc5595 commit 5b77f3d

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)