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 — Fix sign-extension bug in SocksInitRequestDecoder NMETHODS field · netty/netty@ef3a3e1

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-socks/src
    • main/java/io/netty/handler/codec/socks
      • SocksInitRequestDecoder.java
    • test/java/io/netty/handler/codec/socks
      • SocksInitRequestDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: codec-socks/src/main/java/io/netty/handler/codec/socks/SocksInitRequestDecoder.java
+9-10Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.netty.util.internal.UnstableApi;
2323

2424
import java.util.ArrayList;
25-
import java.util.Collections;
2625
import java.util.List;
2726

2827
/**
@@ -46,15 +45,15 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
4645
checkpoint(State.READ_AUTH_SCHEMES);
4746
}
4847
case READ_AUTH_SCHEMES: {
49-
final byte authSchemeNum = byteBuf.readByte();
50-
final List<SocksAuthScheme> authSchemes;
51-
if (authSchemeNum > 0) {
52-
authSchemes = new ArrayList<SocksAuthScheme>(authSchemeNum);
53-
for (int i = 0; i < authSchemeNum; i++) {
54-
authSchemes.add(SocksAuthScheme.valueOf(byteBuf.readByte()));
55-
}
56-
} else {
57-
authSchemes = Collections.emptyList();
48+
final int authSchemeNum = byteBuf.readUnsignedByte();
49+
if (authSchemeNum == 0) {
50+
// RFC 1928 section 3 requires NMETHODS to be at least 1.
51+
out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
52+
break;
53+
}
54+
final List<SocksAuthScheme> authSchemes = new ArrayList<SocksAuthScheme>(authSchemeNum);
55+
for (int i = 0; i < authSchemeNum; i++) {
56+
authSchemes.add(SocksAuthScheme.valueOf(byteBuf.readByte()));
5857
}
5958
out.add(new SocksInitRequest(authSchemes));
6059
break;
Collapse file

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

Copy file name to clipboard
+130Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.socks;
17+
18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.Unpooled;
20+
import io.netty.channel.embedded.EmbeddedChannel;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.List;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
28+
import static org.junit.jupiter.api.Assertions.assertNull;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
31+
public class SocksInitRequestDecoderTest {
32+
33+
/**
34+
* NMETHODS values in [0x80, 0xFF] sign-extend to a negative Java {@code byte},
35+
* so a decoder that reads them with {@link ByteBuf#readByte()} instead of
36+
* {@link ByteBuf#readUnsignedByte()} would wrongly treat the greeting as if
37+
* it offered zero authentication methods, without consuming the method bytes
38+
* that follow on the wire. Those unconsumed bytes would then leak to whatever
39+
* handler follows once the decoder removes itself from the pipeline.
40+
*/
41+
@Test
42+
public void testHighNMethodsIsReadAsUnsigned() {
43+
int authSchemeNum = 0x80; // 128, negative if interpreted as a signed byte
44+
EmbeddedChannel e = new EmbeddedChannel(new SocksInitRequestDecoder());
45+
46+
ByteBuf buf = Unpooled.buffer();
47+
buf.writeByte(SocksProtocolVersion.SOCKS5.byteValue());
48+
buf.writeByte(authSchemeNum);
49+
for (int i = 0; i < authSchemeNum; i++) {
50+
buf.writeByte(SocksAuthScheme.NO_AUTH.byteValue());
51+
}
52+
53+
assertTrue(e.writeInbound(buf));
54+
55+
Object o = e.readInbound();
56+
SocksInitRequest req = assertInstanceOf(SocksInitRequest.class, o);
57+
List<SocksAuthScheme> schemes = req.authSchemes();
58+
assertEquals(authSchemeNum, schemes.size());
59+
for (SocksAuthScheme scheme : schemes) {
60+
assertEquals(SocksAuthScheme.NO_AUTH, scheme);
61+
}
62+
63+
// No leftover bytes must have been forwarded to the next handler after self-removal.
64+
assertNull(e.readInbound());
65+
assertFalse(e.finish());
66+
}
67+
68+
@Test
69+
public void testMaxNMethodsIsReadAsUnsigned() {
70+
int authSchemeNum = 0xFF; // 255
71+
EmbeddedChannel e = new EmbeddedChannel(new SocksInitRequestDecoder());
72+
73+
ByteBuf buf = Unpooled.buffer();
74+
buf.writeByte(SocksProtocolVersion.SOCKS5.byteValue());
75+
buf.writeByte(authSchemeNum);
76+
for (int i = 0; i < authSchemeNum; i++) {
77+
buf.writeByte(SocksAuthScheme.NO_AUTH.byteValue());
78+
}
79+
80+
assertTrue(e.writeInbound(buf));
81+
82+
Object o = e.readInbound();
83+
SocksInitRequest req = assertInstanceOf(SocksInitRequest.class, o);
84+
assertEquals(authSchemeNum, req.authSchemes().size());
85+
86+
assertNull(e.readInbound());
87+
assertFalse(e.finish());
88+
}
89+
90+
/**
91+
* RFC 1928 section 3 requires NMETHODS to be at least 1; a wire value of 0 must be rejected
92+
* rather than accepted as a zero-auth-scheme success.
93+
*/
94+
@Test
95+
public void testZeroNMethodsIsRejected() {
96+
EmbeddedChannel e = new EmbeddedChannel(new SocksInitRequestDecoder());
97+
98+
ByteBuf buf = Unpooled.buffer();
99+
buf.writeByte(SocksProtocolVersion.SOCKS5.byteValue());
100+
buf.writeByte(0x00);
101+
102+
assertTrue(e.writeInbound(buf));
103+
104+
Object o = e.readInbound();
105+
assertInstanceOf(UnknownSocksRequest.class, o);
106+
107+
assertNull(e.readInbound());
108+
assertFalse(e.finish());
109+
}
110+
111+
@Test
112+
public void testNormalNMethodsStillWorks() {
113+
EmbeddedChannel e = new EmbeddedChannel(new SocksInitRequestDecoder());
114+
115+
ByteBuf buf = Unpooled.buffer();
116+
buf.writeByte(SocksProtocolVersion.SOCKS5.byteValue());
117+
buf.writeByte(0x01);
118+
buf.writeByte(SocksAuthScheme.NO_AUTH.byteValue());
119+
120+
assertTrue(e.writeInbound(buf));
121+
122+
Object o = e.readInbound();
123+
SocksInitRequest req = assertInstanceOf(SocksInitRequest.class, o);
124+
assertEquals(1, req.authSchemes().size());
125+
assertEquals(SocksAuthScheme.NO_AUTH, req.authSchemes().get(0));
126+
127+
assertNull(e.readInbound());
128+
assertFalse(e.finish());
129+
}
130+
}

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.