This is an automated email from the ASF dual-hosted git repository.
tomaswolf pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
The following commit(s) were added to refs/heads/master by this push:
new 19b0977d4 GH-397: Handle trailing slashes in SFTP directory operations
(#960)
19b0977d4 is described below
commit 19b0977d48f964a08aab8116298d24b717ad02fa
Author: Kayokoi <[email protected]>
AuthorDate: Tue Nov 24 11:33:31 2026 -0800
GH-397: Handle trailing slashes in SFTP directory operations (#960)
Path translation appends a dot to mkdir and rmdir paths ending in a
slash. Rooted filesystems preserve that component, causing these
operations to fail on Unix.
Trim only their insignificant trailing slashes before resolving the
path, preserving explicit dot and parent components and other
operations.
---
.../sftp/server/AbstractSftpSubsystemHelper.java | 13 ++-
.../apache/sshd/sftp/client/SftpDirectoryTest.java | 96 ++++++++++++++++++++++
2 files changed, 107 insertions(+), 2 deletions(-)
diff --git
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
index 347fe8021..712436ebc 100755
---
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
+++
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
@@ -1579,7 +1579,7 @@ public abstract class AbstractSftpSubsystemHelper
}
protected void doRemoveDirectory(int id, String path) throws IOException {
- Path p = resolveFile(path);
+ Path p = resolveDirectory(path);
if (log.isDebugEnabled()) {
log.debug("doRemoveDirectory({})[id={}] SSH_FXP_RMDIR
(path={})[{}]", getServerSession(), id, path, p);
}
@@ -1643,7 +1643,7 @@ public abstract class AbstractSftpSubsystemHelper
protected void doMakeDirectory(
int id, String path, Map<String, ?> attrs)
throws IOException {
- Path resolvedPath = resolveFile(path);
+ Path resolvedPath = resolveDirectory(path);
ServerSession session = getServerSession();
if (log.isDebugEnabled()) {
log.debug("doMakeDirectory({})[id={}] SSH_FXP_MKDIR (path={}[{}],
attrs={})",
@@ -3101,4 +3101,13 @@ public abstract class AbstractSftpSubsystemHelper
}
return localPath;
}
+
+ private Path resolveDirectory(String remotePath) throws IOException {
+ // Keep explicit dot components; only MKDIR and RMDIR ignore trailing
slashes.
+ int length = remotePath.length();
+ while ((length > 1) && (remotePath.charAt(length - 1) == '/')) {
+ length--;
+ }
+ return resolveFile(remotePath.substring(0, length));
+ }
}
diff --git
a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpDirectoryTest.java
b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpDirectoryTest.java
new file mode 100644
index 000000000..3205f9260
--- /dev/null
+++ b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpDirectoryTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.sftp.client;
+
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
+import org.apache.sshd.common.util.OsUtils;
+import org.apache.sshd.sftp.common.SftpException;
+import org.apache.sshd.util.test.CommonTestSupportUtils;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class SftpDirectoryTest extends AbstractSftpClientTestSupport {
+ private Path root;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ root =
detectTargetFolder().resolve(getClass().getSimpleName()).resolve(getCurrentTestName());
+ CommonTestSupportUtils.deleteRecursive(root,
LinkOption.NOFOLLOW_LINKS);
+ Files.createDirectories(root);
+ sshd.setFileSystemFactory(new VirtualFileSystemFactory(root));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "/", "///" })
+ void makeDirectoryWithTrailingSlashes(String suffix) throws Exception {
+ try (SftpClient sftp = createSingleSessionClient()) {
+ sftp.mkdir("/parent" + suffix);
+ assertTrue(Files.isDirectory(root.resolve("parent")));
+ sftp.mkdir("parent/child" + suffix);
+ assertTrue(Files.isDirectory(root.resolve("parent/child")));
+ assertThrows(SftpException.class, () -> sftp.mkdir(suffix));
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "/", "///" })
+ void removeDirectoryWithTrailingSlashes(String suffix) throws Exception {
+ Path directory = Files.createDirectory(root.resolve("directory"));
+ try (SftpClient sftp = createSingleSessionClient()) {
+ sftp.rmdir("/directory" + suffix);
+ assertFalse(Files.exists(directory));
+ }
+ }
+
+ @Test
+ void directoryOperationsPreservePathComponents() throws Exception {
+ Assumptions.assumeFalse(OsUtils.isWin32(), "Windows resolves dot path
components differently");
+ Path directory = Files.createDirectory(root.resolve("directory"));
+ try (SftpClient sftp = createSingleSessionClient()) {
+ assertThrows(SftpException.class, () ->
sftp.mkdir("missing/../created/"));
+ assertFalse(Files.exists(root.resolve("created")));
+ assertThrows(SftpException.class, () -> sftp.mkdir("created/."));
+ assertFalse(Files.exists(root.resolve("created")));
+ assertThrows(SftpException.class, () ->
sftp.rmdir("missing/../directory/"));
+ assertThrows(SftpException.class, () -> sftp.rmdir("directory/."));
+ assertTrue(Files.isDirectory(directory));
+ }
+ }
+
+ @Test
+ void directorySymlinkIsNotRemoved() throws Exception {
+ Assumptions.assumeFalse(OsUtils.isWin32(), "Symlinks are not supported
on all Windows file systems");
+ Path directory = Files.createDirectory(root.resolve("directory"));
+ Path link = Files.createSymbolicLink(root.resolve("link"),
directory.getFileName());
+ try (SftpClient sftp = createSingleSessionClient()) {
+ assertTrue(sftp.lstat("link").isSymbolicLink());
+ assertTrue(sftp.lstat("link/").isDirectory());
+ assertThrows(SftpException.class, () -> sftp.rmdir("link/"));
+ assertTrue(Files.isSymbolicLink(link));
+ assertTrue(Files.isDirectory(directory));
+ }
+ }
+}