88c94830e48a

add protocolVersion to SYNC_HELLO, reject stale peers with update-required error

time
2026-09-08 22:15 UTC · 2 months ago
machine
koh2
agent
hf:zai-org/GLM-5.2
session
sync protocol version
parent
ef1cd7876e21
6 files VERSION+1 −1app/src/main/java/com/peachpasswords/sync/SyncCoordinator.kt+17 −0app/src/main/java/com/peachpasswords/sync/SyncMessage.kt+7 −0app/src/main/java/com/peachpasswords/sync/SyncOutcomeReason.kt+4 −0app/src/test/java/com/peachpasswords/sync/SyncCoordinatorTest.kt+18 −2app/src/test/java/com/peachpasswords/sync/SyncOutcomeTest.kt+10 −0
VERSION+1 −1
@@ -1 +1 @@1-2.17.91+2.17.10

view file

app/src/main/java/com/peachpasswords/sync/SyncCoordinator.kt+17 −0
@@ -29,6 +29,7 @@2929 /** Internal so the dialler's outer bound can be derived from it, never guessed. */3030 internal const val SESSION_TIMEOUT_MS = 30_000L3131 private const val CLOCK_SKEW_WARN_MS = 120_000L32+private const val SYNC_PROTOCOL_VERSION = 23233 3334 /**3435  * Vault-side dependency of the coordinator. The vault holds the canonical@@ -111,11 +112,19 @@111112                     vault.getOurDeviceId(),112113                     localVersion,113114                     nowMs,115+                    protocolVersion = SYNC_PROTOCOL_VERSION,114116                     deviceManifestVersion =115117                         if (vault.supportsDeviceManifest) DEVICE_MANIFEST_VERSION else null,116118                 ),117119             )118120             val hello = session.recvAs<SyncMessage.Hello>("initiator", "SYNC_HELLO")121+            if ((hello.protocolVersion ?: 1) < SYNC_PROTOCOL_VERSION) {122+                session.sendMsg(SyncMessage.Error("PEER_PROTOCOL_OUTDATED"))123+                return@withTimeout SyncOutcome(124+                    SyncOutcomeStatus.ERRORED,125+                    error = "PEER_PROTOCOL_OUTDATED",126+                )127+            }119128             if (hello.vaultId != ourVaultId) {120129                 session.sendMsg(SyncMessage.Error("vault_id_mismatch"))121130                 return@withTimeout SyncOutcome(SyncOutcomeStatus.VAULT_MISMATCH)@@ -204,6 +213,13 @@204213             val localVersion = vault.getLocalVersion()205214 206215             val hello = session.recvAs<SyncMessage.Hello>("responder", "SYNC_HELLO")216+            if ((hello.protocolVersion ?: 1) < SYNC_PROTOCOL_VERSION) {217+                session.sendMsg(SyncMessage.Error("PEER_PROTOCOL_OUTDATED"))218+                return@withTimeout SyncOutcome(219+                    SyncOutcomeStatus.ERRORED,220+                    error = "PEER_PROTOCOL_OUTDATED",221+                )222+            }207223             // Always answer HELLO so the initiator's recv completes, even on a208224             // mismatch where we bail immediately after.209225             session.sendMsg(@@ -212,6 +228,7 @@212228                     vault.getOurDeviceId(),213229                     localVersion,214230                     nowMs,231+                    protocolVersion = SYNC_PROTOCOL_VERSION,215232                     deviceManifestVersion =216233                         if (vault.supportsDeviceManifest) DEVICE_MANIFEST_VERSION else null,217234                 ),

view file

app/src/main/java/com/peachpasswords/sync/SyncMessage.kt+7 −0
@@ -20,6 +20,13 @@2020         val version: Long,2121         val deviceTime: Long,2222         /**23+         * Wire protocol version, incremented when the sync sequence itself24+         * changes. Distinct from [version] (vault data version) and25+         * [deviceManifestVersion]. Old clients omit this field; treat missing26+         * as 1.27+         */28+        val protocolVersion: Int? = null,29+        /**2330          * Advertises equal-version device-graph exchange support. Older peers2431          * omit this field and retain the legacy SYNC_DONE fast path.2532          */

