|
| 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 | +} |