CVE-2026-57236
JSON

1.7low

Nokogiri: Possible Use-After-Free when `Nokogiri::XML::Document#encoding=` raises an exception

Severity
1.7low CVSS:4.0 by GitHub_M
Exploit likelihood
0.34% (higher than 29% of CVEs; EPSS, by FIRST)
Published
Last changed
Assigned by
GitHub_M
Also known as
GHSA-5v8h-3h3q-446p

Description

Nokogiri is an open source XML and HTML library for the Ruby programming language. Prior to 1.19.4, calling Document#encoding= with an invalid encoding (e.g., a non-string, or a string containing a null byte) raises an exception, but only after freeing the document's current encoding string without replacing it. The document is left referencing freed memory, so the next call to Document#encoding reads invalid memory, which can cause a segfault or leak freed bytes into a Ruby String. Affects the CRuby (libxml2) implementation only; JRuby is not affected. This vulnerability is fixed in 1.19.4.

Affected

Product or packageVersionsReported by
sparklemotion nokogiri< 1.19.4CVE List
nokogiri (RubyGems)before 1.19.4GitHub Advisory Database
nokogiri nokogiribefore 1.19.4NVD

All scores

GitHub_M1.7low CVSS:4.0CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N/E:U
GitHublow CVSS:4.0CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N/E:U
NVD8.2high CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:H

Classification

CWE-416

SSVC (CISA-ADP): exploitation none, automatable no, technical impact partial.

What each source says

CVE List

Nokogiri is an open source XML and HTML library for the Ruby programming language. Prior to 1.19.4, calling Document#encoding= with an invalid encoding (e.g., a non-string, or a string containing a null byte) raises an exception, but only after freeing the document's current encoding string without replacing it. The document is left referencing freed memory, so the next call to Document#encoding reads invalid memory, which can cause a segfault or leak freed bytes into a Ruby String. Affects the CRuby (libxml2) implementation only; JRuby is not affected. This vulnerability is fixed in 1.19.4.

GitHub Advisory Database
Summary

Calling Document#encoding= with an invalid encoding (e.g., a non-string, or a string containing a null byte) raises an exception, but only after freeing the document's current encoding string without replacing it. The document is left referencing freed memory, so the next call to Document#encoding reads invalid memory, which can cause a segfault or leak freed bytes into a Ruby String.

Affects the CRuby (libxml2) implementation only; JRuby is not affected.

Severity

The Nokogiri maintainers have evaluated this as low severity. Reaching it requires an unusual API-usage pattern that does not arise during normal use. The application must pass an invalid encoding to Document#encoding=, rescue the resulting exception, and then continue using the same document. Nokogiri 1.19.4 makes this pattern safe with no change to the public API. The document no longer references freed memory after the exception is raised.

Mitigation

Upgrade to Nokogiri 1.19.4 or later.

If users are unable to upgrade, avoid passing attacker-controlled values to Document#encoding=. Applications that only assign developer-authored encodings are not directly exposed.

Credit

This issue was responsibly reported by Zheng Yu from depthfirst.com.

NVD (es)

Nokogiri es una biblioteca XML y HTML de código abierto para el lenguaje de programación Ruby. Antes de la versión 1.19.4, al llamar a Document#encoding= con una codificación no válida (por ejemplo, una no-cadena, o una cadena que contiene un byte nulo) se lanza una excepción, pero solo después de liberar la cadena de codificación actual del documento sin reemplazarla. El documento queda referenciando memoria liberada, por lo que la siguiente llamada a Document#encoding lee memoria no válida, lo que puede causar un segfault o filtrar bytes liberados en una cadena Ruby. Afecta solo a la implementación de CRuby (libxml2); JRuby no se ve afectado. Esta vulnerabilidad está corregida en la versión 1.19.4.

References

Sources

SourceRecordLicence
CVE ListCVE-2026-57236 changed 2026-06-25CVE Terms of Use
GitHub Advisory DatabaseGHSA-5v8h-3h3q-446p changed 2026-09-10CC-BY-4.0
NVDCVE-2026-57236 changed 2026-10-25Public domain