Skip to content

fix(security): double the quote, so a value cannot end the string it is in - #650

Merged
EVWorth merged 1 commit into
mainfrom
fix/sql-quote-escaping
Sep 11, 2026
Merged

EVWorth merged 1 commit into
mainfrom
fix/sql-quote-escaping

Conversation

@EVWorth

@EVWorth EVWorth commented Sep 11, 2026

Copy link
Copy Markdown
Owner

Follows #646.

A quote escaped as \' is not an escape under NO_BACKSLASH_ESCAPES — the backslash is literal and the quote ends the string. Everything after it is SQL.

Found while checking #285's claim about parameterized queries. Verified against MySQL 8.0.46: a row value of x', 1); DROP TABLE victim; -- in a SQLPilot backup, restored on a server in that mode, dropped the table.

The two exposures are not equal

The backup path is the serious one, because it crosses a trust boundary: the attacker is whoever can write a row, the victim is whoever restores the dump.

It is mitigated today — the dump's header sets SQL_MODE, which clears NO_BACKSLASH_ESCAPES. But that mitigation is load-bearing and unstated, and it's lost the moment one INSERT is copied out of the file, or a restore path drops comments. The escaping shouldn't depend on it.

quoteStringLiteral — which builds the admin panel's user and role statements — has the same shape against a live session whose mode can't be set. That's a correctness bug rather than an escalation: the values are typed by someone who already has the editor. But a user name containing a quote broke the statement, and on an ANSI server it broke out of it.

The fix

Doubling is correct whether backslash escaping is on or off, so the quote is no longer a way out in either. Verified both ways on a live server: the hostile value round-trips as data, and a user named ok'user is created, granted and dropped under NO_BACKSLASH_ESCAPES.

The backslash escaping beside it stays mode-dependent and stays as it was: in the default mode a password of pa\ss written literally is stored as pass, which is the bug that escaping exists for. Under NO_BACKSLASH_ESCAPES it over-escapes — a fidelity bug in a rare configuration, not a way out. Getting both right needs the session's mode, which means asking the server; worth doing if anyone hits it.

Tests

Nine assertions across seven files were written against the old form — several of them the tests specifically about injection safety — and now assert the new one. Two new tests cover the hostile payloads directly.

2629 unit + 341 browser tests.

🤖 Generated with Claude Code

…is in

A quote escaped as `\'` is not an escape under `NO_BACKSLASH_ESCAPES` — the
backslash is literal and the quote ends the string. Everything after it is
SQL.

Found while checking #285's claim about parameterized queries. Verified
against MySQL 8.0.46: a row value of `x', 1); DROP TABLE victim; -- ` in a
SQLPilot backup, restored on a server in that mode, dropped the table.

The exposures are not equal and it is worth being precise about which is
which.

The backup path is the serious one, because it crosses a trust boundary: the
attacker is whoever can write a row, and the victim is whoever restores the
dump. It is mitigated today — the dump's header sets SQL_MODE, which clears
NO_BACKSLASH_ESCAPES — but that mitigation is load-bearing and unstated, and
it is lost the moment one INSERT is copied out of the file or a restore path
drops comments. The escaping should not depend on it.

`quoteStringLiteral`, which builds the admin panel's user and role statements,
has the same shape against a live session whose mode cannot be set. It is a
correctness bug rather than an escalation — the values are typed by someone
who already has the editor — but a user name containing a quote broke the
statement, and on an ANSI server it broke out of it.

Doubling is correct whether backslash escaping is on or off, so the quote is
no longer a way out in either. Verified both ways on a live server: the
hostile value round-trips as data, and a user named `ok'user` is created,
granted and dropped under `NO_BACKSLASH_ESCAPES`.

The backslash escaping beside it stays mode-dependent and stays as it was: in
the default mode a password of `pa\ss` written literally is stored as `pass`,
which is the bug that escaping exists for. Under `NO_BACKSLASH_ESCAPES` it
over-escapes — a fidelity bug in a rare configuration, not a way out. Getting
both right needs the session's mode, which means asking the server.

Nine assertions across seven test files were written against the old form —
several of them the tests specifically about injection safety — and now assert
the new one.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant