GHSA-5v8h-3h3q-446p: Nokogiri Possible Use-After-Free When `Nokogiri::XML::Document#encoding=` Raises Vulnerability

LOW
CWE-416
Published
Advisory Summary

GHSA-5v8h-3h3q-446p is a LOW possible use-after-free when `nokogiri::xml::document#encoding=` raises vulnerability (CWE-416: Use After Free) affecting Nokogiri libraries in the RubyGems ecosystem. The issue involves possible use-after-free when `nokogiri::xml::document#encoding=` raises an exception. Remediation is available by upgrading affected packages to version 1.19.4.

GHSA-5v8h-3h3q-446p at a Glance

CVE Identifier N/A
GHSA Identifier GHSA-5v8h-3h3q-446p
Severity LOW
CVSS v3.1 Base Score N/A
CWE Classification CWE-416: Use After Free
Affected Product Nokogiri
Package Ecosystem RubyGems
Published Date
Last Updated

How to Fix GHSA-5v8h-3h3q-446p

To remediate this vulnerability, upgrade all affected Nokogiri dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

nokogiri fixed: 1.19.4
Upgrade nokogiri to 1.19.4
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

inspect dependencies for nokogiri

How to Check if You Are Affected

To determine whether your application is affected by GHSA-5v8h-3h3q-446p, inspect your project dependencies for Nokogiri:

inspect dependencies for nokogiri

Analyze the terminal output to locate any version of Nokogiri in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 1.19.4), your application is affected.

Note: Verify declared build manifests and deployed runtime container environments.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-416: Use After Free, where the product reuses or references memory after it has been freed. at some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation. any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer. In Nokogiri, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### 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.

GHSA-5v8h-3h3q-446p Affected Nokogiri Versions

1 component(s)
Fixed Versions:
1.19.4
Product / Package Ecosystem Affected Versions Fixed Version
nokogiri
RubyGems
< 1.19.4
1.19.4

References

Frequently Asked Questions

What is GHSA-5v8h-3h3q-446p?
GHSA-5v8h-3h3q-446p is a security vulnerability in Nokogiri involving Possible Use-After-Free when `Nokogiri::XML::Document#encoding=` raises an exception under CWE-416: Use After Free.
Which Nokogiri versions are affected?
Affected versions include releases prior to 1.19.4 across affected packages: nokogiri.
What version fixes GHSA-5v8h-3h3q-446p?
Upgrading to 1.19.4 (or the latest patched release for your respective branch) resolves the vulnerability.
Is nokogiri affected?
Yes, nokogiri is affected in releases prior to 1.19.4. Upgrade your dependency to 1.19.4 or later.
What CWE is associated with GHSA-5v8h-3h3q-446p?
GHSA-5v8h-3h3q-446p is classified under CWE-416: Use After Free (The product reuses or references memory after it has been freed. At some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation. Any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer.).
How can I check RubyGems dependencies for GHSA-5v8h-3h3q-446p?
Run the dependency tree command: 'inspect dependencies for nokogiri'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-5v8h-3h3q-446p",
  "modified" : "2026-06-19T16:35:58Z",
  "published" : "2026-06-19T16:35:58Z",
  "aliases" : [ ],
  "summary" : "Nokogiri: Possible Use-After-Free when `Nokogiri::XML::Document#encoding=` raises an exception",
  "details" : "### Summary\n\nCalling `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`.\n\nAffects the CRuby (libxml2) implementation only; JRuby is not affected.\n\n### Severity\n\nThe 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.\n\n### Mitigation\n\nUpgrade to Nokogiri 1.19.4 or later.\n\nIf users are unable to upgrade, avoid passing attacker-controlled values to `Document#encoding=`. Applications that only assign developer-authored encodings are not directly exposed.\n\n### Credit\n\nThis issue was responsibly reported by Zheng Yu from depthfirst.com.",
  "severity" : [ {
    "type" : "CVSS_V4",
    "score" : "CVSS: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"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "RubyGems",
      "name" : "nokogiri"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "1.19.4"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/sparklemotion/nokogiri/security/advisories/GHSA-5v8h-3h3q-446p"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/sparklemotion/nokogiri"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-416" ],
    "severity" : "LOW",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-06-19T16:35:58Z",
    "nvd_published_at" : null
  }
}