auto: fragmented frames can no longer be spliced or forged from orphan fragments
- Commit
e0e30aa25e3d35ff1d347f70f865d8a5bce8a067- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 04:47 UTC (5 days ago)
- Parents
dfece18
FrameReader got-or-put a reassembly buffer for every non-single frame and emitted on LAST, with no requirement that a FIRST started the assembly. A lone LAST fragment was delivered to the channel owner as a complete message (a truncated/forged payload decoded and dispatched as whole), and an abandoned FIRST from one message spliced its prefix onto the next message on the same channel. A FIRST now starts a fresh buffer, discarding any stale partial on that channel, and a middle/LAST fragment with no FIRST-started assembly is dropped instead of completed. Legitimate fragmented, interleaved-channel, encrypted and byte-at-a-time paths are untouched. Both new regression tests failed pre-fix and pass now. Verified: :auto:protocol:testDebugUnitTest (17/17 FrameReaderTest), :auto:testDevUnitTest and :auto:protocol:lint green; :auto:lint errors are pre-existing on an untouched file.
Diff
auto/protocol/src/main/java/com/vayunmathur/auto/protocol/FrameReader.kt
diff --git a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/FrameReader.kt b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/FrameReader.ktindex b8987dd821..d66f64e164 100644--- a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/FrameReader.kt+++ b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/FrameReader.kt@@ -125,8 +125,17 @@ class FrameReader( // range is honoured as the initial capacity and anything else falls // back to this fragment's size (the stream still grows to fit). val hint = header.totalLength?.takeIf { it in 0..MAX_INITIAL_CAPACITY }- val buffer = partial.getOrPut(header.channelId) {- ByteArrayOutputStream(hint ?: plaintext.size)+ val buffer = if (header.isFirst) {+ // A FIRST fragment starts a new message: any stale partial on this+ // channel belongs to a message the peer abandoned, so discard it+ // rather than splicing the new message onto its prefix.+ ByteArrayOutputStream(hint ?: plaintext.size).also {+ partial[header.channelId] = it+ }+ } else {+ // A middle/LAST fragment with no FIRST-started assembly is an+ // orphan: drop it instead of emitting its tail as a whole message.+ partial[header.channelId] ?: return FrameStep.Consumed } buffer.write(plaintext)
auto/protocol/src/test/java/com/vayunmathur/auto/protocol/FrameReaderTest.kt
diff --git a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/FrameReaderTest.kt b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/FrameReaderTest.ktindex ff9554426d..32d5d977ec 100644--- a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/FrameReaderTest.kt+++ b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/FrameReaderTest.kt@@ -114,6 +114,43 @@ class FrameReaderTest { } } + @Test+ fun `an orphan last fragment is dropped not emitted as a whole message`() {+ // A LAST fragment with no preceding FIRST has no message to complete:+ // emitting it would hand a truncated, peer-forged payload to the+ // channel owner as if it were a complete message.+ fun frame(flags: Int, body: ByteArray): ByteArray {+ val out = java.nio.ByteBuffer.allocate(4 + body.size)+ out.put(3.toByte()).put(flags.toByte()).putShort(body.size.toShort())+ out.put(body)+ return out.array()+ }+ val reader = FrameReader()+ assertTrue(reader.offer(frame(FrameFlags.LAST, payload(9))).isEmpty())+ assertTrue(reader.offer(frame(0, payload(4))).isEmpty(), "orphan middle is dropped too")+ assertFalse(reader.hasPartialMessages)+ // The reader is not poisoned: a following well-formed message arrives.+ val body = payload(16)+ assertContentEquals(body, reader.offer(writer.frame(3, body).single()).single().payload)+ }++ @Test+ fun `a new first fragment discards a stale partial instead of splicing`() {+ // Peer abandons a fragmented message after its FIRST, then starts a+ // new one on the same channel: the result must be the new message+ // alone, not stale-prefix + new message.+ val reader = FrameReader()+ val abandoned = writer.frame(3, payload(40_000))+ assertTrue(reader.offer(abandoned.first()).isEmpty())+ assertTrue(reader.hasPartialMessages)++ val fresh = payload(50_000)+ val collected = writer.frame(3, fresh).flatMap { reader.offer(it) }++ assertContentEquals(fresh, collected.single().payload)+ assertFalse(reader.hasPartialMessages)+ }+ @Test fun `one byte at a time still reassembles`() { val body = payload(200)