GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Don't truncate the token length of QUIC Initial packets to an int (#1… · netty/netty@aeafc7a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-classes-quic/src/main/java/io/netty/handler/codec/quic
    • QuicHeaderParser.java
  • codec-native-quic/src/test/java/io/netty/handler/codec/quic
    • QuicHeaderParserTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicHeaderParser.java‎

Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicHeaderParser.java
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,13 @@ private static ByteBuf sliceToken(QuicPacketType type, ByteBuf packet, int offse
160160
// See
161161
checkReadable(offset, readable, Byte.BYTES);
162162
int numBytes = numBytesForVariableLengthInteger(packet.getByte(offset));
163-
int len = (int) getVariableLengthInteger(packet, offset, numBytes);
163+
// The token length is a variable length integer that can be up to 62 bits, so we must not narrow it
164+
// to an int before we validated it against the readable bytes.
165+
long len = getVariableLengthInteger(packet, offset, numBytes);
164166
offset += numBytes;
165167

166168
checkReadable(offset, readable, len);
167-
return packet.slice(offset, len);
169+
return packet.slice(offset, (int) len);
168170
case RETRY:
169171
// Exclude the integrity tag from the token.
170172
// See https://www.rfc-editor.org/rfc/rfc9000.html#section-17.2.5
@@ -185,7 +187,7 @@ static ByteBuf sliceCid(ByteBuf buffer, int offset, int len) throws QuicExceptio
185187
return buffer.slice(offset, len);
186188
}
187189

188-
private static void checkReadable(int offset, int readable, int needed) throws QuicException {
190+
private static void checkReadable(int offset, int readable, long needed) throws QuicException {
189191
int r = readable - offset;
190192
if (r < needed) {
191193
throw newProtocolViolationException("Not enough bytes to read, " + r + " < " + needed);
Collapse file

‎codec-native-quic/src/test/java/io/netty/handler/codec/quic/QuicHeaderParserTest.java‎

Copy file name to clipboard
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.quic;
17+
18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.Unpooled;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.net.InetSocketAddress;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
27+
public class QuicHeaderParserTest {
28+
29+
private static final InetSocketAddress ADDRESS = new InetSocketAddress("127.0.0.1", 9999);
30+
31+
// Initial packet with version 1, a 2 byte dcid, a 2 byte scid and the given encoded token length.
32+
private static ByteBuf initialPacket(long tokenLengthVarint, int tokenLengthBytes, int tokenBytes) {
33+
ByteBuf packet = Unpooled.buffer();
34+
packet.writeByte(0xc0);
35+
packet.writeInt(1);
36+
packet.writeByte(2).writeShort(0x0102);
37+
packet.writeByte(2).writeShort(0x0304);
38+
switch (tokenLengthBytes) {
39+
case 1:
40+
packet.writeByte((int) tokenLengthVarint);
41+
break;
42+
case 4:
43+
packet.writeInt((int) (tokenLengthVarint | 0x80000000L));
44+
break;
45+
case 8:
46+
packet.writeLong(tokenLengthVarint | 0xc000000000000000L);
47+
break;
48+
default:
49+
throw new IllegalArgumentException();
50+
}
51+
packet.writeZero(tokenBytes);
52+
return packet;
53+
}
54+
55+
private static int parseTokenLength(ByteBuf packet) throws Exception {
56+
int[] tokenLength = { -1 };
57+
try (QuicHeaderParser parser = new QuicHeaderParser(2)) {
58+
parser.parse(ADDRESS, ADDRESS, packet, (sender, recipient, p, type, version, scid, dcid, token) ->
59+
tokenLength[0] = token.readableBytes());
60+
} finally {
61+
packet.release();
62+
}
63+
return tokenLength[0];
64+
}
65+
66+
@Test
67+
public void testTokenLength() throws Exception {
68+
assertEquals(5, parseTokenLength(initialPacket(5, 1, 5)));
69+
assertEquals(5, parseTokenLength(initialPacket(5, 4, 5)));
70+
assertEquals(5, parseTokenLength(initialPacket(5, 8, 5)));
71+
}
72+
73+
@Test
74+
public void testTokenLengthThatDoesNotFitIntoIntIsRejected() {
75+
// 2^32 + 5 would be truncated to 5 when narrowed to an int, which is available in the packet.
76+
assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () ->
77+
parseTokenLength(initialPacket((1L << 32) + 5, 8, 5))).error());
78+
// 2^31 would become negative when narrowed to an int.
79+
assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () ->
80+
parseTokenLength(initialPacket(1L << 31, 8, 5))).error());
81+
assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () ->
82+
parseTokenLength(initialPacket((1L << 62) - 1, 8, 5))).error());
83+
}
84+
85+
@Test
86+
public void testTokenLengthLargerThanPacketIsRejected() {
87+
assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () ->
88+
parseTokenLength(initialPacket(6, 1, 5))).error());
89+
}
90+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.