Summary
A JSON-path traversal vulnerability in Kysely's DefaultQueryCompiler allows attacker-controlled JSON keys to be inserted into SQL JSON-path expressions without proper escaping.
When untrusted input flows into methods like eb.ref(col, '->$').key(input) or .at(input), attackers can inject path separators (such as . or [*]) to traverse into sibling or child fields, gaining read and, in update statements, write access to JSON sub-fields outside the developer's intended scope across MySQL, PostgreSQL, and SQLite.
Affected context
This issue affects applications using Kysely (versions 0.26.0 to 0.28.16) that build JSON-path queries from values influenced by user or external input.
It is especially relevant where:
eb.ref(col, '->$').key(input)or.at(input)are used with untrusted data- JSON columns are typed like
Record<string, T>so thatK extends stringis inferred and perceived as "safe" - Query logic relies on JSON-path segments to restrict which JSON sub-fields can be read or written
In these situations, JSON-path metacharacters within the key are not escaped, allowing the effective path to extend beyond the intended field boundary.
Exploit preconditions
- Attack surface: Any API or feature that allows clients (or other untrusted sources) to influence JSON key names or path segments passed into Kysely's
.key()or.at()helpers. - Required access: Ability to supply or manipulate JSON field names, filters, or selectors used in query construction (for example, request parameters, configuration values, or tenant-controlled schema-like metadata).
- Trigger point: Execution of a Kysely query where
DefaultQueryCompiler.visitJSONPathLegrenders a JSON-path expression using the attacker-controlled key without escaping metacharacters (.,[,],*,**,?). - Why exploitation is possible: The compiler treats the key as a raw path leg, so metacharacters are interpreted by the underlying database JSON-path engine as additional navigation steps instead of literal characters, enabling traversal to unintended sibling or nested JSON fields.
Why this happens
The vulnerability stems from constructing JSON-path expressions by concatenating user-influenced strings directly into the path without escaping JSON-path metacharacters.
In DefaultQueryCompiler.visitJSONPathLeg, Kysely assumes the provided JSON key is a single, safe path leg, but does not encode characters like ., [, ], *, **, and ? that have special meaning in JSON-path dialects used by MySQL, PostgreSQL (->/->>), and SQLite.
This maps to CWE-1284: Improper Validation of Specified Indices or Index Ranges and is closely related to path traversal patterns (CWE-22) and query construction flaws (CWE-89): path segments act as implicit selectors, and failing to constrain or sanitize them lets attackers expand their access surface across the JSON document beyond what the surrounding code intends.
Potential impact
- Attackers may read JSON sub-fields that should be hidden, such as internal flags, access-control data, or sensitive nested attributes.
- Attackers may modify or overwrite unintended JSON sub-fields during update or patch operations, potentially corrupting business-critical state.
- Logical authorization boundaries that rely on per-key isolation within a JSON column can be bypassed, enabling cross-tenant or cross-entity data exposure where multiple entities share a single JSON blob.
- Auditing, logging, or integrity checks that assume only specific JSON paths are touched can be silently circumvented.
General mitigation guidance
Effective mitigation focuses on treating JSON-path segments as untrusted input and enforcing strict boundaries around what can become part of a path:
- Prefer fixed, hard-coded JSON paths for sensitive fields instead of accepting arbitrary paths or keys from callers.
- Where dynamic keys are required, validate them against an explicit allowlist of expected identifiers (e.g., a closed set of field names) before passing them into
.key()or.at(). - Implement a robust escaping or encoding layer for JSON-path segments so that characters like
.,[,],*,**, and?are treated as literals, not navigational operators. - Architect data models so that security-critical distinctions (such as tenant boundaries or authorization scopes) do not rely solely on different keys within a single JSON column.
- Apply least-privilege principles at the query level by constructing narrowly scoped projections and updates that only reference explicitly intended JSON paths.
How teams detect issues like this
Detection typically involves examining how JSON paths are built and where user input flows:
- Static analysis: Flag calls to
eb.ref(...).key(x)or.at(x)wherexoriginates from request data, query parameters, or other untrusted sources, especially when passed through without validation or escaping. - Code review: Look for patterns where JSON keys are treated as "safe" purely because they are typed, such as
Record<string, T>, and verify that the run-time data is constrained to an expected set of field names. - Query inspection: Review generated SQL (or logs of executed statements) for JSON-path expressions containing unvalidated user-supplied segments, including suspicious metacharacters in path components.
- Policy enforcement: Incorporate automated checks in CI/CD that search for dynamic JSON-path construction without accompanying validation/encoding utilities, using lint rules or security-focused scanning tools.

