Skip to content

fix(admin): quote passwords as string literals, and names as identifiers - #521

Merged
EVWorth merged 1 commit into
mainfrom
fix/admin-sql-quoting
Aug 29, 2026
Merged

EVWorth merged 1 commit into
mainfrom
fix/admin-sql-quoting

Conversation

@EVWorth

@EVWorth EVWorth commented Aug 29, 2026

Copy link
Copy Markdown
Owner

Refs #444. Found while reviewing area/admin.

The name hazard, and what it hid

Two functions in this repo were both called escapeIdentifier and did different things:

returns correct for
src/lib/backup-generator.ts `name` identifiers
src/components/admin/userPrivileges.ts 'value' string literals

This is not a hypothetical hazard. Reviewing this area I read the first and reasoned about the second, and spent a while convinced that creating a user was broken. It wasn't — the admin one quotes account names correctly. But that's a decent demonstration of what the name costs a reader, and it's the case #444 makes.

They're now one module, sql-quote.ts, named for what each is for: quoteIdentifier and quoteStringLiteral.

The bug the split exposed

The admin helper doubled single quotes and left backslashes alone. MySQL treats a backslash as an escape inside a string literal unless NO_BACKSLASH_ESCAPES is set — off by default. So a password of pa\ss was written as 'pa\ss' and stored as pass, since \s isn't a recognised escape and collapses to s.

The statement succeeds. Nothing errors. The account is created with a password the user never typed, and the first anyone knows is a login failing.

Verified against MySQL 8 with the old escaping:

CREATE USER 'bsuser'@'%' IDENTIFIED BY 'pa\ss';   -- succeeds

$ mysql -u bsuser -p'pa\ss'   → ERROR 1045 Access denied
$ mysql -u bsuser -p'pass'    → authenticated

Also fixed

applyChanges needed an identifier and admin had no helper for one, so it hand-rolled backticks with no doubling:

const dbScope = `\`${selectedDb}\`.*`;

MySQL permits a database named we`ird, and the resulting GRANT ... ON \we`ird`.*doesn't parse (confirmed:ERROR 1046`). That call site is precisely the confusion the shared naming prevents — the correct helper didn't exist in the module it was reaching for.

Tests

1633 frontend tests, 60 integration tests against MySQL 8 + MariaDB 11, clippy/tsc/dprint clean.

Note on #444

Leaving the issue open for the maintainer to close — the split it asks for is done, but it's worth deciding whether the remaining escapeIdentifier name (now only in backup-generator.ts as quoteIdentifier) satisfies it fully.

🤖 Generated with Claude Code

Two functions in this codebase were both called `escapeIdentifier` and did
different things — backticks in backup-generator.ts, single quotes in
admin/userPrivileges.ts. That is not a hypothetical hazard. Reviewing this
area I read one and reasoned about the other, and spent a while convinced
that creating a user was broken. It was not; the admin one quotes correctly
for account names. But the name is doing real damage to anyone reading it.

They are now one module, `sql-quote.ts`, naming what each is for:
`quoteIdentifier` for names, `quoteStringLiteral` for values.

Splitting them exposed a bug the old admin helper had. It doubled single
quotes and left backslashes alone, but MySQL treats a backslash as an escape
inside a string literal unless NO_BACKSLASH_ESCAPES is set, which is off by
default. A password of `pa\ss` was written as 'pa\ss' and stored as `pass`,
because `\s` is not a recognised escape and collapses to `s`. The account was
created with a password the user never typed, the statement succeeded, and
the first anyone knew was a login failing. Verified against MySQL 8: with the
old escaping the account authenticates as `pass` and rejects `pa\ss`.

Also fixes the one place in admin that needed an identifier and had no helper
for it, so it hand-rolled backticks: `applyChanges` built the grant scope as
`` `${selectedDb}`.* `` with no doubling. MySQL permits a database named
``we`ird``, and the resulting GRANT does not parse. That call site is exactly
the confusion the shared naming is meant to prevent.

The round-trip is pinned by an integration test that creates a user with a
backslash in the password and asserts that the typed password authenticates
and the collapsed one does not — the kind of coverage #445 is about.

Refs #444
@EVWorth
EVWorth merged commit a8fa8b5 into main Aug 29, 2026
9 checks passed
@EVWorth
EVWorth deleted the fix/admin-sql-quoting branch August 29, 2026 14:40
EVWorth added a commit that referenced this pull request Aug 29, 2026
…tself (#522)

Two security holes in the admin area, both of the same shape: a protection
that existed on paper and nowhere in the execution path.

read_only was stored on the profile, carried onto the live connection, and
checked in exactly one place — EXPLAIN ANALYZE. Every other path ignored it.
A read-only production profile would still run DROP TABLE from the editor,
create accounts from the admin panel, or grant SUPER to itself. The executor
now refuses any statement that could change data, schema, privileges or
server state, before the batch is assembled, so a script containing a write
is rejected whole rather than half-run.

Classifying by leading keyword would have left two ways through, both
covered: `WITH doomed AS (...) DELETE` is a DELETE, and `EXPLAIN ANALYZE
<write>` executes the write it measures — so run through the ordinary Run
button, its leading EXPLAIN would have looked harmless. Plain EXPLAIN stays
allowed whatever it plans, since it only plans. The verb list is an
allowlist: an unrecognised statement is treated as a write and refused.

kill_process had no notion of which sessions belonged to SQLPilot, and
PROCESSLIST includes the connections the panel is using to read PROCESSLIST.
Killing one worked; killing the last exhausted the pool and surfaced "pool
timed out", which never told the user they had disconnected themselves. The
pool now records each server thread it opens, kill_process refuses those, and
the UI disables the button with a tooltip explaining why rather than letting
the click fail. Other users' sessions stay killable. ERROR 1095 is translated
into a message naming the privilege that is missing, since PROCESSLIST is
visible to users who cannot act on it.

Writes now carry the account they ran as in the log line, which is the audit
trail #429 asks for.

Two parts of #429 are deliberately not done. Parameterised SQL is not
available for DDL — MySQL takes no placeholders for identifiers or privilege
names, so correct quoting is the remedy, and that landed in #521. Wrapping
GRANT/REVOKE in a transaction is impossible: they force an implicit commit,
verified by a GRANT surviving ROLLBACK. That is recorded on #439.

Refs #429, #433
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