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 e46d77bcca6c64063decf5a3591cbd81fc0ac711 Author: croway <[email protected]> AuthorDate: Mon Nov 23 19:24:14 2026 +0100 CAMEL-27453: camel-ftp - jailStartingDirectory: refuse a name with both a backslash and a .. segment unless separator is UNIX A backslash is a path separator to some servers and part of a name to others. With the default separator option (UNIX), the operations replace a backslash in the name of the file they store by / before they use it, so it separates segments as the starting directory check takes it. With separator=Auto or Windows, or when the producer writes a temp file, a done file or a checksum file, whose names are checked, deleted, renamed or written as given, the FTP client compacts the directory of a stepwise operation on the file separator of the platform Camel runs on only, so where a name with both a backslash and a .. segment goes depends on the server and that platform, and the jailStartingDirectory check of the remote file producers refuses it. A remote file consumer replaces the backslashes of a listed path by / before it uses the path, whatever the options. --- .../camel/component/file/GenericFileHelper.java | 17 +++++++++++- .../camel/component/file/GenericFileProducer.java | 21 +++++++++++++-- .../component/file/GenericFileHelperTest.java | 22 +++++++++++++++ .../component/file/remote/RemoteFileProducer.java | 13 +++++++++ ...emoteFileConsumerStartingDirectoryJailTest.java | 5 ++++ ...emoteFileProducerStartingDirectoryJailTest.java | 31 ++++++++++++++++++++-- ...roducerJailStartingDirectoryResolvedNameIT.java | 27 ++++++++++++++++--- 7 files changed, 128 insertions(+), 8 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 975c84197dc0..a8e1e695e1ff 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 @@ -236,7 +236,9 @@ public final class GenericFileHelper { /** * Determines whether a path on a remote server stays within a directory on that server, such as a path built from a * name in the listing of the server. Both paths are compacted with - * {@link #compactPathForContainment(String, char)}, the way the server resolves them. + * {@link #compactPathForContainment(String, char)}, the way the server resolves them. A backslash separates + * segments, as / does: see {@link #isAmbiguousRemotePath(String)} for a path that is used without replacing its + * backslashes. * * @param target the path, not compacted * @param directory the directory the path must stay within, not compacted, or empty or {@code null} when no @@ -252,6 +254,19 @@ public final class GenericFileHelper { '/'); } + /** + * Whether a remote path has both a backslash and a {@code ..} segment. A backslash is a path separator to some + * servers (such as a server on Windows) and part of a name to others, and the FTP client compacts the directory of + * a stepwise operation on the file separator of the platform Camel runs on only, so where such a path goes depends + * 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 + */ + public static boolean isAmbiguousRemotePath(String path) { + return path != null && path.indexOf('\\') != -1 && hasParentSegment(path); + } + /** * Compacts a path for {@link #isWithinDirectory(String, String, char)} the way a file system or a server resolves * it: segment by segment, dropping empty and {@code .} segments, and dropping a {@code ..} segment together with diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java index 020254de8e69..8e1cf3092c42 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java @@ -571,13 +571,22 @@ public class GenericFileProducer<T> extends DefaultAsyncProducer { return value; } - private static void jailedCheck(String answer, String baseDir) { + private void jailedCheck(String answer, String baseDir) { // check for file must be within starting directory (need to compact // first as the name can be using relative paths via ../ etc), the way // the file system or the remote server resolves the path String compatchAnswer = GenericFileHelper.compactPathForContainment(answer, File.separatorChar); String compatchBaseDir = GenericFileHelper.compactPathForContainment(baseDir, File.separatorChar); - if (!GenericFileHelper.isWithinDirectory(compatchAnswer, compatchBaseDir)) { + boolean within; + if (endpoint.getConfiguration().needToNormalize()) { + within = GenericFileHelper.isWithinDirectory(compatchAnswer, compatchBaseDir); + } else { + // the path is used on a remote server, which may take a backslash in it as part of a name, unless the + // backslashes are replaced by / before the path is used + within = GenericFileHelper.isWithinRemoteDirectory(answer, baseDir) + && (isBackslashNormalized() || !GenericFileHelper.isAmbiguousRemotePath(answer)); + } + if (!within) { throw new IllegalArgumentException( "Cannot write file with name: " + compatchAnswer + " as the filename is jailed to the starting directory: " @@ -585,6 +594,14 @@ public class GenericFileProducer<T> extends DefaultAsyncProducer { } } + /** + * Whether a backslash in the name of a file to write is replaced by / before the name is used, so that it separates + * segments as / does. Only consulted for an endpoint whose names are not normalized for the platform Camel runs on. + */ + protected boolean isBackslashNormalized() { + return false; + } + private String evaluateName(Exchange exchange, Expression expression, Object value) { String name; if (expression != null) { diff --git a/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileHelperTest.java b/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileHelperTest.java index 2a1304aadd10..4ce77a159835 100644 --- a/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileHelperTest.java +++ b/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileHelperTest.java @@ -216,6 +216,28 @@ public class GenericFileHelperTest { assertFalse(GenericFileHelper.isWithinRemoteDirectory(null, "poll")); } + @Test + public void remotePathWithBackslashAndParentSegmentIsAmbiguous() { + // the FTP client on Linux compacts a path on / only, so a\b is one segment that one .. removes, and a server + // may take a\b as one name too, but a server on Windows takes it as two segments + assertTrue(GenericFileHelper.isAmbiguousRemotePath("poll/a\\b/../../secret.txt")); + assertTrue(GenericFileHelper.isAmbiguousRemotePath("a\\b/../../secret.txt")); + // the FTP client on Windows compacts a path on \ only, so poll/a/b is one segment that one .. removes + assertTrue(GenericFileHelper.isAmbiguousRemotePath("poll/a/b\\..\\secret.txt")); + assertTrue(GenericFileHelper.isAmbiguousRemotePath("sub\\..\\x.txt")); + // a backslash without a .. segment, and a .. segment without a backslash + assertFalse(GenericFileHelper.isAmbiguousRemotePath("poll/a\\b.txt")); + assertFalse(GenericFileHelper.isAmbiguousRemotePath("poll/a\\b/c.txt")); + assertFalse(GenericFileHelper.isAmbiguousRemotePath("poll/a/../b.txt")); + assertFalse(GenericFileHelper.isAmbiguousRemotePath("poll/a\\b..c.txt")); + assertFalse(GenericFileHelper.isAmbiguousRemotePath(null)); + + // a backslash separates segments, as / does, when the backslashes are replaced by / before the path is used + assertTrue(GenericFileHelper.isWithinRemoteDirectory("poll/a\\b/../../poll/secret.txt", "poll")); + assertTrue(GenericFileHelper.isWithinRemoteDirectory("poll/sub\\..\\x.txt", "poll")); + assertFalse(GenericFileHelper.isWithinRemoteDirectory("poll/a\\..\\..\\b.txt", "poll")); + } + @Test public void shouldEncodeStreamBodyWithCharsetWhileReading() throws Exception { // longer than the internal character buffer, with characters that are encoded differently in the charsets 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 4a19f74d0a5b..457c1cf8d4a7 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 @@ -53,6 +53,19 @@ public class RemoteFileProducer<T> extends GenericFileProducer<T> { return name; } + /** + * 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. + */ + @Override + protected boolean isBackslashNormalized() { + RemoteFileEndpoint<T> endpoint = getEndpoint(); + return endpoint.getConfiguration().getSeparator() == RemoteFileConfiguration.PathSeparator.UNIX + && endpoint.getTempFileName() == null && endpoint.getDoneFileName() == null + && !(endpoint.isChecksumWriteFile() && endpoint.getChecksumFileAlgorithm() != null); + } + @Override protected void doProcess(Exchange exchange) throws Exception { // store any existing file header which we want to keep and propagate diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerStartingDirectoryJailTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerStartingDirectoryJailTest.java index ccd24beb4083..2427ed44f195 100644 --- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerStartingDirectoryJailTest.java +++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileConsumerStartingDirectoryJailTest.java @@ -108,6 +108,9 @@ class RemoteFileConsumerStartingDirectoryJailTest extends CamelTestSupport { assertFalse(consumer.isWithinStartingDirectory("classpath:/../../other/secret.txt"), uri); assertFalse(consumer.isWithinStartingDirectory("a:/../../other/secret.txt"), uri); assertFalse(consumer.isWithinStartingDirectory("http:\\..\\..\\other\\secret.txt"), uri); + // a listed path is used with its backslashes replaced by /, so a backslash separates segments + assertTrue(consumer.isWithinStartingDirectory("a\\b/../../secret.txt"), uri); + assertFalse(consumer.isWithinStartingDirectory("a\\..\\..\\secret.txt"), uri); // names that stay inside assertTrue(consumer.isWithinStartingDirectory("http:/secret.txt"), uri); @@ -122,6 +125,8 @@ class RemoteFileConsumerStartingDirectoryJailTest extends CamelTestSupport { assertFalse(consumer.isWithinStartingDirectory("poll/http:/../../secret.txt")); assertFalse(consumer.isWithinStartingDirectory("poll/a:/../../secret.txt")); + assertTrue(consumer.isWithinStartingDirectory("poll/a\\b/../../secret.txt")); + assertFalse(consumer.isWithinStartingDirectory("poll/a\\b/../../../secret.txt")); assertTrue(consumer.isWithinStartingDirectory("poll/http:/secret.txt")); } diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProducerStartingDirectoryJailTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProducerStartingDirectoryJailTest.java index fb4fb3f75e3b..2f43054bac2d 100644 --- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProducerStartingDirectoryJailTest.java +++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProducerStartingDirectoryJailTest.java @@ -28,8 +28,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** * The name of the file to upload is resolved by the server, which gives no special meaning to a scheme or a drive at - * the start of it. Verifies that a name that the server resolves outside the starting directory is refused with - * jailStartingDirectory (the default), whether a directory is configured or not. + * the start of it, and may take a backslash in it as part of a name unless the separator option replaces it by /. + * Verifies that a name that the server resolves outside the starting directory is refused with jailStartingDirectory + * (the default), whether a directory is configured or not. */ class RemoteFileProducerStartingDirectoryJailTest extends CamelTestSupport { @@ -67,6 +68,32 @@ class RemoteFileProducerStartingDirectoryJailTest extends CamelTestSupport { assertRefused("ftp://hostname/upload", "a:/../../planted.txt"); } + @Test + void shouldRefuseNamesWithBackslashAndParentSegmentUnlessSeparatorIsUnix() throws Exception { + // the separator option UNIX (the default) replaces a backslash by / before the name is used + assertEquals("upload/sub\\..\\x.txt", createFileName("ftp://hostname/upload", "sub\\..\\x.txt")); + assertEquals("upload/sub\\..\\x.txt", createFileName("sftp://hostname/upload?separator=UNIX", "sub\\..\\x.txt")); + assertEquals("upload/a\\b/../c.txt", createFileName("ftp://hostname/upload", "a\\b/../c.txt")); + // otherwise the FTP client compacts a\b as one segment on Linux, and a/b as one segment on Windows + for (String uri : new String[] { + "ftp://hostname/upload?separator=Auto", "ftp://hostname/upload?separator=Windows", + "sftp://hostname/upload?separator=Auto" }) { + assertRefused(uri, "sub\\..\\x.txt"); + assertRefused(uri, "a\\b/../../planted.txt"); + assertRefused(uri, "a/b\\..\\..\\planted.txt"); + // a backslash without a .. segment + assertEquals("upload/a\\b.txt", createFileName(uri, "a\\b.txt")); + } + // with the separator option UNIX, but a temp file, a done file or a checksum file is checked, deleted, renamed + // or written with the name as given + for (String uri : new String[] { + "ftp://hostname/upload?tempPrefix=.uploading", "ftp://hostname/upload?doneFileName=${file:name}.done", + "ftp://hostname/upload?checksumFileAlgorithm=md5", "sftp://hostname/upload?tempFileName=${file:name}.tmp" }) { + assertRefused(uri, "sub\\..\\x.txt"); + assertEquals("upload/a\\b.txt", createFileName(uri, "a\\b.txt")); + } + } + @Test void shouldAcceptNamesStayingInside() throws Exception { assertEquals("hello.txt", createFileName("ftp://hostname", "hello.txt")); diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpProducerJailStartingDirectoryResolvedNameIT.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpProducerJailStartingDirectoryResolvedNameIT.java index 525d15b4763d..8e57a68cf3df 100644 --- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpProducerJailStartingDirectoryResolvedNameIT.java +++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpProducerJailStartingDirectoryResolvedNameIT.java @@ -27,8 +27,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** * The FTP client compacts the directory of a stepwise upload before it changes into each of its segments, and gives no - * special meaning to a scheme or a drive at the start of it. Verifies that a name that the upload would resolve outside - * the starting directory is refused. + * special meaning to a scheme or a drive at the start of it, or to a backslash where the file separator is /. Verifies + * that a name that the upload would resolve outside the starting directory is refused. */ public class FtpProducerJailStartingDirectoryResolvedNameIT extends FtpServerTestSupport { @@ -37,8 +37,12 @@ public class FtpProducerJailStartingDirectoryResolvedNameIT extends FtpServerTes } private void assertRefused(String directory, String name) { + assertRefusedAt(ftpUrl(directory), name); + } + + private void assertRefusedAt(String uri, String name) { CamelExecutionException e = assertThrows(CamelExecutionException.class, - () -> template.sendBodyAndHeader(ftpUrl(directory), "Hello World", Exchange.FILE_NAME, name)); + () -> template.sendBodyAndHeader(uri, "Hello World", Exchange.FILE_NAME, name)); IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); assertTrue(iae.getMessage().contains("as the filename is jailed to the starting directory"), iae.getMessage()); } @@ -51,6 +55,23 @@ public class FtpProducerJailStartingDirectoryResolvedNameIT extends FtpServerTes assertFalse(service.ftpFile("planted.txt").toFile().exists()); } + @Test + public void testBackslashNameEscapingTheStartingDirectoryIsRefused() { + // separator=Auto uploads the name as it is, and the FTP client compacts a\b as one segment where the file + // separator is /, so the two .. would leave upload/jail + assertRefusedAt(ftpUrl("upload/jail") + "&separator=Auto", "a\\b/../../planted.txt"); + + assertFalse(service.ftpFile("upload/planted.txt").toFile().exists()); + } + + @Test + public void testBackslashNameIsNormalizedWithDefaultSeparator() { + // the default separator=UNIX replaces the backslashes by / before the upload + template.sendBodyAndHeader(ftpUrl("upload/jail"), "Hello World", Exchange.FILE_NAME, "sub\\..\\x.txt"); + + assertTrue(service.ftpFile("upload/jail/x.txt").toFile().exists()); + } + @Test public void testNameInsideTheStartingDirectoryIsWritten() { template.sendBodyAndHeader(ftpUrl("upload/jail"), "Hello World", Exchange.FILE_NAME, "sub/../hello.txt");
