Modern Apps git

← Modern-Apps.git

travel: survive platform backup restore via SqlCipher codec agent

Commit
444d24b8102b29a044f0eb25e695d2b25d61d248
Author
Vayun Mathur <[email protected]>
When
2026-12-04 21:48 UTC (1 day ago)
Parents
b0c9958
Travel declared allowBackup with dataExtractionRules but no backupAgent, so platform backup restored everything except the trips: the raw travel-db was excluded (correctly, as a SQLCipher file whose passphrase is wrapped by a non-exportable AndroidKeyStore key cannot be opened on a new device), and there was no other path for RecentSearch, BookedTrip, FrequentFlyer or Customer rows to migrate. A restore was therefore a clean slate that silently lost all user data.

This follows the weather pattern. TravelDatabase gains travelDbConfigs(context), mirroring weatherDbConfigs, and a new platform/AppBackupAgent extends BaseBackupAgent with SqlCipherDbCodec: the DB is exported to plaintext inside the backup payload and re-encrypted under the restored device key on restore. The agent also carries datastore_default so the shared UI preferences that previously went via Auto Backup are not dropped when cloud backup switches to this key/value agent. The manifest declares android:backupAgent.

data_extraction_rules.xml is unchanged in effect (raw travel-db, its wal/shm/journal sidecars and secure_prefs.xml excluded on both cloud-backup and device-transfer) but its comment now documents the agent scope instead of the former no-agent heads-up. travel/AGENTS.md lists the new agent file.

Verified: :travel:lint and :travel:lintVitalRelease pass. :travel:testDevUnitTest as a whole cannot compile at this base because travel/src/test/java/com/vayunmathur/travel/ui/TravelW6Test.kt contains unresolved merge-conflict markers committed at 30be4a93a (pre-existing, untouched by this change); with that one file temporarily set aside, the new TravelBackupAgentTest passes (3 tests: manifest agent/allowBackup, codec plus datastore wiring, and both rule channels excluding the raw DB and secure_prefs). The codec itself is host-limited (native SQLCipher plus AndroidKeyStore), so host tests prove wiring and rules rather than a codec round-trip.

Diff

