Nokogiri::XSLT.quote_params does not sanitize null bytes in parameter values.
When a null byte is present, StringValueCStr raises an ArgumentError mid-execution — afterruby_xcalloc has already allocated the params array in rb_xslt_stylesheet_transform. Because the exception propagates via longjmp, ruby_xfree(params) is never reached, resulting in a memory leak confirmed by AddressSanitizer / LeakSanitizer.
Details
Root Cause 1 — quote_params does not handle null bytes
lib/nokogiri/xslt.rb:103~113
def quote_params(params) params.flatten.each_slice(2).with_object([]) do |kv, quoted_params| key, value = kv.map(&:to_s) value = if value.include?("'") "concat('#{value.gsub("'", %q{', "'", '})}')" else "'#{value}'" # null byte passes through unhandled end quoted_params << key quoted_params << value endend
A value containing a null byte (e.g. "val\x00ue") passes through quote_params unchanged, producing "'val\x00ue'".
Root Cause 2 — params array allocated before the null byte is detected
ext/nokogiri/xslt_stylesheet.c:279~302
// line 279: params array allocatedparams = ruby_xcalloc((size_t)param_len + 1, sizeof(char *));for (j = 0; j < param_len; j++) { VALUE entry = rb_ary_entry(rb_param, j); const char *ptr = StringValueCStr(entry); // line 282: raises ArgumentError on null byte params[j] = ptr;}// ...ruby_xfree(params); // line 302: never reached due to longjmp exception propagation
ruby_xcalloc allocates the params array at line 279. When StringValueCStr encounters a null byte at line 282, it calls rb_raise, which unwinds the stack via longjmp. ruby_xfree(params) at line 302 is never reached, leaking the allocated memory on every call.
#28 0x7fb3498071be (libruby-3.2.so.3.2+0x1d31be)#29 0x7fb3498076b9 (libruby-3.2.so.3.2+0x1d36b9)Indirect leak of 16 byte(s) in 1 object(s) allocated from: #0 0x7fb349af69c7 in malloc asan_malloc_linux.cpp:69 #1 0x7fb34970c87c (libruby-3.2.so.3.2+0xd887c) #5 0x7fb34989370e in rb_funcallv_kw (libruby-3.2.so.3.2+0x25f70e) #6 0x7fb34977d1d3 in rb_class_new_instance_kw #7 0x7fb3496e3051 in rb_exc_new_str #9 0x7fb3496e4249 in rb_raise #10 0x7fb34982aa1d in rb_string_value_cstr #11 0x7fb341ec554e in rb_xslt_stylesheet_transform ext/nokogiri/xslt_stylesheet.c:282
Impact
Any application that passes user-supplied input through Nokogiri::XSLT.quote_params and then calls Stylesheet#transform is affected. An attacker who can inject a null byte into a parameter value triggers a memory leak on every request. In long-running processes or under repeated requests, this leads to unbounded memory growth and eventual denial of service.
Affects the documented safe path
quote_params is Nokogiri’s own recommended safety mechanism. The leak occurs even when the developer follows the documented API correctly: