GeezerVison is showing github.com — Port 5.0: HAProxyMessage: decode TLVs with an empty value at the end … · netty/netty@d9c5725
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-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -299,8 +299,8 @@ private static HAProxyTLV readNextTLV(final ByteBuf header, int nestingLevel) { | 299 | 299 | throw new HAProxyProtocolException( | 300 | 300 | "Maximum TLV nesting level reached: " + nestingLevel + " (expected: < " + MAX_NESTING_LEVEL + ')'); | 301 | 301 | } | 302 | | - // We need at least 4 bytes for a TLV | 303 | | - if (header.readableBytes() < 4) { | | 302 | + // We need at least 3 bytes for a TLV: the type and the length, the value may be empty | | 303 | + if (header.readableBytes() < 3) { | 304 | 304 | return null; | 305 | 305 | } | 306 | 306 |
| @@ -324,7 +324,7 @@ private static HAProxyTLV readNextTLV(final ByteBuf header, int nestingLevel) { | 324 | 324 | final byte client = byteBuf.readByte(); | 325 | 325 | final int verify = byteBuf.readInt(); | 326 | 326 |
| 327 | | - if (byteBuf.readableBytes() >= 4) { | | 327 | + if (byteBuf.readableBytes() >= 3) { | 328 | 328 |
| 329 | 329 | final List<HAProxyTLV> encapsulatedTlvs = new ArrayList<HAProxyTLV>(4); | 330 | 330 | try { | @@ -334,8 +334,7 @@ private static HAProxyTLV readNextTLV(final ByteBuf header, int nestingLevel) { | 334 | 334 | break; | 335 | 335 | } | 336 | 336 | encapsulatedTlvs.add(haProxyTLV); | 337 | | - } while (byteBuf.readableBytes() >= 4); | 338 | | - | | 337 | + } while (byteBuf.readableBytes() >= 3); | 339 | 338 | } catch (Throwable t) { | 340 | 339 | // Release all previously read TLVs before rethrowing as otherwise we would leak. | 341 | 340 | releaseDeep(encapsulatedTlvs); | |
Collapse file Copy file name to clipboardExpand all lines: codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyMessageDecoderTest.java +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1476,4 +1476,53 @@ public void wellFormedNestedHeaderDecodesAndReleasesCleanly() { | 1476 | 1476 | assertTrue(header.release(), "buffer fully released"); | 1477 | 1477 | assertEquals(0, header.refCnt()); | 1478 | 1478 | } | | 1479 | + | | 1480 | + @Test | | 1481 | + public void testEmptyTLVsAtEndOfHeader() throws Exception { | | 1482 | + final ByteBuf data = Unpooled.buffer(); | | 1483 | + data.writeBytes(new byte[] { | | 1484 | + 0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, | | 1485 | + 0x21, 0x11, // v2, cmd=PROXY, TCP over IPv4 | | 1486 | + 0x00, 0x1A, // 12 bytes of addresses and ports + 14 bytes of TLVs | | 1487 | + 0x7F, 0x00, 0x00, 0x01, | | 1488 | + 0x7F, 0x00, 0x00, 0x01, | | 1489 | + 0x04, (byte) 0xD2, | | 1490 | + 0x00, 0x50, | | 1491 | + 0x20, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, // PP2_TYPE_SSL, client=1, verify=0 | | 1492 | + 0x21, 0x00, 0x00, // with an empty PP2_SUBTYPE_SSL_VERSION | | 1493 | + 0x04, 0x00, 0x00 // empty PP2_TYPE_NOOP | | 1494 | + }); | | 1495 | + | | 1496 | + assertTrue(ch.writeInbound(data)); | | 1497 | + HAProxyMessage msg = ch.readInbound(); | | 1498 | + List<HAProxyTLV> tlvs = msg.tlvs(); | | 1499 | + assertEquals(3, tlvs.size()); | | 1500 | + HAProxySSLTLV ssl = (HAProxySSLTLV) tlvs.get(0); | | 1501 | + assertEquals(1, ssl.encapsulatedTLVs().size()); | | 1502 | + assertEquals(HAProxyTLV.Type.PP2_TYPE_SSL_VERSION, ssl.encapsulatedTLVs().get(0).type()); | | 1503 | + assertEquals(0, ssl.encapsulatedTLVs().get(0).content().readableBytes()); | | 1504 | + assertEquals(HAProxyTLV.Type.OTHER, tlvs.get(2).type()); | | 1505 | + assertEquals(0x04, tlvs.get(2).typeByteValue()); | | 1506 | + assertEquals(0, tlvs.get(2).content().readableBytes()); | | 1507 | + assertTrue(msg.release()); | | 1508 | + assertFalse(ch.finish()); | | 1509 | + } | | 1510 | + | | 1511 | + @Test | | 1512 | + public void testIncompleteTLVAtEndOfHeader() { | | 1513 | + final ByteBuf data = Unpooled.buffer(); | | 1514 | + data.writeBytes(new byte[] { | | 1515 | + 0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, | | 1516 | + 0x21, 0x11, // v2, cmd=PROXY, TCP over IPv4 | | 1517 | + 0x00, 0x0F, // 12 bytes of addresses and ports + 3 bytes of TLV | | 1518 | + 0x7F, 0x00, 0x00, 0x01, | | 1519 | + 0x7F, 0x00, 0x00, 0x01, | | 1520 | + 0x04, (byte) 0xD2, | | 1521 | + 0x00, 0x50, | | 1522 | + 0x01, 0x00, 0x01 // PP2_TYPE_ALPN with a length of 1, but no value | | 1523 | + }); | | 1524 | + | | 1525 | + assertThrows(HAProxyProtocolException.class, () -> ch.writeInbound(data)); | | 1526 | + assertFalse(ch.isActive()); | | 1527 | + } | 1479 | 1528 | } |
|