Skip to content

fix(jdbc): reject restore on a read-only connection - #1511

Merged
gotson merged 1 commit into
xerial:masterfrom
sahvx655-wq:restore-read-only
Nov 23, 2026
Merged

gotson merged 1 commit into
xerial:masterfrom
sahvx655-wq:restore-read-only

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

restore bypasses read-only connections

Statement.execute and executeUpdate hand restore from <file> straight to NativeDB.restore, which copies the file over the target database through the SQLite online backup API. That API writes pages via the pager and never looks at PRAGMA query_only, which is the only mechanism jdbc.explicit_readonly uses to honour Connection.setReadOnly(true). I checked this on master with an explicit read-only connection: insert into t values(9) fails with SQLITE_READONLY, yet restore from '<other.db>' on the same statement returns normally and leaves the database holding the other file's contents (3 rows replaced by 1). With open_mode READONLY the backup step does fail, but NativeDB.restore takes its return code from the source handle, so the statement still reports success without restoring anything. The root cause is that no read-only check sits on the extended-command path at all; #1502 added tryEnforceTransactionMode to the SQL branch only, and that pragma would not have stopped the backup API either.

Left as is, any code relying on a read-only connection to contain user-supplied SQL (report consoles, @Transactional(readOnly = true) paths) can have its whole database swapped for any file the process can read. The fix rejects RestoreCommand with SQLITE_READONLY whenever conn.isReadOnly(), at both dispatch sites; backup only reads the source and is left alone. The new BackupTest case fails on master ("Expecting code to raise a throwable") and passes with the fix, the full suite is green on JDK 21 (506 tests, 13 skipped), and the changed lines match google-java-format 1.11 AOSP output. I could not run spotless:check itself because gjf 1.11 does not start on JDK 21.

@gotson
gotson merged commit 0fd5b76 into xerial:master Nov 23, 2026
32 checks passed
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.

2 participants