[SSHD-890] SFTP susbsystem implementation does not clear response buffer when sending...
authorSakthipriyan Vairamani (thefourtheye) <thechargingvolcano@gmail.com>
Sat, 9 Feb 2019 08:32:29 +0000 (10:32 +0200)
committerLyor Goldstein <lgoldstein@apache.org>
Sun, 10 Feb 2019 05:56:55 +0000 (07:56 +0200)
sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java
sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/extensions/UnsupportedExtensionTest.java [new file with mode: 0644]

index 2ef4b60..bd4ece6 100644 (file)
@@ -1651,7 +1651,7 @@ public abstract class AbstractSftpSubsystemHelper
         if (log.isDebugEnabled()) {
             log.debug("executeExtendedCommand({}) received unsupported SSH_FXP_EXTENDED({})", getServerSession(), extension);
         }
-        sendStatus(buffer, id, SftpConstants.SSH_FX_OP_UNSUPPORTED,
+        sendStatus(prepareReply(buffer), id, SftpConstants.SSH_FX_OP_UNSUPPORTED,
             "Command SSH_FXP_EXTENDED(" + extension + ") is unsupported or not implemented");
     }
 
index 66a0ced..e7f1538 100644 (file)
@@ -947,7 +947,7 @@ public class SftpSubsystem
     @Override
     protected Buffer prepareReply(Buffer buffer) {
         buffer.clear();
-        buffer.putInt(0);
+        buffer.putInt(0);   // reserve space for actual packet length
         return buffer;
     }
 
diff --git a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/extensions/UnsupportedExtensionTest.java b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/extensions/UnsupportedExtensionTest.java
new file mode 100644 (file)
index 0000000..6dcdea0
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * 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.subsystem.sftp.extensions;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.sshd.client.session.ClientSession;
+import org.apache.sshd.client.subsystem.sftp.AbstractSftpClientTestSupport;
+import org.apache.sshd.client.subsystem.sftp.RawSftpClient;
+import org.apache.sshd.client.subsystem.sftp.SftpClient;
+import org.apache.sshd.common.subsystem.sftp.SftpConstants;
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.buffer.Buffer;
+import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class UnsupportedExtensionTest extends AbstractSftpClientTestSupport {
+    public UnsupportedExtensionTest() throws IOException {
+        super();
+    }
+
+    @Test   // see SSHD-890
+    public void testUnsupportedExtension() throws IOException {
+        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port)
+                .verify(7L, TimeUnit.SECONDS)
+                .getSession()) {
+            session.addPasswordIdentity(getCurrentTestName());
+            session.auth().verify(5L, TimeUnit.SECONDS);
+
+            try (SftpClient sftpClient = createSftpClient(session)) {
+                String opcode = getCurrentTestName();
+                Buffer buffer = new ByteArrayBuffer(Integer.BYTES + GenericUtils.length(opcode) + Byte.SIZE, false);
+                buffer.putString(opcode);
+
+                assertObjectInstanceOf("Not a raw SFTP client", RawSftpClient.class, sftpClient);
+                RawSftpClient sftp = (RawSftpClient) sftpClient;
+                int cmd = sftp.send(SftpConstants.SSH_FXP_EXTENDED, buffer);
+                Buffer responseBuffer = sftp.receive(cmd);
+
+                responseBuffer.getInt();                    // Ignoring length
+                int type = responseBuffer.getUByte();
+                responseBuffer.getInt();                    // Ignoring message ID
+                int substatus = responseBuffer.getInt();
+
+                assertEquals("Type is not STATUS", SftpConstants.SSH_FXP_STATUS, type);
+                assertEquals("Sub-Type is not UNSUPPORTED", SftpConstants.SSH_FX_OP_UNSUPPORTED, substatus);
+            }
+        }
+    }
+}
\ No newline at end of file