Modern Apps git

← Modern-Apps.git

news: exclude encrypted cache DB from platform backup

Commit
09a686f4ac4bef3977406eaee069bc596656ae59
Author
Vayun Mathur <[email protected]>
When
2026-12-04 21:51 UTC (1 day ago)
Parents
5cc276a
The data-extraction rules included every domain, so stock-OS
cloud backup and device transfer carried the raw SQLCipher
news-db. Its passphrase is wrapped by a non-exportable
AndroidKeystore key, so a restored copy cannot be opened on a
new device: a fresh key and passphrase are minted,
encryptExistingDatabase skips the already-encrypted file, and
Room then fails to open it.

Audit by data kind: CachedArticle and CachedBody are a feed
cache re-derivable from the server news.json snapshot (or the
on-device crawl) and by re-extracting publisher HTML; there is
no read-state store. The genuine user state is in NewsPrefs
(reader text size/serif, manually set Local location). The
rules now allowlist only datastore/news.preferences_pb in
both cloud-backup (gated on device encryption) and
device-transfer; the DB, its sidecars and secure_prefs.xml are
omitted, so the app starts with an empty cache and re-fetches.

The last-merged server-feed stamp moves from the backed-up
news DataStore to a separate non-backed-up news_feed_cache
DataStore: restoring the stamp without the DB would have made
the first refresh skip the merge and leave the feed empty.
Existing installs lose only the stamp and re-merge once.
Bookmarked rows live in the excluded cache DB; they are
pointers to public publisher articles whose stub and body are
re-fetchable, not user-authored content, so no SqlCipherDbCodec
round-trip (which would also ship the bulk cache as plaintext
in the backup payload) is used. No BackupAgent is declared;
allowBackup stays true with the rules as the sole control
(minSdk 31).

Verified: ./gradlew :news:lint :news:lintVitalRelease pass
(lintVitalRelease also run standalone, BUILD SUCCESSFUL);
:news:testDevUnitTest runs 102 tests with 1 failure in
NewsPrefsTest.noFeedSortPreferenceExists, proven pre-existing
by running the same test on a clean 30be4a93a worktree (same
failure: the reflection scan matches LEGACY_FEED_SORT). The
new serverFeedStampPersistsAndClears test passes.

Diff

