Skip to content

Commit

Permalink
Merge pull request #6357 from seadowg/symlink-support
Browse files Browse the repository at this point in the history
Support symlink `dirPath` in `getAbsoluteFilePath`
  • Loading branch information
grzesiek2010 authored and seadowg committed Aug 23, 2024
1 parent db209a7 commit 951f44e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
7 changes: 4 additions & 3 deletions shared/src/main/java/org/odk/collect/shared/PathUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ object PathUtils {

@JvmStatic
fun getAbsoluteFilePath(dirPath: String, filePath: String): String {
val absolutePath = if (filePath.startsWith(dirPath)) filePath else dirPath + File.separator + filePath
val absolutePath =
if (filePath.startsWith(dirPath)) filePath else dirPath + File.separator + filePath

if (File(absolutePath).canonicalPath.startsWith(dirPath)) {
if (File(absolutePath).canonicalPath.startsWith(File(dirPath).canonicalPath)) {
return absolutePath
} else {
throw SecurityException("Invalid path: $absolutePath")
throw SecurityException("Contact support@getodk.org. Attempt to access file outside of Collect directory: $absolutePath")
}
}

Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/java/org/odk/collect/shared/TempFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ object TempFiles {
}

private fun getTempDir(): File {
val tmpDir = File(System.getProperty("java.io.tmpdir", "."), " org.odk.collect.shared.TempFiles")
val tmpDir = File(System.getProperty("java.io.tmpdir", "."), "org.odk.collect.shared.TempFiles")
if (!tmpDir.exists()) {
tmpDir.mkdir()
}
Expand Down
12 changes: 12 additions & 0 deletions shared/src/test/java/org/odk/collect/shared/PathUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.odk.collect.shared
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import java.io.File

class PathUtilsTest {

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

@Test
fun `getAbsoluteFilePath() works when dirPath is not canonical`() {
val tempDir = TempFiles.createTempDir()
val nonCanonicalPath =
tempDir.canonicalPath + File.separator + ".." + File.separator + tempDir.name
assertThat(File(nonCanonicalPath).canonicalPath, equalTo(tempDir.canonicalPath))

val path = PathUtils.getAbsoluteFilePath(nonCanonicalPath, "file")
assertThat(path, equalTo(nonCanonicalPath + File.separator + "file"))
}

@Test
fun `getRelativeFilePath() returns filePath with dirPath removed`() {
val path = PathUtils.getRelativeFilePath("/root/dir", "/root/dir/file")
Expand Down

0 comments on commit 951f44e

Please sign in to comment.