Nokogiri, Use-After-Free, GHSA-5v8h-3h3q-446p (Low) -DC-Jun2026-484

Listen to this Post

How the CVE Works

This vulnerability resides in the `Nokogiri::XML::Documentencoding=` method, which is used to set the character encoding of an XML document. The issue is triggered when this method is called with an invalid encoding argument—for example, a non-String object or a String containing a null byte.
Under normal operation, when a valid encoding is provided, the method safely updates the document’s internal encoding reference. However, when an invalid encoding is passed, the method raises an exception. The critical flaw is the order of operations during this error path: the method frees the document’s current encoding string before raising the exception, but it fails to replace the freed memory with a new, valid reference.
This leaves the `Nokogiri::XML::Document` object in a corrupted state, with its internal encoding pointer now pointing to memory that has been freed and may be reused by the Ruby interpreter or the underlying libxml2 library. If the application rescues the exception and continues to use the same document object—for instance, by calling `Documentencoding` again—the method will attempt to read from this freed memory.
This use-after-free condition can lead to two primary outcomes:
1. Segmentation Fault (Segfault): The freed memory may have been reallocated for another purpose, and attempting to access it can cause the Ruby process to crash.
2. Information Leak: In some cases, the freed memory might still contain sensitive data (e.g., strings from other parts of the application). The `Documentencoding` method could return these freed bytes as part of a Ruby String, potentially leaking confidential information.
It is important to note that this vulnerability only affects the CRuby (C-based) implementation of Nokogiri, which uses the libxml2 library. The JRuby implementation, which uses a different XML parser, is not affected.
The Nokogiri maintainers have classified this as a low severity issue. Exploitation requires a specific and unusual API usage pattern: the application must intentionally pass an invalid encoding, rescue the resulting exception, and then continue operating on the same document instance. This pattern is not common in normal, well-behaved code.

DailyCVE Form

Platform: ……. Nokogiri
Version: …….. < 1.19.4
Vulnerability :…… Use-After-Free
Severity: ……. Low
date: ………. 2026-06-18

Prediction: …… 2026-06-18

What Undercode Say

This section provides analytical insights and technical details related to the vulnerability.

Affected Method: `Nokogiri::XML::Documentencoding=`

Root Cause: Improper memory management during exception handling.

CWE: CWE-416: Use After Free

Vulnerable Code Pattern:

doc = Nokogiri::XML::Document.new
begin
doc.encoding = invalid_encoding e.g., nil, or a string with a null byte
rescue => e
The exception is rescued, but the document is now in a corrupted state.
end
The following call triggers the use-after-free.
puts doc.encoding

Fixed Code Pattern (Nokogiri 1.19.4+):

In the patched version, the document’s encoding string is not freed until after the encoding argument has been validated and a new reference is securely in place. If an exception is raised, the document’s internal state remains consistent and does not point to freed memory.

Exploit

Exploiting this vulnerability requires a specific sequence of actions:
1. Trigger the Exception: Call `Documentencoding=` with an invalid argument.
2. Rescue the Exception: The application must catch the exception to prevent it from terminating the program.
3. Reuse the Corrupted Document: After rescuing the exception, the application must perform an operation that reads the document’s encoding, such as calling Documentencoding.
A successful exploit can lead to a denial of service (crash) or, under specific memory conditions, information disclosure. The following is a conceptual demonstration:

Simulate an attacker-controlled input
malicious_encoding = "UTF-8\0Invalid"
doc = Nokogiri::XML::Document.new
doc.encoding = "UTF-8" Set a valid encoding initially
begin
This will raise an exception due to the null byte
doc.encoding = malicious_encoding
rescue ArgumentError => e
The exception is caught, but the document is now vulnerable.
puts "Exception caught: {e.message}"
end
The next call reads from freed memory, potentially causing a crash or leak.
puts "Current encoding: {doc.encoding}"

Protection

The primary and recommended mitigation is to upgrade to Nokogiri version 1.19.4 or later. This version includes a fix that ensures the document no longer references freed memory after an exception is raised.
If an immediate upgrade is not possible, the following workarounds can reduce risk:
– Avoid Rescuing Exceptions: Do not rescue exceptions from `Documentencoding=` and continue using the same document. If an exception is raised, treat the document object as invalid and discard it.
– Validate Input: Ensure that any value passed to `Documentencoding=` is a valid, trusted string. Applications that only use hard-coded, developer-authored encodings are not exposed to this vulnerability.
– Avoid Attacker-Controlled Data: Do not pass user-supplied or untrusted data to the `Documentencoding=` method.

Impact

  • Denial of Service (DoS): The most likely impact is a segmentation fault, causing the Ruby application to crash.
  • Information Disclosure: In specific scenarios, the vulnerability could allow an attacker to read freed memory, potentially exposing sensitive data (e.g., strings, session tokens, or other internal information) that was previously stored in that memory location.
  • Low Severity: The impact is limited because the vulnerability is difficult to trigger in typical applications and requires an unusual error-handling pattern.

🎯Let’s Practice Exploiting & Learn Patching For Free:

🎓 Live Courses & Certifications:

Join Undercode Academy for Verified Certifications

🚀 Request a Custom Project:

Secure, high-velocity infrastructure and disruptive technological engineering. Contact our engineering team for high-tier development and proprietary systems:
[email protected]
💎 Smart Architecture | 🛡️ Secure by Design | ⭐ Trusted by Thousands

Sources:

Reported By: github.com
Extra Source Hub:
Undercode

🔐JOIN OUR CYBER WORLD [ CVE News • HackMonitor • UndercodeNews ]

💬 Whatsapp | 💬 Telegram

📢 Follow DailyCVE & Stay Tuned:

𝕏 formerly Twitter 🐦 | @ Threads | 🔗 Linkedin Featured Image

Scroll to Top