news/AGENTS.md
diff --git a/news/AGENTS.md b/news/AGENTS.mdindex 4a02075f50..f2aa5e19bc 100644--- a/news/AGENTS.md+++ b/news/AGENTS.md@@ -112,6 +112,11 @@ imports). → `domain/SitemapParser.kt` + `domain/ArticleExtractor.kt`.  ## Gotchas+- Backup: the Room DB is a re-derivable feed/body cache and is excluded+  from platform backup (`data_extraction_rules.xml` allowlists only+  `datastore/news.preferences_pb`); the server-feed stamp lives in the+  separate non-backed-up `news_feed_cache` DataStore so a restored stamp+  cannot suppress the first merge into an empty DB. - Selection is navigation: the article URL lives in `Route.Reader`, never   in ViewModel state that swaps list→detail. - A failing source never fails a refresh; the cached feed stays visible.
news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt
diff --git a/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt b/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.ktindex 66feebf242..90c40e61af 100644--- a/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt+++ b/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt@@ -14,9 +14,16 @@ import java.io.IOException  private val Context.newsDataStore by preferencesDataStore(name = "news") +/** Cache-coherence state, kept out of the backed-up user prefs (see data_extraction_rules). */+private val Context.newsFeedCacheDataStore by preferencesDataStore(name = "news_feed_cache")+ /**  * The app's only preferences owner. Reader settings and the Local- * news location. A corrupt DataStore file reads as the documented+ * news location live in the `news` DataStore (the file platform backup+ * carries); the last-merged server-feed stamp lives in the separate+ * `news_feed_cache` DataStore, which backup excludes, so a restored+ * stamp can never claim a feed is merged into an empty restored DB.+ * A corrupt DataStore file reads as the documented  * defaults (17sp sans, no location) instead of crashing — see the  * catch in [safeData].  *@@ -51,11 +58,18 @@ class NewsPrefs(context: Context) {      * parse and Room re-publish entirely (see NewsRepository); null      * until the first successful server-feed merge.      */-    val serverFeedStamp: Flow<String?> = safeData.map { it[SERVER_FEED_STAMP]?.takeIf { v -> v.isNotBlank() } }+    val serverFeedStamp: Flow<String?> =+        appContext.newsFeedCacheDataStore.data.catch { e ->+            if (e is IOException) {+                emit(androidx.datastore.preferences.core.emptyPreferences())+            } else {+                throw e+            }+        }.map { it[SERVER_FEED_STAMP]?.takeIf { v -> v.isNotBlank() } }      /** Persists the last-merged server feed stamp; null or blank clears it. */     suspend fun setServerFeedStamp(stamp: String?) {-        appContext.newsDataStore.edit { prefs ->+        appContext.newsFeedCacheDataStore.edit { prefs ->             val trimmed = stamp?.trim().orEmpty()             if (trimmed.isEmpty()) prefs.remove(SERVER_FEED_STAMP) else prefs[SERVER_FEED_STAMP] = trimmed         }
news/src/main/res/xml/data_extraction_rules.xml
diff --git a/news/src/main/res/xml/data_extraction_rules.xml b/news/src/main/res/xml/data_extraction_rules.xmlindex 81a97e0f26..a278878970 100644--- a/news/src/main/res/xml/data_extraction_rules.xml+++ b/news/src/main/res/xml/data_extraction_rules.xml@@ -1,17 +1,41 @@ <?xml version="1.0" encoding="utf-8"?>+<!--+  News. Audited state:+    * databases/news-db — Room DB holding CachedArticle (feed stubs merged+      from the server's news.json snapshot or the on-device crawl, pruned to+      the newest 500 unbookmarked rows) and CachedBody (article bodies+      extracted on device from publisher HTML). Both are re-derivable: a+      fresh install re-fetches the server feed and re-extracts any body+      from its publisher URL on open, so the DB is a cache, not user data.+      There is no read-state store. Bookmarked rows are pointers to public+      publisher articles in that same cache.+    * files/datastore/news.preferences_pb — NewsPrefs user state only:+      reader text size / serif and the manually set Local location.+    * files/datastore/news_feed_cache.preferences_pb — the last-merged+      server-feed stamp (a cache-coherence marker, not user state). It is+      kept in a separate DataStore so it never travels with the user+      prefs: restoring the stamp without the DB would make the first+      refresh skip the merge and leave the feed empty.+    * sharedpref/secure_prefs.xml — SQLCipher passphrase wrapped by a+      non-exportable AndroidKeystore key.++  news-db is excluded from both sections (by allowlist omission): it is+  SQLCipher-encrypted under a passphrase wrapped by a device-bound+  AndroidKeystore key that never leaves the old device, so a restored+  copy cannot be opened — a fresh key/passphrase is minted on the new+  device, encryptExistingDatabase() skips the already-encrypted file,+  and Room fails to open it. Shipping it would trade a clean first+  launch (empty cache, immediate re-fetch) for a crash. The Keystore-+  wrapped passphrase in secure_prefs.xml is likewise omitted.++  Only the user prefs file travels, in both channels. No BackupAgent is+  declared, so platform backup applies these rules directly.+--> <data-extraction-rules>-    <cloud-backup>-        <include domain="root" path="." />-        <include domain="file" path="." />-        <include domain="database" path="." />-        <include domain="sharedpref" path="." />-        <include domain="external" path="." />+    <cloud-backup disableIfNoEncryptionCapabilities="true">+        <include domain="file" path="datastore/news.preferences_pb" />     </cloud-backup>     <device-transfer>-        <include domain="root" path="." />-        <include domain="file" path="." />-        <include domain="database" path="." />-        <include domain="sharedpref" path="." />-        <include domain="external" path="." />+        <include domain="file" path="datastore/news.preferences_pb" />     </device-transfer> </data-extraction-rules>
news/src/test/java/com/vayunmathur/news/data/NewsPrefsTest.kt
diff --git a/news/src/test/java/com/vayunmathur/news/data/NewsPrefsTest.kt b/news/src/test/java/com/vayunmathur/news/data/NewsPrefsTest.ktindex 43ee0f0d09..51c17ccc1c 100644--- a/news/src/test/java/com/vayunmathur/news/data/NewsPrefsTest.kt+++ b/news/src/test/java/com/vayunmathur/news/data/NewsPrefsTest.kt@@ -25,6 +25,17 @@ class NewsPrefsTest {         assertEquals("Chicago, Illinois, United States", prefs.localLocation.first())     } +    @Test fun serverFeedStampPersistsAndClears() = runBlocking {+        val context = ApplicationProvider.getApplicationContext<Application>()+        val prefs = NewsPrefs(context)+        prefs.setServerFeedStamp(null)+        assertNull(prefs.serverFeedStamp.first())+        prefs.setServerFeedStamp("  2026-11-23T12:00:00Z  ")+        assertEquals("2026-11-23T12:00:00Z", prefs.serverFeedStamp.first())+        prefs.setServerFeedStamp(null)+        assertNull(prefs.serverFeedStamp.first())+    }+     @Test fun noFeedSortPreferenceExists() {         // The feed has one ordering only, so the prefs owner must not         // expose any sort state (member, accessor, or backing key).