Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

Make bare-hostname + HTTPS-probe handling production-ready

David Nashash36ce9c803cf6
diff --git a/data/src/main/java/com/streamvault/data/repository/EpgSourceRepositoryImpl.kt b/data/src/main/java/com/streamvault/data/repository/EpgSourceRepositoryImpl.kt
index 0cd6e4c6..96a44e33 100644
--- a/data/src/main/java/com/streamvault/data/repository/EpgSourceRepositoryImpl.kt
+++ b/data/src/main/java/com/streamvault/data/repository/EpgSourceRepositoryImpl.kt
@@ -23,6 +23,7 @@ import com.streamvault.domain.model.EpgMatchType
import com.streamvault.domain.model.EpgOverrideCandidate
import com.streamvault.domain.model.EpgSourceType
import com.streamvault.data.parser.XmltvParser
+import com.streamvault.data.util.ProviderInputSanitizer
import com.streamvault.data.util.UrlSecurityPolicy
import com.streamvault.data.remote.http.HttpRequestProfile
import com.streamvault.data.remote.http.safeRequestIdentitySummary
@@ -96,8 +97,9 @@ class EpgSourceRepositoryImpl @Inject constructor(
epgSourceDao.getById(id)?.toDomain()
override suspend fun addSource(name: String, url: String): Result<EpgSource> {
- val trimmedUrl = url.trim()
- if (trimmedUrl.isBlank()) return Result.error("URL cannot be empty")
+ val trimmed = url.trim()
+ if (trimmed.isBlank()) return Result.error("URL cannot be empty")
+ val trimmedUrl = ProviderInputSanitizer.resolveUrlProtocol(trimmed)
val validationError = UrlSecurityPolicy.validateOptionalEpgUrl(trimmedUrl)
if (validationError != null) return Result.error(validationError)
@@ -117,7 +119,8 @@ class EpgSourceRepositoryImpl @Inject constructor(
}
override suspend fun updateSource(source: EpgSource): Result<Unit> {
- val trimmedUrl = source.url.trim()
+ val trimmed = source.url.trim()
+ val trimmedUrl = if (trimmed.isBlank()) trimmed else ProviderInputSanitizer.resolveUrlProtocol(trimmed)
val validationError = UrlSecurityPolicy.validateOptionalEpgUrl(trimmedUrl)
if (validationError != null) return Result.error(validationError)
diff --git a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
index ad0854f5..bc86b11e 100644
--- a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
+++ b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
@@ -289,7 +289,9 @@ class ProviderRepositoryImpl @Inject constructor(
onProgress: ((String) -> Unit)?,
id: Long?
): Result<Provider> = try {
- val normalizedUrl = ProviderInputSanitizer.normalizeUrl(url)
+ val normalizedUrl = ProviderInputSanitizer.resolveUrlProtocol(
+ ProviderInputSanitizer.normalizeUrl(url)
+ )
val normalizedName = ProviderInputSanitizer.normalizeProviderName(name)
ProviderInputSanitizer.validateUrl(normalizedUrl)?.let { message ->
@@ -369,7 +371,9 @@ class ProviderRepositoryImpl @Inject constructor(
id: Long?
): Result<Provider> {
return try {
- val normalizedServerUrl = ProviderInputSanitizer.normalizeUrl(serverUrl)
+ val normalizedServerUrl = ProviderInputSanitizer.resolveUrlProtocol(
+ ProviderInputSanitizer.normalizeUrl(serverUrl)
+ )
val normalizedUsername = ProviderInputSanitizer.normalizeUsername(username)
val normalizedPassword = ProviderInputSanitizer.normalizePassword(password)
val normalizedName = ProviderInputSanitizer.normalizeProviderName(name)
@@ -427,7 +431,9 @@ class ProviderRepositoryImpl @Inject constructor(
serverUrl: String, name: String, onCode: ((String) -> Unit)?, onProgress: ((String) -> Unit)?, id: Long?
): Result<Provider> {
return try {
- val normalizedServerUrl = ProviderInputSanitizer.normalizeUrl(serverUrl)
+ val normalizedServerUrl = ProviderInputSanitizer.resolveUrlProtocol(
+ ProviderInputSanitizer.normalizeUrl(serverUrl)
+ )
val normalizedName = ProviderInputSanitizer.normalizeProviderName(name)
ProviderInputSanitizer.validateUrl(normalizedServerUrl)?.let { return Result.error(it) }
val providerName = normalizedName.ifBlank {
diff --git a/data/src/main/java/com/streamvault/data/util/ProviderInputSanitizer.kt b/data/src/main/java/com/streamvault/data/util/ProviderInputSanitizer.kt
index f51644cb..669c50e4 100644
--- a/data/src/main/java/com/streamvault/data/util/ProviderInputSanitizer.kt
+++ b/data/src/main/java/com/streamvault/data/util/ProviderInputSanitizer.kt
@@ -57,8 +57,10 @@ object ProviderInputSanitizer {
}
suspend fun resolveUrlProtocol(url: String): String {
- val protocolMatch = URL_PROTOCOL_REGEX.find(url)
- if (protocolMatch != null) return url
+ // If the input already carries any scheme (http, https, file, content, …)
+ // respect it — only schemeless input needs protocol probing. In particular
+ // this keeps file:// and content:// playlist/EPG URLs intact.
+ if (URL_SCHEME_REGEX.containsMatchIn(url)) return url
return kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) {
val httpsUrl = "https://$url"
try {
@@ -71,15 +73,22 @@ object ProviderInputSanitizer {
connection.connect()
val responseCode = connection.responseCode
connection.disconnect()
- // Only accept 2xx (OK) — 3xx redirect means the server
- // is sending traffic elsewhere (often to plain HTTP).
- if (responseCode in 200..299) httpsUrl else "http://$url"
+ if (httpsProbeAccepts(responseCode)) httpsUrl else "http://$url"
} catch (_: Exception) {
"http://$url"
}
}
}
+ /**
+ * Decides whether an HTTPS probe response proves the endpoint genuinely
+ * serves content over HTTPS. Only 2xx qualifies: a 3xx redirect usually
+ * points elsewhere (frequently back to plain HTTP), and 4xx/5xx at the
+ * probe target are treated as "not usable over HTTPS", so we fall back to
+ * HTTP — the common case for self-hosted / third-party IPTV endpoints.
+ */
+ internal fun httpsProbeAccepts(responseCode: Int): Boolean = responseCode in 200..299
+
fun normalizeUsername(input: String): String = sanitizeSingleLine(input, MAX_USERNAME_LENGTH).trim()
fun normalizePassword(input: String): String = sanitizeRaw(input, MAX_PASSWORD_LENGTH)
@@ -173,4 +182,5 @@ object ProviderInputSanitizer {
private val HEADER_SEPARATOR_REGEX = Regex("\\s*\\|\\s*")
private val MAC_ADDRESS_REGEX = Regex("^[0-9A-F]{2}(?::[0-9A-F]{2}){5}$")
private val URL_PROTOCOL_REGEX = Regex("^https?://", RegexOption.IGNORE_CASE)
+ private val URL_SCHEME_REGEX = Regex("^[a-zA-Z][a-zA-Z0-9+.-]*://")
}
diff --git a/data/src/main/java/com/streamvault/data/util/UrlSecurityPolicy.kt b/data/src/main/java/com/streamvault/data/util/UrlSecurityPolicy.kt
index f457dac2..f20aa4b4 100644
--- a/data/src/main/java/com/streamvault/data/util/UrlSecurityPolicy.kt
+++ b/data/src/main/java/com/streamvault/data/util/UrlSecurityPolicy.kt
@@ -65,12 +65,13 @@ object UrlSecurityPolicy {
if (containsNewlines(url)) {
return "Playlist sources must use HTTP, HTTPS, or point to a local file."
}
- val scheme = parseScheme(url) ?: return "Playlist sources must use HTTP, HTTPS, or point to a local file."
+ val withScheme = ensureScheme(url.trim())
+ val scheme = parseScheme(withScheme) ?: return "Playlist sources must use HTTP, HTTPS, or point to a local file."
if (scheme in playlistLocalSchemes) {
return null
}
return validateRemoteUrl(
- url = url,
+ url = withScheme,
allowedSchemes = playlistSourceSchemes,
invalidSchemeMessage = "Playlist sources must use HTTP, HTTPS, or point to a local file.",
missingHostMessage = "Playlist sources must include a host.",
@@ -86,7 +87,8 @@ object UrlSecurityPolicy {
url.startsWith("content://") -> null // SAF local file; validated by OS file picker
// Allow http:// as well as https:// — many IPTV portals serve their XMLTV
// EPG endpoint over plain HTTP on non-standard ports (same policy as playlists).
- !containsNewlines(url) && hasAllowedScheme(url, playlistSourceSchemes) -> null
+ // Bare hostnames are accepted here too and resolved before persistence.
+ !containsNewlines(url) && hasAllowedScheme(ensureScheme(url.trim()), playlistSourceSchemes) -> null
else -> "EPG URLs must use HTTP, HTTPS, or select a local file."
}
}
@@ -120,6 +122,16 @@ object UrlSecurityPolicy {
?.lowercase(Locale.ROOT)
}
+ // Matches an RFC-3986 scheme prefix (e.g. http://, https://, file://, content://)
+ // so schemeless input can be detected reliably rather than via a loose "://" search.
+ private val SCHEME_PREFIX_REGEX = Regex("^[a-zA-Z][a-zA-Z0-9+.-]*://")
+
+ // Prepend https:// to schemeless input so it can be parsed and host-checked.
+ // resolveUrlProtocol (applied before persistence) later downgrades to http://
+ // when the server doesn't actually serve HTTPS.
+ private fun ensureScheme(url: String): String =
+ if (SCHEME_PREFIX_REGEX.containsMatchIn(url)) url else "https://$url"
+
private fun validateRemoteUrl(
url: String,
allowedSchemes: Set<String>,
@@ -131,10 +143,7 @@ object UrlSecurityPolicy {
): String? {
if (containsNewlines(url)) return invalidSchemeMessage
- val trimmed = url.trim()
- // If no scheme, prepend https:// — resolveUrlProtocol will later
- // downgrade to http:// if the server doesn't support HTTPS.
- val withScheme = if (trimmed.contains("://")) trimmed else "https://$trimmed"
+ val withScheme = ensureScheme(url.trim())
val uri = runCatching { URI(withScheme) }.getOrNull() ?: return invalidSchemeMessage
val scheme = uri.scheme?.lowercase(Locale.ROOT) ?: return invalidSchemeMessage
if (scheme !in allowedSchemes) return invalidSchemeMessage
diff --git a/data/src/test/java/com/streamvault/data/util/ProviderInputSanitizerTest.kt b/data/src/test/java/com/streamvault/data/util/ProviderInputSanitizerTest.kt
index f93fb09c..5dd3b083 100644
--- a/data/src/test/java/com/streamvault/data/util/ProviderInputSanitizerTest.kt
+++ b/data/src/test/java/com/streamvault/data/util/ProviderInputSanitizerTest.kt
@@ -47,4 +47,25 @@ class ProviderInputSanitizerTest {
assertThat(value).isEqualTo("http://$host")
}
+
+ @Test
+ fun `resolveUrlProtocol keeps non-http schemes unchanged without probing`() = runBlocking {
+ assertThat(ProviderInputSanitizer.resolveUrlProtocol("file:///storage/emulated/0/playlist.m3u"))
+ .isEqualTo("file:///storage/emulated/0/playlist.m3u")
+ assertThat(ProviderInputSanitizer.resolveUrlProtocol("content://downloads/public_downloads/1"))
+ .isEqualTo("content://downloads/public_downloads/1")
+ }
+
+ @Test
+ fun `httpsProbeAccepts only accepts 2xx responses`() {
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(200)).isTrue()
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(204)).isTrue()
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(299)).isTrue()
+ // 3xx (redirect elsewhere) and 4xx/5xx fall back to HTTP.
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(301)).isFalse()
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(302)).isFalse()
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(401)).isFalse()
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(404)).isFalse()
+ assertThat(ProviderInputSanitizer.httpsProbeAccepts(500)).isFalse()
+ }
}
\ No newline at end of file
diff --git a/data/src/test/java/com/streamvault/data/util/UrlSecurityPolicyTest.kt b/data/src/test/java/com/streamvault/data/util/UrlSecurityPolicyTest.kt
index 2ca4e7d6..e963d9fd 100644
--- a/data/src/test/java/com/streamvault/data/util/UrlSecurityPolicyTest.kt
+++ b/data/src/test/java/com/streamvault/data/util/UrlSecurityPolicyTest.kt
@@ -92,4 +92,26 @@ class UrlSecurityPolicyTest {
assertThat(UrlSecurityPolicy.sanitizeImportedAssetUrl("https://example.com/logo.png"))
.isEqualTo("https://example.com/logo.png")
}
+
+ @Test
+ fun `validateXtreamServerUrl accepts bare hostname without scheme`() {
+ assertThat(UrlSecurityPolicy.validateXtreamServerUrl("provider.example.com")).isNull()
+ assertThat(UrlSecurityPolicy.validateXtreamServerUrl("provider.example.com:8080")).isNull()
+ }
+
+ @Test
+ fun `validatePlaylistSourceUrl accepts bare hostname without scheme`() {
+ assertThat(UrlSecurityPolicy.validatePlaylistSourceUrl("example.com/playlist.m3u")).isNull()
+ }
+
+ @Test
+ fun `validateOptionalEpgUrl accepts bare hostname without scheme`() {
+ assertThat(UrlSecurityPolicy.validateOptionalEpgUrl("epg.example.com/guide.xml")).isNull()
+ }
+
+ @Test
+ fun `bare hostname with embedded credentials is still rejected`() {
+ assertThat(UrlSecurityPolicy.validateXtreamServerUrl("[email protected]"))
+ .isEqualTo("Xtream server URLs must not include embedded credentials.")
+ }
}