Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fun b64(
private fun wellFormed(entry: Entry): Boolean {
val dids = { list: List<String> -> list.isNotEmpty() && list.all { isHex(it, 16) } }
return entry.v == 2 &&
entry.seq >= 0 &&
isHex(entry.group, 16) &&
isHex(entry.prev, 32) &&
isHex(entry.signer, 16) &&
Expand All @@ -143,7 +144,7 @@ private fun wellFormed(entry: Entry): Boolean {
?: true
) &&
(entry.remove?.let(dids) ?: true) &&
(entry.epoch?.let { b64(it.kc, 32) != null && dids(it.to) } ?: true) &&
(entry.epoch?.let { it.n >= 0 && b64(it.kc, 32) != null && dids(it.to) } ?: true) &&
(entry.meta?.all { (did, sealed) -> isHex(did, 16) && sealed.isNotEmpty() } ?: true) &&
entry.sigs.all { (did, signature) -> isHex(did, 16) && b64(signature, 64) != null }
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/test/java/io/github/aedev/flow/sync2/RosterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import io.github.aedev.flow.sync2.roster.forkWinner
import io.github.aedev.flow.sync2.roster.openDeviceMeta
import io.github.aedev.flow.sync2.roster.parseEntry
import io.github.aedev.flow.sync2.roster.replayRoster
import io.github.aedev.flow.sync2.roster.resolveForks
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.int
Expand Down Expand Up @@ -118,4 +120,14 @@ class RosterTest {
val withNull = JsonObject(v["entries"].jsonArray[1].jsonObject + ("remove" to JsonNull))
assertEquals("roster-format", errorCode { parseEntry(withNull) })
}

@Test
fun a_negative_seq_or_epoch_is_malformed_so_it_never_hides_the_chain() {
val raw = v["entries"].jsonArray
val seq = JsonObject(raw[1].jsonObject + ("seq" to JsonPrimitive(-1)))
val epoch = JsonObject(raw[0].jsonObject + ("epoch" to JsonObject(raw[0]["epoch"].jsonObject + ("n" to JsonPrimitive(-1)))))
assertEquals("roster-format", errorCode { parseEntry(seq) })
assertEquals("roster-format", errorCode { parseEntry(epoch) })
assertEquals(entries, resolveForks((raw + seq).mapNotNull { runCatching { parseEntry(it) }.getOrNull() }))
}
}
Loading