Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

home/android: every docId form the apps mint counts as a docId (#755)

nyblnet3e9ed6537ac0
diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md
index a5507e2a..4a1deb75 100644
--- a/docs/DECISIONS.md
+++ b/docs/DECISIONS.md
@@ -14,6 +14,23 @@ Decision. Why. Pointers.
---
+## 2026-10-18 — Android: every docId form the apps mint counts as a docId
+
+**Supersedes "a strict UUID"** in the entry below. Spaces always mints
+`doc-<base36>`, and dash and slides fall back to `d-<base36>…` where
+`crypto.randomUUID` is missing. Read as "no docId", those documents fell back
+to the content hash: every external edit got fresh storage, losing the member
+key and recovery copy, and on update they refused their own legacy storage.
+Not a security issue, because a docId only ever refuses, but it orphaned state
+for whole apps.
+
+A docId is now any `^[A-Za-z0-9_-]{1,128}$`, compared exactly as stored with
+no case folding, the same as the iOS host. JVM cases go through `extractDocId`
+from real document HTML, because the bug was in reading the id: a doc- id and a
+d- id each adopt their own legacy storage, a different doc- id is refused, and
+a doc- id keeps its claim across an external edit. 4 of them fail against the
+strict check.
+
## 2026-10-18 — A document's storage origin follows the file, not its path (iOS)
**Corrects 2026-09-19**, which said the native hosts "were never exposed"
diff --git a/home/android/app/src/main/java/page/bento/home/DocumentOrigin.kt b/home/android/app/src/main/java/page/bento/home/DocumentOrigin.kt
index 7e3cac21..3a5a72c7 100644
--- a/home/android/app/src/main/java/page/bento/home/DocumentOrigin.kt
+++ b/home/android/app/src/main/java/page/bento/home/DocumentOrigin.kt
@@ -99,13 +99,20 @@ object DocumentOrigin {
return hex(hmac(subject.toByteArray())).take(24)
}
- private val UUID = Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
+ /**
+ * Every form the apps mint: a UUID where `crypto.randomUUID` exists, and
+ * otherwise spaces' `doc-<base36>` (always, in fact) and dash's or slides'
+ * `d-<base36>…`. An earlier strict-UUID check read those as "no docId", so
+ * their documents fell back to the content hash — fresh storage on every
+ * external edit — and refused their own legacy storage on update.
+ * Compared exactly as stored, never case-folded, the same on every host.
+ */
+ private val DOC_ID = Regex("^[A-Za-z0-9_-]{1,128}$")
/**
* The docId from the document's `#bento-doc` block, or null when there is
* none to trust: no block, an empty block (a never-saved shell), an
- * encrypted envelope (no top-level docId), or a value that is not a strict
- * UUID.
+ * encrypted envelope (no top-level docId), or a value outside [DOC_ID].
*
* Only the TOP-LEVEL `docId` key counts. Document content can carry the
* string `"docId"` anywhere — a pasted clip, a comment — so this tracks
@@ -137,7 +144,7 @@ object DocumentOrigin {
if (j < json.length && json[j] == '"') {
val vEnd = stringEnd(json, j) ?: return null
val v = json.substring(j + 1, vEnd)
- return if (UUID.matches(v)) v.lowercase() else null
+ return if (DOC_ID.matches(v)) v else null
}
return null
}
@@ -183,7 +190,7 @@ object DocumentOrigin {
for (let i = 0; i < localStorage.length; i++) {
const k = localStorage.key(i)
const m = /^bento-member-(.+)$/.exec(k)
- if (m) ids.add(m[1].toLowerCase())
+ if (m) ids.add(m[1])
}
} catch (e) { unattributed = true }
try {
@@ -198,7 +205,7 @@ object DocumentOrigin {
for (const st of db.objectStoreNames) {
const all = await new Promise(res => { const r = db.transaction(st).objectStore(st).getAll(); r.onsuccess = () => res(r.result || []); r.onerror = () => res(null) })
if (!all) { unattributed = true; continue }
- for (const rec of all) { if (rec && typeof rec.docId === 'string') ids.add(rec.docId.toLowerCase()); else unattributed = true }
+ for (const rec of all) { if (rec && typeof rec.docId === 'string') ids.add(rec.docId); else unattributed = true }
}
db.close()
}
diff --git a/home/android/app/src/test/java/page/bento/home/DocumentOriginTest.kt b/home/android/app/src/test/java/page/bento/home/DocumentOriginTest.kt
index e7226c32..a2d90c05 100644
--- a/home/android/app/src/test/java/page/bento/home/DocumentOriginTest.kt
+++ b/home/android/app/src/test/java/page/bento/home/DocumentOriginTest.kt
@@ -130,12 +130,49 @@ class DocumentOriginTest {
assertNull(DocumentOrigin.extractDocId(doc("""{"slides":[{"docId":"$b"}]}""")))
}
- @Test fun `no block, an empty block, an envelope and a non-UUID all give null`() {
+ @Test fun `no block, an empty block, an envelope and a malformed id all give null`() {
assertNull(DocumentOrigin.extractDocId("<html></html>"))
assertNull(DocumentOrigin.extractDocId(doc("\n")))
assertNull(DocumentOrigin.extractDocId(doc("""{"format":"bento/enc","v":1,"it":300000,"salt":"x","iv":"y","data":"z"}""")))
- assertNull(DocumentOrigin.extractDocId(doc("""{"docId":"not-a-uuid"}""")))
- assertNull(DocumentOrigin.extractDocId(doc("""{"docId":"$a-and-more"}""")))
+ assertNull(DocumentOrigin.extractDocId(doc("""{"docId":""}""")))
+ assertNull(DocumentOrigin.extractDocId(doc("""{"docId":"has space"}""")))
+ assertNull(DocumentOrigin.extractDocId(doc("""{"docId":"../escape"}""")))
+ assertNull(DocumentOrigin.extractDocId(doc("""{"docId":"${"x".repeat(129)}"}""")))
+ }
+
+ // ------------------------------------------------- every docId form the apps mint
+
+ private val spacesId = "doc-k3x9q2ab" // spaces: always uid('doc')
+ private val dashId = "d-4fz8w1qplmz9k2" // dash / slides without crypto.randomUUID
+
+ @Test fun `spaces and fallback docIds are read as docIds, exactly as stored`() {
+ assertEquals(spacesId, DocumentOrigin.extractDocId(doc("""{"format":"bento/spaces","docId":"$spacesId"}""")))
+ assertEquals(dashId, DocumentOrigin.extractDocId(doc("""{"docId":"$dashId"}""")))
+ assertEquals("Doc-MiXeD", DocumentOrigin.extractDocId(doc("""{"docId":"Doc-MiXeD"}""")))
+ }
+
+ // These go through extractDocId from real document HTML, as the host does:
+ // the bug was in reading the id, so a case that hands decide() the id
+ // directly would pass against the broken code.
+ private fun factsOf(id: String, sha: String = "s1") =
+ Facts(uri, DocumentOrigin.extractDocId(doc("""{"format":"bento/spaces","docId":"$id","title":"t"}""")), sha)
+
+ @Test fun `a doc- id adopts its own legacy storage`() {
+ assertTrue(DocumentOrigin.mayAdoptLegacy(factsOf(spacesId), Findings(setOf(spacesId), false)))
+ }
+
+ @Test fun `a d- id adopts its own legacy storage`() {
+ assertTrue(DocumentOrigin.mayAdoptLegacy(factsOf(dashId), Findings(setOf(dashId), false)))
+ }
+
+ @Test fun `a successor with a different doc- id is refused`() {
+ assertFalse(DocumentOrigin.mayAdoptLegacy(factsOf("doc-zzzzzzzz"), Findings(setOf(spacesId), false)))
+ assertEquals(Decision.Fresh, DocumentOrigin.decide(factsOf("doc-zzzzzzzz"), Claim("o", spacesId, "s1")))
+ }
+
+ @Test fun `a doc- id keeps its claim across an external edit`() {
+ // the regression: read as docless, it fell to the content hash and lost its storage
+ assertEquals(Decision.Reuse("o"), DocumentOrigin.decide(factsOf(spacesId, "s2"), Claim("o", spacesId, "s1")))
}
@Test fun `escaped quotes and the save's u003c escaping do not derail the scan`() {