Skip to content

[P0][bug] executeProcedure runs USE + SET + CALL + SELECT @vars as multi-statement SQL — partial apply, identifier interpolation in CALL #394

Closed

Description

@EVWorth

Context

Routine Viewer procedure execute action.

Problem

src/components/routine/RoutineViewer.tsx:115-150 builds sql = "SET @p_in = X; SET @p_out = NULL; CALL proc(@p_in, @p_out); SELECT @p_out AS \p_out`;"and sends via executeQuery. The 4 statements run sequentially on the same pool connection. IfCALL proc() fails mid-execution, the preceding SET statements are committed; the SELECT @p_out doesn't run. No transaction wrapper. Additionally, the multi-statement SQL uses string concatenation: \${database}`.`${routineName}` in the CALL (line 137) and the OUT-param SELECT (line 144) — same escapeId-missing pattern as F1 and compare F4. Editor F7 (resultStore swallows successful results on generation mismatch) interacts: if user cancels mid-CALL, results are dropped but session state committed.

Files

  • src/components/routine/RoutineViewer.tsx:115-150

Repro

  1. CREATE PROCEDURE foo(IN x INT) BEGIN DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SELECT 'handled'; END
  2. Call foo with IN param that triggers the handler — but the SET statements are already committed
  3. Subsequent calls see stale session vars

Expected

A procedure that raises mid-execution leaves no @vars committed. IN param value ' OR 1=1 -- is escaped as a literal value, not interpreted as SQL. Parameter binding round-trips.

Proposed fix

Add a Tauri command execute_routine_procedure(connection_id, database, routine_name, params: Vec<RoutineParamValue>) -> ProcedureResult { result_sets, out_params, execution_time_ms }. Backend wraps SET + CALL in sqlx::Transaction<'_, MySql> and commits only on full success; reads OUT params via a final SELECT @p_out …; binds IN values via ? placeholders with Vec typing (depends on editor F5 parameterized queries). Frontend executeProcedure calls this command and unwraps ProcedureResult into the existing UI state. Scope: L.

Acceptance

A procedure that raises mid-execution leaves no @vars committed. IN param value ' OR 1=1 -- is escaped as a literal value, not interpreted as SQL. Parameter binding round-trips.

Needs human verify

yes

Doc drift

no

Activity

  1. added
    audit
    severity/p0
    kind/bug
    area/routine
    on Jul 31, 2026
  2. EVWorth commented on Sep 5, 2026

    @EVWorth
    OwnerAuthor

    Verified the premise before implementing, and part of it does not hold.

    The transaction proposal cannot work

    The proposed fix is to wrap SET + CALL in a sqlx::Transaction and commit only on success, with the acceptance criterion:

    A procedure that raises mid-execution leaves no @vars committed.

    Session variables are not transactional. Against MySQL 8:

    SET @a = 1;
    START TRANSACTION;
    SET @a = 2;
    ROLLBACK;
    SELECT @a;   -- 2

    ROLLBACK does not restore @a. A transaction around the batch would roll back any data the routine wrote, but the SET @p_in = ... statements would stand regardless. The stated acceptance cannot be met by that design, so implementing it would produce a change that looks like a fix and is not one.

    The practical harm is also smaller than described

    The repro says "subsequent calls see stale session vars". executeProcedure re-initialises every parameter before each CALL, including OUT params set to NULL, so a stale value from a previous run is always overwritten before it can be read. For staleness to be observable, one of the SET statements would itself have to fail — and the next run would re-set it anyway.

    I am not claiming there is no ordering hazard here, only that the "stale vars corrupt the next call" mechanism as written does not reproduce.

    What was real, and is now fixed

    The injection half. #543 closes it:

    • parameter values go through quoteStringLiteral — the old ' → \' escape left backslashes alone, so \' OR 1=1 -- broke out of the literal. Demonstrated against MySQL 8 (details in the PR).
    • identifiers in the CALL and the OUT-param SELECT go through quoteIdentifier
    • parameter names are checked against a plain-identifier pattern, since SET @name takes the name unquoted and offers nothing to escape with

    What is left on this issue

    Two things worth keeping open, both narrower than the original scope:

    1. Partial apply is still possible — a failing CALL leaves the preceding SETs applied. Given the above, the fix is not a transaction; it would be either building the argument list inline in the CALL so no session variables are needed for IN parameters, or accepting the behaviour and documenting it. The first is probably right and is a much smaller change than a new Tauri command.
    2. A CALL on a production connection is not gated. isDestructiveStatement covers DROP/DELETE/TRUNCATE/ALTER/RENAME/GRANT/REVOKE/KILL/FLUSH/SHUTDOWN and some phrases — not CALL. A procedure that deletes rows runs on production with no prompt. Whether every CALL should prompt is a product decision rather than a bug fix, so I have not made it unilaterally.

    Retitling suggestion: this is no longer "runs as multi-statement SQL — partial apply, identifier interpolation". The interpolation half is done; what remains is (1) above.

  3. EVWorth commented on Sep 12, 2026

    @EVWorth
    OwnerAuthor

    Fixed the parts that were real, rejected the transaction, and found something the issue did not mention. #653.

    The identifier interpolation is gone — the CALL and the OUT-param SELECT go through quoteIdentifier, with a test using a database called odd`db. Your ' OR 1=1 -- case was checked live against MySQL 8.0.46 and MariaDB 11.8: the procedure receives it as data and stores it as data.

    Found while looking: the call was overwriting the user's own session variables. Parameters were passed as @name, on the connection the editor also uses. A procedure with a parameter called x silently replaced whatever the user had put in @x, with nothing anywhere to say it had happened. They now go through @sqlpilot_param_<name>.

    The batch moved out of the component into buildProcedureBatch, so what gets sent can be read and tested instead of being assembled across forty lines of JSX. Nine tests on it.

    The transaction

    Rejecting it, on three grounds.

    1. Session variables are not transactional. ROLLBACK does not restore a @var — there is nothing to commit and nothing to undo. "A procedure that raises mid-execution leaves no @vars committed" is not a state MySQL has.
    2. The repro cannot happen. "Subsequent calls see stale session vars" — every execution sets all of them before the CALL, including the OUT ones, which are explicitly set to NULL. There is no path where a previous call's value survives into the next.
    3. Wrapping the CALL would be wrong. Procedures routinely manage their own transactions; START TRANSACTION inside one either raises or implicitly commits depending on what it does. Neither DBeaver nor Workbench wraps a CALL — the procedure's transaction semantics belong to its author, and a client that quietly changes them is worse than one that does not.

    On the fourth part — "the 4 statements run sequentially on the same pool connection" — that is not an accident, it is the mechanism. The executor sends a batch as a single raw_sql, which is what makes the session variables set by the first statements visible to the CALL. Sent separately they could land on a session where the SETs never ran. There is now a test pinning that they stay one batch, so nobody splits them later thinking it is tidier.

    The proposed execute_routine_procedure command with bound parameters is not reachable either: a CALL's arguments can be placeholders, but OUT parameters have to be session variables — the protocol has no other way to read one back. That is why mysql(1) and every client does it this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/routineauditkind/bugseverity/p0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions