youpipe: guard untrusted ids, segments, streams and tokens
- Commit
e2ec61492cf655aee86a97fc82c67b316a5ae9c4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 02:17 UTC (17 days ago)
- Parents
a790a75
Several throw paths were reachable from user, server or provider input and escaped as crashes: - videoURLtoID force-unwraps the v= query parameter and decodes it into a fixed 8-byte buffer. A pasted watch URL with a short or invalid v= threw from resolveWatchUrl in search, and stream items with undecodable URLs threw while mapping feeds. Call sites now runCatching-skip those items/queries; Takeout per-entry parsing now skips any malformed entry instead of only IllegalArgumentException ones. - SponsorSegment start/end indexed segment[0]/segment[1] directly; a mirror or shard entry with fewer than two timestamps crashed the playback position loop and seek bar. Missing values now degrade to a zero-length segment. - Search results used thumbnails.first() for channel items; a channel with no thumbnails threw NoSuchElementException. Now falls back to an empty thumbnail URL. - Import/restore used openInputStream(uri)!! and handed a possibly null stream to ZipInputStream; SAF providers may legally return null. Null now surfaces as IOException, which the existing error handling already reports as an unsupported file. - SABR parseSabrIntegrityTokenData indexed a server JSON array without a size check; a 0/1-element GenerateIT response crashed a bare thread. It now fails as IllegalArgumentException, which the caller already catches. - Extractor: a non-numeric mix playlist index query parameter threw NumberFormatException out of onFetchPage; it is now ignored via toIntOrNull. Verified: ./gradlew :youpipe:testDevUnitTest :youpipe:extractor:test (pass), ./check youpipe (pass).
Diff
youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.kt
diff --git a/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.kt b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.ktindex 49fa5a3b6c..16dbe93d04 100644--- a/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.kt+++ b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.kt@@ -67,8 +67,10 @@ class YoutubeMixPlaylistExtractor( if (videoId != null) { jsonBodyBuilder.value("videoId", videoId) }- if (playlistIndexString != null) {- jsonBodyBuilder.value("playlistIndex", playlistIndexString.toInt())+ // A non-numeric index in a user-supplied mix URL is ignored, not+ // allowed to escape onFetchPage as NumberFormatException.+ playlistIndexString?.toIntOrNull()?.let { playlistIndex ->+ jsonBodyBuilder.value("playlistIndex", playlistIndex) } val body = jsonBodyBuilder.done().toString().toByteArray(Charsets.UTF_8)
youpipe/src/main/java/com/vayunmathur/youpipe/network/Extractor.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/network/Extractor.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/network/Extractor.ktindex 6c1e943434..2678eb5aba 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/network/Extractor.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/network/Extractor.kt@@ -24,8 +24,10 @@ data class SponsorSegment( val segment: List<Float>, @SerialName("UUID") val uuid: String, ) {- val start: Long get() = (segment[0] * 1000).toLong()- val end: Long get() = (segment[1] * 1000).toLong()+ // Mirror/shard data is untrusted: a segment with fewer than two+ // timestamps must degrade to a zero-length segment, not crash playback.+ val start: Long get() = ((segment.getOrNull(0) ?: 0f) * 1000).toLong()+ val end: Long get() = ((segment.getOrNull(1) ?: 0f) * 1000).toLong() } /** Decodes HTML entities/markup to plain text. */@@ -35,9 +37,12 @@ fun String.decodeHtml(): String = /** Maps a NewPipe [StreamInfoItem] to a [VideoInfo], or null if it has no upload date. */ fun StreamInfoItem.toVideoInfo(): VideoInfo? { val date = getUploadDate() ?: return null+ // A stream item whose URL carries no decodable video id is skipped+ // (null), not allowed to crash the feed/search mapping.+ val videoId = runCatching { videoURLtoID(url) }.getOrNull() ?: return null return VideoInfo( name.decodeHtml(),- videoURLtoID(url),+ videoId, getDuration(), getViewCount(), date.instant.toKotlinInstant(),
youpipe/src/main/java/com/vayunmathur/youpipe/network/sabr/SabrLocalDomPoTokenUtil.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/network/sabr/SabrLocalDomPoTokenUtil.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/network/sabr/SabrLocalDomPoTokenUtil.ktindex a98b898fd1..418b3900ca 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/network/sabr/SabrLocalDomPoTokenUtil.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/network/sabr/SabrLocalDomPoTokenUtil.kt@@ -28,6 +28,9 @@ internal fun buildSabrAttChallengeData( internal fun parseSabrIntegrityTokenData(rawIntegrityTokenData: String): Pair<String, Long> { val integrityTokenData = JsonUtils.toJsonArray(rawIntegrityTokenData)+ // A short array from the GenerateIT endpoint is an argument failure+ // the caller already handles; indexing it would crash the raw thread.+ require(integrityTokenData.size >= 2) { "Integrity token data has fewer than 2 elements" } return base64ToU8(integrityTokenData[0].jsonPrimitive.content) to integrityTokenData[1].jsonPrimitive.content.toLong() }
youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeImportExport.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeImportExport.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeImportExport.ktindex f3c4389ab7..2c7fb437ed 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeImportExport.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeImportExport.kt@@ -44,7 +44,9 @@ fun YouPipeViewModel.importYouTubeTakeout(uri: Uri) { } private suspend fun YouPipeViewModel.importTakeoutZip(ctx: Application, uri: Uri) {- ZipInputStream(ctx.contentResolver.openInputStream(uri)).use { zipInputStream ->+ // SAF providers may legally return null; surface it as IOException so+ // the caller's existing error path reports an unsupported file.+ ZipInputStream(ctx.contentResolver.openInputStream(uri) ?: throw java.io.IOException("Provider returned no stream")).use { zipInputStream -> var entry = zipInputStream.nextEntry val subs = mutableListOf<Subscription>() val history = mutableListOf<HistoryVideo>()@@ -101,7 +103,9 @@ private fun importTakeoutJsonHistory( jsonArray.forEach { element -> try { parseTakeoutJsonEntry(element)?.let { history.add(it) }- } catch (e: IllegalArgumentException) {+ } catch (e: Exception) {+ // One malformed entry (incl. an undecodable video id) skips+ // that entry only; it must not abort or crash the import. Log.error(TAG, "Error parsing history item", e) } }@@ -150,7 +154,9 @@ private fun importTakeoutHtmlHistory( regex.findAll(html).forEach { match -> try { parseTakeoutHtmlEntry(match)?.let { history.add(it) }- } catch (e: IllegalArgumentException) {+ } catch (e: Exception) {+ // One malformed entry (incl. an undecodable video id) skips+ // that entry only; it must not abort or crash the import. Log.error(TAG, "Error parsing HTML history item", e) } }@@ -199,7 +205,7 @@ fun YouPipeViewModel.restoreSubscriptions(uri: Uri) { viewModelScope.launch(Dispatchers.IO) { isImportingMutable.value = true try {- val json = ctx.contentResolver.openInputStream(uri)!!.bufferedReader().readText()+ val json = (ctx.contentResolver.openInputStream(uri) ?: throw java.io.IOException("Provider returned no stream")).bufferedReader().readText() val subs = decodeSubscriptionBackup(json) if (subs == null) { // #565: files shaped like {"version":1,"salt":"..."} are an encrypted/salted@@ -241,7 +247,7 @@ fun YouPipeViewModel.importNewPipe(uri: Uri) { isImportingMutable.value = true importProgressMutable.value = 0f try {- val jsonString = ctx.contentResolver.openInputStream(uri)!!.bufferedReader().readText()+ val jsonString = (ctx.contentResolver.openInputStream(uri) ?: throw java.io.IOException("Provider returned no stream")).bufferedReader().readText() val json = Json.parseToJsonElement(jsonString).jsonObject val subsArray = json["subscriptions"]?.jsonArray if (subsArray != null) {
youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeViewModel.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeViewModel.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeViewModel.ktindex 902bffbdc0..7b4d01ba33 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeViewModel.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/YouPipeViewModel.kt@@ -337,7 +337,7 @@ class YouPipeViewModel( channelURLtoID(item.url), item.getSubscriberCount(), 0,- item.thumbnails.first().url,+ item.thumbnails.firstOrNull()?.url ?: "", ) else -> null }@@ -682,5 +682,6 @@ class YouPipeViewModelFactory( /** Returns the resolved videoID if the current search query is a watch URL, else null. */ fun YouPipeViewModel.resolveWatchUrl(): Long? { val q = searchQuery.value- return if (q.contains("/watch?v=")) videoURLtoID(q) else null+ // User-typed/pasted query: a malformed v= value must not crash search.+ return if (q.contains("/watch?v=")) runCatching { videoURLtoID(q) }.getOrNull() else null }