Skip to content

fix: skip quoted SQL and comments when binding parameters - #93

Merged
baiyuqing merged 2 commits into
mainfrom
baiyuqing/lexical-sql-placeholders
Nov 6, 2026
Merged

baiyuqing merged 2 commits into
mainfrom
baiyuqing/lexical-sql-placeholders

Conversation

@baiyuqing

Copy link
Copy Markdown
Collaborator

Problem

The formatter replaces ? and :name inside SQL string literals. The quotes added around a parameter can close the template's literal and place attacker-controlled text in SQL code position. For example, a question mark in 'Are you sure?' consumes a parameter and permits the quote-free payload OR 1=1 -- to alter the statement.

Changes

Scan the SQL template and replace placeholders only outside quoted strings, quoted identifiers, and comments. Preserve existing value formatting, duplicate named parameters, and missing-parameter behavior. Document unquoted placeholders and passing an entire LIKE pattern as a parameter.

Add regression coverage for the reported positional and named payloads, escaped and doubled quotes, comments, and the SQL body sent through the public connection API. This change targets placeholder context; it does not change value escaping or add SQL-mode negotiation.

Validation

  • Confirmed the new regression cases fail before the fix and reproduce the reported injected SQL.
  • npm test -- --runInBand: all 91 tests pass, including the build.
  • npm run lint and ESLint on the changed test files pass.
  • git diff --check passes.

Requests were verified using mocked fetch; no live database was used.

@ti-chi-bot ti-chi-bot Bot added the size/L label Nov 6, 2026
End # and -- comments only at \n. TiDB's lexer (startWithSharp,
startWithDash) does not end them at \r, so ending them at \r let the
template "SELECT ? -- c\r'\n, ' ? '" place the second value inside a
string literal in TiDB's parse, where a quote-free value such as
" OR 1=1 -- " changes the statement.

Reject /*! */ and /*T![...] */ comments in queries with parameters.
TiDB scans their contents as SQL, ends them at a */ token rather than at
a */ inside a string literal, and runs /*T! only for features the server
supports. The formatter cannot reproduce that, and
"SELECT 1 /*! , '*/ ? ' */" produced the same injection. Queries
without parameters are sent unchanged.
@baiyuqing
baiyuqing merged commit 762c9bc into main Nov 6, 2026
3 checks passed
@baiyuqing
baiyuqing deleted the baiyuqing/lexical-sql-placeholders branch November 6, 2026 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant