Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileExist;
import org.apache.camel.component.file.GenericFileHelper;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.component.file.remote.RemoteFile;
import org.apache.camel.component.file.remote.RemoteFileConfiguration;
Expand Down Expand Up @@ -302,9 +303,16 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc
"Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
String relativeName = target.getRelativeFilePath();

File localWorkDir = local;
inProgress = new File(local, relativeName + ".inprogress");
local = new File(local, relativeName);

// ensure the local work file stays within the local work directory (CAMEL-23765)
if (endpoint.isJailStartingDirectory()) {
GenericFileHelper.jailToLocalWorkDirectory(inProgress, localWorkDir);
GenericFileHelper.jailToLocalWorkDirectory(local, localWorkDir);
}

// create directory to local work file
boolean result = local.mkdirs();
if (!result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,39 @@
*/
package org.apache.camel.component.file;

import java.io.File;
import java.util.function.Supplier;

import org.apache.camel.Exchange;
import org.apache.camel.support.MessageHelper;
import org.apache.camel.util.FileUtil;

public final class GenericFileHelper {

private GenericFileHelper() {
}

/**
* Ensures the resolved local work file stays within the configured local work directory. The remote file name used
* to build the local work file path may contain {@code ../} sequences that would otherwise resolve to a path
* outside the work directory.
*
* @param target the resolved local work file (or its in-progress temp file)
Comment thread
davsclaus marked this conversation as resolved.
* @param localWorkDirectory the local work directory the file must stay within
* @throws GenericFileOperationFailedException if the target resolves outside the local work directory
*/
public static void jailToLocalWorkDirectory(File target, File localWorkDirectory) {
// compact first as the remote relative name can use ../ etc
String compactTarget = FileUtil.compactPath(target.getPath());
String compactWork = FileUtil.compactPath(localWorkDirectory.getPath());
if (!compactTarget.startsWith(compactWork)) {
throw new GenericFileOperationFailedException(
"Cannot retrieve file to local work file: " + compactTarget
+ " as it is jailed to the local work directory: "
+ compactWork);
}
}

public static String asExclusiveReadLockKey(GenericFile file, String key) {
// use the copy from absolute path as that was the original path of the
// file when the lock was acquired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.file;

import java.io.File;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class GenericFileHelperTest {

private final File workDir = new File("target/localwork");

@Test
public void shouldAllowFilesWithinLocalWorkDirectory() {
// a plain name, a nested name, and a ../ that still resolves within the work directory are all allowed
assertDoesNotThrow(() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "file.txt"), workDir));
assertDoesNotThrow(() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "sub/dir/file.txt"), workDir));
assertDoesNotThrow(() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "sub/../file.txt"), workDir));
}

@Test
public void shouldRejectFilesEscapingLocalWorkDirectory() {
// a remote file name that resolves outside the configured local work directory must be rejected
assertThrows(GenericFileOperationFailedException.class,
() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "../escape.txt"), workDir));
assertThrows(GenericFileOperationFailedException.class,
() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "../../etc/passwd"), workDir));
assertThrows(GenericFileOperationFailedException.class,
() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "sub/../../escape.txt"), workDir));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileExist;
import org.apache.camel.component.file.GenericFileHelper;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.support.ObjectHelper;
import org.apache.camel.support.task.BlockingTask;
Expand Down Expand Up @@ -525,9 +526,16 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc
"Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
String relativeName = target.getRelativeFilePath();

File localWorkDir = local;
temp = new File(local, relativeName + ".inprogress");
local = new File(local, relativeName);

// ensure the local work file stays within the local work directory (CAMEL-23765)
if (endpoint.isJailStartingDirectory()) {
GenericFileHelper.jailToLocalWorkDirectory(temp, localWorkDir);
GenericFileHelper.jailToLocalWorkDirectory(local, localWorkDir);
}

// create directory to local work file
boolean result = local.mkdirs();
if (!result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileExist;
import org.apache.camel.component.file.GenericFileHelper;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.spi.CamelLogger;
import org.apache.camel.support.ResourceHelper;
Expand Down Expand Up @@ -1037,9 +1038,16 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc
// use relative filename in local work directory
String relativeName = file.getRelativeFilePath();

File localWorkDir = local;
temp = new File(local, relativeName + ".inprogress");
local = new File(local, relativeName);

// ensure the local work file stays within the local work directory (CAMEL-23765)
if (endpoint.isJailStartingDirectory()) {
GenericFileHelper.jailToLocalWorkDirectory(temp, localWorkDir);
GenericFileHelper.jailToLocalWorkDirectory(local, localWorkDir);
}

// create directory to local work file
local.mkdirs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileExist;
import org.apache.camel.component.file.GenericFileHelper;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.component.file.remote.FtpConstants;
import org.apache.camel.component.file.remote.RemoteFile;
Expand Down Expand Up @@ -1225,8 +1226,15 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc

try {
String relativeName = file.getRelativeFilePath();
File localWorkDir = local;
temp = new File(local, relativeName + ".inprogress");
local = new File(local, relativeName);

// ensure the local work file stays within the local work directory (CAMEL-23765)
if (endpoint.isJailStartingDirectory()) {
GenericFileHelper.jailToLocalWorkDirectory(temp, localWorkDir);
GenericFileHelper.jailToLocalWorkDirectory(local, localWorkDir);
}
local.mkdirs();

if (temp.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileExist;
import org.apache.camel.component.file.GenericFileHelper;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.IOHelper;
Expand Down Expand Up @@ -337,12 +338,19 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc
// use relative filename in local work directory
String relativeName = file.getRelativeFilePath();

java.io.File localWorkDir = local;
temp = new java.io.File(local, relativeName + ".inprogress");

// create directory to local work file
local.mkdirs();
local = new java.io.File(local, relativeName);

// ensure the local work file stays within the local work directory (CAMEL-23765)
if (endpoint.isJailStartingDirectory()) {
Comment thread
davsclaus marked this conversation as resolved.
GenericFileHelper.jailToLocalWorkDirectory(temp, localWorkDir);
GenericFileHelper.jailToLocalWorkDirectory(local, localWorkDir);
}

// delete any existing files
if (temp.exists()) {
if (!FileUtil.deleteFile(temp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,15 @@ its signature because no `validateSigningCertificateChain` is configured. A new
unverifiable signed messages with an `insufficient-message-security` error instead of delivering them
unverified. The default behaviour is otherwise unchanged.

=== camel-ftp, camel-sftp, camel-mina-sftp, camel-azure-files, camel-smb

When `localWorkDirectory` is used, the remote-file consumers now ensure the downloaded local work file
stays within the configured work directory, so a remote file name containing `../` sequences can no longer
resolve to a path outside it. The containment check honours the existing `jailStartingDirectory` option
(default `true`), consistent with the file producer; set `jailStartingDirectory=false` to disable it. A
remote file that resolves outside the local work directory is now rejected with a
`GenericFileOperationFailedException`.

=== camel-oauth

`OAuthTokenRequest.refreshTokenGrant(...)` now sends the RFC 6749 `refresh_token` form parameter for
Expand Down
Loading