Backport 4.1: Fix high-order bit aliasing in HttpUtil.validateToken#16303
Merged
Backport 4.1: Fix high-order bit aliasing in HttpUtil.validateToken#16303
Conversation
Motivation: The goal of this PR is to fix a character validation bypass in HttpUtil.validateToken. Prior to this change, the validateCharSequenceToken method was susceptible to "high-order bit aliasing." Because the logic performed a direct cast from a 16-bit char to an 8-bit byte, any Unicode character whose lower 8 bits matched a valid RFC 7230 tchar would be incorrectly validated. For example, the character 'Š' (U+0161) has the bit pattern 00000001 01100001. A narrow cast to byte strips the high-order bits, leaving 01100001, which is the ASCII value for 'a'. Since 'a' is a valid token character, the validator would return -1 (valid), violating the strictness required by RFC 7230. Modification: * Modified validateCharSequenceToken(CharSequence token) to store the character in an int variable to prevent multiple charAt lookups and maintain consistency with Netty's internal coding style. * Introduced an explicit range check (value > 0xFF) to catch high-order characters before they are cast to byte. * Updated HttpUtilTest with a new parameterized test testInvalidTokenCharsHighOrderBitAliasing that specifically tests Unicode characters known to alias into the valid tchar range (U+0161, U+0121, U+0231, U+0361). Result: This change ensures that HttpUtil correctly rejects non-ASCII and multi-byte characters that were previously able to bypass the token validation logic. Co-authored-by: Norman Maurer <norman_maurer@apple.com> (cherry picked from commit 95cc055)
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.
Backport of #16279 to 4.1
Cherry-picked commit: 95cc055
Motivation:
The goal of this PR is to fix a character validation bypass in HttpUtil.validateToken.
Prior to this change, the validateCharSequenceToken method was susceptible to "high-order bit aliasing." Because the logic performed a direct cast from a 16-bit char to an 8-bit byte, any Unicode character whose lower 8 bits matched a valid RFC 7230 tchar would be incorrectly validated.
For example, the character 'Š' (U+0161) has the bit pattern 00000001 01100001. A narrow cast to byte strips the high-order bits, leaving 01100001, which is the ASCII value for 'a'. Since 'a' is a valid token character, the validator would return -1 (valid), violating the strictness required by RFC 7230.
Modification:
Result:
This change ensures that HttpUtil correctly rejects non-ASCII and multi-byte characters that were previously able to bypass the token validation logic.