Merge "Check package name format before using it for SPA nav" into main

This commit is contained in:
Zhibin Liu
2024-12-23 06:47:04 -08:00
committed by Android (Google) Code Review
2 changed files with 11 additions and 1 deletions

View File

@@ -38,7 +38,7 @@ class SpaAppBridgeActivity : Activity() {
companion object {
fun getDestinationForApp(destinationPrefix: String, intent: Intent): String? {
val packageName = intent.data?.schemeSpecificPart ?: return null
val packageName = intent.data?.schemeSpecificPart?.takeIf { Regex("^([a-zA-Z]\\w*\\.)*[a-zA-Z]\\w*$").matches(it) } ?: return null
return "$destinationPrefix/$packageName/${UserHandle.myUserId()}"
}
}

View File

@@ -37,6 +37,16 @@ class SpaAppBridgeActivityTest {
assertThat(destination).isEqualTo("$DESTINATION/$PACKAGE_NAME/${UserHandle.myUserId()}")
}
@Test
fun getDestinationForApp_hasMalformedPackageName() {
val intent = Intent().apply {
data = Uri.parse("package:package.name/10#")
}
val destination = getDestinationForApp(DESTINATION, intent)
assertThat(destination).isNull()
}
@Test
fun getDestinationForApp_noPackageName() {