mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-08-08 14:26:02 +01:00
Improve link preview validations.
This commit is contained in:
committed by
Cody Henthorne
parent
d447af36ba
commit
fa6b512cfc
@@ -1,5 +1,6 @@
|
||||
package org.thoughtcrime.securesms.util
|
||||
|
||||
import com.google.common.net.InetAddresses
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import org.thoughtcrime.securesms.stickers.StickerUrl
|
||||
import java.net.URI
|
||||
@@ -43,7 +44,23 @@ object LinkUtil {
|
||||
return false
|
||||
}
|
||||
|
||||
return linkUrl.toHttpUrlOrNull()?.scheme == "https"
|
||||
val httpUrl = linkUrl.toHttpUrlOrNull() ?: return false
|
||||
|
||||
if (httpUrl.scheme != "https") {
|
||||
return false
|
||||
}
|
||||
|
||||
val host = httpUrl.host
|
||||
|
||||
if (host.matches(INVALID_DOMAINS_REGEX)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (isPrivateOrLocalHost(host)) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,5 +122,26 @@ object LinkUtil {
|
||||
}
|
||||
}
|
||||
|
||||
private fun isPrivateOrLocalHost(host: String): Boolean {
|
||||
if (!InetAddresses.isInetAddress(host)) {
|
||||
return false
|
||||
}
|
||||
|
||||
val address = InetAddresses.forString(host)
|
||||
|
||||
if (address.isAnyLocalAddress ||
|
||||
address.isLoopbackAddress ||
|
||||
address.isLinkLocalAddress ||
|
||||
address.isSiteLocalAddress ||
|
||||
address.isMulticastAddress
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
// IPv6 unique local addresses (fc00::/7) are not covered by the standard helpers above.
|
||||
val bytes = address.address
|
||||
return bytes.size == 16 && (bytes[0].toInt() and 0xfe) == 0xfc
|
||||
}
|
||||
|
||||
private data class LegalCharactersResult(val isLegal: Boolean, val domain: String? = null)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user