Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -41,14 +41,38 @@ 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)) {
if (!isWithinDirectory(compactTarget, compactWork)) {
throw new GenericFileOperationFailedException(
"Cannot retrieve file to local work file: " + compactTarget
+ " as it is jailed to the local work directory: "
+ compactWork);
}
}

/**
* Determines whether a compacted target path is contained within a compacted directory path, using a path-segment
* boundary comparison. Unlike a bare string prefix test, a sibling directory whose name merely extends the
* directory name (for example {@code /work} versus {@code /workspace}) is not considered contained. A trailing
* separator on the directory is tolerated, and an empty directory imposes no boundary.
*
* @param compactTarget the compacted target path (see {@link FileUtil#compactPath(String)})
* @param compactDir the compacted directory the target must stay within
* @return {@code true} if the target is the directory itself or a path inside it
*/
public static boolean isWithinDirectory(String compactTarget, String compactDir) {
if (compactDir.isEmpty()) {
// no directory boundary configured
return true;
}
// drop a trailing separator (if any) so the boundary comparison is exact, regardless of whether the
// directory path was supplied with or without one
String dir = compactDir;
if (dir.charAt(dir.length() - 1) == File.separatorChar) {
dir = dir.substring(0, dir.length() - 1);
}
return compactTarget.equals(dir) || compactTarget.startsWith(dir + File.separator);
}

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
Expand Up @@ -432,7 +432,7 @@ private static void jailedCheck(String answer, String baseDir) {
// first as the name can be using relative paths via ../ etc)
String compatchAnswer = FileUtil.compactPath(answer);
String compatchBaseDir = FileUtil.compactPath(baseDir);
if (!compatchAnswer.startsWith(compatchBaseDir)) {
if (!GenericFileHelper.isWithinDirectory(compatchAnswer, compatchBaseDir)) {
throw new IllegalArgumentException(
"Cannot write file with name: " + compatchAnswer
+ " as the filename is jailed to the starting directory: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import org.junit.jupiter.api.Test;

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

public class GenericFileHelperTest {

Expand All @@ -44,5 +46,29 @@ public void shouldRejectFilesEscapingLocalWorkDirectory() {
() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "../../etc/passwd"), workDir));
assertThrows(GenericFileOperationFailedException.class,
() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "sub/../../escape.txt"), workDir));
// a sibling directory whose name merely extends the work directory name must also be rejected
assertThrows(GenericFileOperationFailedException.class,
() -> GenericFileHelper.jailToLocalWorkDirectory(new File(workDir, "../localworkEVIL/file.txt"), workDir));
}

@Test
public void isWithinDirectoryRespectsPathBoundaries() {
String sep = File.separator;
String work = sep + "data" + sep + "work";

// the directory itself and real children are contained
assertTrue(GenericFileHelper.isWithinDirectory(work, work));
assertTrue(GenericFileHelper.isWithinDirectory(work + sep + "a.txt", work));
assertTrue(GenericFileHelper.isWithinDirectory(work + sep + "sub" + sep + "a.txt", work));

// a trailing separator on the directory (as supplied by the file producer) is tolerated
assertTrue(GenericFileHelper.isWithinDirectory(work + sep + "a.txt", work + sep));

// a sibling whose name merely extends the directory name is NOT contained
assertFalse(GenericFileHelper.isWithinDirectory(sep + "data" + sep + "workspace" + sep + "a.txt", work));
assertFalse(GenericFileHelper.isWithinDirectory(sep + "data" + sep + "workspace" + sep + "a.txt", work + sep));

// an empty directory imposes no boundary
assertTrue(GenericFileHelper.isWithinDirectory("anything.txt", ""));
}
}
Loading