/

CVE-2026-33442

Severity:

HIGH RISK

(8.1)

SQL Injection in Kysely Query Compiler

CVE-2026-33442

Analyzed

Vulnerability type

Category:

SQL Injection

CWE:

CWE-89

Impact:

Arbitrary SQL Execution (SQL Injection)

Severity:

HIGH RISK

(8.1)

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

A SQL injection vulnerability in Kysely's query compiler arises from incomplete escaping in the sanitizeStringLiteral method, which escapes single quotes but leaves backslashes unescaped.

On MySQL instances using the default BACKSLASH_ESCAPES SQL mode, an attacker controlling JSON path fragments or similar string inputs can inject a backslash to bypass escaping and execute arbitrary SQL.

Affected context

This issue affects applications using Kysely 0.28.12 or 0.28.13 with:

  • MySQL (or compatible) backends where BACKSLASH_ESCAPES is enabled (the default mode)
  • Query code that relies on sanitizeStringLiteral to safely embed string values, such as JSON paths, directly into SQL
  • User-controlled or otherwise untrusted input being interpolated into SQL strings without parameterization

The vulnerability typically manifests when developers expect Kysely's string sanitization to fully neutralize special characters but are running in a MySQL mode where backslashes still alter literal interpretation.

Exploit preconditions

  • Attack surface: Application endpoints (HTTP APIs, web forms, RPC handlers, background jobs) that feed user-controlled strings into Kysely queries which use sanitizeStringLiteral.
  • Required access: Ability to supply or influence string values embedded into SQL literals (e.g., JSON path components, dynamically constructed fragments).
  • Trigger point: Execution of a query where a value is placed into a string literal and only single quotes are escaped, while MySQL interprets backslashes as escape characters.
  • Why exploitation is possible: In MySQL with BACKSLASH_ESCAPES enabled, a backslash can escape the closing quote, undoing the doubling of single quotes (''), allowing the attacker to terminate the literal prematurely and append arbitrary SQL.

Why this happens

The root cause is an incomplete output-encoding strategy for SQL string literals in the query compiler. The sanitizeStringLiteral method focuses solely on escaping single quotes (' → '') and does not account for backslash semantics under MySQL's default BACKSLASH_ESCAPES mode.

Under these conditions, backslashes inside user-controlled input remain active escape characters, allowing crafted payloads to alter how the SQL parser interprets the surrounding string literal. This aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where a data value that should be treated as inert text can instead influence SQL structure due to insufficient neutralization of meta-characters.

Potential impact

  • Execution of arbitrary SQL statements within the database context of the affected application
  • Unauthorized reading of sensitive data, including user records or internal configuration
  • Modification or deletion of database contents, leading to data corruption or loss
  • Bypassing of application-level authorization checks enforced via SQL conditions
  • Potential establishment of persistence or leveraging database features to aid further compromise, depending on database configuration

General mitigation guidance

Effective mitigation focuses on eliminating reliance on ad-hoc SQL string sanitization and tightening how untrusted data flows into queries:

  • Prefer parameterized queries and bound parameters for all user-controlled values, including JSON path components and other dynamic fragments, rather than embedding them into string literals.
  • When string literals must be constructed, use database-aware escaping routines that correctly handle all meta-characters for the specific SQL dialect and mode in use, rather than hand-rolled escaping logic.
  • Reduce the scope and complexity of dynamic SQL generation so that untrusted input only ever occupies data positions, not structural positions (identifiers, operators, or raw clauses).
  • Apply least privilege at the database level so that the application account has only the minimal permissions necessary; this limits damage if injection occurs.
  • Where operationally feasible, review and harden database configuration (e.g., SQL modes) so that legacy escaping behaviors are understood and accounted for in the application's encoding strategy.

How teams detect issues like this

Detection typically combines code-centric review with runtime safeguards:

  • Static analysis and code review:
  • Flag uses of custom string escaping functions (like sanitizeStringLiteral) that are applied to untrusted inputs before interpolation into SQL.
  • Search code for concatenation or template literals used to construct SQL strings that contain ${userInput} or similar patterns.
  • Check for logic that assumes escaping a single character class (e.g., only ') is sufficient across all configured database modes.
  • Automated tooling:
  • Integrate SAST/linters with rules targeting string-based SQL construction in TypeScript/JavaScript, emphasizing Kysely query builder usage patterns.
  • Use dependency checking tools to identify known-vulnerable versions of libraries and enforce upgrade policies.
  • Runtime and operational monitoring:
  • Enable logging of SQL queries and alert on anomalous patterns, such as unexpected comment markers, stacked statements (where allowed), or tautological conditions.
  • Employ a database firewall or query allowlist/denylist system, where appropriate, to block clearly malicious patterns.

Sources

SourceLink
github.comgithub.com/kysely-org/kysely/security/advisories/GHSA-fr9j-6mvq-frcv

Frequently asked

questions

Yes, it can be remotely exploitable if an external client (such as a web or API consumer) can influence string values that are embedded into SQL literals through Kysely. Wherever user-controlled input reaches sanitizeStringLiteral and is used in MySQL queries, a remote attacker may be able to trigger SQL injection.

The attacker needs the ability to supply or control data that the application passes into Kysely as string values that end up in SQL string literals. No direct database access is required; the attack is carried out through the application's own query paths.

The safest pattern is to avoid manual string sanitization for SQL entirely and use parameterized queries and bound parameters for all untrusted values. Where literal construction is unavoidable, use robust, dialect-aware escaping mechanisms that correctly handle all special characters relevant to the database and its SQL mode.

Standardize on secure-by-default query construction practices: always treat untrusted input as data, not as query syntax; enforce parameterization in coding guidelines and CI checks; use automated analysis to flag dynamic SQL concatenation; and ensure that library abstractions are used as intended rather than bypassed with manual escaping logic.

Find out what's already
exploitable in your codebase.