This is an automated email from the ASF dual-hosted git repository.

Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3cf79c4997bb30dea5c5031d14d4bf56066452f7
Author: croway <[email protected]>
AuthorDate: Tue Nov 24 10:16:17 2026 +0100

    CAMEL-27453: camel-ftp, camel-mina-sftp, camel-jsch - address review 
feedback
    
    - A stepwise recursive poll changes into a directory listed with a trailing 
/, such as sub/, as it did before.
    - handleDirectoryParserAbsoluteResult: a listed path such as C:/data/in/sub 
is in the listed directory C:/data/in.
    - The scp producer sends a name as given, with / as the only separator, so 
with jailStartingDirectory it refuses a
      name with both a backslash and a .. segment.
    - Move isDirectoryOrParentOf from SftpOperations and MinaSftpOperations to 
FtpUtils.
    - Compile the separator pattern of GenericFileHelper once, use {@code} in 
the new Javadoc.
    - FtpConsumerListedDirectoryNameTest waits with Awaitility instead of 
sleeping.
    - Upgrade guide notes for the behaviour changes, in the 4.24 guide, in the 
4.22.4 section and in a new 4.18.7 section.
---
 .../camel/component/file/GenericFileHelper.java    |  9 ++-
 .../camel/component/file/remote/FtpUtils.java      | 31 ++++++--
 .../component/file/remote/RemoteFileProducer.java  |  7 +-
 .../camel/component/file/remote/FtpConsumer.java   | 15 ++--
 .../component/file/remote/SftpOperations.java      | 10 +--
 .../remote/FtpConsumerListedDirectoryNameTest.java | 63 ++++++++++++++-
 .../apache/camel/component/scp/ScpProducer.java    |  9 +++
 .../scp/ScpProducerBackslashNameTest.java          | 89 ++++++++++++++++++++++
 .../file/remote/mina/MinaSftpOperations.java       | 11 +--
 .../ROOT/pages/camel-4x-upgrade-guide-4_18.adoc    | 31 ++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_22.adoc    | 29 +++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    | 35 ++++++++-
 12 files changed, 296 insertions(+), 43 deletions(-)

diff --git 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
index a8e1e695e1ff..1fe20ab830af 100644
--- 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
+++ 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
@@ -30,6 +30,7 @@ import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayDeque;
 import java.util.Deque;
 import java.util.function.Supplier;
