GeezerVison is showing github.com — Fix high-order bit aliasing in HttpUtil.validateToken · netty/netty@021d2f6
File tree
Expand file treeCollapse file tree
Open diff view settings
main/java/io/netty/handler/codec/http test/java/io/netty/handler/codec/http
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpUtil.java +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -717,8 +717,10 @@ private static int validateAsciiStringToken(AsciiString token) { |
717 | 717 | */ |
718 | 718 | private static int validateCharSequenceToken(CharSequence token) { |
719 | 719 | for (int i = 0, len = token.length(); i < len; i++) { |
720 | | - byte value = (byte) token.charAt(i); |
721 | | - if (!isValidTokenChar(value)) { |
| 720 | + int value = token.charAt(i); |
| 721 | + // 1. Check for truncation (anything above 255) |
| 722 | + // 2. Check against the BitSet (isValidTokenChar handles 128-255 via bit < 0) |
| 723 | + if (value > 0xFF || !isValidTokenChar((byte) value)) { |
722 | 724 | return i; |
723 | 725 | } |
724 | 726 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpUtilTest.java +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -535,4 +535,22 @@ public void testInvalidTokenChars(char invalidChar) { |
535 | 535 | assertEquals(2, validateToken(asciiStringToken)); |
536 | 536 | assertEquals(2, validateToken(token)); |
537 | 537 | } |
| 538 | + |
| 539 | + @ParameterizedTest |
| 540 | + @ValueSource(chars = { |
| 541 | + // High-bit Truncation Candidates (verifying > 0xFF check) |
| 542 | + // These characters are chosen because their lower 8 bits |
| 543 | + // alias to valid US-ASCII 'tchar' values. |
| 544 | + '\u0161', // 0x0161 truncates to 0x61 ('a') |
| 545 | + '\u0121', // 0x0121 truncates to 0x21 ('!') |
| 546 | + '\u0231', // 0x0231 truncates to 0x31 ('1') |
| 547 | + '\u0361' // 0x0361 truncates to 0x61 ('a') |
| 548 | + }) |
| 549 | + public void testInvalidTokenCharsOutsideAsciiRange(char invalidChar) { |
| 550 | + // We use a String here because AsciiString would truncate |
| 551 | + // the char to a byte during construction. |
| 552 | + String token = "GE" + invalidChar + 'T'; |
| 553 | + assertEquals(2, validateToken(token), |
| 554 | + String.format("Character U+%04X should be invalid", (int) invalidChar)); |
| 555 | + } |
538 | 556 | } |