Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package dev.hotwire.navigation.navigator

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.Companion.PROTECTED
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentOnAttachListener
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.security.hasSameOriginAs
import dev.hotwire.navigation.activities.HotwireActivity
import dev.hotwire.navigation.config.HotwireNavigation

internal const val DEEPLINK_EXTRAS_KEY = "android-support-nav:controller:deepLinkExtras"
internal const val DEEPLINK_ARGS_KEY = "android-support-nav:controller:deepLinkArgs"
internal const val LOCATION_KEY = "location"

open class NavigatorHost : NavHostFragment(), FragmentOnAttachListener {
internal lateinit var activity: HotwireActivity
lateinit var navigator: Navigator
Expand All @@ -34,9 +28,14 @@ open class NavigatorHost : NavHostFragment(), FragmentOnAttachListener {
private var registeredStartLocation: String? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

activity = requireActivity() as HotwireActivity

// NavHostFragment sets a graph supplied through its arguments or XML here. That graph can
// match a deep link by the Intent's action alone, so it gets an empty Intent.
withoutLaunchIntentNavigation(launchIntentCopy = { Intent() }) {
super.onCreate(savedInstanceState)
}

registeredStartLocation = configuration.startLocation.also {
Hotwire.config.registerStartLocation(it)
}
Expand Down Expand Up @@ -95,43 +94,61 @@ open class NavigatorHost : NavHostFragment(), FragmentOnAttachListener {

private fun initControllerGraph() {
isGraphInitialized = true
ensureDeeplinkStartLocationValid()

navController.apply {
graph = NavigatorGraphBuilder(
navigatorName = configuration.name,
startLocation = configuration.startLocation,
pathConfiguration = Hotwire.config.pathConfiguration,
navController = findNavController()
).build(
registeredFragments = HotwireNavigation.registeredFragmentDestinations
)

val graph = NavigatorGraphBuilder(
navigatorName = configuration.name,
startLocation = configuration.startLocation,
pathConfiguration = Hotwire.config.pathConfiguration,
navController = findNavController()
).build(
registeredFragments = HotwireNavigation.registeredFragmentDestinations
)

withoutLaunchIntentNavigation {
navController.graph = graph
}
}

/**
* Google's Navigation library automatically navigates to deep links provided in the launching
* Intent, which lets a malicious Intent open an arbitrary page in the WebView. Sanitize the
* Intent's attacker-controllable deep-link arguments so the start location stays within the
* app's origin.
* Google's Navigation library navigates to the launching Intent's deep link when a graph is
* set: to the destination and arguments in its deep-link extras, or to the destination whose
* deep link matches its data. Hotwire navigates from the configuration's start location and
* its own visits instead, so a graph is only set while the Activity holds a copy of its
* launching Intent without the deep link. Hotwire's own graph only has deep links by uri, so
* the copy keeps the Intent's action and the app's extras for app code that runs meanwhile.
* The Activity gets its launching Intent back right after, unchanged.
*/
@VisibleForTesting(otherwise = PROTECTED)
fun ensureDeeplinkStartLocationValid() {
val intent = activity.intent

// NavController merges deepLinkArgs over the validated deepLinkExtras (last write wins), so
// empty each per-destination bundle to stop it overriding the validated start location.
intent.extras?.getParcelableArrayList<Bundle>(DEEPLINK_ARGS_KEY)?.let { args ->
intent.putParcelableArrayListExtra(DEEPLINK_ARGS_KEY, ArrayList(args.map { Bundle() }))
private inline fun withoutLaunchIntentNavigation(
launchIntentCopy: (Intent) -> Intent = { it.withoutDeepLink() },
setGraph: () -> Unit
) {
val launchIntent: Intent? = activity.intent
activity.intent = launchIntent?.let(launchIntentCopy) ?: Intent()

try {
setGraph()
} finally {
activity.intent = launchIntent
}
}

val extrasBundle = intent.extras?.getBundle(DEEPLINK_EXTRAS_KEY) ?: return
val startLocation = extrasBundle.getString(LOCATION_KEY) ?: return

if (!startLocation.hasSameOriginAs(configuration.startLocation)) {
extrasBundle.putString(LOCATION_KEY, configuration.startLocation)
intent.putExtra(DEEPLINK_EXTRAS_KEY, extrasBundle)
private fun Intent.withoutDeepLink() = try {
Intent(this).apply {
setDataAndType(null, null)
DEEP_LINK_EXTRAS.forEach { removeExtra(it) }
}
} catch (e: RuntimeException) {
// Reading the extras unparcels them, which fails for a value whose class the app doesn't
// have. Hide the whole Intent then.
Intent()
}

private companion object {
val DEEP_LINK_EXTRAS = listOf(
"android-support-nav:controller:deepLinkIds",
"android-support-nav:controller:deepLinkArgs",
"android-support-nav:controller:deepLinkExtras"
)
}

private val configuration get() = activity.navigatorConfigurations().firstOrNull {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package dev.hotwire.navigation.navigator

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Looper
import android.view.View
import android.webkit.RenderProcessGoneDetail
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.FragmentFactory
import androidx.navigation.NavHostController
import androidx.navigation.fragment.DialogFragmentNavigator
import dev.hotwire.core.bridge.BridgeComponent
import dev.hotwire.core.bridge.BridgeComponentFactory
import dev.hotwire.core.bridge.BridgeDelegate
Expand All @@ -21,7 +27,6 @@ import dev.hotwire.navigation.fragments.HotwireWebFragment
import dev.hotwire.navigation.views.HotwireSwipeRefreshLayout
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
Expand All @@ -32,77 +37,82 @@ import java.lang.ref.WeakReference
@RunWith(RobolectricTestRunner::class)
class NavigatorHostTest {

private lateinit var activity: TestActivity
private lateinit var host: NavigatorHost

@Before
fun setUp() {
host = NavigatorHost()
}

@After
fun tearDown() {
HotwireNavigation.registeredBridgeComponentFactories = emptyList()
}

@Test
fun `reverts to config start location when deep link host differs`() {
val intent = Intent().apply {
putExtra(DEEPLINK_EXTRAS_KEY, bundleOf(LOCATION_KEY to "https://other.com/path"))
}
activity = Robolectric.buildActivity(TestActivity::class.java, intent).get()
fun `doesn't navigate to a destination matching the launching Intent's data`() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(SHEET_DEEP_LINK))

host.activity = activity
host.ensureDeeplinkStartLocationValid()
val activity = launch(intent)

assertThat(activity.intent.getBundleExtra(DEEPLINK_EXTRAS_KEY)?.getString(LOCATION_KEY))
.isEqualTo("https://example.com/start")
assertThat(activity.navigatorHost.destinations()).containsExactly(START_LOCATION)
}

@Test
fun `does not change start location when deep link host matches config`() {
fun `doesn't navigate to the destination and location in the launching Intent's extras`() {
val (graphId, sheetId) = launch(Intent()).navigatorHost.navController.graph.let { graph ->
graph.id to graph.single { it is DialogFragmentNavigator.Destination }.id
}
val intent = Intent().apply {
putExtra(DEEPLINK_EXTRAS_KEY, bundleOf(LOCATION_KEY to "https://example.com/path"))
putExtra(DEEPLINK_IDS_KEY, intArrayOf(graphId, sheetId))
putExtra(DEEPLINK_EXTRAS_KEY, bundleOf("location" to OTHER_LOCATION))
putParcelableArrayListExtra(DEEPLINK_ARGS_KEY, arrayListOf(Bundle(), bundleOf("location" to OTHER_LOCATION)))
}
activity = Robolectric.buildActivity(TestActivity::class.java, intent).get()

host.activity = activity
host.ensureDeeplinkStartLocationValid()
val activity = launch(intent)

assertThat(activity.intent.getBundleExtra(DEEPLINK_EXTRAS_KEY)?.getString(LOCATION_KEY))
.isEqualTo("https://example.com/path")
assertThat(activity.navigatorHost.destinations()).containsExactly(START_LOCATION)
}

@Test
fun `reverts to config start location when deep link downgrades the scheme`() {
val intent = Intent().apply {
putExtra(DEEPLINK_EXTRAS_KEY, bundleOf(LOCATION_KEY to "http://example.com/path"))
fun `leaves the launching Intent unchanged`() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(SHEET_DEEP_LINK)).apply {
putExtra(DEEPLINK_EXTRAS_KEY, bundleOf("location" to OTHER_LOCATION))
putExtra("app_extra", "kept")
}
activity = Robolectric.buildActivity(TestActivity::class.java, intent).get()

host.activity = activity
host.ensureDeeplinkStartLocationValid()
val activity = launch(intent)

assertThat(activity.intent.getBundleExtra(DEEPLINK_EXTRAS_KEY)?.getString(LOCATION_KEY))
.isEqualTo("https://example.com/start")
assertThat(activity.intent.data).isEqualTo(Uri.parse(SHEET_DEEP_LINK))
assertThat(activity.intent.getBundleExtra(DEEPLINK_EXTRAS_KEY)?.getString("location")).isEqualTo(OTHER_LOCATION)
assertThat(activity.intent.getStringExtra("app_extra")).isEqualTo("kept")
}

// NavController merges deepLinkArgs over deepLinkExtras (last write wins); the intent's args
// must not survive to override the validated start location.
@Test
fun `empties deepLinkArgs so they cannot override the start location`() {
val intent = Intent().apply {
putExtra(DEEPLINK_EXTRAS_KEY, bundleOf(LOCATION_KEY to "https://example.com/ok"))
putParcelableArrayListExtra(DEEPLINK_ARGS_KEY, arrayListOf(bundleOf(LOCATION_KEY to ATTACKER_URL)))
fun `app code sees the launching Intent without its deep link while the graph is set`() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(SHEET_DEEP_LINK)).apply {
putExtra(DEEPLINK_EXTRAS_KEY, bundleOf("location" to OTHER_LOCATION))
putExtra("app_extra", "kept")
}
activity = Robolectric.buildActivity(TestActivity::class.java, intent).get()
val seen = mutableListOf<Intent>()
FactoryHostActivity.onInstantiate = { activity -> seen += Intent(activity.intent) }

host.activity = activity
host.ensureDeeplinkStartLocationValid()
Robolectric.buildActivity(FactoryHostActivity::class.java, intent).setup()
shadowOf(Looper.getMainLooper()).idle()

assertThat(seen).isNotEmpty
assertThat(seen.first().action).isEqualTo(Intent.ACTION_VIEW)
assertThat(seen.first().getStringExtra("app_extra")).isEqualTo("kept")
assertThat(seen.first().data).isNull()
assertThat(seen.first().hasExtra(DEEPLINK_EXTRAS_KEY)).isFalse()
}

// NavHostFragment.onCreate() sets a graph supplied through its arguments or XML, and creates
// its NavController just before. That graph can match a deep link by action alone.
@Test
fun `hides the launching Intent's deep link from NavHostFragment's own setup`() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(SHEET_DEEP_LINK))
RecordingHostActivity.seen.clear()

val activity = Robolectric.buildActivity(RecordingHostActivity::class.java, intent).setup().get()

val survivingArgs = activity.intent.getParcelableArrayListExtra<Bundle>(DEEPLINK_ARGS_KEY)
?.mapNotNull { it.getString(LOCATION_KEY) }.orEmpty()
assertThat(survivingArgs).doesNotContain(ATTACKER_URL)
assertThat(RecordingHostActivity.seen).hasSize(1)
assertThat(RecordingHostActivity.seen.first().data).isNull()
assertThat(RecordingHostActivity.seen.first().action).isNull()
assertThat(activity.intent.data).isEqualTo(Uri.parse(SHEET_DEEP_LINK))
}

// A Bridge is only created when bridge components are registered.
Expand Down Expand Up @@ -163,8 +173,23 @@ class NavigatorHostTest {
assertThat(shadowOf(newSession.webView).wasDestroyCalled()).isFalse()
}

private fun launch(intent: Intent): HostActivity {
val activity = Robolectric.buildActivity(HostActivity::class.java, intent).setup().get()
shadowOf(Looper.getMainLooper()).idle()
return activity
}

private fun NavigatorHost.destinations() = childFragmentManager.fragments
.filterIsInstance<HotwireDestination>()
.map { it.location }

companion object {
private const val ATTACKER_URL = "https://attacker.example/steal"
private const val START_LOCATION = "https://example.com/start"
private const val OTHER_LOCATION = "https://example.com/other"
private const val SHEET_DEEP_LINK = "hotwire://fragment/web/modal/sheet"
private const val DEEPLINK_IDS_KEY = "android-support-nav:controller:deepLinkIds"
private const val DEEPLINK_EXTRAS_KEY = "android-support-nav:controller:deepLinkExtras"
private const val DEEPLINK_ARGS_KEY = "android-support-nav:controller:deepLinkArgs"
private val HOST_ID = View.generateViewId()

private val crashedRenderProcess = object : RenderProcessGoneDetail() {
Expand All @@ -173,22 +198,44 @@ class NavigatorHostTest {
}
}

private class TestActivity : HotwireActivity() {
private val navConfig = NavigatorConfiguration(
name = "test",
startLocation = "https://example.com/start",
navigatorHostId = 0
)
class RecordingNavigatorHost : NavigatorHost() {
override fun onCreateNavHostController(navHostController: NavHostController) {
super.onCreateNavHostController(navHostController)
RecordingHostActivity.seen += Intent(requireActivity().intent)
}
}

class RecordingHostActivity : HostActivity() {
override fun createNavigatorHost() = RecordingNavigatorHost()

companion object {
val seen = mutableListOf<Intent>()
}
}

class FactoryHostActivity : HostActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val activity = this
supportFragmentManager.fragmentFactory = object : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
if (className == HotwireWebFragment::class.java.name) onInstantiate(activity)
return super.instantiate(classLoader, className)
}
}
super.onCreate(savedInstanceState)
}

override fun navigatorConfigurations(): List<NavigatorConfiguration> = listOf(navConfig)
companion object {
var onInstantiate: (Activity) -> Unit = {}
}
}

class TestComponent(name: String, delegate: BridgeDelegate<HotwireDestination>) :
BridgeComponent<HotwireDestination>(name, delegate) {
override fun onReceive(message: Message) {}
}

class HostActivity : HotwireActivity() {
open class HostActivity : HotwireActivity() {
val navigatorHost get() = supportFragmentManager.findFragmentById(HOST_ID) as NavigatorHost

override fun navigatorConfigurations() = listOf(
Expand All @@ -199,13 +246,15 @@ class NavigatorHostTest {
)
)

open fun createNavigatorHost() = NavigatorHost()

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(androidx.appcompat.R.style.Theme_AppCompat_Light_NoActionBar)
super.onCreate(savedInstanceState)
setContentView(FragmentContainerView(this).apply { id = HOST_ID })

if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().add(HOST_ID, NavigatorHost()).commitNow()
supportFragmentManager.beginTransaction().add(HOST_ID, createNavigatorHost()).commitNow()
}
}
}
Expand Down
Loading