Nokogiri: Possible Use-After-Free when `Nokogiri::XML::Document#encoding=` raises an exceptionGHSA-5v8h-3h3q-446p
GHSA-5v8h-3h3q-446p is a Use After Free vulnerability in nokogiri. A fix is available for nokogiri — see the affected versions and patch details below.
Exploitation Status
No confirmed exploitation observed yet
- CISA’s own triage has not observed active exploitation or public proof-of-concept code for this CVE as of its last assessment.
Exploitation and automatability from CISA’s SSVC triage for GHSA-5v8h-3h3q-446p.
EPSS Exploitation Probability
Probability of exploitation in the next 30 days, from FIRST.org EPSS.
Real-World Exposure
nokogiriReal-time download stats are indexed for npm and PyPI packages. This vulnerability affects RubyGems packages — download data is not available via public APIs for these ecosystems.
Description
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.
Affected Packages
| Ecosystem | Package | Vulnerable range | Fix |
|---|---|---|---|
| 💎RubyGems | nokogiri | all versions | 1.19.4bundle update nokogiri --conservative |
Affected Products
nokogirinokogiriDetection & mitigation playbook
Open-source dependencyDetect
Scan your dependency tree (package-lock.json, pnpm-lock.yaml, requirements.txt, go.sum, etc.) for nokogiri, including transitive dependencies — a direct dependency you never call can still pull in a vulnerable version.
Fix
Update nokogiri to 1.19.4 or later, then make sure no transitive (indirect) dependency still pins the vulnerable range — O3 confirms GHSA-5v8h-3h3q-446p is resolved across your whole dependency graph.
Workarounds
Stop feeding it untrusted input: reject or quarantine files and payloads from unverified sources until you can upgrade, restrict accepted formats to the ones you actually need, and run the parsing or decoding step in a least-privileged sandbox or short-lived worker so a crash or corrupted read cannot reach the rest of the process.
Fixing This On Your OS
If you run this on a Linux distribution, patch through your package manager against the distro's own security advisory below — it tracks the exact backported fix for your release, which can ship on a different timeline (and sometimes a different severity) than the upstream project.
This flaw has an Important impact as Nokogiri's CRuby implementation in Red Hat products is vulnerable to a use-after-free when processing invalid encoding values. A remote unauthenticated attacker can trigger a segmentation fault causing denial of service, or leak freed memory contents into a Ruby String causing…
Mitigation for this issue is either not available or the currently available options do not meet the Red Hat Product Security criteria comprising ease of use and deployment, applicability to widespread installation base, or stability.Source: Red Hat security advisory for GHSA-5v8h-3h3q-446p (CC BY 4.0)
| Product | Fixed in | Advisory |
|---|---|---|
| Red Hat Satellite 6.16 for RHEL 8 | rubygem-nokogiri-0:1.15.7-3.el8sat | RHSA-2026:50223 |
| Red Hat Satellite 6.17 for RHEL 9 | rubygem-nokogiri-0:1.15.7-3.el9sat | RHSA-2026:50222 |
| Red Hat Satellite 6.18 for RHEL 9 | rubygem-nokogiri-0:1.15.7-3.el9sat | RHSA-2026:50263 |
| Red Hat Satellite 6.19 for RHEL 9 | rubygem-nokogiri-0:1.17.2-3.el9sat | RHSA-2026:50221 |
Frequently Asked Questions
Is GHSA-5v8h-3h3q-446p in your dependencies?
Find it across RubyGems, including transitive dependencies.