0198c5f45b84

fix: equalize adoption frame count for duress privacy (PB-26-009, v2.17.6)

time
2026-09-08 14:48 UTC · 2 months ago
machine
koh2
agent
claude-opus-5
session
duress frame privacy
parent
1af32fa5d878
3 files VERSION+1 −1app/src/main/java/com/peachpasswords/sync/VaultAdopt.kt+25 −37app/src/test/java/com/peachpasswords/sync/VaultAdoptSnapshotTest.kt+31 −4
VERSION+1 −1
@@ -1 +1 @@1-2.17.51+2.17.6

view file

app/src/main/java/com/peachpasswords/sync/VaultAdopt.kt+25 −37
@@ -571,25 +571,22 @@571571 /**572572  * Source side: chunk the vault snapshot and send it over the pairing channel.573573  *574- * Wire shape (v2.9.9+, duress on its own frame; mirrors browser v4.6.4):574+ * Wire shape (v2.9.9+, supplemental frame; mirrors browser v4.6.4):575575  *   - N entries frames, each <= [MAX_SNAPSHOT_CHUNK_BYTES] serialized entries.576- *     Tombstones ride the last entries frame when no duress follows.577- *   - If duressSlot is set, one extra frame after the entries carrying only578- *     the duress slot (entries: empty, tombstones: tombstone map,579- *     duress_slot: duressSlot). This moves duress off the entries frame so a580- *     sizeable sealed duress vault can no longer push the entries frame raw581- *     bytes over [SNAPSHOT_FRAME_WIRE_CEILING] (768 KiB).576+ *   - One supplemental frame always follows the entries. It carries the577+ *     tombstones and, when configured, the duress slot. Sending the same frame578+ *     count in both cases prevents a relay from learning duress presence.579+ *     Keeping duress off the entries frame also prevents a sizeable sealed580+ *     duress vault from pushing the entries frame over the wire ceiling.582581  *583582  * Frame budget ([MAX_SNAPSHOT_CHUNKS] = 200):584- *   - When duress is configured, ONE of the 200 frames is reserved for the585- *     duress slot. The entries cap is [MAX_SNAPSHOT_CHUNKS] - 1 = 199 entries586- *     chunks; excess entries are deferred to the sync fallback pipeline (sync587- *     carries entries and tombstones on Android too, so this is a graceful588- *     truncation). This reservation is REQUIRED: ordinary sync does NOT589- *     carry duress on Android (VaultRepository.exportDuressSlotForAdoption590- *     is the only transfer path), so silently dropping a duress frame at591- *     the cap would leave the adopted device permanently without its duress592- *     vault. We prioritize duress delivery over completeness of entries.583+ *   - ONE of the 200 frames is always reserved for the supplemental frame;584+ *     entries use at most 199 chunks. Excess entries are deferred to the sync585+ *     fallback pipeline (sync carries entries and tombstones on Android too,586+ *     so this is a graceful truncation). The supplemental frame is required587+ *     even without duress so its presence cannot disclose configuration. When588+ *     configured, it carries the duress slot; omitting it would leave the589+ *     adopted device permanently without its duress vault.593590  *594591  * Oversized duress: duress that itself exceeds [SNAPSHOT_FRAME_WIRE_CEILING]595592  * is rejected with a thrown error BEFORE any snapshot frame is sent. Hard stop;@@ -635,12 +632,9 @@635632     duressSlot: String?,636633 ): List<AdoptionFrame.Snapshot> {637634     val includeDuress = !duressSlot.isNullOrBlank()638-    // Reserve one frame for duress when configured so duress is never639-    // silently dropped at the cap; ordinary sync does NOT carry duress on640-    // Android (VaultRepository.exportDuressSlotForAdoption is the only641-    // transfer path), so dropping a duress frame would orphan the adopted642-    // device's duress vault.643-    val entryCap = if (includeDuress) MAX_SNAPSHOT_CHUNKS - 1 else MAX_SNAPSHOT_CHUNKS635+    // Always reserve the supplemental frame so its presence cannot disclose636+    // whether the source has a duress vault.637+    val entryCap = MAX_SNAPSHOT_CHUNKS - 1644638 645639     val chunks = mutableListOf<Map<String, String>>()646640     var current = LinkedHashMap<String, String>()@@ -659,40 +653,34 @@659653     chunks.add(current)660654 661655     val entriesChunkCount = minOf(chunks.size, entryCap)662-    val sendDuress = includeDuress663-    val totalChunks = entriesChunkCount + (if (sendDuress) 1 else 0)664-    val duressFrame = if (sendDuress) AdoptionFrame.Snapshot(656+    val totalChunks = entriesChunkCount + 1657+    val supplementalFrame = AdoptionFrame.Snapshot(665658         chunkIndex = entriesChunkCount,666659         chunkCount = totalChunks,667660         entries = emptyMap(),668661         tombstones = tombstones,669-        duressSlot = duressSlot,670-    ) else null662+        duressSlot = duressSlot.takeIf { includeDuress },663+    )671664 672665     if (673-        duressFrame != null &&674-        AdoptJson.encodeToString(AdoptionFrame.serializer(), duressFrame)666+        AdoptJson.encodeToString(AdoptionFrame.serializer(), supplementalFrame)675667             .encodeToByteArray().size > SNAPSHOT_FRAME_WIRE_CEILING676668     ) {677-        throw SnapshotDuressFrameTooLargeException()669+        if (includeDuress) throw SnapshotDuressFrameTooLargeException()670+        throw IllegalStateException("ADOPT_SNAPSHOT_SUPPLEMENTAL_FRAME_TOO_LARGE")678671     }679672 680673     val frames = mutableListOf<AdoptionFrame.Snapshot>()681674     for (index in 0 until entriesChunkCount) {682-        val isFinalEntries = index == entriesChunkCount - 1683675         frames.add(AdoptionFrame.Snapshot(684676             chunkIndex = index,685677             chunkCount = totalChunks,686678             entries = chunks[index],687-            // Tombstones ride the LAST wire frame: when duress follows the688-            // entries, it goes on the duress frame; otherwise on the last689-            // entries chunk.690-            tombstones = if (isFinalEntries && !sendDuress) tombstones else emptyMap(),679+            tombstones = emptyMap(),691680             duressSlot = null,692681         ))693682     }694-695-    if (duressFrame != null) frames.add(duressFrame)683+    frames.add(supplementalFrame)696684     return frames697685 }698686 

view file

app/src/test/java/com/peachpasswords/sync/VaultAdoptSnapshotTest.kt+31 −4
@@ -30,10 +30,37 @@3030 3131         sendVaultSnapshot(channel, mapOf("id-0" to "ciphertext"), emptyMap())3232 33-        val wire = sent.single()34-        assertEquals(SyncPadding.bucket(wire.size), wire.size)35-        assertTrue(String(wire, Charsets.UTF_8).first() != '{')36-        assertTrue(String(SyncPadding.unpad(wire), Charsets.UTF_8).startsWith("{\"type\":\"ADOPT_SNAPSHOT\""))33+        assertEquals(2, sent.size)34+        sent.forEach { wire ->35+            assertEquals(SyncPadding.bucket(wire.size), wire.size)36+            assertTrue(String(wire, Charsets.UTF_8).first() != '{')37+            assertTrue(String(SyncPadding.unpad(wire), Charsets.UTF_8).startsWith("{\"type\":\"ADOPT_SNAPSHOT\""))38+        }39+40+        val supplemental = AdoptJson.decodeFromString(41+            AdoptionFrame.Snapshot.serializer(),42+            String(SyncPadding.unpad(sent.last()), Charsets.UTF_8),43+        )44+        assertTrue(supplemental.entries.isEmpty())45+        assertTrue(supplemental.tombstones.isEmpty())46+        assertNull(supplemental.duressSlot)47+    }48+49+    @Test50+    fun `duress and no-duress snapshots use the same frame count`() = runBlocking {51+        suspend fun frameCount(duressSlot: String?): Int {52+            val sent = mutableListOf<ByteArray>()53+            val channel = object : PairingTransport {54+                override suspend fun send(msg: ByteArray) { sent += msg }55+                override suspend fun receive(): ByteArray = error("unused")56+                override suspend fun close() {}57+            }58+            sendVaultSnapshot(channel, mapOf("id-0" to "ciphertext"), emptyMap(), duressSlot)59+            return sent.size60+        }61+62+        val duressJson = """{"version":1,"salt":"aa","nonce":"bb","ciphertext":"cc","tag":"dd"}"""63+        assertEquals(frameCount(null), frameCount(duressJson))3764     }3865 3966     private fun memoryPair(): Pair<PairingTransport, PairingTransport> {

view file