-
Notifications
You must be signed in to change notification settings - Fork 623
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
Prohibited using of zero and negative filed number in ProtoNumber and zero field numbers in input bytes #2766
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b712494
Implemented skipping of filed with proto Id=0 if it missed in the des…
shanshin c5c5d9f
Prohibited using of zero and negative filed number in ProtoNumber and…
shanshin 49c8a93
Apply suggestions from code review
shanshin 18b4846
~review fixes
shanshin 27f5ccf
~fixes
shanshin d9297f3
~fixes
shanshin 5e6a068
Update formats/protobuf/commonMain/src/kotlinx/serialization/protobuf…
shanshin 79b8c12
~review fixes
shanshin babc823
~fixes
shanshin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either next line should be a complete sentence (it doesn't look like one now), or the dot shouldn't be here