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_ESCAPESis enabled (the default mode) - Query code that relies on
sanitizeStringLiteralto 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_ESCAPESenabled, 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.

