-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from hotwired/path-traversal-fix
Prevent path traversal vulnerability in TurboUriHelper
- Loading branch information
Showing
2 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
turbo/src/test/kotlin/dev/hotwire/turbo/util/TurboUriHelperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package dev.hotwire.turbo.util | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import android.os.Build | ||
import androidx.test.core.app.ApplicationProvider | ||
import dev.hotwire.turbo.BaseUnitTest | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.TestCoroutineDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.test.setMain | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.Shadows | ||
import org.robolectric.annotation.Config | ||
import java.io.File | ||
|
||
@ExperimentalCoroutinesApi | ||
@RunWith(RobolectricTestRunner::class) | ||
@Config(sdk = [Build.VERSION_CODES.O]) | ||
class TurboUriHelperTest : BaseUnitTest() { | ||
|
||
private val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() | ||
|
||
private lateinit var context: Context | ||
private lateinit var turboUriHelper: TurboUriHelper | ||
|
||
@Before | ||
override fun setup() { | ||
super.setup() | ||
Dispatchers.setMain(testDispatcher) | ||
dispatcherProvider.io = Dispatchers.Main | ||
|
||
context = ApplicationProvider.getApplicationContext() | ||
turboUriHelper = TurboUriHelper(context) | ||
} | ||
|
||
override fun teardown() { | ||
super.teardown() | ||
Dispatchers.resetMain() | ||
testDispatcher.cleanupTestCoroutines() | ||
} | ||
|
||
@Test | ||
fun validUriIsWrittenToFileSuccessfully() = runTest { | ||
val inputFile = File("/tmp/file.txt") | ||
val inputFileUri = Uri.fromFile(inputFile) | ||
Shadows.shadowOf(context.contentResolver).registerInputStream(inputFileUri, "fileContent".byteInputStream()) | ||
|
||
val destFile = turboUriHelper.writeFileTo(inputFileUri, TurboFileProvider.directory(context)) | ||
|
||
assertThat(destFile).isNotNull() | ||
} | ||
|
||
@Test | ||
fun pathTraversingUriWithRelativePathFailsToWriteToFile() = runTest { | ||
val inputFileUri = Uri.parse("../../tmp/file.txt") | ||
Shadows.shadowOf(context.contentResolver).registerInputStream(inputFileUri, "fileContent".byteInputStream()) | ||
|
||
val destFile = turboUriHelper.writeFileTo(inputFileUri, TurboFileProvider.directory(context)) | ||
|
||
assertThat(destFile).isNull() | ||
} | ||
|
||
@Test | ||
fun pathTraversingUriWithNameArgFailsToWriteToFile() = runTest { | ||
val inputFileUri = Uri.parse("content://malicious.app?path=/data/data/malicious.app/files/file.txt&name=../../file.txt") | ||
Shadows.shadowOf(context.contentResolver).registerInputStream(inputFileUri, "fileContent".byteInputStream()) | ||
|
||
val destFile = turboUriHelper.writeFileTo(inputFileUri, TurboFileProvider.directory(context)) | ||
|
||
assertThat(destFile).isNull() | ||
} | ||
} |