-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prohibited using of zero and negative filed number in ProtoNumber and…
… zero field numbers in input bytes (#2766) - implemented throwing error if decoded field number = 0 - prohibited using of zero and negative filed number in @ProtoNumber annotation - optimized skipping of size delimited fields - removed the creation of an byte array in case of skipping Fixes #2649 Fixes #1566 Co-authored-by: Leonid Startsev <sandwwraith@users.noreply.github.com>
- Loading branch information
1 parent
4ca05dd
commit 4646740
Showing
7 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/InvalidFieldNumberTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf | ||
|
||
|
||
import kotlinx.serialization.* | ||
import kotlin.test.* | ||
|
||
class InvalidFieldNumberTest { | ||
|
||
@Serializable | ||
data class Holder(val value: Int) | ||
|
||
@Serializable | ||
data class ListHolder(val value: List<Int>) | ||
|
||
@Serializable | ||
data class ZeroProtoNumber(@ProtoNumber(0) val value: Int) | ||
|
||
@Serializable | ||
data class NegativeProtoNumber(@ProtoNumber(-5) val value: Int) | ||
|
||
@Test | ||
fun testDeserializeZeroInput() { | ||
assertFailsWithMessage<SerializationException>("0 is not allowed as the protobuf field number in kotlinx.serialization.protobuf.InvalidFieldNumberTest.Holder, the input bytes may have been corrupted") { | ||
// first value with field number = 0 | ||
val hexString = "000f" | ||
ProtoBuf.decodeFromHexString<Holder>(hexString) | ||
} | ||
} | ||
|
||
@Test | ||
fun testDeserializeZeroInputForElement() { | ||
assertFailsWithMessage<SerializationException>("0 is not allowed as the protobuf field number in kotlinx.serialization.protobuf.InvalidFieldNumberTest.ListHolder, the input bytes may have been corrupted") { | ||
// first element with field number = 0 | ||
val hexString = "000f" | ||
ProtoBuf.decodeFromHexString<ListHolder>(hexString) | ||
} | ||
} | ||
|
||
@Test | ||
fun testSerializeZeroProtoNumber() { | ||
assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber', because protobuf supports field numbers in range 1..2147483647") { | ||
ProtoBuf.encodeToHexString(ZeroProtoNumber(42)) | ||
} | ||
} | ||
|
||
@Test | ||
fun testDeserializeZeroProtoNumber() { | ||
assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber', because protobuf supports field numbers in range 1..2147483647") { | ||
ProtoBuf.decodeFromHexString<ZeroProtoNumber>("000f") | ||
} | ||
} | ||
|
||
@Test | ||
fun testSerializeNegativeProtoNumber() { | ||
assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber', because protobuf supports field numbers in range 1..2147483647") { | ||
ProtoBuf.encodeToHexString(NegativeProtoNumber(42)) | ||
} | ||
} | ||
|
||
@Test | ||
fun testDeserializeNegativeProtoNumber() { | ||
assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber', because protobuf supports field numbers in range 1..2147483647") { | ||
ProtoBuf.decodeFromHexString<NegativeProtoNumber>("000f") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/SkipFieldsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf | ||
|
||
|
||
import kotlinx.serialization.* | ||
import kotlin.test.* | ||
|
||
class SkipFieldsTest { | ||
|
||
@Serializable | ||
data class Holder(val value: Int) | ||
|
||
@Test | ||
fun testSkipBigFieldNumber() { | ||
// first value with id = 2047 and takes 2 bytes | ||
val hexString = "f87f20082a" | ||
val holder = ProtoBuf.decodeFromHexString<Holder>(hexString) | ||
assertEquals(42, holder.value) | ||
} | ||
|
||
@Test | ||
fun testSkipUnknownFiledNumberForString() { | ||
// first value is size delimited (string) with id = 42 | ||
val hexString = "d2020c48656c6c6f20576f726c6421082a" | ||
val holder = ProtoBuf.decodeFromHexString<Holder>(hexString) | ||
assertEquals(42, holder.value) | ||
} | ||
} |