travel/AGENTS.md
diff --git a/travel/AGENTS.md b/travel/AGENTS.mdindex e71c730700..1e85097cb4 100644--- a/travel/AGENTS.md+++ b/travel/AGENTS.md@@ -73,6 +73,7 @@ _Install: ./install travel (dev by default)._ - com/vayunmathur/travel/platform/TravelStayOps.kt - com/vayunmathur/travel/platform/ContactAutofill.kt - com/vayunmathur/travel/platform/StripeCheckout.kt+- com/vayunmathur/travel/platform/AppBackupAgent.kt — platform backup: `SqlCipherDbCodec` export/re-encrypt of `travel-db` (`travelDbConfigs` in `data/TravelDatabase.kt`) + `datastore_default`; raw DB/`secure_prefs` excluded in `res/xml/data_extraction_rules.xml` ### ui/ — search, results, booking, trips, stays - com/vayunmathur/travel/ui/HomePage.kt - com/vayunmathur/travel/ui/FlightSearchForm.kt
travel/src/main/AndroidManifest.xml
diff --git a/travel/src/main/AndroidManifest.xml b/travel/src/main/AndroidManifest.xmlindex e158187146..11c1d66bfb 100644--- a/travel/src/main/AndroidManifest.xml+++ b/travel/src/main/AndroidManifest.xml@@ -15,6 +15,7 @@      <application         android:allowBackup="true"+        android:backupAgent=".platform.AppBackupAgent"         android:dataExtractionRules="@xml/data_extraction_rules"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"
travel/src/main/java/com/vayunmathur/travel/data/TravelDatabase.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/data/TravelDatabase.kt b/travel/src/main/java/com/vayunmathur/travel/data/TravelDatabase.ktindex c7e164d675..7d064faef1 100644--- a/travel/src/main/java/com/vayunmathur/travel/data/TravelDatabase.kt+++ b/travel/src/main/java/com/vayunmathur/travel/data/TravelDatabase.kt@@ -9,11 +9,17 @@ import androidx.room3.RoomDatabase import androidx.room3.Upsert import androidx.sqlite.SQLiteConnection import androidx.sqlite.execSQL+import android.content.Context+import com.vayunmathur.library.util.DatabaseHelper import com.vayunmathur.library.util.DatabaseMigrations import kotlinx.coroutines.flow.Flow  const val DB_NAME = "travel-db" +/** Backup config shared by [com.vayunmathur.travel.platform.AppBackupAgent]. */+fun travelDbConfigs(context: Context): List<Pair<String, String>> =+    listOf(DB_NAME to DatabaseHelper(context).getPassphrase())+ /** Contract for recent search dao. */ @Dao interface RecentSearchDao {
travel/src/main/java/com/vayunmathur/travel/platform/AppBackupAgent.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/platform/AppBackupAgent.kt b/travel/src/main/java/com/vayunmathur/travel/platform/AppBackupAgent.ktnew file mode 100644index 0000000000..f9eb8c1679--- /dev/null+++ b/travel/src/main/java/com/vayunmathur/travel/platform/AppBackupAgent.kt@@ -0,0 +1,23 @@+package com.vayunmathur.travel.platform++import com.vayunmathur.library.room.SqlCipherDbCodec+import com.vayunmathur.library.util.BaseBackupAgent+import com.vayunmathur.travel.data.travelDbConfigs++/**+ * Platform backup for travel: the SQLCipher DB cannot travel as a raw file+ * (its passphrase is wrapped by a non-exportable AndroidKeyStore key), so+ * [SqlCipherDbCodec] exports it to plaintext inside the backup payload and+ * re-encrypts it under the restored device's key on restore — the weather+ * pattern. The shared DataStore file rides along so UI preferences that+ * previously went via Auto Backup are not dropped by switching cloud+ * backup to this key/value agent.+ */+class AppBackupAgent : BaseBackupAgent() {+    override val dbCodec = SqlCipherDbCodec++    override val dbConfigs: List<Pair<String, String>>+        get() = travelDbConfigs(this)++    override val datastoreNames: List<String> get() = listOf("datastore_default")+}
travel/src/main/res/xml/data_extraction_rules.xml
diff --git a/travel/src/main/res/xml/data_extraction_rules.xml b/travel/src/main/res/xml/data_extraction_rules.xmlindex 0eadf621e8..9875c859a7 100644--- a/travel/src/main/res/xml/data_extraction_rules.xml+++ b/travel/src/main/res/xml/data_extraction_rules.xml@@ -2,35 +2,39 @@ <!--   Travel. Audited state:     * databases/travel-db — Room DB with RecentSearch, BookedTrip, FrequentFlyer-      and Customer entities.-    * secure_prefs.xml — SQLCipher passphrase for that DB.-    * filesDir/datastore_default.preferences_pb — UI preferences (TravelViewModel).+      and Customer entities (trips and passenger profiles: the user data that+      must survive a restore).+    * secure_prefs.xml — SQLCipher passphrase for that DB, wrapped by a+      non-exportable AndroidKeyStore key.+    * filesDir/datastore_default.preferences_pb — shared DataStore UI+      preferences. -  travel-db is excluded from both sections. Two independent reasons:+  travel-db is excluded from both sections as a raw file: it is+  SQLCipher-encrypted under a passphrase wrapped by a hardware-bound+  AndroidKeyStore key that never backs up, so a restored raw copy cannot be+  opened on the new device (a fresh key/passphrase is minted and Room fails+  with SQLiteNotADatabaseException). secure_prefs.xml is excluded for the+  same reason — restoring the wrapped passphrase without its KeyStore key+  is useless and would block minting a fresh one cleanly. -  1. Correctness. It is built with library:room buildDatabase(), so it is-     SQLCipher-encrypted under a passphrase wrapped by an AndroidKeystore key-     that is hardware-bound and never backed up. On the restored device-     DatabaseHelper.isKeyGenerated() is false, a new key and passphrase are-     generated, encryptExistingDatabase() leaves the already-encrypted file-     alone, and Room opens it with the wrong key —-     SQLiteNotADatabaseException. The restored file is unusable by-     construction.+  The trips still migrate: travel declares+  android:backupAgent=".platform.AppBackupAgent", which uses+  SqlCipherDbCodec (the weather pattern) to export the DB to plaintext+  inside the backup payload and re-encrypt it under the new device's key+  on restore, plus the datastore_default file. That path is the one that+  works; a raw file copy would only get in its way. -  2. Sensitivity. Customer and FrequentFlyer rows are passenger PII (names,-     loyalty numbers) and BookedTrip is an itinerary. Even if the encryption-     problem were solved, this is not content to push to cloud backup casually.--  Heads-up for whoever owns this app: unlike weather and games/hub, travel does-  NOT declare an android:backupAgent, so there is currently no SqlCipherDbCodec-  path either. That means bookings do not migrate to a new device at all today.-  These rules do not cause that — they just stop the migration from failing-  loudly at first launch instead of quietly. Fixing it needs a BackupAgent, not-  a backup rule.--  Preferences and the DataStore file still back up normally.+  SCOPE: travel declares android:backupAgent and does NOT set+  android:fullBackupOnly, so Android uses key/value backup here and the+  <cloud-backup> section below is NOT consulted. Cloud content is exactly+  AppBackupAgent's travelDbConfigs(this) (travel-db via SqlCipherDbCodec)+  plus datastore_default. The rules below govern <device-transfer>, and+  are the correct cloud control if the backupAgent is ever removed: on+  that path the DB and secure_prefs stay excluded, so a restore is a+  clean first launch rather than an undecryptable DB. --> <data-extraction-rules>+    <!-- Not consulted while android:backupAgent is declared; see SCOPE above. -->     <cloud-backup>         <include domain="root" path="." />         <include domain="file" path="." />
travel/src/test/java/com/vayunmathur/travel/platform/TravelBackupAgentTest.kt
diff --git a/travel/src/test/java/com/vayunmathur/travel/platform/TravelBackupAgentTest.kt b/travel/src/test/java/com/vayunmathur/travel/platform/TravelBackupAgentTest.ktnew file mode 100644index 0000000000..0ac825aa98--- /dev/null+++ b/travel/src/test/java/com/vayunmathur/travel/platform/TravelBackupAgentTest.kt@@ -0,0 +1,79 @@+package com.vayunmathur.travel.platform++import android.content.Context+import android.content.pm.ApplicationInfo+import android.content.res.XmlResourceParser+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.library.room.SqlCipherDbCodec+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertSame+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Host-side wiring proof for travel platform backup: the SQLCipher codec+ * itself needs a device (native SQLCipher + AndroidKeyStore), so this+ * pins the parts a host JVM can prove — the manifest selects+ * [AppBackupAgent] with allowBackup, the agent carries the codec and the+ * shared DataStore file, and both extraction-rule channels exclude the+ * raw encrypted DB and its wrapped passphrase.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [35])+class TravelBackupAgentTest {++    private val ctx: Context get() = ApplicationProvider.getApplicationContext()++    @Test+    fun manifestSelectsBackupAgentWithAllowBackup() {+        val info = ctx.packageManager.getApplicationInfo(ctx.packageName, 0)+        assertTrue(info.flags and ApplicationInfo.FLAG_ALLOW_BACKUP != 0)+        assertEquals(+            "com.vayunmathur.travel.platform.AppBackupAgent",+            info.backupAgentName,+        )+    }++    @Test+    fun agentCarriesSqlCipherCodecAndDatastore() {+        val agent = AppBackupAgent()+        assertSame(SqlCipherDbCodec, agent.dbCodec)+        assertEquals(listOf("datastore_default"), agent.datastoreNames)+    }++    @Test+    fun extractionRulesExcludeRawDbAndSecurePrefsOnBothChannels() {+        val resId = ctx.resources.getIdentifier("data_extraction_rules", "xml", ctx.packageName)+        assertTrue(resId != 0, "manifest dataExtractionRules resource resolves")+        val parser: XmlResourceParser = ctx.resources.getXml(resId)+        val sections = mutableMapOf<String, MutableSet<String>>()+        var section: String? = null+        var event = parser.eventType+        while (event != XmlResourceParser.END_DOCUMENT) {+            if (event == XmlResourceParser.START_TAG) {+                when (parser.name) {+                    "cloud-backup", "device-transfer" -> {+                        section = parser.name+                        sections[section] = mutableSetOf()+                    }+                    "exclude", "include" -> section?.let {+                        sections.getValue(it).add(+                            "${parser.name}:${parser.getAttributeValue(null, "domain")}:${parser.getAttributeValue(null, "path")}",+                        )+                    }+                }+            }+            if (event == XmlResourceParser.END_TAG && parser.name in sections) section = null+            event = parser.next()+        }+        for (channel in listOf("cloud-backup", "device-transfer")) {+            val rules = sections[channel].orEmpty()+            assertTrue("exclude:database:travel-db" in rules, "$channel must exclude raw travel-db, was $rules")+            assertTrue("exclude:database:travel-db-wal" in rules, "$channel must exclude travel-db-wal, was $rules")+            assertTrue("exclude:sharedpref:secure_prefs.xml" in rules, "$channel must exclude secure_prefs, was $rules")+        }+    }+}