This is an automated email from the ASF dual-hosted git repository.
tomaswolf pushed a commit to branch dev_3.0
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
The following commit(s) were added to refs/heads/dev_3.0 by this push:
new 279281567 Socks5ProxyConnector: handle split connection reply
279281567 is described below
commit 279281567ab1e74fa6240e45c7db08461bf42b9f
Author: Thomas Wolf <[email protected]>
AuthorDate: Mon Nov 9 23:20:44 2026 +0100
Socks5ProxyConnector: handle split connection reply
If the message received is only a partial proxy reply, keep accumulating
data until we have the full reply. Whatever is left after having parsed
the full reply is returned for further handling.
Also-by: t2itto
---
.../client/session/proxy/Socks5ProxyConnector.java | 132 +++++++++++++++++----
.../session/proxy/Socks5ProxyConnectorTest.java | 60 ++++++++++
2 files changed, 167 insertions(+), 25 deletions(-)
diff --git
a/sshd-core/src/main/java/org/apache/sshd/client/session/proxy/Socks5ProxyConnector.java
b/sshd-core/src/main/java/org/apache/sshd/client/session/proxy/Socks5ProxyConnector.java
index d11eea4f5..af1a650ac 100644
---
a/sshd-core/src/main/java/org/apache/sshd/client/session/proxy/Socks5ProxyConnector.java
+++
b/sshd-core/src/main/java/org/apache/sshd/client/session/proxy/Socks5ProxyConnector.java
@@ -92,7 +92,7 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
INIT {
@Override
- public void handleMessage(Socks5ProxyConnector connector, Buffer
data) throws Exception {
+ public Buffer handleMessage(Socks5ProxyConnector connector, Buffer
data) throws Exception {
connector.versionCheck(data.getByte());
SocksAuthenticationMethod authMethod =
connector.getAuthMethod(data.getByte());
switch (authMethod) {
@@ -108,35 +108,22 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
default:
throw new IOException("Cannot authenticate at SOCKS
proxy " + connector.proxyAddress);
}
+ return null;
}
},
AUTHENTICATING {
@Override
- public void handleMessage(Socks5ProxyConnector connector, Buffer
data) throws Exception {
+ public Buffer handleMessage(Socks5ProxyConnector connector, Buffer
data) throws Exception {
connector.authStep(data);
+ return null;
}
},
CONNECTING {
@Override
- public void handleMessage(Socks5ProxyConnector connector, Buffer
data) throws Exception {
- // Special case: when GSS-API authentication completes, the
- // client moves into CONNECTING as soon as the GSS context is
- // established and sends the connect request. This is per RFC
- // 1961. But for the server, RFC 1961 says it _should_ send an
- // empty token even if none generated when its server side
- // context is established. That means we may actually get an
- // empty token here. That message is 4 bytes long (and has
- // content 0x01, 0x01, 0x00, 0x00). We simply skip this message
- // if we get it here. If the server for whatever reason sends
- // back a "GSS failed" message (it shouldn't, at this point)
- // it will be two bytes 0x01 0xFF, which will fail the version
- // check.
- if (data.available() != 4) {
- connector.versionCheck(data.getByte());
- connector.establishConnection(data);
- }
+ public Buffer handleMessage(Socks5ProxyConnector connector, Buffer
data) throws Exception {
+ return connector.receiveConnectReply(data);
}
},
@@ -144,7 +131,7 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
FAILED;
- public void handleMessage(Socks5ProxyConnector connector, Buffer data)
throws Exception {
+ public Buffer handleMessage(Socks5ProxyConnector connector, Buffer
data) throws Exception {
throw new IOException(MessageFormat.format("Unexpected reply from
SOCKS proxy {0}: {1}", connector.proxyAddress,
BufferUtils.toHex(data.array())));
}
@@ -158,6 +145,13 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
private byte[] authenticationProposals;
+ // Part of the connect reply received so far, while it is incomplete
+ private Buffer pendingReply;
+
+ // True while an empty GSS-API token may still precede the connect reply
+ // (only after GSS-API authentication, RFC 1961)
+ private boolean emptyGssTokenPossible;
+
/**
* Creates a new {@link Socks5ProxyConnector}. The connector supports
anonymous connections as well as
* username-password or Kerberos5 (GSS-API) authentication.
@@ -259,6 +253,7 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
private void doGssApiAuth() throws Exception {
authenticator = new SocksGssApiAuthentication();
+ emptyGssTokenPossible = true;
startAuth();
}
@@ -295,7 +290,95 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
}
}
- private void establishConnection(Buffer data) throws IOException {
+ // Accumulates the connect reply until it is complete, then hands only the
+ // data following it (typically the server's SSH identification) to the
+ // SSH session.
+ //
+ // Package-visible for a unit test
+ Buffer receiveConnectReply(Buffer data) throws Exception {
+ Buffer reply = data;
+ if (pendingReply != null) {
+ pendingReply.putBuffer(data);
+ reply = pendingReply;
+ }
+ if (emptyGssTokenPossible && reply.available() > 0) {
+ int pos = reply.rpos();
+ if (reply.rawByte(pos) == 1) {
+ if (reply.available() < 4) {
+ keepReply(reply);
+ return null;
+ }
+ // Special case: when GSS-API authentication completes, the
+ // client moves into CONNECTING as soon as the GSS context is
+ // established and sends the connect request. This is per RFC
+ // 1961. But for the server, RFC 1961 says it _should_ send an
+ // empty token even if none generated when its server side
+ // context is established. That means we may actually get an
+ // empty token here. That message is 4 bytes long (and has
+ // content 0x01, 0x01, 0x00, 0x00). We simply skip this message
+ // if we get it here. If the server for whatever reason sends
+ // back a "GSS failed" message (it shouldn't, at this point)
+ // it will be two bytes 0x01 0xFF, which will fail the version
+ // check.
+ if (reply.rawByte(pos + 1) == 1 && reply.rawByte(pos + 2) == 0
&& reply.rawByte(pos + 3) == 0) {
+ reply.rpos(pos + 4);
+ }
+ }
+ emptyGssTokenPossible = false;
+ }
+ if (reply.available() == 0) {
+ pendingReply = null;
+ return null;
+ }
+ versionCheck(reply.rawByte(reply.rpos()));
+ if (reply.available() >= 2 && reply.rawByte(reply.rpos() + 1) !=
Socks5.REPLY_SUCCESS) {
+ // Fail at once; don't wait for the rest of a failure reply
+ pendingReply = null;
+ reply.getByte();
+ establishConnection(reply);
+ return null;
+ }
+ int length = getReplyLength(reply);
+ if (length < 0 || reply.available() < length) {
+ keepReply(reply);
+ return null;
+ }
+ pendingReply = null;
+ reply.getByte();
+ return establishConnection(reply);
+ }
+
+ private void keepReply(Buffer reply) {
+ if (pendingReply == null) {
+ pendingReply = new ByteArrayBuffer(reply.available() + 64, false);
+ pendingReply.putBuffer(reply);
+ }
+ }
+
+ // Length of the whole connect reply, or -1 if not enough of it has been
received yet to tell
+ private int getReplyLength(Buffer reply) throws IOException {
+ if (reply.available() < 4) {
+ return -1;
+ }
+ int pos = reply.rpos();
+ byte type = reply.rawByte(pos + 3);
+ switch (type) {
+ case Socks5.ADDRESS_IPV4:
+ return 4 + 4 + 2;
+ case Socks5.ADDRESS_IPV6:
+ return 4 + 16 + 2;
+ case Socks5.ADDRESS_FQDN:
+ if (reply.available() < 5) {
+ return -1;
+ }
+ return 4 + 1 + (reply.rawByte(pos + 4) & 0xFF) + 2;
+ default:
+ throw new IOException(
+ "Unexpected address type" + (type & 0xFF) + " in SOCKS
connect reply from " + proxyAddress);
+ }
+ }
+
+ private Buffer establishConnection(Buffer data) throws IOException {
byte reply = data.getByte();
switch (reply) {
case Socks5.REPLY_SUCCESS:
@@ -325,7 +408,7 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
}
state = ProtocolState.CONNECTED;
done = true;
- return;
+ return data;
case Socks5.REPLY_FAILURE:
throw new IOException("Tunneling failed for proxy " +
proxyAddress);
case Socks5.REPLY_FORBIDDEN:
@@ -380,8 +463,7 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
// Dispatch according to protocol state
ByteArrayBuffer data = new ByteArrayBuffer(buffer.available(),
false);
data.putBuffer(buffer);
- state.handleMessage(this, data);
- return data;
+ return state.handleMessage(this, data);
} catch (Exception e) {
state = ProtocolState.FAILED;
if (authenticator != null) {
@@ -395,7 +477,7 @@ public class Socks5ProxyConnector extends
AbstractProxyConnector {
private void versionCheck(byte version) throws IOException {
if (version != Socks5.VERSION) {
- throw new IOException("Unexpected SOCK version " + (version &
0xFF));
+ throw new IOException("Unexpected SOCKS version " + (version &
0xFF));
}
}
diff --git
a/sshd-core/src/test/java/org/apache/sshd/client/session/proxy/Socks5ProxyConnectorTest.java
b/sshd-core/src/test/java/org/apache/sshd/client/session/proxy/Socks5ProxyConnectorTest.java
new file mode 100644
index 000000000..3f82d6265
--- /dev/null
+++
b/sshd-core/src/test/java/org/apache/sshd/client/session/proxy/Socks5ProxyConnectorTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd.client.session.proxy;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.sshd.client.proxy.ProxyData;
+import org.apache.sshd.common.forward.SocksConstants.Socks5;
+import org.apache.sshd.common.util.buffer.Buffer;
+import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
+import org.apache.sshd.util.test.JUnitTestSupport;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Basic tests for accumulating the SOCKS connection reply.
+ */
+@Tag("NoIoTestCase")
+class Socks5ProxyConnectorTest extends JUnitTestSupport {
+
+ @Test
+ void splitConnectionReply() throws Exception {
+ byte[] chunk1 = new byte[] { 5 }; // Socks version
+ byte[] chunk2 = new byte[] { Socks5.REPLY_SUCCESS, 0 };
+ byte[] chunk3 = new byte[] { Socks5.ADDRESS_IPV4, 1, 2, 3 };
+ byte[] chunk4 = new byte[] { 4, 0, 100, (byte) 'S', (byte) 'S', (byte)
'H', (byte) '-', (byte) '2', (byte) '.',
+ (byte) '0', (byte) '-', (byte) 'X', (byte) '\n' };
+ InetSocketAddress unused = new
InetSocketAddress(InetAddress.getLocalHost(), 2000);
+ ProxyData proxy = new ProxyData(new Proxy(Proxy.Type.SOCKS, unused),
"user", "pass".toCharArray());
+ try (Socks5ProxyConnector connector = new Socks5ProxyConnector(proxy,
unused, b -> null, a -> null)) {
+ assertNull(connector.receiveConnectReply(new
ByteArrayBuffer(chunk1)));
+ assertNull(connector.receiveConnectReply(new
ByteArrayBuffer(chunk2)));
+ assertNull(connector.receiveConnectReply(new
ByteArrayBuffer(chunk3)));
+ Buffer rest = connector.receiveConnectReply(new
ByteArrayBuffer(chunk4));
+ assertTrue(connector.isDone());
+ String ident = new String(rest.array(), rest.rpos(),
rest.available(), StandardCharsets.US_ASCII);
+ assertEquals("SSH-2.0-X\n", ident);
+ }
+ }
+
+}