voxels: one bad roster record no longer rejects every editor
- Commit
ddb11c377673a8618046ab0fed7f9da8f25a45e6- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 20:31 UTC (4 days ago)
- Parents
faaf22d
Bug: VoxelsSync.fetchRoster decoded each member record's signature with an unguarded Base64.decode inside the verification loop. The members channel is append-only and pulled from the start on every roster refresh, so a single corrupted or crafted record whose sig is not Base64 threw IllegalArgumentException, aborted the whole fetch, and MultiplayerPump.refreshRoster (which runCatching-wraps the fetch) silently kept an empty/stale roster. Every legitimate owner-signed editor record after the poisoned one was dropped on every refresh, forever: the host rejected all editors' signed ops as non-roster even though their invites were valid. Records with undecodable JSON were already skipped per-record; only the Base64 step was unguarded (compare MultiplayerPump.verify and VoxelsSync.decrypt, which both guard their decodes). Fix: fetchRoster now delegates to a new internal verifiedRoster that skips a record whose signature is not Base64 exactly like a record whose signature fails verification. verifiedRoster is split from the relay pull (with an injectable signature check) so the verification rules are unit-testable without the relay or native PQC. No wire format changes: SignedMember, the signing bytes, and the members channel are untouched, and valid/invalid-signature records behave exactly as before. Test: new VoxelsSyncRosterTest places a not-Base64 signature record and a non-JSON record before a legitimate owner-signed record, plus a cross-world replay check. Pre-fix it fails with IllegalArgumentException (Invalid symbol '!' at Base64.decode in verifiedRoster); post-fix it passes, as do VoxelsSyncTest and VoxelsSyncInboxTest. Verification: targeted tests pass; full :games:voxels:testDevUnitTest (222 tests) has 209 passing and 13 failures that are all native-availability artifacts: the Rust cargo tasks fail pre-existing with 'cannot update the lock file because --locked was passed' (Cargo.lock untouched by this change, Rust owned by another agent), so with the cargo tasks excluded the native-backed CovX4/compose tests fail with UnsatisfiedLinkError (12) and one MainUi clickable-count assertion that depends on the native surface (1). compileDevKotlin is green; ktlint findings in the module are pre-existing (including untouched files).
Diff
games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.ktindex a3052823c2..d923b54d68 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt@@ -233,11 +233,35 @@ object VoxelsSync { /** Reads + verifies the roster: only records with a valid OWNER signature are honored. Returns * device id → role. */- suspend fun fetchRoster(worldId: String, key: ByteArray, ownerBundle: ByteArray): Map<String, String> {+ suspend fun fetchRoster(worldId: String, key: ByteArray, ownerBundle: ByteArray): Map<String, String> =+ verifiedRoster(worldId, pullMembers(worldId, key), ownerBundle)++ /**+ * Verifies decrypted member [items] for [worldId] against [ownerBundle]; only records with a+ * valid OWNER signature are honored. Split from [fetchRoster] so the verification rules can be+ * unit-tested without the relay.+ */+ internal suspend fun verifiedRoster(+ worldId: String,+ items: List<String>,+ ownerBundle: ByteArray,+ ): Map<String, String> = verifiedRoster(worldId, items) { data, sig -> verify(ownerBundle, data, sig) }++ /** [verifiedRoster] with the signature check injected, so it can be unit-tested without a key. */+ internal suspend fun verifiedRoster(+ worldId: String,+ items: List<String>,+ verify: suspend (data: ByteArray, sig: ByteArray) -> Boolean,+ ): Map<String, String> { val map = LinkedHashMap<String, String>()- for (item in pullMembers(worldId, key)) {+ for (item in items) { val sm = runCatching { json.decodeFromString<SignedMember>(item) }.getOrNull() ?: continue- if (verify(ownerBundle, memberSigningBytes(worldId, sm.member), Base64.decode(sm.sig))) {+ // A record whose signature is not even Base64 is just another unverifiable+ // record: skip it like a bad signature, never let it abort the whole roster+ // (the members channel is append-only, so one poisoned entry would otherwise+ // reject every legitimate editor on every refresh, forever).+ val sig = runCatching { Base64.decode(sm.sig) }.getOrNull() ?: continue+ if (verify(memberSigningBytes(worldId, sm.member), sig)) { map[sm.member.id] = sm.member.role } }
games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncRosterTest.kt
diff --git a/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncRosterTest.kt b/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncRosterTest.ktnew file mode 100644index 0000000000..6227d0fbb1--- /dev/null+++ b/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncRosterTest.kt@@ -0,0 +1,45 @@+package com.vayunmathur.games.voxels.network++import kotlin.io.encoding.Base64+import kotlin.io.encoding.ExperimentalEncodingApi+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlinx.serialization.json.Json++/**+ * Roster verification ([VoxelsSync.verifiedRoster]): one malformed member record on the+ * members channel must not drop the legitimate, owner-signed records around it. The+ * signature check is injected (a record verifies iff its signature bytes are "good"+ * and it is bound to the world and member it claims), so no native PQC is needed.+ */+@OptIn(ExperimentalEncodingApi::class)+class VoxelsSyncRosterTest {+ private val json = Json { ignoreUnknownKeys = true }++ private fun member(id: String, sig: String): String =+ json.encodeToString(VoxelsSync.SignedMember(VoxelsSync.Member(id = id, role = VoxelsRoles.EDITOR), sig))++ @Test fun malformedSignatureDoesNotPoisonRoster() = kotlinx.coroutines.runBlocking {+ val worldId = "roster-test-world"+ val goodSig = Base64.encode("good".encodeToByteArray())+ // A member-shaped record whose signature is not Base64 at all (a corrupted or+ // crafted members-channel entry), placed before the legitimate record.+ val malformed = member("ghost", "!!!not-base64!!!")+ val valid = member("editor-1", goodSig)+ // Valid Base64 and a "good" signature, but the record replayed under another+ // world's id must not verify there (the fake binds world|id|role, as the real one does).+ val foreign = member("intruder", goodSig)++ val roster = VoxelsSync.verifiedRoster(worldId, listOf(malformed, "not json", valid)) { data, sig ->+ sig.contentEquals("good".encodeToByteArray()) &&+ data.decodeToString() == "$worldId|editor-1|${VoxelsRoles.EDITOR}"+ }+ assertEquals(mapOf("editor-1" to VoxelsRoles.EDITOR), roster)++ val foreignRoster = VoxelsSync.verifiedRoster(worldId, listOf(foreign)) { data, sig ->+ sig.contentEquals("good".encodeToByteArray()) &&+ data.decodeToString() == "other-world|intruder|${VoxelsRoles.EDITOR}"+ }+ assertEquals(emptyMap(), foreignRoster)+ }+}