Skip to content

Commit

Permalink
Merge pull request getodk#6340 from seadowg/path-utils
Browse files Browse the repository at this point in the history
Guard against incorrect paths
  • Loading branch information
grzesiek2010 authored and seadowg committed Aug 23, 2024
1 parent e12bc09 commit 124ea51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion shared/src/main/java/org/odk/collect/shared/PathUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ object PathUtils {

@JvmStatic
fun getAbsoluteFilePath(dirPath: String, filePath: String): String {
return 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)) {
return absolutePath
} else {
throw SecurityException()
}
}

// https://stackoverflow.com/questions/2679699/what-characters-allowed-in-file-names-on-android
Expand Down
5 changes: 5 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 @@ -24,6 +24,11 @@ class PathUtilsTest {
assertThat(path, equalTo("/root/dir/file"))
}

@Test(expected = SecurityException::class)
fun `getAbsoluteFilePath() throws SecurityException when filePath is outside the dirPath`() {
PathUtils.getAbsoluteFilePath("/root/dir", "../tmp/file")
}

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

0 comments on commit 124ea51

Please sign in to comment.