Skip to content

feat: SQLCipher-encrypted local database migration (expo-sqlite + Drizzle)#7421

Draft
diegolmello wants to merge 14 commits into
developfrom
feat/native-1272-sqlcipher-migration
Draft

feat: SQLCipher-encrypted local database migration (expo-sqlite + Drizzle)#7421
diegolmello wants to merge 14 commits into
developfrom
feat/native-1272-sqlcipher-migration

Conversation

@diegolmello

Copy link
Copy Markdown
Member

Proposed changes

Long-lived integration branch for replacing WatermelonDB with expo-sqlite + Drizzle ORM, with every local database SQLCipher-encrypted from byte zero. Goal: offline forensic extraction of a powered-down or backed-up device yields no chat content or auth tokens.

Each sub-task lands here as its own PR (so each gets full native iOS/Android CI); this branch merges to develop only once the facade cutover is ready. The Drizzle schema, the driver adapter, the native key store, and the encrypted native readers are all additive and inert — nothing reads them until the cutover flips the database singleton from WatermelonDB to Drizzle and enables SQLCipher, so each piece can merge and revert independently.

Landed so far:

  • Fail-closed DatabaseKeyStore TurboModule (feat: fail-closed DatabaseKeyStore TurboModule #7418) — per-database 32-byte key minted and stored in the iOS Keychain (App Group access group, accessible after first unlock, non-synchronizable) and the Android Keystore (hardware-backed where available). Durable writes, write-once semantics symmetric across both platforms, and 64-hex key-format validation before the key reaches SQLCipher.

Issue(s)

NATIVE-1272 — https://rocketchat.atlassian.net/browse/NATIVE-1272

How to test or reproduce

Nothing is wired into the running app yet — the key store is additive and inert until the cutover, so there is no user-visible change to exercise. Verify the merged sub-task in isolation:

  • TZ=UTC pnpm test --testPathPattern='keyStore' — JS wrapper contract (mint, fail-closed read, malformed-key rejection).
  • pnpm lint — eslint + tsc.

Native key-store behavior (durable write, write-once, Keychain/Keystore) has no JS-reachable harness; it is exercised by the iOS and Android builds on CI.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

Further comments

Subsequent sub-tasks — driver adapter + key service, Drizzle schema, native reader rewrite, facade cutover, wipe-and-restore migration, performance parity gates, hardening, and rollout — target this branch as separate PRs. Delivery sequencing and dependency order are tracked on NATIVE-1272.

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4dfc6e5f-25b8-4a5c-86a7-3a19f63879d6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

getLocalAnchor and its test imported Q directly from @nozbe/watermelondb,
which the facade lint rule bans app-wide. Point both at app/lib/database/facade;
the facade re-exports Q, so the query and assertions are unchanged.

Claude-Session: https://claude.ai/code/session_01TE9VsFTeXsXc8ssqeR8MJ7
The merged develop test inspected WatermelonDB's clause internals
(left, comparison.right.value), but useMessages builds queries with the
facade's Q, whose where clause exposes column and comparison.value. Read
the facade shape so the upper-bound ts assertions match the engine in use.

Claude-Session: https://claude.ai/code/session_01TE9VsFTeXsXc8ssqeR8MJ7
@github-actions

Copy link
Copy Markdown

iOS Build Available

Rocket.Chat 4.74.0.109185

…qlcipher-migration

# Conflicts:
#	app/sagas/messages.js
A develop merge duplicated both the import and the add() call in the package
list. Kotlin rejects the repeated import as ambiguous, breaking compileDebugKotlin.

Claude-Session: https://claude.ai/code/session_01R71aVJWSdFAv7R3FjD1VnJ
The facade's Collection.create / Model.update wrap their work in db.write(),
and call sites wrap again (the WMDB db.write(() => collection.create()) idiom).
The WriterQueue was non-reentrant, so a nested write deadlocked: the outer
writer awaited the inner, which was queued behind it and could never run. At
first boot this hung NewServerView's Connect on the very first server upsert.
Make the queue run a nested enqueue inline within the active writer.

With the deadlock gone the next defect surfaced: a freshly opened encrypted
database had no tables (no such table: servers). The migrate() call that
applies the Drizzle DDL bundles was dropped from openDb in a develop merge.
Restore it; the migrator creates tables on first open and is a no-op after.

Claude-Session: https://claude.ai/code/session_01R71aVJWSdFAv7R3FjD1VnJ
@github-actions

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

iOS Build Available

Rocket.Chat 4.74.0.109191

On a deep-link or call-push cold start, app/index.tsx dispatches the
deep-link action and returns BEFORE appInit(), so restore() never runs
and database.initServers() never opens the encrypted servers DB. The
handlers then read database.servers, whose getter throws "Servers
database accessed before initServers() resolved", killing the saga task
before login can complete — so rooms-list-view never appears and every
deep-link-login E2E shard fails its visibility assertion.

This regressed when the synchronous WatermelonDB database.servers became
an async-initialised facade: warm launches still work because restore()
opens the DB first, but the cold-start deep-link/push paths bypass
appInit() entirely.

Fix: call the idempotent database.initServers at the top of handleOpen
and handleClickCallPush, before any servers-DB read. No-op on a warm
launch where restore() already opened it. Verified on-device: an auth
deep-link into a freshly-cleared app now reaches the rooms list.

Claude-Session: https://claude.ai/code/session_01R71aVJWSdFAv7R3FjD1VnJ
@github-actions

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

iOS Build Available

Rocket.Chat 4.74.0.109193

Comment thread android/gradle.properties
edgeToEdgeEnabled=false

# Enable SQLCipher via expo-sqlite (required for encrypted-at-rest databases)
expo.sqlite.useSQLCipher=true No newline at end of file

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to be added to CI gradle.properties change.

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.

1 participant