You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2026/09/05 13:25:46 UTC

(camel) branch camel-4.18.x updated: [backport camel-4.18.x] CAMEL-24627: resolve symlinks when containing local work directory downloads (#26139)

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

gnodet pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.18.x by this push:
     new 3f9b409d581a [backport camel-4.18.x] CAMEL-24627: resolve symlinks when containing local work directory downloads (#26139)
3f9b409d581a is described below

commit 3f9b409d581a7a7cb94fcf34d95396eaa9c67ad7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Sep 5 15:25:39 2026 +0200

    [backport camel-4.18.x] CAMEL-24627: resolve symlinks when containing local work directory downloads (#26139)
    
    CAMEL-24627: resolve symlinks when containing local work directory downloads (#26121)
    
    GenericFileHelper.jailToLocalWorkDirectory performed a lexical path-boundary
    check only. It now also resolves the existing filesystem path segments
    (following symbolic links) before checking that the destination stays inside
    the configured localWorkDirectory, so a symbolic link inside the work directory
    that resolves outside of it cannot be used to write files elsewhere. This brings
    the file-based components (camel-file, camel-ftp/sftp, camel-smb, camel-mina-sftp,
    camel-azure-files) to parity with the cloud storage hardening done in
    CAMEL-24548 / CAMEL-24549. Valid nested download paths continue to work unchanged.
    
    
    
    (cherry picked from commit 4df6808daf98837cd202d4c4b23759dffb02b3b9)
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
    Co-authored-by: Andrea Cosentino <an...@gmail.com>
    Co-authored-by: Claude Opus 4.8 <no...@anthropic.com>
---
 .../camel/component/file/GenericFileHelper.java    | 41 ++++++++++++++++++++++
 .../component/file/GenericFileHelperTest.java      | 29 +++++++++++++++
 2 files changed, 70 insertions(+)

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 ff3957e296e5..278a93c88441 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
@@ -17,6 +17,10 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
 import java.util.function.Supplier;
 
 import org.apache.camel.Exchange;
@@ -47,6 +51,43 @@ public final class GenericFileHelper {
                                                           + " as it is jailed to the local work directory: "
                                                           + compactWork);
         }
+        // defense-in-depth: the lexical check above cannot see symbolic links, so also resolve the existing
+        // filesystem path segments (following links) and re-check. This prevents a symbolic link inside the
+        // local work directory that resolves outside of it from being used to write files elsewhere. Valid
+        // nested paths continue to work unchanged. Skipped when no work directory boundary is configured.
+        if (!compactWork.isEmpty()) {
+            try {
+                Path resolvedWork = resolveExistingPathSegments(localWorkDirectory.toPath());
+                Path resolvedTarget = resolveExistingPathSegments(target.toPath());
+                if (!resolvedTarget.startsWith(resolvedWork)) {
+                    throw new GenericFileOperationFailedException(
+                            "Cannot retrieve file to local work file: " + compactTarget
+                                                                  + " as it is jailed to the local work directory: "
+                                                                  + compactWork);
+                }
+            } catch (IOException e) {
+                throw new GenericFileOperationFailedException(
+                        "Cannot verify local work file: " + compactTarget
+                                                              + " is within the local work directory: " + compactWork,
+                        e);
+            }
+        }
+    }
+
+    private static Path resolveExistingPathSegments(Path path) throws IOException {
+        // Preserve the raw path segments here. Normalizing before resolving links changes the filesystem meaning of
+        // paths such as link/../file when link points to another directory.
+        final Path absolutePath = path.toAbsolutePath();
+        Path existingPath = absolutePath;
+        while (existingPath != null && !Files.exists(existingPath, LinkOption.NOFOLLOW_LINKS)) {
+            existingPath = existingPath.getParent();
+        }
+        if (existingPath == null) {
+            throw new IOException("No existing ancestor found for " + path);
+        }
+
+        final Path resolvedExistingPath = existingPath.toRealPath();
+        return resolvedExistingPath.resolve(existingPath.relativize(absolutePath)).normalize();
     }
 
     /**
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 2b6c29d10b79..65bbb25d30ed 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
@@ -17,8 +17,13 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
+import org.junit.jupiter.api.Assumptions;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -51,6 +56,30 @@ public class GenericFileHelperTest {
                 () -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "../localworkEVIL/file.txt"), workDir));
     }
 
+    @Test
+    public void shouldRejectSymlinkEscapingLocalWorkDirectory(@TempDir Path tmp) throws IOException {
+        Path work = Files.createDirectories(tmp.resolve("work"));
+        Path outside = Files.createDirectories(tmp.resolve("outside"));
+
+        // a symbolic link inside the work directory that points outside of it
+        Path link = work.resolve("link");
+        try {
+            Files.createSymbolicLink(link, outside);
+        } catch (UnsupportedOperationException | IOException e) {
+            Assumptions.abort("Symbolic links are not supported on this platform: " + e.getMessage());
+        }
+
+        // a file written through the symlink resolves outside the work directory and must be rejected, even though
+        // it passes a lexical-only containment check
+        File escaping = new File(link.toFile(), "evil.txt");
+        assertThrows(GenericFileOperationFailedException.class,
+                () -> GenericFileHelper.jailToLocalWorkDirectory(escaping, work.toFile()));
+
+        // a legitimate file within a real (not-yet-existing) subdirectory of the work directory is still allowed
+        File legit = new File(work.toFile(), "sub/ok.txt");
+        assertDoesNotThrow(() -> GenericFileHelper.jailToLocalWorkDirectory(legit, work.toFile()));
+    }
+
     @Test
     public void isWithinDirectoryRespectsPathBoundaries() {
         String sep = File.separator;