Unchecked return value from xmlTextReaderExpand in Nokogiri
Description
Nokogiri is an open source XML and HTML library for the Ruby programming language. Nokogiri 1.13.8 and 1.13.9 fail to check the return value from xmlTextReaderExpand in the method Nokogiri::XML::Reader#attribute_hash. This can lead to a null pointer exception when invalid markup is being parsed. For applications using XML::Reader to parse untrusted inputs, this may potentially be a vector for a denial of service attack. Users are advised to upgrade to Nokogiri >= 1.13.10. Users may be able to search their code for calls to either XML::Reader#attributes or XML::Reader#attribute_hash to determine if they are affected.
AI Insight
LLM-synthesized narrative grounded in this CVE's description and references.
Nokogiri 1.13.8/1.13.9 fail to check xmlTextReaderExpand return value, causing DoS via null pointer exception on malformed XML.
Vulnerability
CVE-2022-23476 is a null pointer dereference vulnerability in the Ruby Nokogiri library (versions 1.13.8 and 1.13.9). The flaw exists in Nokogiri::XML::Reader#attribute_hash (and consequently #attributes), which invokes xmlTextReaderExpand without verifying the return value. When the parser encounters invalid or malformed markup, xmlTextReaderExpand may return NULL, leading to a null pointer exception [1][2].
Exploitation
An attacker can trigger this vulnerability by supplying crafted, malformed XML or HTML input to an application that uses Nokogiri's XML::Reader to parse untrusted data. No authentication is required; the attack is purely data-driven. The affected methods are #attributes and #attribute_hash; developers can review their code for calls to these methods to assess exposure [2][4].
Impact
Successful exploitation results in a null pointer exception, causing the Ruby process to crash. This constitutes a denial of service (DoS) condition. The impact is limited to denial of service; the description does not indicate potential for code execution or data breach [2].
Mitigation
The vulnerability is fixed in Nokogiri version 1.13.10 and later. Users should upgrade to at least 1.13.10. The fix ensures that when xmlTextReaderExpand returns NULL, #attribute_hash returns nil instead of proceeding with a null pointer [2][4]. No workaround is provided other than upgrading or avoiding the use of XML::Reader with untrusted data on vulnerable versions.
AI Insight generated on May 20, 2026. Synthesized from this CVE's description and the cited reference URLs; citations are validated against the source bundle.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
nokogiriRubyGems | >= 1.13.8, < 1.13.10 | 1.13.10 |
Affected products
4- ghsa-coords3 versionspkg:gem/nokogiripkg:rpm/opensuse/ruby3.2-rubygem-nokogiri&distro=openSUSE%20Tumbleweedpkg:rpm/opensuse/rubygem-nokogiri&distro=openSUSE%20Tumbleweed
>= 1.13.8, < 1.13.10+ 2 more
- (no CPE)range: >= 1.13.8, < 1.13.10
- (no CPE)range: < 1.15.4-1.1
- (no CPE)range: < 1.15.5-1.5
- sparklemotion/nokogiriv5Range: >= 1.13.8, < 1.13.10
Patches
285410e38410fMerge pull request #2715 from sparklemotion/flavorjones-fix-reader-error-handling_v1.13.x
3 files changed · +43 −0
CHANGELOG.md+7 −0 modified@@ -4,6 +4,13 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA --- +## next / unreleased + +### Improvements + +* [CRuby] `XML::Reader#attribute_hash` now returns `nil` on parse errors. This restores the behavior of `#attributes` from v1.13.7 and earlier. [[#2715](https://github.com/sparklemotion/nokogiri/issues/2715)] + + ## 1.13.9 / 2022-10-18 ### Security
ext/nokogiri/xml_reader.c+4 −0 modified@@ -212,6 +212,10 @@ rb_xml_reader_attribute_hash(VALUE rb_reader) } c_node = xmlTextReaderExpand(c_reader); + if (c_node == NULL) { + return Qnil; + } + c_property = c_node->properties; while (c_property != NULL) { VALUE rb_name = NOKOGIRI_STR_NEW2(c_property->name);
test/xml/test_reader.rb+32 −0 modified@@ -681,6 +681,38 @@ def test_nonexistent_attribute reader.read # el assert_nil(reader.attribute("other")) end + + def test_broken_markup_attribute_hash + xml = <<~XML + <root><foo bar="asdf" xmlns:quux="qwer"> + XML + reader = Nokogiri::XML::Reader(xml) + reader.read # root + reader.read # foo + + assert_equal("foo", reader.name) + if Nokogiri.jruby? + assert_equal({ "bar" => "asdf" }, reader.attribute_hash) + else + assert_nil(reader.attribute_hash) + end + end + + def test_broken_markup_namespaces + xml = <<~XML + <root><foo bar="asdf" xmlns:quux="qwer"> + XML + reader = Nokogiri::XML::Reader(xml) + reader.read # root + reader.read # foo + + assert_equal("foo", reader.name) + if Nokogiri.jruby? + assert_equal({ "xmlns:quux" => "qwer" }, reader.namespaces) + else + assert_nil(reader.namespaces) + end + end end end end
9fe0761c47c0fix(cruby): XML::Reader#attribute_hash returns nil on error
3 files changed · +43 −0
CHANGELOG.md+7 −0 modified@@ -4,6 +4,13 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA --- +## next / unreleased + +### Improvements + +* [CRuby] `XML::Reader#attribute_hash` now returns `nil` on parse errors. This restores the behavior of `#attributes` from v1.13.7 and earlier. [[#2715](https://github.com/sparklemotion/nokogiri/issues/2715)] + + ## 1.13.9 / 2022-10-18 ### Security
ext/nokogiri/xml_reader.c+4 −0 modified@@ -212,6 +212,10 @@ rb_xml_reader_attribute_hash(VALUE rb_reader) } c_node = xmlTextReaderExpand(c_reader); + if (c_node == NULL) { + return Qnil; + } + c_property = c_node->properties; while (c_property != NULL) { VALUE rb_name = NOKOGIRI_STR_NEW2(c_property->name);
test/xml/test_reader.rb+32 −0 modified@@ -681,6 +681,38 @@ def test_nonexistent_attribute reader.read # el assert_nil(reader.attribute("other")) end + + def test_broken_markup_attribute_hash + xml = <<~XML + <root><foo bar="asdf" xmlns:quux="qwer"> + XML + reader = Nokogiri::XML::Reader(xml) + reader.read # root + reader.read # foo + + assert_equal("foo", reader.name) + if Nokogiri.jruby? + assert_equal({ "bar" => "asdf" }, reader.attribute_hash) + else + assert_nil(reader.attribute_hash) + end + end + + def test_broken_markup_namespaces + xml = <<~XML + <root><foo bar="asdf" xmlns:quux="qwer"> + XML + reader = Nokogiri::XML::Reader(xml) + reader.read # root + reader.read # foo + + assert_equal("foo", reader.name) + if Nokogiri.jruby? + assert_equal({ "xmlns:quux" => "qwer" }, reader.namespaces) + else + assert_nil(reader.namespaces) + end + end end end end
Vulnerability mechanics
Generated on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
6- github.com/advisories/GHSA-qv4q-mr5r-qprjghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2022-23476ghsaADVISORY
- github.com/rubysec/ruby-advisory-db/blob/master/gems/nokogiri/CVE-2022-23476.ymlghsaWEB
- github.com/sparklemotion/nokogiri/commit/85410e38410f670cbbc8c5b00d07b843caee88ceghsax_refsource_MISCWEB
- github.com/sparklemotion/nokogiri/commit/9fe0761c47c0d4270d1a5220cfd25de080350d50ghsax_refsource_MISCWEB
- github.com/sparklemotion/nokogiri/security/advisories/GHSA-qv4q-mr5r-qprjghsax_refsource_CONFIRMWEB
News mentions
0No linked articles in our index yet.