Summary
Kysely's MySQL query compiler previously escaped only single quotes in string literals and failed to correctly handle backslashes, leaving generated SQL vulnerable to injection.
In environments where MySQL's NO_BACKSLASH_ESCAPES is disabled (the default), an attacker controlling values used in certain schema-building APIs could break out of string literals and inject arbitrary SQL during index or view creation.
Affected context
This vulnerability affects applications using Kysely with:
- The MySQL dialect (or compatible engines honoring MySQL backslash escapes)
- Default MySQL configuration where
NO_BACKSLASH_ESCAPESis OFF - Code paths that rely on
ImmediateValueTransformerto inline values into SQL,
particularly:
CreateIndexBuilder.where()CreateViewBuilder.as()
The issue becomes relevant when untrusted or partially trusted data can influence string values passed into these builders, and the resulting SQL is executed against a MySQL database.
Exploit preconditions
- Attack surface: Application code that generates DDL or schema-related SQL through Kysely, especially index predicates and view definitions.
- Required access: Ability for an attacker to supply or influence string values that are passed into APIs ultimately handled by
ImmediateValueTransformerand compiled byDefaultQueryCompiler.sanitizeStringLiteral(). - Trigger point: Execution of SQL statements that inline attacker-controlled values into MySQL string literals without correctly escaping backslashes.
- Why exploitation is possible: The sanitizer doubles single quotes but leaves backslashes unescaped. In MySQL with
NO_BACKSLASH_ESCAPESturned OFF, a backslash can escape the closing quote of a string literal, causing the rest of the attacker-controlled input to be interpreted as SQL syntax rather than data.
Why this happens
The core issue is incomplete server-side escaping of string literals in SQL generated by Kysely's MySQL compiler.
Specifically, DefaultQueryCompiler.sanitizeStringLiteral() performs quote escaping (' → '') but does not account for MySQL's use of the backslash as an escape character when NO_BACKSLASH_ESCAPES is OFF.
This maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where user-controlled input is inlined into SQL without robust neutralization of all special characters relevant to the target database dialect.
Because ImmediateValueTransformer chooses to inline certain values directly into the query string instead of using parameterized placeholders, unsafe escaping of backslashes allows attacker-controlled content to alter the intended SQL structure.
Potential impact
- Attackers could inject arbitrary SQL fragments into index predicates or view definitions.
- Malicious SQL could modify or drop tables, indexes, or views, impacting database integrity.
- Crafted injections could expose sensitive data through expanded view definitions or side-channel queries.
- Persistent schema-level changes may facilitate further abuse, such as logic manipulation or privilege escalation in the application's data layer.
General mitigation guidance
Effective mitigation focuses on eliminating unsafe string inlining and ensuring dialect-aware escaping:
- Favor parameterized queries or bind parameters wherever possible, avoiding direct inlining of user-controlled values into SQL.
- When literals must be inlined, use escaping routines that are explicitly designed for the specific SQL dialect, covering all special characters, including backslashes for MySQL in default configurations.
- Restrict untrusted input from influencing schema-building APIs such as index or view builders; confine those APIs to static, code-defined values.
- Consider enforcing safer MySQL modes (such as enabling
NO_BACKSLASH_ESCAPES) as a defense-in-depth measure, while not relying solely on server configuration for security. - Apply the principle of least privilege to database accounts so that, even if injection occurs, the executing role cannot perform destructive schema operations.
How teams detect issues like this
Organizations can detect this class of issue through a combination of code and behavior analysis:
- Static analysis and code review:
- Flag manual string concatenation or inlining of variables into SQL fragments, particularly in schema-building code.
- Identify uses of query builders that inline values rather than using parameters, especially around MySQL-specific compilation paths.
- Review escaping utilities to confirm that they are dialect-aware and uniformly applied.
- Policy and tooling:
- Add linting or custom static-analysis rules to disallow direct construction of SQL literals with unescaped or partially escaped user input.
- Use automated code review tools to highlight uses of lower-level compiler hooks or transformers that bypass parameterization.
- Runtime monitoring:
- Enable SQL logging and alert on anomalous DDL statements or unexpected index/view definitions originating from application paths that normally perform read or simple write operations.
- Monitor for repeated schema changes or unusual query patterns that may indicate abuse of injection primitives.

