File tree Expand file tree Collapse file tree 3 files changed +17
-4
lines changed
main/java/org/odk/collect/shared
test/java/org/odk/collect/shared Expand file tree Collapse file tree 3 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -11,12 +11,13 @@ object PathUtils {
11
11
12
12
@JvmStatic
13
13
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
15
16
16
- if (File (absolutePath).canonicalPath.startsWith(dirPath)) {
17
+ if (File (absolutePath).canonicalPath.startsWith(File ( dirPath).canonicalPath )) {
17
18
return absolutePath
18
19
} else {
19
- throw SecurityException (" Invalid path : $absolutePath " )
20
+ throw SecurityException (
" Contact [email protected] . Attempt to access file outside of Collect directory : $absolutePath " )
20
21
}
21
22
}
22
23
Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ object TempFiles {
84
84
}
85
85
86
86
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" )
88
88
if (! tmpDir.exists()) {
89
89
tmpDir.mkdir()
90
90
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package org.odk.collect.shared
3
3
import org.hamcrest.MatcherAssert.assertThat
4
4
import org.hamcrest.Matchers.equalTo
5
5
import org.junit.Test
6
+ import java.io.File
6
7
7
8
class PathUtilsTest {
8
9
@@ -29,6 +30,17 @@ class PathUtilsTest {
29
30
PathUtils .getAbsoluteFilePath(" /root/dir" , " ../tmp/file" )
30
31
}
31
32
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
+
32
44
@Test
33
45
fun `getRelativeFilePath() returns filePath with dirPath removed` () {
34
46
val path = PathUtils .getRelativeFilePath(" /root/dir" , " /root/dir/file" )
You can’t perform that action at this time.
0 commit comments