Skip to content

Commit

Permalink
fix: sanitize urls before displaying warning dialog [WPB-4782] (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
gongracr authored Oct 12, 2023
1 parent 2928999 commit 594aa8a
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions app/src/main/kotlin/com/wire/android/util/UriUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.wire.android.util

import com.wire.android.appLogger
import java.net.IDN
import java.net.URI

fun containsSchema(url: String): Boolean {
Expand All @@ -28,9 +30,34 @@ fun containsSchema(url: String): Boolean {
}

fun normalizeLink(url: String): String {
return if (containsSchema(url)) {
url
val sanitizedUrl = sanitizeUrl(url)
return if (containsSchema(sanitizedUrl)) {
sanitizedUrl
} else {
"https://$url"
"https://$sanitizedUrl"
}
}

@Suppress("TooGenericExceptionCaught")
fun sanitizeUrl(url: String): String {
try {
val urlComponents = url.split("://", limit = 2)
val scheme = urlComponents[0] // Extract the URL scheme (e.g., "http")
val restOfUrl = urlComponents[1] // Extract the rest of the URL

// Split the rest of the URL by '/' to isolate the domain
val domainAndPath = restOfUrl.split("/")
val domain = domainAndPath[0] // Extract the domain

// Use IDN.toASCII to convert the domain to ASCII representation
val asciiDomain = IDN.toASCII(domain)

// Reconstruct the sanitized URL
return "$scheme://$asciiDomain" +
if (domainAndPath.size > 1) "/" + domainAndPath.subList(1, domainAndPath.size).joinToString("/") else ""
} catch (e: Exception) {
// Handle any exceptions that might occur during the processing
appLogger.w("Error sanitizing URL: $url", e)
return url // Return the original URL if any errors occur
}
}

0 comments on commit 594aa8a

Please sign in to comment.