From 9f21017a5f990c2e142bd9500c773d6a10c897d9 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:19:57 +0800 Subject: [PATCH 1/3] GH-397: Handle trailing slashes in SFTP directory operations 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 +++++++++++-- 1 file changed, 11 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 @@ protected void doRemoveDirectory(Buffer buffer, int id) throws IOException { } 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 @@ protected void doMakeDirectory(Buffer buffer, int id) throws IOException { protected void doMakeDirectory( int id, String path, Map 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 @@ protected Path resolveFile(String remotePath) } 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)); + } } From 7f7cad246712fbf8da6fad51f34f9783a51013f2 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:20:35 +0800 Subject: [PATCH 2/3] GH-397: Cover rooted SFTP directory path semantics --- .../sshd/sftp/client/SftpDirectoryTest.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpDirectoryTest.java 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..db4667dc2 --- /dev/null +++ b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpDirectoryTest.java @@ -0,0 +1,95 @@ +/* + * 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.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); + 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)); + } + } +} From 30b5a4c08d3121e2216b3fc2b504ce0c0b21d5c6 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:20:19 -0800 Subject: [PATCH 3/3] GH-397: Do not follow symlinks during SFTP test cleanup Use NOFOLLOW_LINKS when removing the previous test directory so dangling links do not survive into the next run. Suggested-by: @tomaswolf Assisted-by: OpenAI ChatGPT --- .../java/org/apache/sshd/sftp/client/SftpDirectoryTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index db4667dc2..3205f9260 100644 --- 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 @@ -19,6 +19,7 @@ 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; @@ -37,7 +38,7 @@ class SftpDirectoryTest extends AbstractSftpClientTestSupport { @BeforeEach void setUp() throws Exception { root = detectTargetFolder().resolve(getClass().getSimpleName()).resolve(getCurrentTestName()); - CommonTestSupportUtils.deleteRecursive(root); + CommonTestSupportUtils.deleteRecursive(root, LinkOption.NOFOLLOW_LINKS); Files.createDirectories(root); sshd.setFileSystemFactory(new VirtualFileSystemFactory(root)); }