Fix UTF8FirstLetterNumBytes to handle malformed UTF-8 correctly#1379
Open
kodareef5 wants to merge 1 commit intobufbuild:mainfrom
Open
Fix UTF8FirstLetterNumBytes to handle malformed UTF-8 correctly#1379kodareef5 wants to merge 1 commit intobufbuild:mainfrom
kodareef5 wants to merge 1 commit intobufbuild:mainfrom
Conversation
UTF8FirstLetterNumBytes returns the byte count from OneCharLen without validating that the expected continuation bytes actually follow the leader byte. Malformed UTF-8 (e.g., bare leader bytes without continuations) causes Utf8Len to undercount characters by 2-4x, bypassing string length validation constraints. For example, 20 bytes of 0xC0 (invalid 2-byte leaders) produces Utf8Len=10 instead of 20, allowing a max_len=10 constraint to accept 20 bytes of data. Fix: - Clamp consumed bytes to remaining buffer length - Validate continuation bytes have the 10xxxxxx pattern - Return 1 for any invalid byte (count as single character) Valid UTF-8 counting is unchanged.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
UTF8FirstLetterNumBytesinvalidate/validate.hreturns the byte count fromOneCharLenwithout validating that the expected continuation bytes actually follow the leader byte. Malformed UTF-8 causesUtf8Lento undercount characters by 2-4x, bypassing string length validation constraints (min_len,max_len,len).Example: 20 bytes of
\xC0(bare 2-byte leaders, no continuations) producesUtf8Len=10instead of 20. A field withmax_len = 10incorrectly accepts this input.This is exploitable when C++ protobuf deserialization doesn't enforce UTF-8 validity (the default), allowing malformed strings to reach pgv validation.
Fix:
10xxxxxxpatternValid UTF-8 counting is unchanged: "hello"=5, "café"=4, "你好"=2, "😀😀"=2.