feat(event): 딥링크 입력 계약을 검증한다

This commit is contained in:
2026-10-29 15:25:37 +09:00
parent 4c58d7593c
commit 86f6f85009
2 changed files with 144 additions and 0 deletions
@@ -0,0 +1,54 @@
package kr.co.vividnext.sodalive.v2.event
import android.net.Uri
sealed interface EventDeepLinkContract {
data class Direct(val eventId: Long) : EventDeepLinkContract
data object SdkOwned : EventDeepLinkContract
data object InvalidEvent : EventDeepLinkContract
data object Unrelated : EventDeepLinkContract
companion object {
private val routingKeys = setOf(
"deep_link_value", "deep_link_sub5", "room_id", "channel_id", "message_id", "audition_id",
"content_id", "postId", "community_creator_id", "community_post_id"
)
fun classify(uri: Uri, appScheme: String): EventDeepLinkContract {
val values = if (uri.isHierarchical) uri.getQueryParameters("deep_link_value") else emptyList()
val isEvent = uri.host == "event-detail" || "event-detail" in uri.pathSegments ||
"event-detail" in values || (!uri.isHierarchical && uri.schemeSpecificPart.startsWith("event-detail"))
if (!isEvent) return Unrelated
if (!uri.isHierarchical || uri.encodedAuthority != uri.host) return InvalidEvent
if (routingKeys.any { uri.getQueryParameters(it).size > 1 }) return InvalidEvent
if (uri.scheme == appScheme && uri.host == "event-detail") {
val rawId = uri.encodedPath?.removePrefix("/")
val id = positiveId(rawId) ?: return InvalidEvent
if (values.isNotEmpty() && values.single() != "event-detail") return InvalidEvent
val queryId = uri.getQueryParameter("deep_link_sub5")
if (queryId != null && positiveId(queryId) != id) return InvalidEvent
if (routingKeys.minus(setOf("deep_link_value", "deep_link_sub5")).any { it in uri.queryParameterNames }) {
return InvalidEvent
}
return Direct(id)
}
return if (uri.scheme == "https" && uri.host == "$appScheme.onelink.me" &&
parseUdl(values.singleOrNull(), uri.getQueryParameter("deep_link_sub5")) != null
) {
SdkOwned
} else {
InvalidEvent
}
}
fun parseUdl(value: String?, sub5: String?): Long? =
if (value == "event-detail") positiveId(sub5) else null
fun canonicalUri(eventId: Long, appScheme: String): Uri = Uri.parse("$appScheme://event-detail/$eventId")
private fun positiveId(raw: String?): Long? = raw?.takeIf { it.isNotEmpty() && it.all { digit -> digit in '0'..'9' } }
?.toLongOrNull()?.takeIf { it > 0 }
}
}
@@ -0,0 +1,90 @@
package kr.co.vividnext.sodalive.v2.event
import android.app.Application
import android.net.Uri
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
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 EventDeepLinkContractTest {
@Test
fun `직접 링크는 환경별 양수 Long 전체 범위를 보존한다`() {
listOf("voiceon", "voiceon-test").forEach { scheme ->
listOf(1L, 2147483648L, Long.MAX_VALUE).forEach { id ->
assertEquals(
EventDeepLinkContract.Direct(id),
EventDeepLinkContract.classify(Uri.parse("$scheme://event-detail/$id"), scheme)
)
assertEquals("$scheme://event-detail/$id", EventDeepLinkContract.canonicalUri(id, scheme).toString())
}
}
}
@Test
fun `잘못된 ID와 권한 경로 및 라우팅 query 충돌은 이벤트로 거부한다`() {
listOf(
"voiceon-test://event-detail", "voiceon-test://event-detail/0", "voiceon-test://event-detail/-1",
"voiceon-test://event-detail/+1", "voiceon-test://event-detail/%201", "voiceon-test://event-detail/1",
"voiceon-test://event-detail/9223372036854775808", "voiceon-test://event-detail/1/2",
"voiceon-test://event-detail//1", "voiceon-test://event-detail/1/",
"voiceon-test://user@event-detail/1", "voiceon-test://event-detail:80/1",
"voiceon-test://event-detail:invalid/1", "voiceon-test://event-detail:/1",
"voiceon://event-detail/1", "https://foreign.example/event-detail/1",
"voiceon-test://event-detail/1?deep_link_value=live&deep_link_sub5=2",
"voiceon-test://event-detail/1?deep_link_sub5=1&deep_link_sub5=2",
"voiceon-test://event-detail/1?room_id=2", "voiceon-test:event-detail/1"
).forEach { raw ->
assertEquals(raw, EventDeepLinkContract.InvalidEvent, EventDeepLinkContract.classify(Uri.parse(raw), "voiceon-test"))
}
}
@Test
fun `현재 환경 HTTPS OneLink만 SDK 소유이며 잘못된 이벤트는 다른 도메인으로 바뀌지 않는다`() {
assertEquals(
EventDeepLinkContract.SdkOwned,
EventDeepLinkContract.classify(
Uri.parse("https://voiceon-test.onelink.me/abc?deep_link_value=event-detail&deep_link_sub5=9223372036854775807"),
"voiceon-test"
)
)
listOf(
"https://foreign.example/abc",
"https://voiceon.onelink.me/abc",
"http://voiceon-test.onelink.me/abc",
"https://user@voiceon-test.onelink.me/abc",
"https://voiceon-test.onelink.me:443/abc"
).forEach { prefix ->
assertEquals(
EventDeepLinkContract.InvalidEvent,
EventDeepLinkContract.classify(Uri.parse("$prefix?deep_link_value=event-detail&deep_link_sub5=1"), "voiceon-test")
)
}
assertEquals(
EventDeepLinkContract.InvalidEvent,
EventDeepLinkContract.classify(
Uri.parse("https://voiceon-test.onelink.me/a?deep_link_value=live&deep_link_value=event-detail&deep_link_sub5=1"),
"voiceon-test"
)
)
assertEquals(
EventDeepLinkContract.Unrelated,
EventDeepLinkContract.classify(Uri.parse("sodalive://chat/2"), "voiceon-test")
)
}
@Test
fun `UDL은 정확한 value와 ASCII 양수 문자열만 수용한다`() {
assertEquals(Long.MAX_VALUE, EventDeepLinkContract.parseUdl("event-detail", "9223372036854775807"))
listOf(null, "", "0", "-1", "+1", " 1", "1 ", "1", "9223372036854775808").forEach { id ->
assertNull(EventDeepLinkContract.parseUdl("event-detail", id))
}
listOf(null, "live", "EVENT-DETAIL").forEach { value ->
assertNull(EventDeepLinkContract.parseUdl(value, "1"))
}
}
}