feat(event): 참여 링크 검증과 안전한 실행을 추가한다
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package kr.co.vividnext.sodalive.v2.event
|
||||
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import kr.co.vividnext.sodalive.BuildConfig
|
||||
import java.net.URI
|
||||
import java.net.URISyntaxException
|
||||
import java.util.Locale
|
||||
|
||||
object EventDetailLinkPolicy {
|
||||
fun toIntent(link: String?): Intent? {
|
||||
val value = link?.trim()?.takeIf { it.isNotBlank() } ?: return null
|
||||
val uri = try {
|
||||
URI(value)
|
||||
} catch (_: URISyntaxException) {
|
||||
return null
|
||||
}
|
||||
val scheme = uri.scheme?.lowercase(Locale.ROOT) ?: return null
|
||||
when (scheme) {
|
||||
"http", "https" -> if (uri.host.isNullOrBlank()) return null
|
||||
BuildConfig.APPSCHEME -> Unit
|
||||
else -> return null
|
||||
}
|
||||
return Intent(Intent.ACTION_VIEW, Uri.parse(value).normalizeScheme())
|
||||
}
|
||||
|
||||
fun launch(context: Context, link: String?): Boolean {
|
||||
val intent = toIntent(link) ?: return false
|
||||
return try {
|
||||
context.startActivity(intent)
|
||||
true
|
||||
} catch (_: ActivityNotFoundException) {
|
||||
false
|
||||
} catch (_: SecurityException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package kr.co.vividnext.sodalive.v2.event
|
||||
|
||||
import android.app.Application
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.Intent
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import kr.co.vividnext.sodalive.BuildConfig
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class EventDetailLinkPolicyTest {
|
||||
@Test
|
||||
fun `웹과 앱 scheme만 정규화한 ACTION_VIEW Intent로 변환한다`() {
|
||||
listOf(
|
||||
" https://example.com/events/7?q=a%20b#join " to "https://example.com/events/7?q=a%20b#join",
|
||||
"HTTP://example.com/path" to "http://example.com/path",
|
||||
"${BuildConfig.APPSCHEME}://creator/7" to "${BuildConfig.APPSCHEME}://creator/7"
|
||||
).forEach { (link, expected) ->
|
||||
val intent = EventDetailLinkPolicy.toIntent(link)
|
||||
assertEquals(Intent.ACTION_VIEW, intent?.action)
|
||||
assertEquals(expected, intent?.dataString)
|
||||
assertNull(intent?.extras)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `누락과 빈 링크 및 임의 scheme은 거부한다`() {
|
||||
listOf(
|
||||
null,
|
||||
"",
|
||||
" ",
|
||||
"example.com",
|
||||
"//example.com",
|
||||
"javascript:alert(1)",
|
||||
"intent://event#Intent;end",
|
||||
"file:///tmp/event",
|
||||
"content://event/1",
|
||||
"tel:123",
|
||||
"ftp://example.com"
|
||||
)
|
||||
.forEach { assertNull(it, EventDetailLinkPolicy.toIntent(it)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `host 없는 웹과 잘못된 URI는 거부한다`() {
|
||||
listOf(
|
||||
"https:",
|
||||
"http:/path",
|
||||
"https:///path",
|
||||
"https://",
|
||||
"https://?q=1",
|
||||
"https://exa mple.com",
|
||||
"https://example.com/%zz",
|
||||
"https://[bad",
|
||||
"https://example.com:bad",
|
||||
"https://example.com/\npath"
|
||||
)
|
||||
.forEach { assertNull(it, EventDetailLinkPolicy.toIntent(it)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `허용 링크는 한 번 실행하고 차단 링크는 실행하지 않는다`() {
|
||||
val context = LaunchContext(ApplicationProvider.getApplicationContext())
|
||||
assertTrue(EventDetailLinkPolicy.launch(context, "https://example.com/join"))
|
||||
assertEquals(listOf("https://example.com/join"), context.started.map { it.dataString })
|
||||
assertFalse(EventDetailLinkPolicy.launch(context, "javascript:alert(1)"))
|
||||
assertEquals(1, context.started.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `처리 앱이 없거나 실행 권한이 없으면 예외 대신 실패를 반환한다`() {
|
||||
listOf(ActivityNotFoundException("내부 정보"), SecurityException("내부 정보")).forEach { failure ->
|
||||
val context = LaunchContext(ApplicationProvider.getApplicationContext(), failure)
|
||||
assertFalse(EventDetailLinkPolicy.launch(context, "https://example.com/join"))
|
||||
assertEquals(1, context.started.size)
|
||||
}
|
||||
}
|
||||
|
||||
private class LaunchContext(base: Context, private val failure: RuntimeException? = null) : ContextWrapper(base) {
|
||||
val started = mutableListOf<Intent>()
|
||||
|
||||
override fun startActivity(intent: Intent) {
|
||||
started += intent
|
||||
failure?.let { throw it }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user