Skip to content

Commit d1967a1

Browse files
authored
Merge pull request #6357 from seadowg/symlink-support
Support symlink `dirPath` in `getAbsoluteFilePath`
2 parents b8609cd + 6482ae2 commit d1967a1

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

shared/src/main/java/org/odk/collect/shared/PathUtils.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ object PathUtils {
1111

1212
@JvmStatic
1313
fun getAbsoluteFilePath(dirPath: String, filePath: String): String {
14-
val absolutePath = if (filePath.startsWith(dirPath)) filePath else dirPath + File.separator + filePath
14+
val absolutePath =
15+
if (filePath.startsWith(dirPath)) filePath else dirPath + File.separator + filePath
1516

16-
if (File(absolutePath).canonicalPath.startsWith(dirPath)) {
17+
if (File(absolutePath).canonicalPath.startsWith(File(dirPath).canonicalPath)) {
1718
return absolutePath
1819
} else {
19-
throw SecurityException("Invalid path: $absolutePath")
20+
throw SecurityException("Contact [email protected]. Attempt to access file outside of Collect directory: $absolutePath")
2021
}
2122
}
2223

shared/src/main/java/org/odk/collect/shared/TempFiles.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ object TempFiles {
8484
}
8585

8686
private fun getTempDir(): File {
87-
val tmpDir = File(System.getProperty("java.io.tmpdir", "."), " org.odk.collect.shared.TempFiles")
87+
val tmpDir = File(System.getProperty("java.io.tmpdir", "."), "org.odk.collect.shared.TempFiles")
8888
if (!tmpDir.exists()) {
8989
tmpDir.mkdir()
9090
}

shared/src/test/java/org/odk/collect/shared/PathUtilsTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.odk.collect.shared
33
import org.hamcrest.MatcherAssert.assertThat
44
import org.hamcrest.Matchers.equalTo
55
import org.junit.Test
6+
import java.io.File
67

78
class PathUtilsTest {
89

@@ -29,6 +30,17 @@ class PathUtilsTest {
2930
PathUtils.getAbsoluteFilePath("/root/dir", "../tmp/file")
3031
}
3132

33+
@Test
34+
fun `getAbsoluteFilePath() works when dirPath is not canonical`() {
35+
val tempDir = TempFiles.createTempDir()
36+
val nonCanonicalPath =
37+
tempDir.canonicalPath + File.separator + ".." + File.separator + tempDir.name
38+
assertThat(File(nonCanonicalPath).canonicalPath, equalTo(tempDir.canonicalPath))
39+
40+
val path = PathUtils.getAbsoluteFilePath(nonCanonicalPath, "file")
41+
assertThat(path, equalTo(nonCanonicalPath + File.separator + "file"))
42+
}
43+
3244
@Test
3345
fun `getRelativeFilePath() returns filePath with dirPath removed`() {
3446
val path = PathUtils.getRelativeFilePath("/root/dir", "/root/dir/file")

0 commit comments

Comments
 (0)