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 — Harden the SOCKS4/5 input validation at encoding time · netty/netty@4625406

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-socks/src
    • main/java/io/netty/handler/codec/socksx
      • v4
        • Socks4ClientEncoder.java
      • v5
        • Socks5ClientEncoder.java
        • Socks5ServerEncoder.java
    • test/java/io/netty/handler/codec/socksx
      • v4
        • Socks4ClientEncoderTest.java
      • v5
        • Socks5ClientEncoderTest.java
        • Socks5ServerEncoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4ClientEncoder.java‎

Copy file name to clipboardExpand all lines: codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4ClientEncoder.java
+16-3Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import io.netty.buffer.ByteBufUtil;
2121
import io.netty.channel.ChannelHandler.Sharable;
2222
import io.netty.channel.ChannelHandlerContext;
23+
import io.netty.handler.codec.EncoderException;
2324
import io.netty.handler.codec.MessageToByteEncoder;
25+
import io.netty.util.AsciiString;
2426
import io.netty.util.NetUtil;
2527

2628
/**
@@ -47,14 +49,25 @@ protected void encode(ChannelHandlerContext ctx, Socks4CommandRequest msg, ByteB
4749
ByteBufUtil.writeShortBE(out, msg.dstPort());
4850
if (NetUtil.isValidIpV4Address(msg.dstAddr())) {
4951
out.writeBytes(NetUtil.createByteArrayFromIpAddressString(msg.dstAddr()));
50-
ByteBufUtil.writeAscii(out, msg.userId());
52+
ByteBufUtil.writeAscii(out, sanitize("userId", msg.userId()));
5153
out.writeByte(0);
5254
} else {
5355
out.writeBytes(IPv4_DOMAIN_MARKER);
54-
ByteBufUtil.writeAscii(out, msg.userId());
56+
ByteBufUtil.writeAscii(out, sanitize("userId", msg.userId()));
5557
out.writeByte(0);
56-
ByteBufUtil.writeAscii(out, msg.dstAddr());
58+
ByteBufUtil.writeAscii(out, sanitize("dstAddr", msg.dstAddr()));
5759
out.writeByte(0);
5860
}
5961
}
62+
63+
private CharSequence sanitize(String fieldName, String strValue) {
64+
for (int i = 0, len = strValue.length(); i < len; i++) {
65+
char c = strValue.charAt(i);
66+
// SOCKS4 uses NUL-bytes as field delimiters.
67+
if (AsciiString.c2b(c) == 0) {
68+
throw new EncoderException("Illegal character in " + fieldName + " field.");
69+
}
70+
}
71+
return strValue;
72+
}
6073
}
Collapse file

‎codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5ClientEncoder.java‎

Copy file name to clipboardExpand all lines: codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5ClientEncoder.java
+20-5Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private static void encodeAuthMethodRequest(Socks5InitialRequest msg, ByteBuf ou
8080

8181
final List<Socks5AuthMethod> authMethods = msg.authMethods();
8282
final int numAuthMethods = authMethods.size();
83-
out.writeByte(numAuthMethods);
83+
writeFieldLength(out, numAuthMethods);
8484

8585
if (authMethods instanceof RandomAccess) {
8686
for (int i = 0; i < numAuthMethods; i ++) {
@@ -97,18 +97,18 @@ private static void encodePasswordAuthRequest(Socks5PasswordAuthRequest msg, Byt
9797
out.writeByte(0x01);
9898

9999
final String username = msg.username();
100-
out.writeByte(username.length());
100+
writeFieldLength(out, username.length());
101101
ByteBufUtil.writeAscii(out, username);
102102

103103
final String password = msg.password();
104-
out.writeByte(password.length());
104+
writeFieldLength(out, password.length());
105105
ByteBufUtil.writeAscii(out, password);
106106
}
107107

108108
private static void encodePrivateAuthRequest(Socks5PrivateAuthRequest msg, ByteBuf out) {
109109
byte[] bytes = msg.privateToken();
110110
out.writeByte(0x01);
111-
out.writeByte(bytes.length);
111+
writeFieldLength(out, bytes.length);
112112
out.writeBytes(bytes);
113113
}
114114

@@ -119,7 +119,22 @@ private void encodeCommandRequest(Socks5CommandRequest msg, ByteBuf out) throws
119119

120120
final Socks5AddressType dstAddrType = msg.dstAddrType();
121121
out.writeByte(dstAddrType.byteValue());
122-
addressEncoder.encodeAddress(dstAddrType, msg.dstAddr(), out);
122+
String addrValue = msg.dstAddr();
123+
if (addrValue != null && dstAddrType == Socks5AddressType.DOMAIN) {
124+
checkFieldLength(addrValue.length());
125+
}
126+
addressEncoder.encodeAddress(dstAddrType, addrValue, out);
123127
ByteBufUtil.writeShortBE(out, msg.dstPort());
124128
}
129+
130+
private static void writeFieldLength(ByteBuf out, int length) {
131+
checkFieldLength(length);
132+
out.writeByte(length);
133+
}
134+
135+
private static void checkFieldLength(int length) {
136+
if (length > 255 || length < 0) {
137+
throw new EncoderException("Invalid field length value: " + length);
138+
}
139+
}
125140
}
Collapse file

‎codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5ServerEncoder.java‎

Copy file name to clipboardExpand all lines: codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5ServerEncoder.java
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,18 @@ private void encodeCommandResponse(Socks5CommandResponse msg, ByteBuf out) throw
9494

9595
final Socks5AddressType bndAddrType = msg.bndAddrType();
9696
out.writeByte(bndAddrType.byteValue());
97-
addressEncoder.encodeAddress(bndAddrType, msg.bndAddr(), out);
97+
String addrValue = msg.bndAddr();
98+
if (addrValue != null && bndAddrType == Socks5AddressType.DOMAIN) {
99+
checkFieldLength(addrValue.length());
100+
}
101+
addressEncoder.encodeAddress(bndAddrType, addrValue, out);
98102

99103
ByteBufUtil.writeShortBE(out, msg.bndPort());
100104
}
105+
106+
private static void checkFieldLength(int length) {
107+
if (length > 255 || length < 0) {
108+
throw new EncoderException("Invalid field length value: " + length);
109+
}
110+
}
101111
}
Collapse file

‎codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4ClientEncoderTest.java‎

Copy file name to clipboard
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.socksx.v4;
17+
18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.channel.embedded.EmbeddedChannel;
20+
import io.netty.handler.codec.EncoderException;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
24+
import static org.junit.jupiter.api.Assertions.assertFalse;
25+
import static org.junit.jupiter.api.Assertions.assertNotNull;
26+
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
28+
class Socks4ClientEncoderTest {
29+
@Test
30+
public void mustEncodeCommandRequestToIPv4() {
31+
EmbeddedChannel encoder = new EmbeddedChannel(Socks4ClientEncoder.INSTANCE);
32+
DefaultSocks4CommandRequest request = new DefaultSocks4CommandRequest(
33+
Socks4CommandType.CONNECT, "127.0.0.1", 8008, "user");
34+
assertTrue(encoder.writeOutbound(request));
35+
ByteBuf buf = encoder.readOutbound();
36+
assertNotNull(buf);
37+
buf.release();
38+
assertFalse(encoder.finish());
39+
}
40+
@Test
41+
public void mustEncodeCommandRequestToDomain() {
42+
EmbeddedChannel encoder = new EmbeddedChannel(Socks4ClientEncoder.INSTANCE);
43+
DefaultSocks4CommandRequest request = new DefaultSocks4CommandRequest(
44+
Socks4CommandType.CONNECT, "unix://uds.sock", 8008, "user");
45+
assertTrue(encoder.writeOutbound(request));
46+
ByteBuf buf = encoder.readOutbound();
47+
assertNotNull(buf);
48+
buf.release();
49+
assertFalse(encoder.finish());
50+
}
51+
52+
@Test
53+
public void mustRejectNulByteInUserIdWithIPv4Destination() {
54+
EmbeddedChannel encoder = new EmbeddedChannel(Socks4ClientEncoder.INSTANCE);
55+
DefaultSocks4CommandRequest request = new DefaultSocks4CommandRequest(
56+
Socks4CommandType.CONNECT, "127.0.0.1", 8008, "use\0r");
57+
assertThatThrownBy(() -> encoder.writeOutbound(request))
58+
.isInstanceOf(EncoderException.class)
59+
.hasMessageContaining("Illegal character");
60+
assertFalse(encoder.finish());
61+
}
62+
63+
@Test
64+
public void mustRejectNulByteInUserIdWithDomainDestination() {
65+
EmbeddedChannel encoder = new EmbeddedChannel(Socks4ClientEncoder.INSTANCE);
66+
DefaultSocks4CommandRequest request = new DefaultSocks4CommandRequest(
67+
Socks4CommandType.CONNECT, "unix://uds.sock", 8008, "use\0r");
68+
assertThatThrownBy(() -> encoder.writeOutbound(request))
69+
.isInstanceOf(EncoderException.class)
70+
.hasMessageContaining("Illegal character");
71+
assertFalse(encoder.finish());
72+
}
73+
74+
@Test
75+
public void mustRejectNulByteInDstAddr() {
76+
EmbeddedChannel encoder = new EmbeddedChannel(Socks4ClientEncoder.INSTANCE);
77+
DefaultSocks4CommandRequest request = new DefaultSocks4CommandRequest(
78+
Socks4CommandType.CONNECT, "unix://uds\0.sock", 8008, "user");
79+
assertThatThrownBy(() -> encoder.writeOutbound(request))
80+
.isInstanceOf(EncoderException.class)
81+
.hasMessageContaining("Illegal character");
82+
assertFalse(encoder.finish());
83+
}
84+
}

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.