/

CVE-2026-39356

Severity:

HIGH RISK

(7.5)

SQL Injection in Drizzle ORM

CVE-2026-39356

Analyzed

Vulnerability type

Category:

SQL Injection

CWE:

CWE-89

Impact:

Arbitrary SQL Injection and Data Compromise

Severity:

HIGH RISK

(7.5)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

Drizzle ORM contained a SQL injection issue in its dialect-specific escapeName() implementations, where quoted SQL identifiers were not properly escaped before being wrapped in quotes or backticks.

Applications that pass attacker-controlled values into APIs that construct SQL identifiers or aliases (for example, sql.identifier(), .as()) may allow an attacker to break out of the quoted identifier context and inject arbitrary SQL under certain conditions.

Affected context

This vulnerability affects applications using Drizzle ORM versions prior to 0.45.2 and 1.0.0-beta.20 that:

  • Dynamically construct SQL identifiers (table names, column names, aliases, etc.)
  • Pass untrusted or partially untrusted input into identifier-related APIs such as sql.identifier() or .as()
  • Rely on Drizzle's dialect-specific escapeName() logic to safely quote and escape identifiers

The issue is most relevant where identifier values can be influenced by user input, external configuration, or multi-tenant routing data that has not been strictly validated.

Exploit preconditions

  • Attack surface: SQL queries generated through Drizzle ORM that incorporate dynamic identifiers or aliases.
  • Required access: Ability for an attacker to influence values passed into identifier construction APIs (e.g., request parameters, headers, tenant IDs, or other user-controlled fields).
  • Trigger point: Use of Drizzle APIs such as sql.identifier() or .as() that delegate to escapeName() to quote identifiers, combined with unvalidated attacker-controlled content.
  • Why exploitation is possible: Drizzle's identifier escaping did not sanitize embedded identifier delimiters (such as backticks or quotes) inside the untrusted identifier. When wrapped again in quotes/backticks, these delimiters allow the attacker to terminate the identifier context and append arbitrary SQL fragments.

Why this happens

The root cause is an incomplete escaping strategy within Drizzle ORM's escapeName() implementations for different SQL dialects.

Instead of treating identifier delimiters (like " or `) as untrusted characters needing duplication or escaping inside quoted identifiers, the code wrapped the entire input in delimiters without neutralizing any delimiters present in the input itself. This creates a variant of CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), focused on identifier construction rather than value binding. The logic assumed that quoting alone was sufficient to protect against injection, but failed to account for attacker-controlled text that already contains the terminating quote or backtick sequence.

Potential impact

  • Attacker-controlled modification of SQL queries by closing a quoted identifier and appending arbitrary SQL.
  • Unauthorized reading of sensitive data by injecting additional SELECT clauses or expanding query scope.
  • Unauthorized modification or deletion of data via injected UPDATE, INSERT, or DELETE statements where the database driver and context permit stacked queries.
  • Circumvention of row-level or tenant isolation logic that relies on dynamically constructed identifiers.
  • Potential execution of database-specific dangerous functions, depending on privileges and DBMS configuration.

General mitigation guidance

Effective mitigation focuses on safely handling dynamic identifiers and reducing trust in any data used to construct SQL strings:

  • Avoid passing user input directly into APIs that construct identifiers or aliases; prefer fixed, hard-coded identifier sets where possible.
  • Introduce strict allowlists for dynamic identifiers (e.g., mapping user-supplied keys to a predefined set of column or table names).
  • Use ORM or query-builder features that separate identifiers and values, relying on safe, library-provided identifier escaping rather than manual string concatenation.
  • Apply rigorous validation and normalization to any value used as an identifier (e.g., limiting to ASCII letters, digits, and underscores) before it reaches query-building APIs.
  • Apply principle of least privilege at the database level so that, if injection occurs, the impact is constrained by limited roles and permissions.

How teams detect issues like this

Detection typically combines code-level review and automated analysis:

  • Static analysis: Search for uses of sql.identifier(), .as(), or other identifier-construction APIs where arguments originate from HTTP parameters, request bodies, headers, environment variables, or other untrusted sources.
  • Code review signals: Look for conditional or dynamic selection of table/column/alias names based on user-controlled values without clear allowlists or validation. Pay attention to any string concatenation around Drizzle-generated SQL fragments.
  • Security testing: Use fuzzing or targeted penetration tests that inject delimiter characters (quotes, backticks, closing brackets) into suspected identifier inputs and observe for SQL errors or anomalous behavior.
  • Automated tooling: Integrate SAST tools or custom linters into CI to flag patterns where untrusted data controls identifiers, and add unit tests that verify correct escaping of edge cases in identifier handling.

Sources

SourceLink
github.comgithub.com/drizzle-team/drizzle-orm/security/advisories/GHSA-gpj5-g38j-94v9

Frequently asked

questions

It can be remotely exploitable if external inputs (such as HTTP request parameters, headers, query strings, or multi-tenant identifiers) are passed into Drizzle APIs that build SQL identifiers or aliases without strict validation or allowlisting.

That depends on where the identifier values come from. If only authenticated users can influence these values (e.g., tenant IDs in a logged-in context), then at least some form of authenticated access is required. If the values can be controlled by unauthenticated users (e.g., open APIs or public endpoints), exploitation may be possible without authentication.

The safest pattern is to avoid constructing identifiers directly from user input. Instead, define a fixed mapping from external values to allowed identifiers, validate inputs against this mapping, and rely on the ORM's corrected escaping logic for the remaining dynamic parts. Treat identifier names like configuration, not free-form user data.

Design query-building practices that:

  • Separate untrusted data from SQL structure (values go through parameters, not concatenation).
  • Restrict dynamic identifiers to controlled, validated sets.
  • Undergo security-focused code review whenever low-level SQL construction or dialect-specific logic is modified.
  • Integrate static analysis and automated tests that specifically target SQL construction paths, including identifiers and aliases, not just value parameters.
Find out what's already
exploitable in your codebase.