CVE-2026-57437: Nokogiri XPathContext Use-After-Free Memory Safety Vulnerability
Nokogiri, a widely-used Ruby library for parsing XML and HTML, contains a memory safety issue in its XPath evaluation feature. If application code manually creates an XPathContext object and then allows the underlying XML/HTML document to be garbage collected while the context is still in use, subsequent XPath queries can read from freed memory, potentially crashing the application. This is a narrow vulnerability—it requires specific patterns in application code and cannot be triggered by feeding a malicious document to the library. The standard document search methods (Document#xpath, Document#css) are not affected.
Source data · NVD / CISA · public domain
- CVSS
- 3.1 · 5.3 MEDIUM · CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L
- Weaknesses (CWE)
- CWE-416
- Affected products
- 1 configuration(s)
- Published / Modified
- 2026-06-25 / 2026-06-26
NVD description (verbatim)
Nokogiri is an open source XML and HTML library for the Ruby programming language. Prior to 1.19.4, Nokogiri::XML::XPathContext did not keep its source document alive for garbage collection. If an XPathContext outlived its document and the document was collected, evaluating an XPath expression could read invalid memory and potentially segfault. This is only reachable when application code constructs an XPathContext directly and lets the document become unreachable while continuing to use the context. The normal Document#xpath, #css, and related search methods are not affected, and it is not triggerable by malicious document input. This vulnerability is fixed in 1.19.4.
1 reference(s) · View on NVD →
SEC.co analysis · AI-assisted, reviewed against source
Technical summary
The vulnerability exists in Nokogiri's XPathContext class, which maintains a reference to its source document for query execution. In versions before 1.19.4, the context did not prevent its document from being garbage collected, creating a use-after-free condition (CWE-416). When an application constructs an XPathContext directly, stores it, and allows the parent document to become unreachable without actively holding a strong reference, the garbage collector may reclaim the document's memory. Subsequent calls to evaluate XPath expressions on that context will then operate on dangling pointers. The CVSS 3.1 score of 5.3 (MEDIUM) reflects limited exploitability: the attack vector is network-accessible, but the condition must be triggered by vulnerable application code patterns, not by document content alone.
Business impact
For most Nokogiri users, this vulnerability poses minimal risk because standard library usage patterns—Document#xpath and Document#css—are not affected. However, applications that directly instantiate XPathContext objects while relying on implicit garbage collection of the document face availability risk. An attacker able to trigger the vulnerable code path could cause a denial of service through application crashes. Data confidentiality and integrity are not at risk. Organizations heavily dependent on Ruby-based XML/HTML processing should review whether their code uses XPathContext directly; if so, prioritize upgrading.
Affected systems
Nokogiri versions prior to 1.19.4 are affected. Ruby applications using this library are potentially at risk only if they construct XPathContext objects directly and do not maintain explicit references to the underlying document. This affects a limited subset of Nokogiri users, as most applications rely on the higher-level Document methods which manage the context-document lifecycle safely.
Exploitability
This vulnerability is difficult to exploit in practice. It requires two conditions: (1) application code must explicitly create XPathContext objects rather than using Document#xpath or Document#css, and (2) the source document must become unreachable while the context is still in use. An attacker cannot trigger this through malicious XML or HTML input alone. The vulnerability is exploitable by remote attackers only in applications with this specific architectural pattern. The lack of KEV designation reflects the narrow attack surface.
Remediation
Upgrade Nokogiri to version 1.19.4 or later. This version fixes the issue by ensuring XPathContext maintains a reference to its source document, preventing premature garbage collection. No configuration changes or workarounds are necessary. Organizations should apply this update during normal maintenance windows; while the risk is limited, updating removes the vulnerability entirely.
Patch guidance
Update the nokogiri gem to version 1.19.4 or newer. If using Bundler, update your Gemfile and run `bundle update nokogiri`. Verify the upgrade with `gem list nokogiri` or check your application's lock file. Test any code that directly uses XPathContext to ensure compatibility, though the fix is internal and should not require application code changes. Consult the vendor's release notes at nokogiri.org for full details on this and concurrent security or stability improvements.
Detection guidance
Review your Ruby application codebase for direct instantiation of Nokogiri::XML::XPathContext or Nokogiri::HTML::XPathContext. Search for patterns like `XPathContext.new` or direct context construction outside of the Document class. Most applications using standard patterns (Document#xpath, Document#css) require no investigation. If you find direct XPathContext usage, verify that the source document is held in scope for the lifetime of the context. Monitor application logs for segmentation faults or unexpected crashes that might indicate memory corruption. Upgrade to 1.19.4 to eliminate the risk regardless of code patterns.
Why prioritize this
While the CVSS score is MEDIUM (5.3), the practical risk is lower due to limited exploitability: this is not a vulnerability in the library's default behavior or triggered by document content. Prioritize this update for applications known to use XPathContext directly, or as part of routine dependency updates. Organizations without such code patterns can schedule this as a normal update cycle, though addressing it promptly is still prudent given it is a memory safety issue.
Risk score, explained
The CVSS 3.1 score of 5.3 reflects a network-reachable denial-of-service condition (availability impact only) with no confidentiality or integrity risk. The score does not account for the narrow attack surface—vulnerability requires direct XPathContext usage by the application, not by the library's standard API. This makes real-world risk lower than the numeric score suggests. The vector (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L) indicates no privilege or user interaction required, but 'AC:L' assumes the vulnerable code path is reachable, which in practice is limited.
Frequently asked questions
Will upgrading Nokogiri break my application?
No. Version 1.19.4 contains only a fix to reference management and no breaking API changes. If your application uses the standard Document#xpath or Document#css methods, there is no functional difference. Even if you use XPathContext directly, the fix is internal; your code should work identically but safely.
How do I know if my application is vulnerable?
Search your codebase for direct creation of Nokogiri::XML::XPathContext or Nokogiri::HTML::XPathContext. If you only use Document#xpath and Document#css (the most common patterns), you are not vulnerable. If you do use XPathContext directly, check whether the source document is held in scope as long as the context is used. Upgrade to 1.19.4 to eliminate any doubt.
Is this vulnerability exploitable through malicious XML or HTML files?
No. The vulnerability cannot be triggered by document content alone. It requires application code to explicitly create an XPathContext object and then allow the source document to be garbage collected. Feeding a malicious document to a standard Nokogiri application is not a viable attack vector.
What should I do if I cannot upgrade immediately?
If upgrading is blocked, review your code for direct XPathContext usage and ensure the source document is kept in memory as long as the context is needed. This may require refactoring to store explicit references. However, this is a temporary workaround; plan to upgrade to 1.19.4 as soon as possible.
This analysis is based on information available as of June 26, 2026. CVE details, vendor advisories, and patch availability are subject to change. Always verify patch version numbers and compatibility against official Nokogiri release notes and your vendor's security bulletin before deploying updates. This intelligence does not constitute professional security advice; consult your organization's security team for guidance specific to your environment and risk profile. Source: NVD (public-domain), retrieved 2026-08-03. Analysis generated by SEC.co (claude-haiku-4-5).
Related vulnerabilities
- CVE-2026-57436MEDIUMNokogiri XML/HTML Library Use-After-Free Vulnerability
- CVE-2026-57438MEDIUMNokogiri XInclude Use-After-Free Vulnerability (CVSS 6.6)
- CVE-2026-57236HIGHNokogiri Use-After-Free in Document Encoding (CVSS 8.2)
- CVE-2026-57435HIGHNokogiri XML Attribute Memory Corruption (DoS)
- CVE-2025-55644MEDIUMHeap Use-After-Free in GPAC MP4Box v2.4 DoS Vulnerability
- CVE-2025-55650MEDIUMHeap Use-After-Free in GPAC MP4Box v2.4 DoS Vulnerability
- CVE-2025-60466MEDIUMGPAC MP4Box Use-After-Free DoS Vulnerability
- CVE-2025-60471MEDIUMUse-After-Free in GPAC MP4Box Before 26.02.0 Denial of Service