The vulnerability is a JSON-path traversal injection in the Kysely query builder. It arises from insufficient sanitization of user-provided input used in constructing JSON paths for SQL queries. The core of the vulnerability lies in the visitJSONPathLeg function in default-query-compiler.ts, which builds the JSON path string. This function uses sanitizeStringLiteral for sanitization, but this function only escapes single quotes and fails to address other JSON path metacharacters like . or [.
An attacker can supply a specially crafted string to the .key() or .at() methods of the JSONPathBuilder. This input is then passed to visitJSONPathLeg without proper sanitization. For example, an input like "internal.secret" to the .key() method would result in a JSON path like $.internal.secret instead of the intended $."internal.secret". This allows the attacker to traverse into nested JSON objects and access or modify data that they are not supposed to, leading to an authorization bypass. The vulnerability affects MySQL, PostgreSQL, and SQLite dialects.
The fix involves introducing a new, stricter sanitization function, sanitizeJSONPathMemberValue, which properly quotes and escapes the JSON path segments, and adding input validation to the .at() method. This ensures that user input is treated as a single literal value rather than being interpreted as part of the JSON path structure.