bcb7ad4b77cd

fix: preserve mesh peers on adoption identity collision (v2.17.12)

time
2026-09-09 01:36 UTC · 2 months ago
machine
koh2
parent
98758bdccd38
6 files VERSION+1 −1app/src/main/java/com/peachpasswords/sync/PairedDeviceStore.kt+2 −0app/src/main/java/com/peachpasswords/sync/VaultAdopt.kt+20 −2app/src/main/java/com/peachpasswords/ui/pairing/AdoptVaultViewModel.kt+3 −0app/src/main/java/com/peachpasswords/ui/pairing/CodePairViewModel.kt+3 −0app/src/test/java/com/peachpasswords/sync/VaultAdoptTest.kt+43 −0
VERSION+1 −1
@@ -1 +1 @@1-2.17.111+2.17.12

view file

app/src/main/java/com/peachpasswords/sync/PairedDeviceStore.kt+2 −0
@@ -70,6 +70,8 @@7070             it.longTermPublicKeyHex?.let { key -> CryptoCompare.hex(key, pubkeyHex) } == true7171         }7272 73+    fun hasIdentityCollision(device: PairedDevice): Boolean = get(device.deviceId) != null74+7375     /** Insert or replace by [PairedDevice.deviceId]. */7476     override fun upsert(device: PairedDevice) {7577         mutationGate.withLock {

view file

app/src/main/java/com/peachpasswords/sync/VaultAdopt.kt+20 −2
@@ -317,6 +317,7 @@317317      */318318     suspend fun onConsent(request: AdoptionFrame.Request): Boolean319319     suspend fun upsertPairedDevice(device: PairedDevice)320+    fun hasDeviceIdentityCollision(device: PairedDevice): Boolean = false320321     val pairState: AdoptionPairState? get() = null321322     /** Return all encrypted entries + tombstones for the inline snapshot. */322323     suspend fun vaultSnapshot(): Pair<Map<String, String>, Map<String, Long>>? = null@@ -526,11 +527,11 @@526527         snapshotCapable = request.snapshotCapable == true,527528     ))528529 529-    val identity = receiveFrame(channel)530+    var identity = receiveFrame(channel)530531     if (!isAdoptIdentity(identity)) return null531532     identity as AdoptionFrame.Identity532533 533-    val device = PairedDevice(534+    var device = PairedDevice(534535         deviceId = identity.deviceId,535536         deviceName = identity.deviceName,536537         deviceType = identity.deviceType,@@ -541,6 +542,23 @@541542         relayUrl = deps.pairState?.relayUrl,542543         pairedAt = nowMs(),543544     )545+    if (deps.hasDeviceIdentityCollision(device)) {546+        sendFrame(channel, AdoptionFrame.Reject("ADOPT_DEVICE_ID_COLLISION"))547+        identity = receiveFrame(channel)548+        if (!isAdoptIdentity(identity)) return null549+        identity as AdoptionFrame.Identity550+        device = device.copy(551+            deviceId = identity.deviceId,552+            deviceName = identity.deviceName,553+            deviceType = identity.deviceType,554+            longTermPublicKeyHex = identity.longTermPublicKeyHex,555+            signingPublicKeyHex = identity.signingPublicKeyHex,556+        )557+        if (deps.hasDeviceIdentityCollision(device)) {558+            sendFrame(channel, AdoptionFrame.Reject("ADOPT_DEVICE_ID_COLLISION"))559+            return null560+        }561+    }544562     deps.upsertPairedDevice(device)545563     sendFrame(channel, AdoptionFrame.Complete)546564     if (!isAdoptApplied(receiveFrame(channel))) return null

view file

app/src/main/java/com/peachpasswords/ui/pairing/AdoptVaultViewModel.kt+3 −0
@@ -391,6 +391,9 @@391391 392392             override suspend fun onConsent(request: AdoptionFrame.Request): Boolean = true393393 394+            override fun hasDeviceIdentityCollision(device: PairedDevice): Boolean =395+                devices.hasIdentityCollision(device)396+394397             override suspend fun upsertPairedDevice(device: PairedDevice) {395398                 devices.clearRevocation(device.deviceId)396399                 devices.upsert(device)

view file

app/src/main/java/com/peachpasswords/ui/pairing/CodePairViewModel.kt+3 −0
@@ -191,6 +191,9 @@191191             // consent. The scanning device confirms the SAS before any grant.192192             override suspend fun onConsent(request: AdoptionFrame.Request): Boolean = true193193 194+            override fun hasDeviceIdentityCollision(device: PairedDevice): Boolean =195+                deviceStore.hasIdentityCollision(device)196+194197             override suspend fun upsertPairedDevice(device: PairedDevice) {195198                 // SAS-confirmed direct adoption — the one consent signal strong196199                 // enough to lift a terminal revocation.

view file

app/src/test/java/com/peachpasswords/sync/VaultAdoptTest.kt+43 −0
@@ -93,6 +93,7 @@9393         var licenseApplied: String? = null9494         var consentAsked = false9595         var rootRequested = false96+        var collidingDeviceId: String? = null9697         override suspend fun rootKeyHex(): String {9798             rootRequested = true9899             return rootKeyHex@@ -107,6 +108,8 @@107108             consentAsked = true108109             return consent109110         }111+        override fun hasDeviceIdentityCollision(device: PairedDevice): Boolean =112+            device.deviceId == collidingDeviceId110113         override suspend fun upsertPairedDevice(device: PairedDevice) { upserted += device }111114     }112115 @@ -138,6 +141,46 @@138141         assertEquals("source-id", receiver.upserted.single().deviceId)139142         assertEquals("receiver-id", source.upserted.single().deviceId)140143         assertEquals("android", source.upserted.single().deviceType)144+    }145+146+    @Test fun source_preserves_existing_peer_and_accepts_rotated_identity_after_collision() = runBlocking {147+        val (receiverCh, sourceCh) = memoryPair()148+        val rootKeyHex = "13".repeat(32)149+        val vaultId = vaultIdFor(rootKeyHex)150+        val source = RecorderSource(identity("android"), rootKeyHex, vaultId).apply {151+            collidingDeviceId = "chrome-id"152+        }153+154+        val sourcePromise = async {155+            grantAdoption(sourceCh, AdoptionFrame.Request(156+                sessionRequestId = "receiver-ephemeral",157+                deviceName = "Firefox",158+                deviceType = DeviceType.EXTENSION.wireValue,159+            ), source)160+        }161+        sendFrame(receiverCh, AdoptionFrame.Confirm(true))162+        assertTrue(receiveFrame(receiverCh) is AdoptionFrame.Grant)163+        sendFrame(receiverCh, AdoptionFrame.Identity(164+            deviceId = "chrome-id",165+            deviceName = "Firefox",166+            deviceType = DeviceType.EXTENSION.wireValue,167+            longTermPublicKeyHex = "11".repeat(32),168+        ))169+        val rejection = receiveFrame(receiverCh)170+        assertTrue(rejection is AdoptionFrame.Reject)171+        assertEquals("ADOPT_DEVICE_ID_COLLISION", (rejection as AdoptionFrame.Reject).reason)172+        source.collidingDeviceId = null173+        sendFrame(receiverCh, AdoptionFrame.Identity(174+            deviceId = "firefox-id",175+            deviceName = "Firefox",176+            deviceType = DeviceType.EXTENSION.wireValue,177+            longTermPublicKeyHex = "22".repeat(32),178+        ))179+        assertTrue(receiveFrame(receiverCh) is AdoptionFrame.Complete)180+        sendFrame(receiverCh, AdoptionFrame.Applied)181+182+        assertEquals("firefox-id", sourcePromise.await()?.deviceId)183+        assertEquals(listOf("firefox-id"), source.upserted.map { it.deviceId })141184     }142185 143186     @Test fun rejects_without_creating_or_pairing_when_neither_peer_has_pro() = runBlocking {

view file