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 — Port 5.0: Read SOCKS5 length octets unsigned in the legacy socks deco… · netty/netty@089dd15

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-socks/src
    • main/java/io/netty/handler/codec/socks
      • SocksAuthRequestDecoder.java
      • SocksCmdRequestDecoder.java
      • SocksCmdResponseDecoder.java
    • test/java/io/netty/handler/codec/socks
      • SocksAuthRequestDecoderTest.java
      • SocksCmdRequestDecoderTest.java
      • SocksCmdResponseDecoderTest.java
      • SocksCommonTestUtils.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-socks/src/main/java/io/netty/handler/codec/socks/SocksAuthRequestDecoder.java‎

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 numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
4848
if (byteBuf.readableBytes() < 1) {
4949
return;
5050
}
51-
int fieldLength = byteBuf.getByte(byteBuf.readerIndex());
51+
int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex());
5252
if (byteBuf.readableBytes() < 1 + fieldLength) {
5353
return;
5454
}
@@ -60,7 +60,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
6060
if (byteBuf.readableBytes() < 1) {
6161
return;
6262
}
63-
int fieldLength = byteBuf.getByte(byteBuf.readerIndex());
63+
int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex());
6464
if (byteBuf.readableBytes() < 1 + fieldLength) {
6565
return;
6666
}
Collapse file

‎codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdRequestDecoder.java‎

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 numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
7171
if (byteBuf.readableBytes() < 1) {
7272
return;
7373
}
74-
int fieldLength = byteBuf.getByte(byteBuf.readerIndex());
74+
int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex());
7575
if (byteBuf.readableBytes() < 3 + fieldLength) {
7676
return;
7777
}
Collapse file

‎codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponseDecoder.java‎

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 numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
7171
if (byteBuf.readableBytes() < 1) {
7272
return;
7373
}
74-
int fieldLength = byteBuf.getByte(byteBuf.readerIndex());
74+
int fieldLength = byteBuf.getUnsignedByte(byteBuf.readerIndex());
7575
if (byteBuf.readableBytes() < 3 + fieldLength) {
7676
return;
7777
}
Collapse file

‎codec-socks/src/test/java/io/netty/handler/codec/socks/SocksAuthRequestDecoderTest.java‎

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 numberDiff line numberDiff line change
@@ -70,4 +70,21 @@ public void testAuthRequestDecoderPartialSend() throws Exception {
7070

7171
assertFalse(ch.finishAndReleaseAll());
7272
}
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+
}
7390
}
Collapse file

‎codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java‎

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 numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323

2424
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertFalse;
2526
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2627
import static org.junit.jupiter.api.Assertions.assertNull;
2728
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -49,6 +50,7 @@ private static void testSocksCmdRequestDecoderWithDifferentParams(SocksCmdType c
4950
assertEquals(msg.port(), port);
5051
}
5152
assertNull(embedder.readInbound());
53+
assertFalse(embedder.finish());
5254
}
5355

5456
@Test
@@ -109,4 +111,19 @@ public void testCmdRequestDecoderUnknown() throws Exception {
109111
testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.UNKNOWN, host, port);
110112
}
111113
}
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+
}
112129
}
Collapse file

‎codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdResponseDecoderTest.java‎

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 numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.function.Executable;
2323

2424
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertFalse;
2526
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2627
import static org.junit.jupiter.api.Assertions.assertNull;
2728
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -47,6 +48,7 @@ private static void testSocksCmdResponseDecoderWithDifferentParams(
4748
assertEquals(((SocksCmdResponse) msg).port(), port);
4849
}
4950
assertNull(embedder.readInbound());
51+
assertFalse(embedder.finish());
5052
}
5153

5254
/**
@@ -91,4 +93,15 @@ public void testSocksCmdResponseDecoderIncludingHost() throws Exception {
9193
"1111:111:11:1:0:0:0:1", 80);
9294
}
9395
}
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+
}
94107
}
Collapse file

‎codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCommonTestUtils.java‎

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 numberDiff line numberDiff line change
@@ -33,4 +33,35 @@ public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, SocksMessa
3333
msg.encodeAsByteBuf(buf);
3434
embedder.writeInbound(buf);
3535
}
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+
}
3667
}

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.