Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce recursion limit on nested groups #3119

Merged
merged 1 commit into from
Sep 24, 2024
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 @@ -210,7 +210,18 @@ internal class ByteArrayProtoReader32(
if (tagAndFieldEncoding == 0) throw ProtocolException("Unexpected tag 0")
val tag = tagAndFieldEncoding shr TAG_FIELD_ENCODING_BITS
when (val groupOrFieldEncoding = tagAndFieldEncoding and FIELD_ENCODING_MASK) {
STATE_START_GROUP -> skipGroup(tag) // Nested group.
STATE_START_GROUP -> {
recursionDepth++
try {
if (recursionDepth > RECURSION_LIMIT) {
throw IOException("Wire recursion limit exceeded")
}
// Nested group.
skipGroup(tag)
} finally {
recursionDepth--
}
}
STATE_END_GROUP -> {
if (tag == expectedEndTag) return // Success!
throw ProtocolException("Unexpected end group")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,18 @@ open class ProtoReader(private val source: BufferedSource) {
if (tagAndFieldEncoding == 0) throw ProtocolException("Unexpected tag 0")
val tag = tagAndFieldEncoding shr TAG_FIELD_ENCODING_BITS
when (val groupOrFieldEncoding = tagAndFieldEncoding and FIELD_ENCODING_MASK) {
STATE_START_GROUP -> skipGroup(tag) // Nested group.
STATE_START_GROUP -> {
recursionDepth++
try {
if (recursionDepth > RECURSION_LIMIT) {
throw IOException("Wire recursion limit exceeded")
}
// Nested group.
skipGroup(tag)
} finally {
recursionDepth--
}
}
STATE_END_GROUP -> {
if (tag == expectedEndTag) return // Success!
throw ProtocolException("Unexpected end group")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package com.squareup.wire

import com.squareup.wire.ReverseProtoWriterTest.Person
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import okio.ByteString.Companion.decodeHex
import okio.IOException

class ProtoReader32Test {
@Test fun packedExposedAsRepeated() {
Expand Down Expand Up @@ -58,5 +61,20 @@ class ProtoReader32Test {
reader.endMessageAndGetUnknownFields(secondToken)
}

/** We had a bug where we weren't enforcing recursion limits for groups. */
@Test fun testSkipGroupNested() {
val data = ByteArray(50000) {
when {
it % 2 == 0 -> 0xa3.toByte()
else -> 0x01.toByte()
}
}

val failed = assertFailsWith<IOException> {
Person.ADAPTER.decode(data)
}
assertEquals("Wire recursion limit exceeded", failed.message)
}

// Consider pasting new tests into ProtoReaderTest.kt also.
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package com.squareup.wire

import com.squareup.wire.ReverseProtoWriterTest.Person
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import okio.Buffer
import okio.ByteString.Companion.decodeHex
import okio.IOException

class ProtoReaderTest {
@Test fun packedExposedAsRepeated() {
Expand Down Expand Up @@ -59,5 +62,20 @@ class ProtoReaderTest {
reader.endMessageAndGetUnknownFields(secondToken)
}

/** We had a bug where we weren't enforcing recursion limits for groups. */
@Test fun testSkipGroupNested() {
val data = ByteArray(50000) {
when {
it % 2 == 0 -> 0xa3.toByte()
else -> 0x01.toByte()
}
}

val failed = assertFailsWith<IOException> {
Person.ADAPTER.decode(Buffer().write(data))
}
assertEquals("Wire recursion limit exceeded", failed.message)
}

// Consider pasting new tests into ProtoReader32Test.kt also.
}