view file

app/src/main/java/com/peachpasswords/sync/SyncOutcomeReason.kt+4 −0
@@ -42,6 +42,7 @@4242     RELAY_UNREACHABLE,4343     HANDSHAKE_FAILED,4444     VAULT_ID_MISMATCH,45+    PEER_PROTOCOL_OUTDATED,4546     SYNC_TIMEOUT,4647     CONNECTION_LOST,4748     RUN_FAILED,@@ -57,6 +58,7 @@5758             PRIVATE_RELAY_UNSET,5859             FOREGROUND_START_BLOCKED,5960             VAULT_ID_MISMATCH,61+            PEER_PROTOCOL_OUTDATED,6062             RUN_FAILED,6163             -> true6264 @@ -165,6 +167,7 @@165167         SyncOutcomeReason.RELAY_UNREACHABLE -> "Couldn't reach the relay"166168         SyncOutcomeReason.HANDSHAKE_FAILED -> "Reached the relay, but the device didn't answer"167169         SyncOutcomeReason.VAULT_ID_MISMATCH -> "That device holds a different vault"170+        SyncOutcomeReason.PEER_PROTOCOL_OUTDATED -> "Your other device needs to update before it can sync"168171         SyncOutcomeReason.SYNC_TIMEOUT -> "The device stopped responding mid-sync"169172         SyncOutcomeReason.CONNECTION_LOST -> "The connection dropped mid-sync — will retry"170173         SyncOutcomeReason.RUN_FAILED -> "Sync failed"@@ -187,6 +190,7 @@187190 internal fun classifySyncError(error: String): SyncOutcomeReason = when {188191     error.contains("VAULT_LOCKED") -> SyncOutcomeReason.VAULT_LOCKED189192     error.contains("vault_id_mismatch") -> SyncOutcomeReason.VAULT_ID_MISMATCH193+    error.contains("PEER_PROTOCOL_OUTDATED") -> SyncOutcomeReason.PEER_PROTOCOL_OUTDATED190194     error.contains("timed out") || error.contains("sync_timeout") -> SyncOutcomeReason.SYNC_TIMEOUT191195     // A KK failure, a mismatched PAIR_IDENTITY or a wrong room all mean the192196     // relay was fine and the peer was not who we expected.

view file

app/src/test/java/com/peachpasswords/sync/SyncCoordinatorTest.kt+18 −2
@@ -190,6 +190,21 @@190190         assertEquals(SyncOutcomeStatus.VAULT_MISMATCH, outB.status)191191     }192192 193+    @Test fun stale_peer_is_rejected_with_protocol_outdated() = runBlocking {194+        val staleHello =195+            SyncMessage.Hello("VAULT", "B", version = 2, deviceTime = 0L).encodeToBytes()196+        val queue = ArrayDeque(listOf(staleHello))197+        val session = SyncSession(198+            remoteStaticPublicKey = ByteArray(32),199+            send = { },200+            receive = { queue.removeFirstOrNull() ?: throw java.io.IOException("WebSocket closed") },201+            close = { },202+        )203+        val out = runSyncResponder(session, FakeAdapter("B", version = 2), nowMs = 0L)204+        assertEquals(SyncOutcomeStatus.ERRORED, out.status)205+        assertEquals("PEER_PROTOCOL_OUTDATED", out.error)206+    }207+193208     @Test fun tombstone_propagates_to_peer() = runBlocking {194209         val (sa, sb) = duplex()195210         val a = FakeAdapter("A", version = 3).apply { tombstones["gone"] = 500 }@@ -299,7 +314,7 @@299314         // 1: peer's HELLO (same vault, different version, with a future300315         // mtime so the equal-version short-circuit doesn't fire).301316         frames.add(302-            SyncMessage.Hello("VAULT", "B", version = 2, deviceTime = 0L).encodeToBytes(),317+            SyncMessage.Hello("VAULT", "B", version = 2, deviceTime = 0L, protocolVersion = 2).encodeToBytes(),303318         )304319         // 2: peer's MANIFEST (y@200).305320         frames.add(@@ -350,7 +365,8 @@350365         runSyncResponder(session, FakeAdapter("B", version = 2), nowMs = 0L)351366     }352367 353-    private fun hello() = SyncMessage.Hello("VAULT", "A", version = 1, deviceTime = 0L)368+    private fun hello() =369+        SyncMessage.Hello("VAULT", "A", version = 1, deviceTime = 0L, protocolVersion = 2)354370 355371     @Test fun oversized_manifest_is_refused() {356372         val over = (0..SyncLimits.MAX_ENTRY_COUNT).associate { "id-$it" to 1L }

view file

app/src/test/java/com/peachpasswords/sync/SyncOutcomeTest.kt+10 −0
@@ -50,6 +50,7 @@5050                 // person can do something about it.5151                 SyncOutcomeReason.FOREGROUND_START_BLOCKED,5252                 SyncOutcomeReason.VAULT_ID_MISMATCH,53+                SyncOutcomeReason.PEER_PROTOCOL_OUTDATED,5354                 SyncOutcomeReason.RUN_FAILED,5455             ),5556             SyncOutcomeReason.entries.filter { it.isFailure }.toSet(),@@ -136,6 +137,10 @@136137         assertEquals("Couldn't reach the relay", copy(SyncOutcomeReason.RELAY_UNREACHABLE))137138         assertEquals("Reached the relay, but the device didn't answer", copy(SyncOutcomeReason.HANDSHAKE_FAILED))138139         assertEquals("That device holds a different vault", copy(SyncOutcomeReason.VAULT_ID_MISMATCH))140+        assertEquals(141+            "Your other device needs to update before it can sync",142+            copy(SyncOutcomeReason.PEER_PROTOCOL_OUTDATED),143+        )139144         assertEquals("The device stopped responding mid-sync", copy(SyncOutcomeReason.SYNC_TIMEOUT))140145         assertEquals("The connection dropped mid-sync — will retry", copy(SyncOutcomeReason.CONNECTION_LOST))141146         assertEquals("Sync failed", copy(SyncOutcomeReason.RUN_FAILED))@@ -170,6 +175,7 @@170175     @Test fun `opaque session errors are classified, not lumped into one failure`() {171176         assertEquals(SyncOutcomeReason.VAULT_LOCKED, classifySyncError("VAULT_LOCKED"))172177         assertEquals(SyncOutcomeReason.VAULT_ID_MISMATCH, classifySyncError("vault_id_mismatch"))178+        assertEquals(SyncOutcomeReason.PEER_PROTOCOL_OUTDATED, classifySyncError("PEER_PROTOCOL_OUTDATED"))173179         assertEquals(SyncOutcomeReason.SYNC_TIMEOUT, classifySyncError("session timed out after 30s"))174180         assertEquals(175181             SyncOutcomeReason.HANDSHAKE_FAILED,@@ -210,6 +216,10 @@210216         assertEquals(211217             SyncOutcomeReason.RELAY_UNREACHABLE,212218             SyncOutcome(SyncOutcomeStatus.ERRORED, error = "relay_receive_queue_full").toSummary().reason,219+        )220+        assertEquals(221+            SyncOutcomeReason.PEER_PROTOCOL_OUTDATED,222+            SyncOutcome(SyncOutcomeStatus.ERRORED, error = "PEER_PROTOCOL_OUTDATED").toSummary().reason,213223         )214224     }215225 

view file