-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AND-9449 [Security] Add validation of external urls for WebView
Signed-off-by: Mama1emon <andrew.khokhlove@gmail.com>
- Loading branch information
Showing
5 changed files
with
90 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
plugins { | ||
alias(deps.plugins.kotlin.jvm) | ||
alias(deps.plugins.android.library) | ||
alias(deps.plugins.kotlin.android) | ||
id("configuration") | ||
} | ||
|
||
android { | ||
namespace = "com.tangem.common" | ||
} | ||
|
||
dependencies { | ||
|
||
// region Firebase libraries | ||
implementation(platform(deps.firebase.bom)) | ||
implementation(deps.firebase.analytics) | ||
implementation(deps.firebase.crashlytics) | ||
implementation(deps.firebase.messaging) | ||
// end | ||
|
||
implementation(deps.timber) | ||
|
||
implementation(deps.arrow.core) | ||
|
||
implementation(deps.test.junit) | ||
implementation(deps.test.truth) | ||
} |
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions
31
common/src/main/kotlin/com/tangem/common/uri/ExternalUrlValidator.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,31 @@ | ||
package com.tangem.common.uri | ||
|
||
import com.google.firebase.crashlytics.FirebaseCrashlytics | ||
import timber.log.Timber | ||
import java.net.URI | ||
|
||
/** | ||
* External url validator | ||
* | ||
* @author Andrew Khokhlov on 23/12/2024 | ||
*/ | ||
object ExternalUrlValidator { | ||
|
||
private val trustedHost: List<String> = listOf("tangem.com") | ||
|
||
/** Check if [externalUri] is trusted */ | ||
fun isUriTrusted(externalUri: String): Boolean { | ||
return try { | ||
val uri = URI.create(externalUri) | ||
|
||
uri.scheme == "https" && uri.host in trustedHost | ||
} catch (e: Exception) { | ||
val exception = IllegalStateException("Failed to validate URI: $externalUri", e) | ||
|
||
Timber.e(exception) | ||
FirebaseCrashlytics.getInstance().recordException(exception) | ||
|
||
false | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
common/src/testDebug/kotlin/com/tangem/common/uri/ExternalUrlValidatorTest.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,36 @@ | ||
package com.tangem.common.uri | ||
|
||
import com.google.common.truth.Truth | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
/** | ||
* @author Andrew Khokhlov on 23/12/2024 | ||
*/ | ||
@RunWith(Parameterized::class) | ||
class ExternalUrlValidatorTest(private val model: Model) { | ||
|
||
@Test | ||
fun test() { | ||
val actual = ExternalUrlValidator.isUriTrusted(externalUri = model.url) | ||
|
||
Truth.assertThat(actual).isEqualTo(model.expected) | ||
} | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun data(): Collection<Model> = listOf( | ||
Model(url = "https://tangem.com", expected = true), | ||
Model(url = "https://tange.com", expected = false), | ||
Model(url = "https://fake.tangem.com", expected = false), | ||
Model(url = "http://tangem.com", expected = false), | ||
Model(url = "http://tandem.com", expected = false), | ||
Model(url = "adawdawdassdw", expected = false), | ||
) | ||
|
||
data class Model(val url: String, val expected: Boolean) | ||
} | ||
} |