Summary
Kysely versions up to and including 0.28.11 contain a SQL injection vulnerability in the JSON path compilation logic for MySQL and SQLite dialects.
Unescaped user-controlled values passed via .key() and .at() are concatenated into single-quoted JSON path string literals, allowing attackers to break out of the JSON path context and inject arbitrary SQL when those values are influenced by untrusted input.
Affected context
The vulnerability affects applications using Kysely (≤ 0.28.11) with the MySQL or SQLite dialects that:
- Construct JSON queries using Kysely's JSON path helpers (e.g.,
.key(),.at()), - Allow user-supplied or otherwise untrusted data to control JSON path segments, and
- Rely on Kysely to safely build the final SQL query string.
It is most relevant where JSON path fragments are derived directly or indirectly from request parameters, form data, or other external sources without additional validation or escaping.
Exploit preconditions
- Attack surface: Application endpoints or features that:
- Use Kysely to build SQL queries against MySQL or SQLite, and
- Expose JSON path selection or indexing as user-controllable input.
- Required access: Ability to supply or influence the values passed into
.key()or.at()(e.g., authenticated or unauthenticated HTTP requests, API calls, or other data entry points). - Trigger point: Execution of a Kysely-built query that compiles a JSON path containing the attacker-controlled key/index, which is then emitted as part of a single-quoted JSON path string in the final SQL.
- Why exploitation is possible: The
visitJSONPathLeg()function directly concatenates user-controlled input into a single-quoted JSON path literal without escaping embedded single quotes. This lets an attacker terminate the JSON path string and append arbitrary SQL, bypassing the protections that parameterization offers for other query components.
Why this happens
The underlying bug is a classic SQL injection pattern arising from improper manual construction of SQL fragments that are not parameterizable.
Specifically, the JSON path compiler for MySQL and SQLite uses a function (visitJSONPathLeg()) that builds JSON path string literals like '$.key' by directly appending values from .key() and .at() calls. Unlike identifiers protected by sanitizeIdentifier(), these JSON path segments are not subjected to consistent escaping logic. Because single quotes inside the user-controlled segments are not doubled or escaped, they can prematurely terminate the surrounding single-quoted literal. This behavior maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where untrusted input is inserted into SQL commands without correct neutralization.
Potential impact
- Attackers may inject arbitrary SQL clauses into affected queries.
- Attackers could exfiltrate sensitive data from other tables or rows beyond the intended JSON field.
- Attackers may modify, insert, or delete database records, leading to integrity loss.
- Under certain database configurations, attackers could execute stored procedures or invoke dangerous database functions, potentially escalating impact to broader system compromise.
- Application logic that depends on database integrity and query correctness can be subverted, causing privilege or authorization bypass.
General mitigation guidance
Effective mitigation focuses on eliminating direct string concatenation of untrusted values into non-parameterizable SQL constructs such as JSON paths:
- Treat JSON paths and similar SQL fragments (e.g., ORDER BY, identifiers, function names) as separate trust domains from normal parameterized values; ensure they are never directly derived from untrusted input without strict controls.
- Introduce a dedicated escaping routine for JSON path segments that:
- Properly escapes or doubles single quotes inside keys and indices, and
- Aligns with the database vendor's JSON path syntax rules.
- Prefer whitelisting/allow-listing patterns for JSON keys and indices by:
- Mapping user-facing options (e.g., field selectors) to a fixed internal set of known-safe paths, or
- Accepting only predefined enum-like values that are not free-form strings.
- Keep parameters for values (e.g., JSON values, comparison targets) strictly separate from any SQL syntax elements; use proper parameter binding wherever supported.
- Apply the principle of least privilege at the database level so that, even if injection occurs, the affected database user has minimal rights.
- Centralize SQL-fragment construction logic (identifiers, JSON paths, etc.) in well-reviewed helpers to avoid scattered custom concatenation patterns.
How teams detect issues like this
To detect this and similar issues, organizations can adopt several complementary approaches:
- Static analysis and linters:
- Flag direct string concatenation used to produce SQL fragments where the concatenated part may derive from external input.
- Add rules that specifically target JSON path construction functions or helpers that wrap
.key()/.at()with user-controlled values. - Highlight any usages where untrusted input is inserted into single-quoted string literals rather than passed as parameters.
- Code review practices:
- Require explicit review for any code paths that manipulate SQL fragments like identifiers, JSON paths, ORDER BY clauses, or schema/table names.
- Check that all non-parameterizable constructs use centralized escaping utilities (e.g.,
sanitizeIdentifier()equivalents) and that JSON path handling is consistent with identifier handling. - Inspect abstractions around Kysely dialects to ensure they do not introduce manual string concatenation for user-controlled values.
- Policy and tooling enforcement:
- Integrate automated security scanning (SAST) into CI pipelines to detect unescaped concatenation patterns in query builders.
- Define internal coding guidelines that forbid building SQL strings directly when parameterization or approved escaping helpers are available.
- Use database-level logging and anomaly detection to spot suspicious or malformed JSON path expressions and unusual SQL patterns at runtime.

