Skip to content

[P1][security] Restore runs statements serially with no transaction wrapper; mid-file errors leave DB half-populated #359

Closed

Description

@EVWorth

Context

Restore feature — backup/restore feature (DESIGN_REQUIREMENTS FR-8). Per docs/audits/backup.md F3.

Problem

src/components/backup/RestoreDialog.tsx reads SQL file, splits via splitSqlStatements, runs each via api.executeQuery. Grep for BEGIN|COMMIT|ROLLBACK|FOREIGN_KEY_CHECKS in RestoreDialog returns zero hits. No START TRANSACTION wrapper, no SET FOREIGN_KEY_CHECKS=0. A dump with CREATE TABLE users (...) followed by INSERT INTO users VALUES (...) where INSERT fails leaves the DB with the new (empty) table but not the inserts — silent data loss.

Files

  • src/components/backup/RestoreDialog.tsx:65-385

Repro

  1. Create test dump with intentional column-count mismatch mid-file
  2. Restore against empty DB with stopOnError=true
  3. Observe first N-1 statements committed, dump aborted mid-way

Expected

Whole restore wrapped in transaction. On error, full rollback (when stopOnError=true).

Proposed fix

M: wrap whole restore in BEGIN...COMMIT/ROLLBACK block. Add SET FOREIGN_KEY_CHECKS=0/=1 prologue/epilogue. Surface partial-restore status explicitly.

Acceptance

Dump with one mid-file error restored against empty DB with stopOnError=true results in zero tables created (full rollback).

Needs human verify

yes

Activity

  1. added
    audit
    severity/p1
    kind/security
    area/backup
    on Jul 31, 2026
  2. EVWorth commented on Sep 12, 2026

    @EVWorth
    OwnerAuthor

    Fixed, but not with the transaction the issue asks for — that one cannot work, and here is the check.

    START TRANSACTION;
    CREATE TABLE a (id INT);
    INSERT INTO a VALUES (1);
    ROLLBACK;

    On MySQL 8.0.46 this leaves both the table and the row. MySQL commits the open transaction before every CREATE, DROP and ALTER; there is no client-side switch for it. So the acceptance criterion — "a dump with one mid-file error restored against an empty DB results in zero tables created" — is not reachable by any client, and a wrapper that appeared to promise it would be worse than none.

    What #652 does instead:

    The restore moved to the backend and runs on one connection. It used to read the whole file into the renderer, split it there, and send back one statement per call — each on whichever pooled connection it landed on, so the USE that opened the database was not guaranteed to apply to the next statement, and neither would any SET SESSION or START TRANSACTION. That is the deeper version of what this issue found: not only was there no transaction, there was no session for one to live in.

    The transaction is there, and it is honest about its reach. A data-only file is now all-or-nothing — there is a test that fails a third INSERT and checks the first two are gone. A file with DDL cannot be, so the summary carries partiallyApplied and the dialog says which of these is true:

    • "Rolled back — the database is as it was."
    • "Rolled back, but part of the file had already been applied: MySQL commits before every CREATE, DROP or ALTER, so the structural changes stand."

    The old dialog said "Restore completed with N error(s)" and left the user to guess.

    FOREIGN_KEY_CHECKS and UNIQUE_CHECKS go off for the restoring session and back on when it ends, which is the other half of your proposed fix and the part that does work. A file whose tables are in the wrong order now loads; there is a test for that, and another that asks every connection in the pool afterwards whether its checks are back on, because leaving one off would silently accept orphans for every later query on it.

    Eleven live-server tests, passing on MySQL 8.0.46 and MariaDB 11.8.

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/backupauditkind/securityseverity/p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions