Skip to content

fix(routine): close the parameter injection, and put DROP behind the gate - #543

Merged
EVWorth merged 1 commit into
mainfrom
fix/routine-sql-safety
Sep 5, 2026
Merged

EVWorth merged 1 commit into
mainfrom
fix/routine-sql-safety

Conversation

@EVWorth

@EVWorth EVWorth commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Closes #393, #397, #398.

The injection — #397

Routine arguments were escaped by replacing ' with \', leaving backslashes alone. MySQL treats a backslash as an escape inside a string literal unless NO_BACKSLASH_ESCAPES is set, and it is off by default — so an argument of \' OR 1=1 -- became '\\' OR 1=1 -- ': the one-character string \ followed by live SQL.

Demonstrated against MySQL 8, same input through both escapings:

-- old
SELECT COUNT(*) FROM (SELECT 1) t WHERE '\\' OR 1=1 -- ' = 'x';    -- 1 row
-- new
SELECT COUNT(*) FROM (SELECT 1) t WHERE '\\\' OR 1=1 -- ' = 'x';   -- 0 rows

The old form returns a row: the injected predicate ran. The new form is one string literal, compared and not matched.

Values now go through quoteStringLiteral, which escapes backslashes first so the ones it introduces aren't re-escaped — the same helper that fixed the identical bug in admin.

The DROP — #393

handleDrop called api.executeQuery directly. The production-safety check lives in the result store, so going straight to the IPC layer skipped it: dropping a routine on a connection marked production was one unstyled window.confirm away, with no app-level confirmation at all.

It runs through the store now, which raises the real dialog. The tab stays open while that dialog is waiting — closing it would remove what the dialog is asking about.

Identifiers in DROP, CALL and the function SELECT go through quoteIdentifier, which doubles an embedded backtick rather than letting it end the quoting. Parameter names get a different treatment: SET @name takes the name unquoted, so there is nothing to escape with, and they're checked against a plain-identifier pattern instead.

Silent coercion — #398

A non-numeric argument for a numeric parameter fell through to the string branch, so an INT parameter given abc was sent as 'abc' — which MySQL accepts and coerces to 0, running the routine with a number nobody typed. Same shape as the boolean cell in #421. Unparseable values are refused and named now. 1e999 is refused too: it survived isNaN as Infinity and stringified to "Infinity". BIGINT and DECIMAL keep their digits as text so a large id doesn't lose its last one.

Structure

SQL building moves to src/lib/routine-sql.ts, where it can be tested without a component. 15 unit tests on the builders, 3 on the drop path — the two asserting new drop behaviour fail against the previous component; the third guards the tab-closing case and passes either way.

1718 unit + 298 browser green, tsc/dprint/eslint clean.

What I did not do, and why

#394's transaction proposal rests on a false premise, so I left it alone rather than implementing something that would look like a fix. Session variables are not transactional — verified on MySQL 8, SET @a=1; START TRANSACTION; SET @a=2; ROLLBACK; SELECT @a returns 2. Wrapping the batch in a transaction cannot deliver its stated acceptance criterion. Full write-up left on #394, including two narrower things that are genuinely left: partial apply on a failing CALL, and the fact that CALL isn't in isDestructiveStatement, so a procedure that deletes rows runs on production with no prompt. That second one is a product decision, so I haven't made it unilaterally.

🤖 Generated with Claude Code

…gate

Closes #393, #397, #398.

## The injection (#397)

Routine arguments were escaped by replacing ' with \', leaving backslashes
alone. MySQL treats a backslash as an escape inside a string literal unless
NO_BACKSLASH_ESCAPES is set, and it is off by default, so an argument of

    \' OR 1=1 --

became  '\\' OR 1=1 -- '  — the one-character string \ followed by live SQL.
Demonstrated against MySQL 8, comparing the two escapings on the same input:

    old:  SELECT COUNT(*) FROM (SELECT 1) t WHERE '\\' OR 1=1 -- ' = 'x'
          -> 1 row. The injected predicate ran.
    new:  ... WHERE '\\\' OR 1=1 -- ' = 'x'
          -> 0 rows. One string literal, compared and not matched.

Values now go through quoteStringLiteral, which escapes backslashes first so
the ones it introduces are not re-escaped. Same helper that fixed the
identical bug in admin.

## The DROP (#393)

handleDrop called api.executeQuery directly. The production-safety check
lives in the result store, so going straight to the IPC layer skipped it:
dropping a routine on a connection marked production was one unstyled
window.confirm away, with no app-level confirmation at all. It now runs
through the store, which raises the real dialog. The tab stays open while
that dialog is waiting, since closing it would remove what the dialog is
asking about.

Identifiers in DROP, CALL and the function SELECT go through
quoteIdentifier, which doubles an embedded backtick rather than letting it
end the quoting. Parameter names are checked against a plain-identifier
pattern instead: SET @name takes the name unquoted, so there is nothing to
escape with, and refusing is the only safe answer.

## Silent coercion (#398)

A non-numeric argument for a numeric parameter fell through to the string
branch, so an INT parameter given "abc" was sent as 'abc' — which MySQL
accepts and coerces to 0, running the routine with a number nobody typed.
Same silent-wrong-value shape as the boolean cell in #421. Unparseable
values are now refused and named. "1e999" is refused too: it survived
isNaN as Infinity and stringified to "Infinity".

BIGINT and DECIMAL keep their digits as text, so a large id does not lose
its last digit on the way through Number().

The SQL building moves to src/lib/routine-sql.ts, where it can be tested
without a component.

15 unit tests on the builders, 3 on the drop path. The two drop tests that
assert new behaviour fail against the previous component; the third guards
the tab-closing case and passes either way.

Not addressed here, deliberately: #394's transaction proposal, which rests
on a false premise. See the issue comment.

Co-Authored-By: Claude Opus 5 <[email protected]>
@EVWorth
EVWorth merged commit dd9c818 into main Sep 5, 2026
10 checks passed
@EVWorth
EVWorth deleted the fix/routine-sql-safety branch September 5, 2026 16:38
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.

[P0][security] RoutineViewer DROP routes through raw executeQuery with identifier string concat — no escape, no production-safety, no read-only

1 participant