diff --git a/app/src/main/kotlin/com/wire/android/util/UriUtil.kt b/app/src/main/kotlin/com/wire/android/util/UriUtil.kt index f1de47a5b28..ea335286df1 100644 --- a/app/src/main/kotlin/com/wire/android/util/UriUtil.kt +++ b/app/src/main/kotlin/com/wire/android/util/UriUtil.kt @@ -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 { @@ -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 } }