GeezerVison is showing github.com — HAProxyMessage: validate the length of all TLV types (#17834) · netty/netty@f25d65c
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/haproxy test/java/io/netty/handler/codec/haproxy
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-haproxy/src/main/java/io/netty/handler/codec/haproxy/HAProxyMessage.java +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -308,15 +308,15 @@ private static HAProxyTLV readNextTLV(final ByteBuf header, int nestingLevel) { | 308 | 308 | final HAProxyTLV.Type type = HAProxyTLV.Type.typeForByteValue(typeAsByte); | 309 | 309 |
| 310 | 310 | final int length = header.readUnsignedShort(); | | 311 | + if (length > header.readableBytes()) { | | 312 | + throw new HAProxyProtocolException("TLV length must be smaller or equal the readable bytes (" + | | 313 | + header.readableBytes() + ") but was: " + length); | | 314 | + } | 311 | 315 | switch (type) { | 312 | 316 | case PP2_TYPE_SSL: | 313 | 317 | if (length < 5) { | 314 | 318 | throw new HAProxyProtocolException("TLV length must be at least 5 but was: " + length); | 315 | 319 | } | 316 | | - if (length > header.readableBytes()) { | 317 | | - throw new HAProxyProtocolException("TLV length must be smaller or equal the readable bytes (" + | 318 | | - header.readableBytes() + ") but was: " + length); | 319 | | - } | 320 | 320 | // Slice the rawContent but only retain it if we didn't see an error as otherwise we might | 321 | 321 | // leak. | 322 | 322 | final ByteBuf rawContent = header.slice(header.readerIndex(), length); | |
Collapse file Copy file name to clipboardExpand all lines: codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyMessageDecoderTest.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1420,6 +1420,26 @@ public void execute() { | 1420 | 1420 | }); | 1421 | 1421 | } | 1422 | 1422 |
| | 1423 | + @ParameterizedTest | | 1424 | + @ValueSource(bytes = { 0x01, 0x02, 0x21, 0x22, 0x30, (byte) 0xE0 }) | | 1425 | + public void testInvalidNonSslTLVLengthCorrectlyHandled(byte type) { | | 1426 | + final ByteBuf data = Unpooled.buffer(); | | 1427 | + data.writeBytes(new byte[] { | | 1428 | + 0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, | | 1429 | + 0x21, 0x11, // v2, cmd=PROXY, TCP over IPv4 | | 1430 | + 0x00, 0x10, // 12 bytes of addresses and ports + 4 bytes of TLV | | 1431 | + 0x7F, 0x00, 0x00, 0x01, | | 1432 | + 0x7F, 0x00, 0x00, 0x01, | | 1433 | + 0x04, (byte) 0xD2, | | 1434 | + 0x00, 0x50, | | 1435 | + type, 0x00, (byte) 0xFF, 'h' // TLV with a length of 255, but only one byte of value | | 1436 | + }); | | 1437 | + | | 1438 | + assertThrows(HAProxyProtocolException.class, () -> ch.writeInbound(data)); | | 1439 | + assertFalse(ch.isActive()); | | 1440 | + assertFalse(ch.finish()); | | 1441 | + } | | 1442 | + | 1423 | 1443 | @Test | 1424 | 1444 | public void testReadTlvsLeaksRetainedBufferWhenSecondSSLTLVIsMalformed() { | 1425 | 1445 | ByteBuf data = Unpooled.buffer(); | |
|