+import java.util.regex.Pattern;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
@@ -42,7 +43,7 @@ import org.apache.camel.util.IOHelper;
 public final class GenericFileHelper {
 
     // the separators that split a path into segments when compacting it for a 
containment check
-    private static final String SEPARATORS = "[/\\\\]";
+    private static final Pattern SEPARATORS = Pattern.compile("[/\\\\]");
 
     private GenericFileHelper() {
     }
@@ -261,7 +262,7 @@ public final class GenericFileHelper {
      * on the server and that platform, unless its backslashes are replaced by 
/ before it is used.
      *
      * @param  path the path
-     * @return      <tt>true</tt> if the path has both a backslash and a 
{@code ..} segment
+     * @return      {@code true} if the path has both a backslash and a {@code 
..} segment
      */
     public static boolean isAmbiguousRemotePath(String path) {
         return path != null && path.indexOf('\\') != -1 && 
hasParentSegment(path);
@@ -286,7 +287,7 @@ public final class GenericFileHelper {
         }
         boolean absolute = !path.isEmpty() && (path.charAt(0) == '/' || 
path.charAt(0) == '\\');
         Deque<String> segments = new ArrayDeque<>();
-        for (String segment : path.split(SEPARATORS)) {
+        for (String segment : SEPARATORS.split(path)) {
             if (segment.isEmpty() || ".".equals(segment)) {
                 continue;
             }
@@ -301,7 +302,7 @@ public final class GenericFileHelper {
     }
 
     private static boolean hasParentSegment(String path) {
-        for (String segment : path.split(SEPARATORS)) {
+        for (String segment : SEPARATORS.split(path)) {
             if ("..".equals(segment)) {
                 return true;
             }
diff --git 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
index c94a9a5c6e5c..3ae2431b6f17 100644
--- 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
+++ 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
@@ -144,16 +144,35 @@ public final class FtpUtils {
 
     /**
      * Whether a directory name from a listing of the server is the name of 
one directory in the listed directory: not
-     * blank, not . or .., and without a / or a \. A stepwise recursive poll 
changes into such a directory one segment
-     * at a time, and back to its parent afterwards, so a name such as sub/.. 
or a\b would leave the poll in another
-     * directory than the listed one.
+     * blank, not . or .., and without a / or a \, once any trailing / is 
removed, as some servers list a directory with
+     * a trailing /. A stepwise recursive poll changes into such a directory 
one segment at a time, and back to its
+     * parent afterwards, so a name such as sub/.. or a\b would leave the poll 
in another directory than the listed one.
      *
      * @param  name the name of the directory, as listed
-     * @return      <tt>true</tt> if the poll can change into the directory
+     * @return      {@code true} if the poll can change into the directory
      */
     public static boolean isSubDirectoryName(String name) {
-        return name != null && !name.isBlank() && !".".equals(name) && 
!"..".equals(name) && name.indexOf('/') == -1
-                && name.indexOf('\\') == -1;
+        if (name == null) {
+            return false;
+        }
+        String directory = name;
+        while (directory.length() > 1 && directory.endsWith("/")) {
+            directory = directory.substring(0, directory.length() - 1);
+        }
+        return !directory.isBlank() && !".".equals(directory) && 
!"..".equals(directory) && directory.indexOf('/') == -1
+                && directory.indexOf('\\') == -1;
+    }
+
+    /**
+     * Whether a path is a directory itself or one of its parent directories. 
A path that only starts with the same
+     * characters, such as /home/cam for /home/camel, is not.
+     *
+     * @param  path      the path
+     * @param  directory the directory
+     * @return           {@code true} if the path is the directory or one of 
its parent directories
+     */
+    public static boolean isDirectoryOrParentOf(String path, String directory) 
{
+        return directory.equals(path) || 
directory.startsWith(path.endsWith("/") ? path : path + "/");
     }
 
     public static String absoluteFilePath(String absolutePath, String name) {
diff --git 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
index 457c1cf8d4a7..6db1e9b6675c 100644
--- 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
+++ 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
@@ -54,9 +54,10 @@ public class RemoteFileProducer<T> extends 
GenericFileProducer<T> {
     }
 
     /**
-     * The operations replace a backslash in the name of the file they store 
by / when the separator option is UNIX (the
-     * default). They use the name as given to check, delete or rename a file, 
and to write a checksum file, so not when
-     * the producer writes a temp file, a done file or a checksum file.
+     * The ftp, ftps and sftp operations replace a backslash in the name of 
the file they store by / when the separator
+     * option is UNIX (the default). They use the name as given to check, 
delete or rename a file, and to write a
+     * checksum file, so not when the producer writes a temp file, a done file 
or a checksum file. A producer whose
+     * operations send the name as given, such as scp, overrides this.
      */
     @Override
     protected boolean isBackslashNormalized() {
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
index 26947aae1eb1..735ccacc5fe1 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
@@ -234,16 +234,17 @@ public class FtpConsumer extends 
RemoteFileConsumer<FTPFile> {
     /**
      * Whether a directory entry, as listed by the server, is in the directory 
being listed. A server that lists the
      * absolute path of each entry (handleDirectoryParserAbsoluteResult) lists 
the entries of the listed directory, so
-     * the absolute path ends with the path of the listed directory and the 
name of the entry. A relative name must be
-     * the name of the entry, followed by / at most, as a relative path of 
more than one name is not an entry of the
-     * listed directory. Both paths are compacted first.
+     * the absolute path ends with the path of the listed directory and the 
name of the entry, or is that path when the
+     * path of the listed directory starts with a drive, such as C:/data/in. A 
relative name must be the name of the
+     * entry, followed by / at most, as a relative path of more than one name 
is not an entry of the listed directory.
+     * Both paths are compacted first.
      *
      * @param  listed        the name of the entry as listed
      * @param  absolutePath  the path of the listed directory
      * @param  directoryName the name of the entry
-     * @return               <tt>true</tt> if the entry is in the listed 
directory
+     * @return               {@code true} if the entry is in the listed 
directory
      */
-    private static boolean isListedIn(String listed, String absolutePath, 
String directoryName) {
+    static boolean isListedIn(String listed, String absolutePath, String 
directoryName) {
         if (!isAbsolute(listed)) {
             // one name, which may end with a /, as a relative path of more 
than one name is not an entry of the listed
             // directory
@@ -257,7 +258,9 @@ public class FtpConsumer extends 
RemoteFileConsumer<FTPFile> {
         String directory = FileUtil.stripLeadingSeparator(
                 GenericFileHelper.compactPathForContainment(absolutePath != 
null ? absolutePath : "", '/'));
         String expected = directory.isEmpty() ? directoryName : directory + 
"/" + directoryName;
-        return path.endsWith("/" + expected);
+        // a path with a drive is the path of the listed directory and the 
name of the entry when that directory starts
+        // with the same drive, so it does not start with a /
+        return path.endsWith("/" + expected) || !path.isEmpty() && 
isAbsolute(path) && path.equals(expected);
     }
 
     /**
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index eb54b1fb95bc..1f62154b8cff 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -1134,7 +1134,7 @@ public class SftpOperations
                 return;
             }
             String current = getCurrentDirectory();
-            if (isDirectoryOrParentOf(path, current)) {
+            if (FtpUtils.isDirectoryOrParentOf(path, current)) {
                 // extract the path segment relative to the target path and 
make
                 // sure it keeps the preceding '/' for the regex op
                 String p = current.substring(path.length() - 
(path.endsWith("/") ? 1 : 0));
@@ -1181,14 +1181,6 @@ public class SftpOperations
         }
     }
 
-    /**
-     * Whether the path is the directory itself or one of its parent 
directories. A path that only starts with the same
-     * characters, such as /home/cam for /home/camel, is not.
-     */
-    private static boolean isDirectoryOrParentOf(String path, String 
directory) {
-        return directory.equals(path) || 
directory.startsWith(path.endsWith("/") ? path : path + "/");
-    }
-
     private void doChangeDirectory(String path) {
         if (path == null || ".".equals(path) || ObjectHelper.isEmpty(path)) {
             return;
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
index fa8684187875..121e4be2d0fb 100644
--- 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
@@ -74,7 +74,7 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
 
         await().atMost(10, TimeUnit.SECONDS).until(() -> received.size() >= 2);
         // nothing more arrives: the poll does not list the inbox again 
through sub/..
-        TimeUnit.MILLISECONDS.sleep(500);
+        await().during(500, TimeUnit.MILLISECONDS).atMost(2, 
TimeUnit.SECONDS).until(() -> received.size() == 2);
 
         assertEquals(2, received.size(), received.toString());
         assertTrue(received.contains("hello.txt"), received.toString());
@@ -111,7 +111,7 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
 
         // the poll changes into each listed directory by its name, and 
downloads its file
         await().atMost(10, TimeUnit.SECONDS).until(() -> received.size() >= 5);
-        TimeUnit.MILLISECONDS.sleep(500);
+        await().during(500, TimeUnit.MILLISECONDS).atMost(2, 
TimeUnit.SECONDS).until(() -> received.size() == 5);
         assertEquals(List.of("fifth/five.txt=five", "fourth/four.txt=four", 
"real/deep.txt=deep", "second/two.txt=two",
                 "third/three.txt=three"), received.stream().sorted().toList());
     }
@@ -125,6 +125,25 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
         await().atMost(10, TimeUnit.SECONDS).until(() -> 
received.contains("child/report.txt=report"));
     }
 
+    @Test
+    void pollsListedRelativeDirectoryWithTrailingSeparatorStepwise() throws 
Exception {
+        // a server can list a directory with a trailing /, which a stepwise 
poll changes into by its name
+        server.addFile("/inbox/child/report.txt", 
"report".getBytes(StandardCharsets.UTF_8));
+        server.addListedDirectory("/inbox", "child/");
+        List<String> received = new CopyOnWriteArrayList<>();
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("ftp://[email protected]:"; + server.port() + 
"/inbox?password=secret&passiveMode=true"
+                     + "&recursive=true&noop=true&initialDelay=0&delay=600000")
+                        .process(e -> 
received.add(e.getMessage().getHeader(Exchange.FILE_NAME, String.class)));
+            }
+        });
+
+        await().atMost(10, TimeUnit.SECONDS).until(() -> !received.isEmpty());
+        assertEquals(List.of("child/report.txt"), received);
+    }
+
     @Test
     void entersListedAbsoluteDirectoryByItsNameInTheListedDirectory() throws 
Exception {
         // /x/inbox/real ends like /inbox/real, but the poll changes into real 
in the listed directory
@@ -135,7 +154,7 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
         List<String> received = pollInbox("");
 
         await().atMost(10, TimeUnit.SECONDS).until(() -> !received.isEmpty());
-        TimeUnit.MILLISECONDS.sleep(500);
+        await().during(500, TimeUnit.MILLISECONDS).atMost(2, 
TimeUnit.SECONDS).until(() -> received.size() == 1);
         assertEquals(List.of("real/deep.txt=deep"), received);
     }
 
@@ -195,7 +214,7 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
         });
 
         await().atMost(10, TimeUnit.SECONDS).until(() -> 
received.contains("hello.txt"));
-        TimeUnit.MILLISECONDS.sleep(500);
+        await().during(500, TimeUnit.MILLISECONDS).atMost(2, 
TimeUnit.SECONDS).until(() -> received.size() == 1);
 
         assertEquals(List.of("hello.txt"), received);
         assertFalse(listed.contains("secret.txt"), listed.toString());
@@ -246,5 +265,41 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
         assertFalse(FtpUtils.isSubDirectoryName("\\sub"));
         assertFalse(FtpUtils.isSubDirectoryName("a\\b"));
         assertFalse(FtpUtils.isSubDirectoryName(" "));
+        // a trailing / is not part of the name, as some servers list a 
directory that way
+        assertTrue(FtpUtils.isSubDirectoryName("sub/"));
+        assertTrue(FtpUtils.isSubDirectoryName("sub//"));
+        assertFalse(FtpUtils.isSubDirectoryName("/"));
+        assertFalse(FtpUtils.isSubDirectoryName("//"));
+        assertFalse(FtpUtils.isSubDirectoryName("./"));
+        assertFalse(FtpUtils.isSubDirectoryName("../"));
+        assertFalse(FtpUtils.isSubDirectoryName(" /"));
+        assertFalse(FtpUtils.isSubDirectoryName("sub/../"));
+        assertFalse(FtpUtils.isSubDirectoryName("sub\\"));
+        assertFalse(FtpUtils.isSubDirectoryName("sub\\/"));
+    }
+
+    @Test
+    void listedAbsolutePathsInTheListedDirectory() {
+        assertTrue(FtpConsumer.isListedIn("/data/in/sub", "data/in", "sub"));
+        assertTrue(FtpConsumer.isListedIn("/x/data/in/sub", "data/in", "sub"));
+        assertTrue(FtpConsumer.isListedIn("C:/data/in/sub", "data/in", "sub"));
+        // the listed directory starts with the same drive as the listed path
+        assertTrue(FtpConsumer.isListedIn("C:/data/in/sub", "C:/data/in", 
"sub"));
+        assertTrue(FtpConsumer.isListedIn("C:\\data\\in\\sub\\", "C:/data/in", 
"sub"));
+        assertFalse(FtpConsumer.isListedIn("D:/data/in/sub", "C:/data/in", 
"sub"));
+        assertFalse(FtpConsumer.isListedIn("/data/other/sub", "data/in", 
"sub"));
+        // a path whose .. goes above its drive is not an absolute path in the 
listed directory
+        assertFalse(FtpConsumer.isListedIn("C:/../sub", "", "sub"));
+        assertFalse(FtpConsumer.isListedIn("C:/../data/in/sub", "data/in", 
"sub"));
+    }
+
+    @Test
+    void directoryOrParentOf() {
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/home/camel", 
"/home/camel"));
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/home", "/home/camel"));
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/home/", "/home/camel"));
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/", "/home/camel"));
+        assertFalse(FtpUtils.isDirectoryOrParentOf("/home/cam", 
"/home/camel"));
+        assertFalse(FtpUtils.isDirectoryOrParentOf("/home/camel/sub", 
"/home/camel"));
     }
 }
diff --git 
a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpProducer.java
 
b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpProducer.java
index 420c3ab29b91..1fb1d498638a 100644
--- 
a/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpProducer.java
+++ 
b/components/camel-jsch/src/main/java/org/apache/camel/component/scp/ScpProducer.java
@@ -24,4 +24,13 @@ public class ScpProducer extends RemoteFileProducer<ScpFile> 
{
     protected ScpProducer(ScpEndpoint endpoint, RemoteFileOperations<ScpFile> 
operations) {
         super(endpoint, operations);
     }
+
+    /**
+     * The scp operations send the name of the file as given, with / as the 
only separator, so a backslash in it is not
+     * replaced by /.
+     */
+    @Override
+    protected boolean isBackslashNormalized() {
+        return false;
+    }
 }
diff --git 
a/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpProducerBackslashNameTest.java
 
b/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpProducerBackslashNameTest.java
new file mode 100644
index 000000000000..5743f54db55e
--- /dev/null
+++ 
b/components/camel-jsch/src/test/java/org/apache/camel/component/scp/ScpProducerBackslashNameTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.camel.component.scp;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Exchange;
+import org.apache.sshd.scp.server.ScpCommandFactory;
+import org.apache.sshd.server.channel.ChannelSession;
+import org.apache.sshd.server.command.Command;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+/**
+ * The scp producer sends the name of a file as given, with / as the only 
separator. With jailStartingDirectory (the
+ * default) it refuses a name with both a backslash and a .. segment, as where 
such a name goes depends on the server,
+ * and writes a name with a backslash and no .. segment as given.
+ */
+public class ScpProducerBackslashNameTest extends ScpServerTestSupport {
+
+    private final List<String> scpCommands = new CopyOnWriteArrayList<>();
+
+    public ScpProducerBackslashNameTest() {
+        // records the scp commands the server starts
+        serverConfigurer = sshd -> sshd.setCommandFactory(new 
ScpCommandFactory() {
+            @Override
+            public Command createCommand(ChannelSession channel, String 
command) throws IOException {
+                scpCommands.add(command);
+                return super.createCommand(channel, command);
+            }
+        });
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "a\\b/../../hello.txt", "sub\\..\\hello.txt", 
"sub/..\\hello.txt" })
+    public void testNameWithBackslashAndParentSegmentIsRefused(String name) {
+        assumeTrue(isSetupComplete());
+
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.sendBodyAndHeader(getProducerUri(), "Hello 
World", Exchange.FILE_NAME, name));
+
+        IllegalArgumentException cause = 
assertInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertTrue(cause.getMessage().contains("jailed to the starting 
directory"), cause.getMessage());
+        // nothing is sent: the scp command is not started on the server
+        assertEquals(List.of(), scpCommands, "scp commands started on the 
server");
+    }
+
+    @Test
+    @DisabledOnOs(value = OS.WINDOWS, disabledReason = "a Windows file name 
cannot hold a backslash")
+    public void testNameWithBackslashIsWrittenAsGiven() throws Exception {
+        assumeTrue(isSetupComplete());
+
+        template.sendBodyAndHeader(getProducerUri(), "Hello World", 
Exchange.FILE_NAME, "a\\b.txt");
+
+        assertEquals("Hello World", Files.readString(Paths.get(getScpPath(), 
"a\\b.txt")));
+    }
+
+    private String getProducerUri() {
+        return getScpUri() + "?username=admin&password=admin&knownHostsFile=" 
+ getKnownHostsFile();
+    }
+}
diff --git 
a/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
 
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
index 26983a0c4303..fa07b6237475 100644
--- 
a/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
+++ 
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
@@ -49,6 +49,7 @@ import org.apache.camel.component.file.GenericFileHelper;
 import org.apache.camel.component.file.GenericFileListingOperations;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.component.file.remote.FtpConstants;
+import org.apache.camel.component.file.remote.FtpUtils;
 import org.apache.camel.component.file.remote.RemoteFile;
 import org.apache.camel.component.file.remote.RemoteFileConfiguration;
 import org.apache.camel.component.file.remote.RemoteFileOperations;
@@ -1154,7 +1155,7 @@ public class MinaSftpOperations
                 return;
             }
             String current = getCurrentDirectory();
-            if (isDirectoryOrParentOf(path, current)) {
+            if (FtpUtils.isDirectoryOrParentOf(path, current)) {
                 // extract the path segment relative to the target path and 
make
                 // sure it keeps the preceding '/' for the regex op
                 String p = current.substring(path.length() - 
(path.endsWith("/") ? 1 : 0));
@@ -1201,14 +1202,6 @@ public class MinaSftpOperations
         }
     }
 
-    /**
-     * Whether the path is the directory itself or one of its parent 
directories. A path that only starts with the same
-     * characters, such as /home/cam for /home/camel, is not.
-     */
-    private static boolean isDirectoryOrParentOf(String path, String 
directory) {
-        return directory.equals(path) || 
directory.startsWith(path.endsWith("/") ? path : path + "/");
-    }
-
     private void doChangeDirectory(String path) {
         if (path == null || ".".equals(path) || ObjectHelper.isEmpty(path)) {
             return;
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc
index bf9b5b1338eb..8868f5cbb692 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_18.adoc
@@ -11,6 +11,37 @@ Note that manual migration is still required.
 See the xref:camel-upgrade-recipes-tool.adoc[documentation] page for details.
 ====
 
+== Upgrading from 4.18.6 to 4.18.7
+
+=== camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - the starting 
directory check resolves a name as a server does
+
+The check that a file is in the starting directory 
(`jailStartingDirectory=true`, the default, for a producer, and the
+same check of a consumer) now resolves the `.` and `..` segments of a name the 
way a server does, and refuses a name
+with a `..` segment that goes above the start of the name, also when the 
endpoint has no directory or its directory is
+the root (`/`). A name that starts like a scheme or a drive, such as `http:`, 
`file:` or `C:`, is resolved like any
+other name. For example, a producer without a directory now refuses 
`/../report.txt`, which a server wrote as
+`/report.txt`. The exchange fails with an `IllegalArgumentException` stating 
that the filename is jailed to the starting
+directory. Remove the `..` segments from such a name.
+
+=== camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - a file name 
with both a backslash and a `..` segment is refused in some configurations
+
+With `jailStartingDirectory=true` (the default), a producer now refuses a file 
name that has both a backslash and a
+`..` segment, such as `sub\..\report.txt`, when the `separator` option is 
`Windows` or `Auto`, when the producer
+writes a temp file (`tempPrefix` or `tempFileName`) or a done file 
(`doneFileName`), or when `checksumFileAlgorithm` is
+set (with `checksumWriteFile` at its default, `true`). Where such a name goes 
depends on the server and on the platform
+that Camel runs on. The exchange fails with an `IllegalArgumentException` 
stating that the filename is jailed to the
+starting directory. With the default options, the producer replaces each 
backslash by `/` and writes such a name as
+before. Remove the backslashes or the `..` segments from the name.
+
+=== camel-ftp, camel-mina-sftp - a stepwise recursive poll skips a listed 
directory whose name is not one name
+
+A recursive consumer with `stepwise=true` (the default) changes into each sub 
directory by its name, and back to its
+parent afterwards. It now skips a directory from the listing of the server 
whose name is blank, `.` or `..`, or has a
+`/` or a `\` (other than a trailing `/`), and logs it at DEBUG level, as such 
a name would leave the poll in another
+directory than the one it lists. With 
`handleDirectoryParserAbsoluteResult=true` (ftp and ftps), the last name of the
+path that the server lists is checked. A backslash is a valid character in the 
name of a directory on a server on
+Linux: to poll such a directory, set `stepwise=false`.
+
 == Upgrading from 4.18.5 to 4.18.6
 
 === camel-aws2-sns - string array message attributes
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
index 752d5a1fa3c9..068ec12e2d33 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
@@ -26,6 +26,35 @@ A URL whose path is not the endpoint of the server, such as 
`http://host:8080/v1
 because its path was ignored, and now fails: set the URL of the endpoint, here 
`http://host:8080/mcp`, or remove the
 path. The URL must be absolute, with a scheme and a host.
 
+=== camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - the starting 
directory check resolves a name as a server does
+
+The check that a file is in the starting directory 
(`jailStartingDirectory=true`, the default, for a producer, and the
+same check of a consumer) now resolves the `.` and `..` segments of a name the 
way a server does, and refuses a name
+with a `..` segment that goes above the start of the name, also when the 
endpoint has no directory or its directory is
+the root (`/`). A name that starts like a scheme or a drive, such as `http:`, 
`file:` or `C:`, is resolved like any
+other name. For example, a producer without a directory now refuses 
`/../report.txt`, which a server wrote as
+`/report.txt`. The exchange fails with an `IllegalArgumentException` stating 
that the filename is jailed to the starting
+directory. Remove the `..` segments from such a name.
+
+=== camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - a file name 
with both a backslash and a `..` segment is refused in some configurations
+
+With `jailStartingDirectory=true` (the default), a producer now refuses a file 
name that has both a backslash and a
+`..` segment, such as `sub\..\report.txt`, when the `separator` option is 
`Windows` or `Auto`, when the producer
+writes a temp file (`tempPrefix` or `tempFileName`) or a done file 
(`doneFileName`), or when `checksumFileAlgorithm` is
+set (with `checksumWriteFile` at its default, `true`). Where such a name goes 
depends on the server and on the platform
+that Camel runs on. The exchange fails with an `IllegalArgumentException` 
stating that the filename is jailed to the
+starting directory. With the default options, the producer replaces each 
backslash by `/` and writes such a name as
+before. Remove the backslashes or the `..` segments from the name.
+
+=== camel-ftp, camel-mina-sftp - a stepwise recursive poll skips a listed 
directory whose name is not one name
+
+A recursive consumer with `stepwise=true` (the default) changes into each sub 
directory by its name, and back to its
+parent afterwards. It now skips a directory from the listing of the server 
whose name is blank, `.` or `..`, or has a
+`/` or a `\` (other than a trailing `/`), and logs it at DEBUG level, as such 
a name would leave the poll in another
+directory than the one it lists. With 
`handleDirectoryParserAbsoluteResult=true` (ftp and ftps), the last name of the
+path that the server lists is checked. A backslash is a valid character in the 
name of a directory on a server on
+Linux: to poll such a directory, set `stepwise=false`.
+
 == Upgrading from 4.22.2 to 4.22.3
 
 === Undertow servlet implementation
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index fc5381c5fe67..561844eebbcc 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -564,7 +564,7 @@ current directory between two uploads is the directory of 
the last file, not the
 example the one set with the `ftpClient` option) and sends commands with 
relative names between two operations of Camel
 must change directory itself.
 
-=== camel-ftp, camel-sftp, camel-mina-sftp - an absolute file name is refused 
when the endpoint has no directory
+=== camel-ftp, camel-mina-sftp, camel-jsch - an absolute file name is refused 
when the endpoint has no directory
 
 With the default `jailStartingDirectory=true`, a producer whose endpoint has 
no directory (for example
 `+sftp://user@host+`, which uploads into the home directory of the login) now 
refuses an absolute file name: a name
@@ -574,7 +574,8 @@ absolute (on other servers such a name was written as a 
relative name). The exch
 not refused.
 
 When the endpoint has a directory, an absolute name is relative to it, as 
before. To write files at absolute paths
-from an endpoint without a directory, set `jailStartingDirectory=false` on the 
endpoint.
+from an endpoint without a directory, set `jailStartingDirectory=false` on the 
endpoint. This also applies to the `scp`
+producer, which wrote a name starting with `/` relative to the home directory: 
use a relative name instead.
 
 === camel-ftp - handleDirectoryParserAbsoluteResult: a recursive poll changes 
into a listed directory by its name
 
@@ -586,6 +587,36 @@ their path was built from the absolute path of the sub 
directory. A listed sub d
 hidden directory (whose name starts with a dot) is skipped, as with other 
servers. Listed files are matched as listed,
 as before.
 
+=== camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - the starting 
directory check resolves a name as a server does
+
+The check that a file is in the starting directory 
(`jailStartingDirectory=true`, the default, for a producer, and the
+same check of a consumer) now resolves the `.` and `..` segments of a name the 
way a server does, and refuses a name
+with a `..` segment that goes above the start of the name, also when the 
endpoint has no directory or its directory is
+the root (`/`). A name that starts like a scheme or a drive, such as `http:`, 
`file:` or `C:`, is resolved like any
+other name. The exchange fails with an `IllegalArgumentException` stating that 
the filename is jailed to the starting
+directory. Remove the `..` segments from such a name.
+
+=== camel-ftp, camel-mina-sftp, camel-jsch, camel-azure-files - a file name 
with both a backslash and a `..` segment is refused in some configurations
+
+With `jailStartingDirectory=true` (the default), a producer now refuses a file 
name that has both a backslash and a
+`..` segment, such as `sub\..\report.txt`, when the `separator` option is 
`Windows` or `Auto`, when the producer
+writes a temp file (`tempPrefix` or `tempFileName`) or a done file 
(`doneFileName`), or when `checksumFileAlgorithm` is
+set (with `checksumWriteFile` at its default, `true`). Where such a name goes 
depends on the server and on the platform
+that Camel runs on. The exchange fails with an `IllegalArgumentException` 
stating that the filename is jailed to the
+starting directory. With the default options, the producer replaces each 
backslash by `/` and writes such a name as
+before. Remove the backslashes or the `..` segments from the name.
+
+The `scp` producer (camel-jsch) sends a name as given, so it refuses such a 
name whatever its options.
+
+=== camel-ftp, camel-mina-sftp - a stepwise recursive poll skips a listed 
directory whose name is not one name
+
+A recursive consumer with `stepwise=true` (the default) changes into each sub 
directory by its name, and back to its
+parent afterwards. It now skips a directory from the listing of the server 
whose name is blank, `.` or `..`, or has a
+`/` or a `\` (other than a trailing `/`), and logs it at DEBUG level, as such 
a name would leave the poll in another
+directory than the one it lists. A backslash is a valid character in the name 
of a directory on a server on Linux: to
+poll such a directory, set `stepwise=false`. With 
`handleDirectoryParserAbsoluteResult=true`, a backslash always
+separates names, so such a directory cannot be polled.
+
 === camel-ai-resource - fixed resourceUri cannot contain braces
 
 Starting in Camel 4.24, `ai-resource` rejects a fixed `resourceUri` containing 
`{` or `}` at route startup. The MCP SDK interprets those characters as 
URI-template expressions. Routes that intend variable path segments must use 
`uriTemplate` instead, for example 
`+ai-resource:customer?uriTemplate=camel:///customers/{id}+`. If the braces are 
literal URI characters, percent-encode them as `%7B` and `%7D`.

Reply via email to