CVE-2024-39677
Patch Available.
This Vulnerability has been fixed in the Never-Ending Support (NES) version offered by HeroDevs.
Overview
NHibernate is an object-relational mapper for .NET that maps application classes to relational database tables and generates the SQL to load and save them. HeroDevs NES Essentials Plus maintains an NHibernate fork for .NET environments that need continued security fixes after End-of-Life while staying on the NES package line.
A SQL injection vulnerability (CVE-2024-39677) exists in NHibernate types that implement ILiteralType.ObjectToSQLString. These types wrapped values in quotes without escaping them and formatted numbers and dates with the current culture, so a crafted value or culture setting could change the SQL NHibernate generates.
Per CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), the weakness is a product constructing an SQL command from externally influenced input without neutralizing elements that can modify the intended command.
Exposure is conditional. Only applications that reach ObjectToSQLString are affected: inheritance mappings with discriminator values, HQL queries that reference a static field of the application, the AddColumn overloads of SqlInsertBuilder and SqlUpdateBuilder that take a literal value, and direct calls to ObjectToSQLString. Ordinary parameterized queries are not affected. Upstream fixed the 5.4 and 5.5 lines only, so the 4.0 line remains vulnerable without NES.
Details
Module Info
- Product: NHibernate
- Affected packages: NHibernate (an independently versioned NuGet package, not part of .NET)
- Affected versions:
- NHibernate < 5.4.9
- NHibernate >= 5.5.0, < 5.5.2
- NES Essentials Plus NHibernate 4.0.0.x < 4.0.0.4002
- Package manager: NuGet
- GitHub repository: https://github.com/nhibernate/nhibernate-core
- Published packages: NHibernate on NuGet
- Fixed in: NES Essentials Plus NHibernate 4.0.0.4002
Vulnerability Info
This High-severity vulnerability is found in the NHibernate package in versions before 5.4.9 and 5.5.2, and in the NES Essentials Plus fork in the 4.0.0.x line before 4.0.0.4002.
NHibernate sometimes writes a value into SQL as a literal instead of a parameter, for example a discriminator value that identifies a subclass. Each type's ObjectToSQLString builds that literal. The string, character, Guid, Uri and date and time types put quotes around the value without doubling quotes inside it, and numeric types called ToString with the current culture. A value containing a quote, or a culture whose negative sign or date format has been altered, could close the literal early and append SQL.
The fix adds Dialect.ToStringLiteral, which doubles quotes, escapes backslashes on databases that treat them as escape characters, and adds an N prefix to Unicode strings where the database needs one. The affected types now build literals through it, and numeric types format with the invariant culture. HeroDevs investigation found that in 4.0, DateTimeType and TimestampType carry their own literal code that 5.x inherits from a shared base class, so the NES fix covers both directly.
The advisory's lower bound is open (< 5.4.9), so the weakness is present in every earlier NHibernate release, including the 4.0 line the NES fork tracks.
Mitigation
NHibernate 4.0 is End-of-Life and will not receive any updates to address this issue. The upstream fix is available only in NHibernate 5.4.9 and 5.5.2 and later.
Users of the affected components should apply one of the following mitigations:
- Upgrade affected applications to one of:
- NHibernate >= 5.4.9
- NHibernate >= 5.5.2
- Until patched, keep quotes out of string discriminator values in mappings, and do not let untrusted code change the current culture's number and date formats.
- Leverage a commercial support partner like HeroDevs for post-EOL security support, including NES Essentials Plus NHibernate 4.0.0.4002 or later.
Credits
The GitHub advisory credits fredericDelaporte (remediation developer) and hazzik (remediation reviewer). No finder is publicly credited.