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 toescapeName()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.

