Skip to content

Native Android client for Spotify, built with Jetpack Compose

Extension media and downloads through a safe client: local and private addresses refused after DNS, redirects followed by hand (5 at most) with each hop re-checked, extension headers dropped on another host, no https to http, refused hops logged without query strings

kota-zo081cc15c250b
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/DashFlacRemux.kt b/base/app/src/main/java/com/music/spotui/data/offline/DashFlacRemux.kt
index 4a6b2185..93172029 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/DashFlacRemux.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/DashFlacRemux.kt
@@ -110,16 +110,9 @@ internal object DashFlacRemux {
}
private fun httpBytes(url: String, headers: Map<String, String>): ByteArray? = runCatching {
- val conn = (java.net.URL(url).openConnection() as java.net.HttpURLConnection).apply {
- connectTimeout = 15_000
- readTimeout = 30_000
- setRequestProperty("User-Agent", UA)
- headers.forEach { (k, v) -> setRequestProperty(k, v) }
- }
- try {
- if (conn.responseCode !in 200..299) null else conn.inputStream.use { it.readBytes() }
- } finally {
- conn.disconnect()
+ // The safe client (see MediaHttp): no local address, redirects re-checked, the extension's headers kept on its own host only.
+ com.music.spotui.extensions.MediaHttp.open(url, headers = headers, fixed = mapOf("User-Agent" to UA), timeoutMs = 30_000).use { c ->
+ if (c.response.code !in 200..299) null else c.response.body.bytes()
}
}.getOrNull()
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/DashLive.kt b/base/app/src/main/java/com/music/spotui/data/offline/DashLive.kt
index 8a26c73b..75702a00 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/DashLive.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/DashLive.kt
@@ -1,8 +1,7 @@
package com.music.spotui.data.offline
import java.io.IOException
-import java.net.HttpURLConnection
-import java.net.URL
+import com.music.spotui.extensions.MediaHttp
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.FutureTask
@@ -166,39 +165,25 @@ internal class DashLive(
return out.array()
}
- private fun connect(url: String, method: String, headers: Map<String, String>, userAgent: String): HttpURLConnection =
- (URL(url).openConnection() as HttpURLConnection).apply {
- requestMethod = method
- connectTimeout = 10_000
- readTimeout = 15_000
- setRequestProperty("User-Agent", userAgent)
- headers.forEach { (k, v) -> setRequestProperty(k, v) }
- }
+ /** Through the safe client (see [MediaHttp]): the extension's headers go to its own host only, the app's User-Agent and Range anywhere. */
+ private fun connect(url: String, method: String, headers: Map<String, String>, userAgent: String, range: String? = null) =
+ MediaHttp.open(url, method, headers, buildMap { put("User-Agent", userAgent); range?.let { put("Range", it) } })
- private fun httpBytes(url: String, headers: Map<String, String>, userAgent: String): ByteArray {
- val c = connect(url, "GET", headers, userAgent)
- try {
- if (c.responseCode !in 200..299) throw IOException("segment HTTP ${c.responseCode}")
- return c.inputStream.use { it.readBytes() }
- } finally {
- c.disconnect()
+ private fun httpBytes(url: String, headers: Map<String, String>, userAgent: String): ByteArray =
+ connect(url, "GET", headers, userAgent).use { c ->
+ if (c.response.code !in 200..299) throw IOException("segment HTTP ${c.response.code}")
+ c.response.body.bytes()
}
- }
/** A segment's length: HEAD, else the total of a one-byte range. */
private fun size(url: String, headers: Map<String, String>, userAgent: String): Long {
- val h = connect(url, "HEAD", headers, userAgent)
- try {
- if (h.responseCode in 200..299 && h.contentLengthLong > 0) return h.contentLengthLong
- } finally {
- h.disconnect()
+ connect(url, "HEAD", headers, userAgent).use { h ->
+ val n = h.response.header("Content-Length")?.toLongOrNull() ?: -1L
+ if (h.response.code in 200..299 && n > 0) return n
}
- val r = connect(url, "GET", headers + ("Range" to "bytes=0-0"), userAgent)
- try {
- val total = r.getHeaderField("Content-Range")?.substringAfter('/')?.toLongOrNull()
- return total ?: throw IOException("segment size unknown (HTTP ${r.responseCode})")
- } finally {
- r.disconnect()
+ connect(url, "GET", headers, userAgent, range = "bytes=0-0").use { r ->
+ val total = r.response.header("Content-Range")?.substringAfter('/')?.toLongOrNull()
+ return total ?: throw IOException("segment size unknown (HTTP ${r.response.code})")
}
}
}
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt
index 8c69eb07..22010c90 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt
@@ -136,6 +136,10 @@ object OfflineCrypto {
private var uri: Uri? = null
private var left = 0L
private var usingOther = false
+ /** The address an extension gave is read through the safe client, never through [other] (see [MediaDataSource]). */
+ private val net = com.music.spotui.extensions.MediaDataSource()
+ /** Which of [other] and [net] is open when [usingOther]. */
+ private var active: DataSource = other
/** An extension's DASH answer, read as one file (see [DashLive]). */
private var dash: DashLive? = null
private var dashAt = 0L
@@ -156,7 +160,7 @@ object OfflineCrypto {
if (stream.manifest != null) openDash(dataSpec, stream) else openUrl(dataSpec, stream)
} catch (e: IOException) {
// Closed before it is opened again (it keeps the source that failed).
- runCatching { other.close() }
+ runCatching { net.close() }
throw e
}
},
@@ -168,6 +172,7 @@ object OfflineCrypto {
val file = dataSpec.uri.takeIf { it.scheme == "file" }?.path?.let(::File)
if (file == null || !isEncrypted(file)) {
usingOther = true
+ active = other
return other.open(dataSpec)
}
transferInitializing(dataSpec)
@@ -186,23 +191,24 @@ object OfflineCrypto {
/** The address an extension gave; from its start read ahead when there is one (else as usual). */
private fun openUrl(dataSpec: DataSpec, stream: app.myspoti.extensions.AudioStream): Long {
+ active = net
val spec = dataSpec.buildUpon().setUri(Uri.parse(stream.url))
.setHttpRequestHeaders(dataSpec.httpRequestHeaders + stream.headers).build()
val ahead = if (dataSpec.position == 0L && dataSpec.length == C.LENGTH_UNSET.toLong())
StreamHead.take(dataSpec.uri.lastPathSegment.orEmpty(), stream.url) else null
if (ahead != null) {
try {
- val rest = other.open(spec.buildUpon().setPosition(ahead.bytes.size.toLong()).build())
+ val rest = net.open(spec.buildUpon().setPosition(ahead.bytes.size.toLong()).build())
head = ahead.bytes
headAt = 0
android.util.Log.d(LOG, "stream: opened from its read-ahead start (${ahead.bytes.size} bytes), ${if (rest == C.LENGTH_UNSET.toLong()) "length unknown" else "${ahead.bytes.size + rest} bytes"}")
return if (rest == C.LENGTH_UNSET.toLong()) rest else ahead.bytes.size + rest
} catch (e: IOException) {
// The rest could not be had from there: the whole file, as without a head.
- runCatching { other.close() }
+ runCatching { net.close() }
}
}
- return other.open(spec).also { n ->
+ return net.open(spec).also { n ->
android.util.Log.d(LOG, "stream: opened at ${dataSpec.position}, ${if (n == C.LENGTH_UNSET.toLong()) "length unknown" else "$n bytes"}")
}
}
@@ -230,7 +236,7 @@ object OfflineCrypto {
}
head = null
}
- if (usingOther) return other.read(buffer, offset, length)
+ if (usingOther) return active.read(buffer, offset, length)
dash?.let { live ->
if (length == 0) return 0
if (left == 0L) return C.RESULT_END_OF_INPUT
@@ -251,11 +257,11 @@ object OfflineCrypto {
return n
}
- override fun getUri(): Uri? = if (usingOther) other.uri else uri
+ override fun getUri(): Uri? = if (usingOther) active.uri else uri
override fun close() {
head = null
- if (usingOther) { usingOther = false; other.close(); return }
+ if (usingOther) { usingOther = false; active.close(); return }
if (dash != null) { dash = null; if (uri != null) { uri = null; transferEnded() }; return }
raf?.close(); raf = null; cipher = null
if (uri != null) { uri = null; transferEnded() }
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
index ac6cb09d..4d0204de 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
@@ -27,8 +27,7 @@ import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.io.IOException
-import java.net.HttpURLConnection
-import java.net.URL
+import com.music.spotui.extensions.MediaHttp
import java.util.concurrent.ConcurrentHashMap
/**
@@ -492,43 +491,36 @@ object OfflineDownloader {
while (true) {
attempt++
val position = output.length()
- val conn = (URL(url).openConnection() as HttpURLConnection).apply {
- connectTimeout = 20_000
- readTimeout = 60_000
- instanceFollowRedirects = true
- setRequestProperty("User-Agent", DownloadUa)
- headers.forEach { (k, v) -> setRequestProperty(k, v) }
- setRequestProperty("Range", "bytes=$position-$end")
- }
+ // The safe client (see MediaHttp): redirects followed by hand and re-checked, the headers kept on their own host only.
try {
- val code = conn.responseCode
- // Asked past the end of a file whose size was unknown: it is complete.
- if (code == 416 && total <= 0 && position > 0) { done = true; break }
- if (code !in 200..299) throw IOException("stream HTTP $code")
- val full = code != 206 // server ignored Range → the whole body, from byte 0
- if (full) output.setLength(0)
- if (total <= 0) total = contentRangeTotal(conn.getHeaderField("Content-Range")) ?: -1L
- output.seek(output.length())
- val expected = if (full) -1L else end - position + 1
- var got = 0L
- conn.inputStream.use { input ->
- val buf = ByteArray(256 * 1024)
- while (true) {
- val n = input.read(buf)
- if (n < 0) break
- output.write(buf, 0, n)
- got += n
+ MediaHttp.open(url, headers = headers, fixed = mapOf("User-Agent" to DownloadUa, "Range" to "bytes=$position-$end"), timeoutMs = 60_000).use { conn ->
+ val code = conn.response.code
+ // Asked past the end of a file whose size was unknown: it is complete.
+ if (code == 416 && total <= 0 && position > 0) { done = true; return@use }
+ if (code !in 200..299) throw IOException("stream HTTP $code")
+ val full = code != 206 // server ignored Range → the whole body, from byte 0
+ if (full) output.setLength(0)
+ if (total <= 0) total = contentRangeTotal(conn.response.header("Content-Range")) ?: -1L
+ output.seek(output.length())
+ val expected = if (full) -1L else end - position + 1
+ var got = 0L
+ conn.response.body.byteStream().use { input ->
+ val buf = ByteArray(256 * 1024)
+ while (true) {
+ val n = input.read(buf)
+ if (n < 0) break
+ output.write(buf, 0, n)
+ got += n
+ }
}
+ if (full) done = true
+ // A short chunk with the size still unknown is the end of the stream.
+ else if (total <= 0 && got < expected) done = true
}
- if (full) done = true
- // A short chunk with the size still unknown is the end of the stream.
- else if (total <= 0 && got < expected) done = true
break
} catch (e: IOException) {
- if (attempt >= 3) throw e
+ if (attempt >= 3 || e is app.myspoti.extensions.ExtensionMediaHttp.Refused || app.myspoti.extensions.SafeNet.isRefusal(e)) throw e
Log.w(TAG, "chunk at ${output.length()} failed (attempt $attempt): ${e.message}")
- } finally {
- conn.disconnect()
}
}
if (done) break
@@ -556,18 +548,8 @@ object OfflineDownloader {
private fun contentLength(url: String, headers: Map<String, String> = emptyMap()): Long =
runCatching {
- val conn = (URL(url).openConnection() as HttpURLConnection).apply {
- requestMethod = "HEAD"
- connectTimeout = 15_000
- readTimeout = 15_000
- instanceFollowRedirects = true
- setRequestProperty("User-Agent", DownloadUa)
- headers.forEach { (k, v) -> setRequestProperty(k, v) }
- }
- try {
- conn.contentLengthLong.takeIf { it > 0 } ?: -1L
- } finally {
- conn.disconnect()
+ MediaHttp.open(url, "HEAD", headers, mapOf("User-Agent" to DownloadUa)).use { conn ->
+ conn.response.header("Content-Length")?.toLongOrNull()?.takeIf { it > 0 } ?: -1L
}
}.getOrDefault(-1L)
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/StreamHead.kt b/base/app/src/main/java/com/music/spotui/data/offline/StreamHead.kt
index 2705b639..b56a7cf1 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/StreamHead.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/StreamHead.kt
@@ -1,7 +1,6 @@
package com.music.spotui.data.offline
-import java.net.HttpURLConnection
-import java.net.URL
+import com.music.spotui.extensions.MediaHttp
/**
* The first bytes of the next song an extension streams (an `audio-source` answer with a url),
@@ -43,19 +42,14 @@ internal object StreamHead {
* song [key] until [until]. Stops early, keeping nothing, once [active] says the song changed.
*/
fun fetch(key: String, url: String, headers: Map<String, String>, until: Long, active: () -> Boolean) {
- val c = (URL(url).openConnection() as HttpURLConnection).apply {
- connectTimeout = 10_000
- readTimeout = 15_000
- headers.forEach { (k, v) -> setRequestProperty(k, v) }
- setRequestProperty("Range", "bytes=0-${BYTES - 1}")
- }
- try {
- if (c.responseCode != 206) return
- val total = total(c.getHeaderField("Content-Range")) ?: return
+ // The safe client: no local address, redirects re-checked, the extension's headers kept on its own host only.
+ MediaHttp.open(url, headers = headers, fixed = mapOf("Range" to "bytes=0-${BYTES - 1}")).use { c ->
+ if (c.response.code != 206) return
+ val total = total(c.response.header("Content-Range")) ?: return
if (total <= BYTES) return
val buf = ByteArray(BYTES)
var at = 0
- c.inputStream.use { s ->
+ c.response.body.byteStream().use { s ->
while (at < BYTES) {
if (!active()) return
val n = s.read(buf, at, BYTES - at)
@@ -64,8 +58,6 @@ internal object StreamHead {
}
}
if (at == BYTES && active()) put(Head(key, url, buf, total, until))
- } finally {
- c.disconnect()
}
}
}
diff --git a/base/app/src/main/java/com/music/spotui/extensions/MediaDataSource.kt b/base/app/src/main/java/com/music/spotui/extensions/MediaDataSource.kt
new file mode 100644
index 00000000..3788a0cc
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/extensions/MediaDataSource.kt
@@ -0,0 +1,134 @@
+package com.music.spotui.extensions
+
+import android.net.Uri
+import androidx.media3.common.C
+import androidx.media3.common.PlaybackException
+import androidx.media3.common.util.UnstableApi
+import androidx.media3.datasource.BaseDataSource
+import androidx.media3.datasource.DataSource
+import androidx.media3.datasource.DataSourceException
+import androidx.media3.datasource.DataSpec
+import androidx.media3.datasource.HttpDataSource
+import androidx.media3.datasource.HttpUtil
+import app.myspoti.extensions.ExtensionMediaHttp
+import java.io.IOException
+import java.io.InputStream
+
+/**
+ * The player's HTTP data source for an address an extension gave: [MediaHttp] (never a local
+ * address, redirects followed by hand and re-checked, the extension's headers dropped on another
+ * host) with the same Range / seek behaviour as ExoPlayer's own: a Range header from the
+ * position, the bytes skipped when a server answers 200 to a Range, a 416 at the end of the file
+ * read as an empty rest. The headers are the data spec's (the extension's included).
+ */
+@UnstableApi
+internal class MediaDataSource : BaseDataSource(true), HttpDataSource {
+ private val props = HttpDataSource.RequestProperties()
+ private var opened: ExtensionMediaHttp.Opened? = null
+ private var input: InputStream? = null
+ private var spec: DataSpec? = null
+ private var started = false
+ private var toRead = C.LENGTH_UNSET.toLong()
+ private var skip = 0L
+ private var code = -1
+
+ override fun open(dataSpec: DataSpec): Long {
+ spec = dataSpec
+ toRead = 0; skip = 0
+ transferInitializing(dataSpec)
+ val fixed = LinkedHashMap<String, String>()
+ if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {
+ fixed["Range"] = "bytes=${dataSpec.position}-" + if (dataSpec.length != C.LENGTH_UNSET.toLong()) (dataSpec.position + dataSpec.length - 1).toString() else ""
+ }
+ System.getProperty("http.agent")?.let { fixed["User-Agent"] = it }
+ val o = try {
+ MediaHttp.open(dataSpec.uri.toString(), headers = props.snapshot + dataSpec.httpRequestHeaders, fixed = fixed, timeoutMs = TIMEOUT_MS)
+ } catch (e: IOException) {
+ throw HttpDataSource.HttpDataSourceException.createForIOException(e, dataSpec, HttpDataSource.HttpDataSourceException.TYPE_OPEN)
+ }
+ opened = o
+ val r = o.response
+ code = r.code
+ if (code !in 200..299) {
+ val message = r.message
+ val headers = r.headers.toMultimap()
+ // At the very end of the file: nothing left to read, not an error.
+ if (code == 416 && dataSpec.position == HttpUtil.getDocumentSize(r.header("Content-Range"))) {
+ started = true
+ transferStarted(dataSpec)
+ return if (dataSpec.length != C.LENGTH_UNSET.toLong()) dataSpec.length else 0
+ }
+ val body = runCatching { r.peekBody(4096).bytes() }.getOrDefault(ByteArray(0))
+ closeResponse()
+ throw HttpDataSource.InvalidResponseCodeException(code, message, null, headers, dataSpec, body)
+ }
+ skip = if (code == 200 && dataSpec.position != 0L) dataSpec.position else 0L
+ toRead = if (dataSpec.length != C.LENGTH_UNSET.toLong()) dataSpec.length else {
+ val length = HttpUtil.getContentLength(r.header("Content-Length"), r.header("Content-Range"))
+ if (length != C.LENGTH_UNSET.toLong()) length - skip else C.LENGTH_UNSET.toLong()
+ }
+ input = r.body.byteStream()
+ started = true
+ transferStarted(dataSpec)
+ try { skipBytes() } catch (e: IOException) {
+ closeResponse()
+ throw HttpDataSource.HttpDataSourceException.createForIOException(e, dataSpec, HttpDataSource.HttpDataSourceException.TYPE_OPEN)
+ }
+ return toRead
+ }
+
+ private fun skipBytes() {
+ val s = input ?: return
+ val junk = ByteArray(4096)
+ while (skip > 0) {
+ val n = s.read(junk, 0, minOf(skip, junk.size.toLong()).toInt())
+ if (n < 0) throw DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE)
+ skip -= n
+ bytesTransferred(n)
+ }
+ }
+
+ override fun read(buffer: ByteArray, offset: Int, length: Int): Int {
+ if (length == 0) return 0
+ if (toRead == 0L) return C.RESULT_END_OF_INPUT
+ val s = input ?: return C.RESULT_END_OF_INPUT
+ val want = if (toRead == C.LENGTH_UNSET.toLong()) length else minOf(length.toLong(), toRead).toInt()
+ val n = try {
+ s.read(buffer, offset, want)
+ } catch (e: IOException) {
+ throw HttpDataSource.HttpDataSourceException.createForIOException(e, spec!!, HttpDataSource.HttpDataSourceException.TYPE_READ)
+ }
+ if (n < 0) return C.RESULT_END_OF_INPUT
+ if (toRead != C.LENGTH_UNSET.toLong()) toRead -= n
+ bytesTransferred(n)
+ return n
+ }
+
+ override fun close() {
+ closeResponse()
+ if (started) { started = false; transferEnded() }
+ }
+
+ private fun closeResponse() {
+ runCatching { input?.close() }
+ runCatching { opened?.close() }
+ input = null; opened = null
+ }
+
+ override fun getUri(): Uri? = opened?.url?.toString()?.let(Uri::parse) ?: spec?.uri
+
+ override fun getResponseCode(): Int = code
+ override fun getResponseHeaders(): Map<String, List<String>> = opened?.response?.headers?.toMultimap().orEmpty()
+ override fun setRequestProperty(name: String, value: String) = props.set(name, value)
+ override fun clearRequestProperty(name: String) = props.remove(name)
+ override fun clearAllRequestProperties() = props.clear()
+
+ /** What opens the data source above. */
+ class Factory : DataSource.Factory {
+ override fun createDataSource(): DataSource = MediaDataSource()
+ }
+
+ private companion object {
+ const val TIMEOUT_MS = 15_000
+ }
+}
diff --git a/base/app/src/main/java/com/music/spotui/extensions/MediaHttp.kt b/base/app/src/main/java/com/music/spotui/extensions/MediaHttp.kt
new file mode 100644
index 00000000..4d802535
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/extensions/MediaHttp.kt
@@ -0,0 +1,17 @@
+package com.music.spotui.extensions
+
+import app.myspoti.extensions.ExtensionMediaHttp
+import app.myspoti.host.android.Logbook
+
+/** The app's one door for fetching an address an extension gave (see [ExtensionMediaHttp]); a refused hop goes to the log. */
+internal object MediaHttp {
+ val http = ExtensionMediaHttp(log = { Logbook.log(it) })
+
+ fun open(
+ url: String,
+ method: String = "GET",
+ headers: Map<String, String> = emptyMap(),
+ fixed: Map<String, String> = emptyMap(),
+ timeoutMs: Int = 15_000,
+ ): ExtensionMediaHttp.Opened = http.open(url, method, headers, fixed, timeoutMs)
+}
diff --git a/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionMediaHttp.kt b/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionMediaHttp.kt
new file mode 100644
index 00000000..2f377ad9
--- /dev/null
+++ b/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionMediaHttp.kt
@@ -0,0 +1,116 @@
+package app.myspoti.extensions
+
+import okhttp3.Headers
+import okhttp3.HttpUrl
+import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
+import okhttp3.Request
+import okhttp3.Response
+import java.io.Closeable
+import java.io.IOException
+import java.util.concurrent.TimeUnit
+
+/**
+ * Every fetch of a media address an extension gave (a stream, a download, a DASH manifest and its
+ * segments) goes through here, never through the platform's own HTTP stack:
+ * - the names are resolved by [SafeNet.SafeDns] (a local answer is refused), IP literals by the
+ * client's interceptor;
+ * - redirects are followed here, at most [MAX_REDIRECTS], and the scheme, the host and (when the
+ * connection is made) its address are checked again on every hop; a hop to https never goes
+ * back to http;
+ * - the headers the extension set (Authorization, tokens, cookies it chose to send) are dropped
+ * on a hop to another host or port; the app's own ([open]'s `fixed`: Range, ...) are kept;
+ * - the time the whole thing took is on the result, and a refused hop is logged without its
+ * query string.
+ */
+class ExtensionMediaHttp(
+ private val transport: Transport = DefaultTransport,
+ private val log: (String) -> Unit = {},
+ private val clock: () -> Long = { System.nanoTime() / 1_000_000 },
+) {
+ /** One request, no redirect followed, for at most [timeoutMs] per step. */
+ fun interface Transport {
+ @Throws(IOException::class)
+ fun execute(request: Request, timeoutMs: Int): Response
+ }
+
+ /** The response of the last hop; closing it ends the connection. */
+ class Opened(val response: Response, val url: HttpUrl, val redirects: Int, val elapsedMs: Long) : Closeable {
+ override fun close() = response.close()
+ }
+
+ /** An address that may not be fetched (not http(s), a local host, a downgrade, too many redirects). */
+ class Refused(message: String) : IOException(message)
+
+ /**
+ * Fetches [url] with the extension's [headers] and the app's [fixed] ones (these win on the same
+ * name). The response can be any status; only a 3xx with a `Location` is followed.
+ */
+ @Throws(IOException::class)
+ fun open(
+ url: String,
+ method: String = "GET",
+ headers: Map<String, String> = emptyMap(),
+ fixed: Map<String, String> = emptyMap(),
+ timeoutMs: Int = 15_000,
+ ): Opened {
+ val t0 = clock()
+ var current = url.toHttpUrlOrNull() ?: throw refuse(null, "not an http(s) address")
+ var sent = headers
+ var hops = 0
+ while (true) {
+ checkHop(current)
+ val request = Request.Builder().url(current).method(method, null).headers(build(sent, fixed)).build()
+ val response = try {
+ transport.execute(request, timeoutMs)
+ } catch (e: IOException) {
+ if (SafeNet.isRefusal(e)) log("media: refused ${SafeNet.redact(current)}: ${e.message}")
+ throw e
+ }
+ val location = response.header("Location")
+ if (response.code !in REDIRECTS || location == null) return Opened(response, current, hops, clock() - t0)
+ response.close()
+ if (hops >= MAX_REDIRECTS) throw refuse(current, "more than $MAX_REDIRECTS redirects")
+ val next = current.resolve(location) ?: throw refuse(current, "bad redirect")
+ if (current.isHttps && !next.isHttps) throw refuse(next, "redirect from https to http")
+ if (!sameOrigin(current, next)) sent = emptyMap()
+ hops++
+ current = next
+ }
+ }
+
+ private fun checkHop(url: HttpUrl) {
+ if (url.scheme != "https" && url.scheme != "http") throw refuse(url, "not http(s)")
+ if (SafeNet.isPrivateHost(url.host)) throw refuse(url, "${url.host} is a local address")
+ }
+
+ private fun refuse(url: HttpUrl?, why: String): Refused {
+ log("media: refused ${url?.let(SafeNet::redact) ?: "an address"}: $why")
+ return Refused(why)
+ }
+
+ companion object {
+ const val MAX_REDIRECTS = 5
+ private val REDIRECTS = setOf(301, 302, 303, 307, 308)
+
+ /** The same host and port: where the extension's headers may follow. */
+ fun sameOrigin(a: HttpUrl, b: HttpUrl): Boolean = a.host.equals(b.host, ignoreCase = true) && a.port == b.port
+
+ /** The extension's [headers] under the app's [fixed] ones; `Accept-Encoding: identity` unless either set it (media is never compressed). */
+ internal fun build(headers: Map<String, String>, fixed: Map<String, String>): Headers {
+ val b = Headers.Builder()
+ headers.forEach { (k, v) -> b.set(k, v) }
+ fixed.forEach { (k, v) -> b.set(k, v) }
+ if (b.get("Accept-Encoding") == null) b.set("Accept-Encoding", "identity")
+ return b.build()
+ }
+
+ /** The safe client, with [Transport]'s timeout as connect and read timeout. */
+ val DefaultTransport = Transport { request, timeoutMs ->
+ SafeNet.client.newBuilder()
+ .connectTimeout(timeoutMs.toLong(), TimeUnit.MILLISECONDS)
+ .readTimeout(timeoutMs.toLong(), TimeUnit.MILLISECONDS)
+ .build()
+ .newCall(request).execute()
+ }
+ }
+}
diff --git a/base/extensions/src/main/kotlin/app/myspoti/extensions/SafeNet.kt b/base/extensions/src/main/kotlin/app/myspoti/extensions/SafeNet.kt
index 28c15718..31c252fc 100644
--- a/base/extensions/src/main/kotlin/app/myspoti/extensions/SafeNet.kt
+++ b/base/extensions/src/main/kotlin/app/myspoti/extensions/SafeNet.kt
@@ -1,6 +1,7 @@
package app.myspoti.extensions
import okhttp3.Dns
+import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import java.net.Inet4Address
import java.net.Inet6Address
@@ -88,20 +89,26 @@ object SafeNet {
first == 0x0100 && (0..5).all { address[2 + it].toInt() == 0 } // 100::/64 discard
}
- /** Resolves names and refuses the whole answer when any address is local (no mix to pick from). */
- object SafeDns : Dns {
+ /** A host that is, or resolves to, a local address (the connection is never made). */
+ class LocalAddress(message: String) : UnknownHostException(message)
+
+ /** Resolves names with [upstream] and refuses the whole answer when any address is local (no mix to pick from). */
+ class CheckedDns(private val upstream: Dns = Dns.SYSTEM) : Dns {
override fun lookup(hostname: String): List<InetAddress> {
- if (isPrivateHost(hostname)) throw UnknownHostException("$hostname is a local address")
- val all = Dns.SYSTEM.lookup(hostname)
+ if (isPrivateHost(hostname)) throw LocalAddress("$hostname is a local address")
+ val all = upstream.lookup(hostname)
for (a in all) {
if (a is Inet4Address || a is Inet6Address) {
- if (isPrivateAddress(a.address) != false) throw UnknownHostException("$hostname resolves to a local address")
+ if (isPrivateAddress(a.address) != false) throw LocalAddress("$hostname resolves to a local address")
}
}
return all
}
}
+ /** The system resolver, checked. */
+ val SafeDns: Dns = CheckedDns()
+
/**
* An OkHttp client that never connects to a local address, follows no redirect (the caller
* decides, and checks the next address with [checkUrlHost]), keeps no cookie and no cache.
@@ -124,6 +131,20 @@ object SafeNet {
/** Throws when [host] is a local address (the name, or an IP literal). */
fun checkUrlHost(host: String) {
- if (isPrivateHost(host)) throw java.io.IOException("$host is a local address")
+ if (isPrivateHost(host)) throw LocalAddress("$host is a local address")
}
+
+ /** Whether [e], or something it was caused by, is a refusal of a local address. */
+ fun isRefusal(e: Throwable): Boolean {
+ var t: Throwable? = e
+ var depth = 0
+ while (t != null && depth++ < 8) {
+ if (t is LocalAddress) return true
+ t = t.cause
+ }
+ return false
+ }
+
+ /** [url] as it is written in a log: no user info, no query, no fragment (they carry tokens). */
+ fun redact(url: HttpUrl): String = "${url.scheme}://${url.host}${if (url.port != HttpUrl.defaultPort(url.scheme)) ":${url.port}" else ""}${url.encodedPath}"
}
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/ExtensionMediaHttpTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/ExtensionMediaHttpTest.kt
new file mode 100644
index 00000000..3a26ce91
--- /dev/null
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/ExtensionMediaHttpTest.kt
@@ -0,0 +1,186 @@
+package app.myspoti.extensions
+
+import okhttp3.Dns
+import okhttp3.HttpUrl.Companion.toHttpUrl
+import okhttp3.Protocol
+import okhttp3.Request
+import okhttp3.Response
+import okhttp3.ResponseBody.Companion.toResponseBody
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Assert.fail
+import org.junit.Test
+import java.io.IOException
+import java.net.InetAddress
+
+/** Address classification, and the redirect loop that fetches an extension's media with a fake transport. */
+class ExtensionMediaHttpTest {
+ private fun ip(vararg b: Int) = ByteArray(b.size) { b[it].toByte() }
+ private fun v6(text: String) = InetAddress.getByName(text).address
+
+ // ── Address classification ──
+
+ @Test fun ipv4Boundaries() {
+ val local = listOf(ip(0, 0, 0, 0), ip(10, 0, 0, 1), ip(127, 255, 255, 254), ip(100, 64, 0, 0), ip(100, 127, 255, 255), ip(169, 254, 0, 1), ip(172, 16, 0, 0), ip(172, 31, 255, 255),
+ ip(192, 168, 255, 255), ip(192, 0, 0, 1), ip(198, 18, 0, 1), ip(198, 19, 255, 255), ip(224, 0, 0, 1), ip(239, 255, 255, 250), ip(255, 255, 255, 255))
+ local.forEach { assertEquals(it.joinToString("."), true, SafeNet.isPrivateAddress(it)) }
+ val public = listOf(ip(8, 8, 8, 8), ip(100, 63, 255, 255), ip(100, 128, 0, 0), ip(172, 15, 255, 255), ip(172, 32, 0, 0), ip(169, 253, 255, 255), ip(192, 167, 1, 1), ip(198, 17, 0, 1), ip(198, 20, 0, 1), ip(223, 255, 255, 255))
+ public.forEach { assertEquals(it.joinToString("."), false, SafeNet.isPrivateAddress(it)) }
+ }
+
+ @Test fun ipv6Forms() {
+ listOf("::", "::1", "::ffff:127.0.0.1", "::ffff:10.1.2.3", "::ffff:192.168.0.9", "::127.0.0.1", "fc00::1", "fdff::1", "fe80::1", "febf::1", "ff02::1", "ff00::", "64:ff9b::a00:1", "2002:c0a8:101::1")
+ .forEach { assertEquals(it, true, SafeNet.isPrivateAddress(v6(it))) }
+ listOf("2001:4860:4860::8888", "2606:4700::1111", "::ffff:8.8.8.8", "64:ff9b::808:808", "2002:808:808::1", "fbff::1", "2a00:1450::1")
+ .forEach { assertEquals(it, false, SafeNet.isPrivateAddress(v6(it))) }
+ }
+
+ @Test fun notAnAddress() {
+ assertNull(SafeNet.isPrivateAddress(ByteArray(5)))
+ assertNull(SafeNet.isPrivateAddress(ByteArray(0)))
+ }
+
+ @Test fun hostSpellings() {
+ listOf("LOCALHOST.", "x.internal", "nas.lan", "router.home.arpa", "", " ", "0x7f.0.0.1", "[::ffff:a00:1]").forEach { assertTrue("[$it]", SafeNet.isPrivateHost(it)) }
+ listOf("cdn.example.org", "xn--bcher-kva.example", "1.2.3.4").forEach { assertFalse(it, SafeNet.isPrivateHost(it)) }
+ }
+
+ // ── DNS: every address of the answer counts ──
+
+ private fun dnsReturning(vararg addresses: String) = SafeNet.CheckedDns(Dns { _ -> addresses.map { InetAddress.getByName(it) } })
+
+ @Test fun dnsRefusesAMixedAnswer() {
+ assertEquals(1, dnsReturning("93.184.216.34").lookup("cdn.example.org").size)
+ listOf(arrayOf("93.184.216.34", "10.0.0.5"), arrayOf("::1"), arrayOf("fe80::1"), arrayOf("169.254.169.254"), arrayOf("::ffff:192.168.1.1"), arrayOf("100.64.1.1")).forEach {
+ try { dnsReturning(*it).lookup("cdn.example.org"); fail(it.joinToString()) } catch (e: SafeNet.LocalAddress) { assertTrue(SafeNet.isRefusal(IOException(e))) }
+ }
+ }
+
+ @Test fun dnsDoesNotAskForLocalNames() {
+ val dns = SafeNet.CheckedDns(Dns { fail("asked"); emptyList() })
+ try { dns.lookup("printer.local"); fail() } catch (_: SafeNet.LocalAddress) { /* refused before any lookup */ }
+ }
+
+ @Test fun redactedUrlHasNoQuery() {
+ val u = "https://user:[email protected]:8443/a/b.flac?token=SECRET#frag".toHttpUrl()
+ assertEquals("https://cdn.example.org:8443/a/b.flac", SafeNet.redact(u))
+ }
+
+ // ── Redirects ──
+
+ private class Fake(val routes: Map<String, (Request) -> Response>) {
+ val seen = mutableListOf<Request>()
+ val transport = ExtensionMediaHttp.Transport { r, _ ->
+ seen += r
+ (routes[r.url.toString()] ?: error("unexpected ${r.url}")).invoke(r)
+ }
+ }
+
+ private fun reply(r: Request, code: Int, location: String? = null, body: String = "") =
+ Response.Builder().request(r).protocol(Protocol.HTTP_1_1).code(code).message("m").apply { location?.let { header("Location", it) } }.body(body.toByteArray().toResponseBody()).build()
+
+ private fun http(fake: Fake, logs: MutableList<String> = mutableListOf(), clock: () -> Long = { 0 }) = ExtensionMediaHttp(fake.transport, { logs += it }, clock)
+
+ @Test fun followsRedirectsAndKeepsHeadersOnTheSameHost() {
+ val fake = Fake(mapOf(
+ "https://a.example.org/s" to { r -> reply(r, 302, "/t") },
+ "https://a.example.org/t" to { r -> reply(r, 200, body = "ok") },
+ ))
+ val o = http(fake).open("https://a.example.org/s", headers = mapOf("Authorization" to "Bearer x", "X-Token" to "t"), fixed = mapOf("Range" to "bytes=5-"))
+ assertEquals(200, o.response.code)
+ assertEquals(1, o.redirects)
+ assertEquals("https://a.example.org/t", o.url.toString())
+ fake.seen.forEach {
+ assertEquals("Bearer x", it.header("Authorization")); assertEquals("t", it.header("X-Token")); assertEquals("bytes=5-", it.header("Range")); assertEquals("identity", it.header("Accept-Encoding"))
+ }
+ }
+
+ @Test fun dropsTheExtensionsHeadersOnAnotherHostOrPort() {
+ val fake = Fake(mapOf(
+ "https://a.example.org/s" to { r -> reply(r, 301, "https://cdn.example.net/f") },
+ "https://cdn.example.net/f" to { r -> reply(r, 307, "https://cdn.example.net:8443/g") },
+ "https://cdn.example.net:8443/g" to { r -> reply(r, 200) },
+ ))
+ http(fake).open("https://a.example.org/s", headers = mapOf("Authorization" to "Bearer x", "Cookie" to "a=b", "X-Token" to "t"), fixed = mapOf("Range" to "bytes=0-9", "User-Agent" to "app"))
+ assertEquals("Bearer x", fake.seen[0].header("Authorization"))
+ fake.seen.drop(1).forEach {
+ assertNull(it.header("Authorization")); assertNull(it.header("Cookie")); assertNull(it.header("X-Token"))
+ assertEquals("bytes=0-9", it.header("Range")); assertEquals("app", it.header("User-Agent"))
+ }
+ assertEquals(3, fake.seen.size)
+ }
+
+ @Test fun refusesAHopToALocalAddressAndLogsWithoutTheQuery() {
+ val logs = mutableListOf<String>()
+ for (target in listOf("http://169.254.169.254/latest?token=SECRET", "http://127.0.0.1:8080/x?k=SECRET", "http://localhost/x", "http://[::1]/x", "http://2130706433/x", "http://nas.local/x")) {
+ val fake = Fake(mapOf("https://a.example.org/s?sig=SECRET" to { r -> reply(r, 302, target) }))
+ try { http(fake, logs).open("https://a.example.org/s?sig=SECRET"); fail(target) } catch (_: ExtensionMediaHttp.Refused) { /* refused */ }
+ assertEquals("no request to $target", 1, fake.seen.size)
+ }
+ assertEquals(6, logs.size)
+ logs.forEach { assertTrue(it, it.startsWith("media: refused ")); assertFalse(it, it.contains("SECRET")); assertFalse(it, it.contains('?')) }
+ }
+
+ @Test fun refusesAStartAtALocalAddress() {
+ val fake = Fake(emptyMap())
+ try { http(fake).open("http://192.168.1.10/a.flac"); fail() } catch (_: ExtensionMediaHttp.Refused) { /* refused */ }
+ assertTrue(fake.seen.isEmpty())
+ for (bad in listOf("ftp://example.org/a", "file:///etc/passwd", "not a url")) {
+ try { http(fake).open(bad); fail(bad) } catch (_: ExtensionMediaHttp.Refused) { /* refused */ }
+ }
+ }
+
+ @Test fun refusesADowngradeAndAllowsAnUpgrade() {
+ val down = Fake(mapOf("https://a.example.org/s" to { r -> reply(r, 302, "http://a.example.org/s") }))
+ try { http(down).open("https://a.example.org/s"); fail() } catch (_: ExtensionMediaHttp.Refused) { /* refused */ }
+ val up = Fake(mapOf("http://a.example.org/s" to { r -> reply(r, 301, "https://a.example.org/s") }, "https://a.example.org/s" to { r -> reply(r, 200) }))
+ assertEquals(200, http(up).open("http://a.example.org/s", headers = mapOf("X-Token" to "t")).response.code)
+ assertNull(up.seen[1].header("X-Token")) // another port: treated as another origin
+ }
+
+ @Test fun stopsAfterFiveRedirects() {
+ val five = Fake((0..4).associate { i -> "https://a.example.org/$i" to { r: Request -> reply(r, 302, "/${i + 1}") } } + ("https://a.example.org/5" to { r: Request -> reply(r, 200) }))
+ val o = http(five).open("https://a.example.org/0")
+ assertEquals(5, o.redirects)
+ val six = Fake((0..5).associate { i -> "https://a.example.org/$i" to { r: Request -> reply(r, 302, "/${i + 1}") } } + ("https://a.example.org/6" to { r: Request -> reply(r, 200) }))
+ try { http(six).open("https://a.example.org/0"); fail() } catch (e: ExtensionMediaHttp.Refused) { assertTrue(e.message, e.message!!.contains("redirects")) }
+ assertEquals(6, six.seen.size)
+ }
+
+ @Test fun redirectLoopEnds() {
+ val fake = Fake(mapOf("https://a.example.org/l" to { r -> reply(r, 302, "/l") }))
+ try { http(fake).open("https://a.example.org/l"); fail() } catch (_: ExtensionMediaHttp.Refused) { /* refused */ }
+ }
+
+ @Test fun aRedirectWithoutLocationIsTheAnswer() {
+ val fake = Fake(mapOf("https://a.example.org/s" to { r -> reply(r, 302) }))
+ assertEquals(302, http(fake).open("https://a.example.org/s").response.code)
+ }
+
+ @Test fun errorStatusesAreReturnedNotThrown() {
+ val fake = Fake(mapOf("https://a.example.org/s" to { r -> reply(r, 403) }))
+ assertEquals(403, http(fake).open("https://a.example.org/s").response.code)
+ }
+
+ @Test fun elapsedTimeCoversAllHops() {
+ var t = 1_000L
+ val fake = Fake(mapOf("https://a.example.org/s" to { r -> t += 40; reply(r, 302, "/t") }, "https://a.example.org/t" to { r -> t += 60; reply(r, 200) }))
+ assertEquals(100L, http(fake, clock = { t }).open("https://a.example.org/s").elapsedMs)
+ }
+
+ @Test fun aRefusedConnectionIsLoggedOnce() {
+ val logs = mutableListOf<String>()
+ val transport = ExtensionMediaHttp.Transport { _, _ -> throw SafeNet.LocalAddress("cdn.example.org resolves to a local address") }
+ try { ExtensionMediaHttp(transport, { logs += it }).open("https://cdn.example.org/a?sig=SECRET"); fail() } catch (_: java.net.UnknownHostException) { /* refused */ }
+ assertEquals(listOf("media: refused https://cdn.example.org/a: cdn.example.org resolves to a local address"), logs)
+ }
+
+ @Test fun theAppsHeadersWinOnTheSameName() {
+ val h = ExtensionMediaHttp.build(mapOf("range" to "bytes=1-", "X-A" to "1"), mapOf("Range" to "bytes=0-9"))
+ assertEquals(listOf("bytes=0-9"), h.values("Range"))
+ assertEquals("identity", h["Accept-Encoding"])
+ assertEquals("gzip", ExtensionMediaHttp.build(mapOf("Accept-Encoding" to "gzip"), emptyMap())["Accept-Encoding"])
+ }
+}