Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against incorrect paths #6340

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
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()
seadowg marked this conversation as resolved.
Show resolved Hide resolved
}
}

// 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