Skip to content

PRAGMA rekey with the encryption build on OPFSCoopSyncVFS reports ok but leaves a file no key can open #1144

Open

Description

@DwayneGit

Versions

  • @powersync/web 2.4.2, @powersync/common 2.3.1
  • @journeyapps/wa-sqlite 2.0.6 (mc-wa-sqlite.mjs), sqlite3mc_version() = 2.3.3, SQLite 3.53.0, default cipher chacha20
  • Chromium 156 (headless, Playwright 1.64), Windows 11

What happens

On a database stored with OPFSCoopSyncVFS and opened with the SQLite3MultipleCiphers build, PRAGMA rekey returns ok. From then on, no key opens the file: neither the old key nor the new key, and neither the empty key nor a plain build. Each open fails with file is not a database. The data is lost, and nothing reports an error.

The same steps on IDBBatchAtomicVFS (with mc-wa-sqlite-async.mjs) work as expected.

Case OPFS (OPFSCoopSyncVFS) IndexedDB (IDBBatchAtomicVFS)
Plain file → encrypted (PRAGMA rekey = '<key>' with the cipher build) Reports ok; the file can then never be opened Works
Change the key of a database PowerSync created encrypted (PRAGMA rekey = '<new>' on PowerSync's own connection) Reports ok; neither the old nor the new key opens it Works (old key refused, new key opens)
Encrypted → plain (PRAGMA rekey = '') Not reachable Works
VACUUM INTO 'file:copy.db?key=…' or ATTACH … KEY … as a workaround unable to open database / database is locked Both work

It doesn't depend on temp_store (MEMORY, as PowerSync sets it, or DEFAULT) or on size (5 rows or 2,000 rows). The journal mode is delete. A database created encrypted on OPFS from the start opens fine in a new worker and in PowerSync, so the harness itself is sound.

Steps to reproduce (key change, PowerSync only)

  1. Create a PowerSyncDatabase with database: { dbFilename: 'rekey-test.db', vfs: 'OPFSCoopSyncVFS', encryptionKey: "x'<64 hex>'" } and write a few rows.
  2. Run PRAGMA rekey = "x'<other 64 hex>'" through db.execute. It returns ok.
  3. Close the database and open it again with the new key, then with the old key. Both fail with file is not a database.

A standalone reproduction (a Playwright runner, a page, and a raw wa-sqlite worker that does the plain → encrypted case without PowerSync) is in our spike: tools/spikes/web-encryption/ (node tools/spikes/web-encryption/run.mjs --only=rekeySelfTest,rekey; see its README). We can share it as a gist.

Expected

Either PRAGMA rekey works on OPFS as it does on IndexedDB, or it fails with an error and leaves the file readable. A silent ok that loses the database is the dangerous part.

Also noticed (may deserve separate issues):

  • A top-level encryptionKey on PowerSyncDatabase options is silently ignored, and the file stays plain. Only database.encryptionKey takes effect. The README example could mislead.
  • In IndexedDB multi-tab mode, the shared worker hands an already-open connection to a second tab without checking that tab's encryptionKey. A tab with a wrong key, or with no key, can read everything. On OPFS, such a tab is refused.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions