GeezerVison is showing github.com — Port 5.0: Read SOCKS5 length octets unsigned in the legacy socks deco… · netty/netty@089dd15
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/socks test/java/io/netty/handler/codec/socks
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-socks/src/main/java/io/netty/handler/codec/socks/SocksAuthRequestDecoder.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,7 +48,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o | 48 | 48 | if (byteBuf.readableBytes() < 1) { | 49 | 49 | return; | 50 | 50 | } | 51 | | - int fieldLength = byteBuf.getByte(byteBuf.readerIndex()); | | 51 | + int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex()); | 52 | 52 | if (byteBuf.readableBytes() < 1 + fieldLength) { | 53 | 53 | return; | 54 | 54 | } | @@ -60,7 +60,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o | 60 | 60 | if (byteBuf.readableBytes() < 1) { | 61 | 61 | return; | 62 | 62 | } | 63 | | - int fieldLength = byteBuf.getByte(byteBuf.readerIndex()); | | 63 | + int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex()); | 64 | 64 | if (byteBuf.readableBytes() < 1 + fieldLength) { | 65 | 65 | return; | 66 | 66 | } | |
Collapse file Copy file name to clipboardExpand all lines: codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdRequestDecoder.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,7 +71,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o | 71 | 71 | if (byteBuf.readableBytes() < 1) { | 72 | 72 | return; | 73 | 73 | } | 74 | | - int fieldLength = byteBuf.getByte(byteBuf.readerIndex()); | | 74 | + int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex()); | 75 | 75 | if (byteBuf.readableBytes() < 3 + fieldLength) { | 76 | 76 | return; | 77 | 77 | } | |
Collapse file Copy file name to clipboardExpand all lines: codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponseDecoder.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,7 +71,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o | 71 | 71 | if (byteBuf.readableBytes() < 1) { | 72 | 72 | return; | 73 | 73 | } | 74 | | - int fieldLength = byteBuf.getByte(byteBuf.readerIndex()); | | 74 | + int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex()); | 75 | 75 | if (byteBuf.readableBytes() < 3 + fieldLength) { | 76 | 76 | return; | 77 | 77 | } | |
Collapse file Copy file name to clipboardExpand all lines: codec-socks/src/test/java/io/netty/handler/codec/socks/SocksAuthRequestDecoderTest.java +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,4 +70,21 @@ public void testAuthRequestDecoderPartialSend() throws Exception { | 70 | 70 |
| 71 | 71 | assertFalse(ch.finishAndReleaseAll()); | 72 | 72 | } | | 73 | + | | 74 | + /** | | 75 | + * ULEN and PLEN are unsigned octets (RFC 1929); user names and passwords of 128 to 255 bytes must decode. | | 76 | + */ | | 77 | + @Test | | 78 | + public void testAuthRequestDecoderLongUsernameAndPassword() throws Exception { | | 79 | + String longUsername = SocksCommonTestUtils.asciiStringOfLength(128); | | 80 | + String longPassword = SocksCommonTestUtils.asciiStringOfLength(255); | | 81 | + SocksAuthRequest msg = new SocksAuthRequest(longUsername, longPassword); | | 82 | + EmbeddedChannel embedder = new EmbeddedChannel(new SocksAuthRequestDecoder()); | | 83 | + SocksCommonTestUtils.writeMessageIntoEmbedder(embedder, msg); | | 84 | + msg = embedder.readInbound(); | | 85 | + assertEquals(longUsername, msg.username()); | | 86 | + assertEquals(longPassword, msg.password()); | | 87 | + assertNull(embedder.readInbound()); | | 88 | + assertFalse(embedder.finish()); | | 89 | + } | 73 | 90 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import org.junit.jupiter.api.Test; | 23 | 23 |
| 24 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; | | 25 | +import static org.junit.jupiter.api.Assertions.assertFalse; | 25 | 26 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; | 26 | 27 | import static org.junit.jupiter.api.Assertions.assertNull; | 27 | 28 | import static org.junit.jupiter.api.Assertions.assertSame; | @@ -49,6 +50,7 @@ private static void testSocksCmdRequestDecoderWithDifferentParams(SocksCmdType c | 49 | 50 | assertEquals(msg.port(), port); | 50 | 51 | } | 51 | 52 | assertNull(embedder.readInbound()); | | 53 | + assertFalse(embedder.finish()); | 52 | 54 | } | 53 | 55 |
| 54 | 56 | @Test | @@ -109,4 +111,19 @@ public void testCmdRequestDecoderUnknown() throws Exception { | 109 | 111 | testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.UNKNOWN, host, port); | 110 | 112 | } | 111 | 113 | } | | 114 | + | | 115 | + /** | | 116 | + * The domain length octet is unsigned; lengths in [0x80, 0xFF] sign-extend to a negative Java | | 117 | + * {@code byte}, so a decoder that reads them with {@link io.netty.buffer.ByteBuf#readByte()} | | 118 | + * fails on spec-legal domain names of 128 to 255 characters. | | 119 | + */ | | 120 | + @Test | | 121 | + public void testCmdRequestDecoderLongDomain() throws Exception { | | 122 | + String[] hosts = {SocksCommonTestUtils.domainOfLength(128), SocksCommonTestUtils.domainOfLength(255)}; | | 123 | + for (SocksCmdType cmdType : SocksCmdType.values()) { | | 124 | + for (String host : hosts) { | | 125 | + testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.DOMAIN, host, 80); | | 126 | + } | | 127 | + } | | 128 | + } | 112 | 129 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdResponseDecoderTest.java +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import org.junit.jupiter.api.function.Executable; | 23 | 23 |
| 24 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; | | 25 | +import static org.junit.jupiter.api.Assertions.assertFalse; | 25 | 26 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; | 26 | 27 | import static org.junit.jupiter.api.Assertions.assertNull; | 27 | 28 | import static org.junit.jupiter.api.Assertions.assertThrows; | @@ -47,6 +48,7 @@ private static void testSocksCmdResponseDecoderWithDifferentParams( | 47 | 48 | assertEquals(((SocksCmdResponse) msg).port(), port); | 48 | 49 | } | 49 | 50 | assertNull(embedder.readInbound()); | | 51 | + assertFalse(embedder.finish()); | 50 | 52 | } | 51 | 53 |
| 52 | 54 | /** | @@ -91,4 +93,15 @@ public void testSocksCmdResponseDecoderIncludingHost() throws Exception { | 91 | 93 | "1111:111:11:1:0:0:0:1", 80); | 92 | 94 | } | 93 | 95 | } | | 96 | + | | 97 | + /** | | 98 | + * The bound domain length octet is unsigned; a bound domain of 128 to 255 characters must decode. | | 99 | + */ | | 100 | + @Test | | 101 | + public void testSocksCmdResponseDecoderLongDomain() throws Exception { | | 102 | + testSocksCmdResponseDecoderWithDifferentParams( | | 103 | + SocksCmdStatus.SUCCESS, SocksAddressType.DOMAIN, SocksCommonTestUtils.domainOfLength(128), 80); | | 104 | + testSocksCmdResponseDecoderWithDifferentParams( | | 105 | + SocksCmdStatus.SUCCESS, SocksAddressType.DOMAIN, SocksCommonTestUtils.domainOfLength(255), 80); | | 106 | + } | 94 | 107 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCommonTestUtils.java +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,4 +33,35 @@ public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, SocksMessa | 33 | 33 | msg.encodeAsByteBuf(buf); | 34 | 34 | embedder.writeInbound(buf); | 35 | 35 | } | | 36 | + | | 37 | + /** | | 38 | + * Returns a domain name of exactly {@code length} characters made of labels of at most 63 characters. | | 39 | + */ | | 40 | + static String domainOfLength(int length) { | | 41 | + StringBuilder sb = new StringBuilder(length); | | 42 | + while (sb.length() < length) { | | 43 | + int remaining = length - sb.length(); | | 44 | + if (sb.length() > 0) { | | 45 | + sb.append('.'); | | 46 | + remaining--; | | 47 | + } | | 48 | + int label = Math.min(63, remaining); | | 49 | + if (remaining - label == 1) { | | 50 | + // Never leave room for only a trailing dot. | | 51 | + label--; | | 52 | + } | | 53 | + for (int i = 0; i < label; i++) { | | 54 | + sb.append('a'); | | 55 | + } | | 56 | + } | | 57 | + return sb.toString(); | | 58 | + } | | 59 | + | | 60 | + static String asciiStringOfLength(int length) { | | 61 | + StringBuilder sb = new StringBuilder(length); | | 62 | + for (int i = 0; i < length; i++) { | | 63 | + sb.append((char) ('a' + i % 26)); | | 64 | + } | | 65 | + return sb.toString(); | | 66 | + } | 36 | 67 | } |
|