react-native-audio-browser 0.2.0-next.10 → 0.2.0-next.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +6 -5
- package/android/gradle.properties +1 -1
- package/android/src/main/java/com/audiobrowser/tls/AiaCertChaser.kt +46 -5
- package/android/src/main/java/com/audiobrowser/tls/AiaChasingExtendedTrustManager.kt +86 -27
- package/android/src/main/java/com/audiobrowser/tls/AiaChasingTrustManager.kt +58 -14
- package/android/src/main/java/com/audiobrowser/tls/AiaTls.kt +20 -6
- package/android/src/main/java/com/audiobrowser/tls/CachingCertificateFetcher.kt +249 -86
- package/android/src/test/java/com/audiobrowser/TestFixtures.kt +52 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerLayerResolutionTest.kt +6 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerTest.kt +167 -8
- package/android/src/test/java/com/audiobrowser/tls/AiaCertChaserTest.kt +152 -8
- package/android/src/test/java/com/audiobrowser/tls/AiaChasingTrustManagerTest.kt +218 -10
- package/android/src/test/java/com/audiobrowser/tls/CachingCertificateFetcherCallSiteTest.kt +322 -0
- package/ios/Model/NitroTypeStubs.swift +18 -2
- package/ios/Tests/QueueManagerTests.swift +4 -4
- package/package.json +3 -2
- package/src/nitro-stub-drift.test.ts +237 -0
|
@@ -8,39 +8,69 @@ import java.net.Socket
|
|
|
8
8
|
import java.net.URL
|
|
9
9
|
import java.security.cert.CertificateFactory
|
|
10
10
|
import java.security.cert.X509Certificate
|
|
11
|
-
import java.util.concurrent.
|
|
11
|
+
import java.util.concurrent.atomic.AtomicBoolean
|
|
12
12
|
import javax.net.ssl.HttpsURLConnection
|
|
13
13
|
import javax.net.ssl.SSLContext
|
|
14
|
+
import javax.net.ssl.SSLSocket
|
|
14
15
|
import javax.net.ssl.SSLSocketFactory
|
|
16
|
+
import kotlin.concurrent.thread
|
|
15
17
|
import timber.log.Timber
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* Fetches and caches issuer certificates from AIA "CA Issuers" URLs. Used by
|
|
19
21
|
* [AiaChasingTrustManager] to fill in intermediates a server failed to send.
|
|
20
22
|
*
|
|
21
|
-
* Runs on the TLS handshake thread, so it
|
|
22
|
-
* fetch resolves to an empty list
|
|
23
|
-
* chasing
|
|
24
|
-
*
|
|
23
|
+
* Runs on the TLS handshake thread, so it is bounded on every axis and never throws — a failed,
|
|
24
|
+
* oversized or slow fetch resolves to an empty list, and the handshake then fails exactly as it
|
|
25
|
+
* would have without AIA chasing. Every input here is chosen by a server we are already failing to
|
|
26
|
+
* validate: the AIA extension names the host, port and path, and then that host chooses the
|
|
27
|
+
* response. So:
|
|
28
|
+
* - the URL is rejected unless it is free of control characters ([isSafeUrl]),
|
|
29
|
+
* - a response is capped at [MAX_RESPONSE_BYTES] and [MAX_RESPONSE_MILLIS],
|
|
30
|
+
* - at most [MAX_REDIRECTS] redirects are followed,
|
|
31
|
+
* - the cache holds at most [MAX_CACHE_ENTRIES], and only successful fetches, so a round trip
|
|
32
|
+
* happens at most once per intermediate while transient failures stay retryable,
|
|
33
|
+
* - the whole call — every connect, handshake, read and redirect hop — ends at the caller's
|
|
34
|
+
* `deadlineNanos`: each per-operation timeout is clipped to the time remaining, connect and read
|
|
35
|
+
* by their socket timeouts and the TLS handshake plus the exchange by a watchdog that closes the
|
|
36
|
+
* underlying transport, and once the deadline has passed nothing further is even started.
|
|
25
37
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
38
|
+
* The one step no deadline reaches is DNS: `InetSocketAddress` resolves synchronously and Java
|
|
39
|
+
* exposes no timeout for it, so a lookup begun just inside the deadline can overrun it by its own
|
|
40
|
+
* duration. A lookup is never *started* past the deadline, and [AiaCertChaser.MAX_URLS_PER_CERT]
|
|
41
|
+
* keeps the count down — a certificate can only make us resolve a few names, not a few hundred.
|
|
30
42
|
*
|
|
31
|
-
*
|
|
32
|
-
* virtually always plain `http`
|
|
33
|
-
* chicken-and-egg TLS dependency when fetching the
|
|
34
|
-
* Android blocks cleartext `HttpURLConnection` traffic by
|
|
35
|
-
* socket is not subject to `NetworkSecurityPolicy`, so the
|
|
36
|
-
* app's cleartext setting
|
|
37
|
-
* verified to be the real issuer before
|
|
43
|
+
* Both schemes go over a raw [Socket] rather than `HttpURLConnection`. For `http` that is because
|
|
44
|
+
* CA "CA Issuers" URLs are virtually always plain `http` — the CA/Browser Forum Baseline
|
|
45
|
+
* Requirements mandate it, to avoid a chicken-and-egg TLS dependency when fetching the certificate
|
|
46
|
+
* needed to complete a TLS chain — while Android blocks cleartext `HttpURLConnection` traffic by
|
|
47
|
+
* default on apps targeting API 28+. A raw socket is not subject to `NetworkSecurityPolicy`, so the
|
|
48
|
+
* fetch succeeds regardless of the host app's cleartext setting; safely, because the fetched
|
|
49
|
+
* certificate is still cryptographically verified to be the real issuer before
|
|
50
|
+
* [AiaChasingTrustManager] trusts it.
|
|
51
|
+
*
|
|
52
|
+
* For `https` it is because `HttpURLConnection.getInputStream()` performs the connect, request and
|
|
53
|
+
* response *headers* before returning a stream, so a read cap applied to that stream bounds only
|
|
54
|
+
* the body: a server dripping header bytes just inside the per-read timeout would hold the
|
|
55
|
+
* handshake thread for as long as it liked. Reading the headers ourselves puts them inside the same
|
|
56
|
+
* budget as the body. It also keeps one redirect policy — the platform stack would otherwise follow
|
|
57
|
+
* its own, ignoring [MAX_REDIRECTS].
|
|
38
58
|
*/
|
|
39
59
|
class CachingCertificateFetcher(
|
|
40
60
|
private val connectTimeoutMs: Int = 5_000,
|
|
41
61
|
private val readTimeoutMs: Int = 5_000,
|
|
62
|
+
private val nowNanos: () -> Long = System::nanoTime,
|
|
42
63
|
) {
|
|
43
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Bounded, least-recently-used. An attacker can name any URL and serve any parseable certificate
|
|
66
|
+
* from it, so an unbounded map would retain a megabyte per hostile handshake for the process
|
|
67
|
+
* lifetime. A real app needs a handful of intermediates.
|
|
68
|
+
*/
|
|
69
|
+
private val cache =
|
|
70
|
+
object : LinkedHashMap<String, List<X509Certificate>>(16, 0.75f, true) {
|
|
71
|
+
override fun removeEldestEntry(eldest: Map.Entry<String, List<X509Certificate>>) =
|
|
72
|
+
size > MAX_CACHE_ENTRIES
|
|
73
|
+
}
|
|
44
74
|
|
|
45
75
|
/**
|
|
46
76
|
* A plain system-default socket factory for `https` AIA fetches. Without it the fetch would use
|
|
@@ -51,98 +81,243 @@ class CachingCertificateFetcher(
|
|
|
51
81
|
SSLContext.getInstance("TLS").apply { init(null, null, null) }.socketFactory
|
|
52
82
|
}
|
|
53
83
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Fetches the certificates served at [url], giving up no later than [deadlineNanos] — absolute,
|
|
86
|
+
* on [nowNanos]'s origin. [AiaCertChaser.completeChain] passes its whole-walk deadline here, so
|
|
87
|
+
* one budget bounds the sum of every fetch it makes. The default covers a standalone call.
|
|
88
|
+
*
|
|
89
|
+
* A cache hit is answered even past the deadline — it costs no I/O.
|
|
90
|
+
*/
|
|
91
|
+
fun fetch(
|
|
92
|
+
url: String,
|
|
93
|
+
deadlineNanos: Long = nowNanos() + AiaCertChaser.MAX_CHASE_MILLIS * 1_000_000,
|
|
94
|
+
): List<X509Certificate> {
|
|
95
|
+
synchronized(cache) { cache[url] }
|
|
96
|
+
?.let {
|
|
97
|
+
return it
|
|
98
|
+
}
|
|
99
|
+
val certs = download(url, deadlineNanos)?.let { parseCertificates(it) }.orEmpty()
|
|
100
|
+
if (certs.isNotEmpty()) synchronized(cache) { cache[url] = certs }
|
|
60
101
|
return certs
|
|
61
102
|
}
|
|
62
103
|
|
|
63
|
-
|
|
104
|
+
/** Milliseconds until [deadlineNanos]; zero or negative once it has passed. */
|
|
105
|
+
private fun remainingMs(deadlineNanos: Long): Long = (deadlineNanos - nowNanos()) / 1_000_000
|
|
106
|
+
|
|
107
|
+
private fun download(
|
|
108
|
+
url: String,
|
|
109
|
+
deadlineNanos: Long,
|
|
110
|
+
redirectsLeft: Int = MAX_REDIRECTS,
|
|
111
|
+
): ByteArray? =
|
|
64
112
|
try {
|
|
113
|
+
// Checked before anything is started — DNS included, since it is the one step below that
|
|
114
|
+
// no timeout reaches. Redirects recurse through here, so every hop re-checks.
|
|
115
|
+
require(remainingMs(deadlineNanos) > 0) { "AIA chase budget exhausted" }
|
|
65
116
|
require(isSafeUrl(url)) { "AIA URL contains characters that are not valid in a URL" }
|
|
66
117
|
val parsed = URL(url)
|
|
67
118
|
when (parsed.protocol) {
|
|
68
|
-
|
|
69
|
-
"
|
|
70
|
-
|
|
119
|
+
// restrict to http/https — file:, ftp: etc. are never valid CA-issuer sources
|
|
120
|
+
"http" -> fetchOverSocket(parsed, deadlineNanos, redirectsLeft, secure = false)
|
|
121
|
+
"https" -> fetchOverSocket(parsed, deadlineNanos, redirectsLeft, secure = true)
|
|
122
|
+
else -> null
|
|
71
123
|
}
|
|
72
124
|
} catch (e: Exception) {
|
|
73
|
-
|
|
125
|
+
// The URL is attacker-supplied text and this is the one place it survives the safety
|
|
126
|
+
// check, so it is escaped rather than handed to the log verbatim.
|
|
127
|
+
Timber.w(e, "AIA CA-issuer fetch failed: %s", sanitizeForLog(url))
|
|
74
128
|
null
|
|
75
129
|
}
|
|
76
130
|
|
|
77
131
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
132
|
+
* A connected socket to speak HTTP over, plus the plain TCP transport underneath it. They differ
|
|
133
|
+
* for `https`: closing an `SSLSocket` writes a `close_notify` alert, which can itself block
|
|
134
|
+
* against a peer that stopped reading — so anything that needs to *unblock* a stalled call must
|
|
135
|
+
* close [transport], whose close is immediate and fails every read layered above it.
|
|
81
136
|
*/
|
|
82
|
-
private
|
|
83
|
-
val connection = url.openConnection() as? HttpsURLConnection ?: return null
|
|
84
|
-
connection.connectTimeout = connectTimeoutMs
|
|
85
|
-
connection.readTimeout = readTimeoutMs
|
|
86
|
-
connection.sslSocketFactory = plainSslSocketFactory
|
|
87
|
-
return connection.inputStream.use { readCapped(it) }
|
|
88
|
-
}
|
|
137
|
+
private class Connection(val socket: Socket, val transport: Socket)
|
|
89
138
|
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
139
|
+
/** One HTTP/1.0 exchange over a socket, TLS-wrapped when [secure], then redirects. */
|
|
140
|
+
private fun fetchOverSocket(
|
|
141
|
+
url: URL,
|
|
142
|
+
deadlineNanos: Long,
|
|
143
|
+
redirectsLeft: Int,
|
|
144
|
+
secure: Boolean,
|
|
145
|
+
): ByteArray? {
|
|
95
146
|
val host = url.host
|
|
96
|
-
val port = if (url.port != -1) url.port else 80
|
|
147
|
+
val port = if (url.port != -1) url.port else if (secure) 443 else 80
|
|
97
148
|
val path = url.file.ifEmpty { "/" } // URL.file is path + query
|
|
149
|
+
val connection = connect(host, port, secure, deadlineNanos)
|
|
98
150
|
val raw =
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
151
|
+
try {
|
|
152
|
+
withWatchdog(
|
|
153
|
+
minOf(remainingMs(deadlineNanos), connectTimeoutMs + MAX_RESPONSE_MILLIS),
|
|
154
|
+
abort = { connection.transport.close() },
|
|
155
|
+
) {
|
|
156
|
+
val socket = connection.socket
|
|
157
|
+
socket.soTimeout = timeoutMs(deadlineNanos, readTimeoutMs)
|
|
158
|
+
// HTTP/1.0 + `Connection: close` so the server closes after the body and we can read
|
|
159
|
+
// to EOF without parsing Content-Length. A non-default port belongs in Host, or a
|
|
160
|
+
// name-based virtual host answers for the wrong site.
|
|
161
|
+
val hostHeader = if (port == (if (secure) 443 else 80)) host else "$host:$port"
|
|
162
|
+
val request =
|
|
163
|
+
"GET $path HTTP/1.0\r\n" +
|
|
164
|
+
"Host: $hostHeader\r\n" +
|
|
165
|
+
"Connection: close\r\n" +
|
|
166
|
+
"User-Agent: react-native-audio-browser\r\n" +
|
|
167
|
+
"Accept: */*\r\n\r\n"
|
|
168
|
+
socket.getOutputStream().apply {
|
|
169
|
+
write(request.toByteArray(Charsets.ISO_8859_1))
|
|
170
|
+
flush()
|
|
171
|
+
}
|
|
172
|
+
socket.getInputStream().use {
|
|
173
|
+
readCapped(
|
|
174
|
+
it,
|
|
175
|
+
budgetMs = minOf(remainingMs(deadlineNanos), MAX_RESPONSE_MILLIS),
|
|
176
|
+
nowNanos = nowNanos,
|
|
177
|
+
)
|
|
178
|
+
}
|
|
113
179
|
}
|
|
114
|
-
|
|
180
|
+
} finally {
|
|
181
|
+
// Transport first: its close never blocks and unblocks anything above it, so the
|
|
182
|
+
// SSLSocket's close_notify attempt afterwards fails fast instead of stalling.
|
|
183
|
+
runCatching { connection.transport.close() }
|
|
184
|
+
runCatching { connection.socket.close() }
|
|
115
185
|
} ?: return null
|
|
116
186
|
val response = parseHttpResponse(raw) ?: return null
|
|
117
187
|
return when {
|
|
118
188
|
response.status in 200..299 -> response.body
|
|
119
189
|
response.status in 300..399 && response.location != null && redirectsLeft > 0 ->
|
|
120
|
-
// Follow CA-issuer redirects (rare).
|
|
121
|
-
|
|
190
|
+
// Follow CA-issuer redirects (rare). `download` is the recursion point, so the target
|
|
191
|
+
// goes through the same URL check and the same deadline.
|
|
192
|
+
download(URL(url, response.location).toString(), deadlineNanos, redirectsLeft - 1)
|
|
122
193
|
else -> null
|
|
123
194
|
}
|
|
124
195
|
}
|
|
125
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Connects to [host]:[port], wrapping in TLS when [secure]. Both the connect and the TLS
|
|
199
|
+
* handshake are clipped to the caller's deadline.
|
|
200
|
+
*
|
|
201
|
+
* The TLS socket verifies the hostname explicitly. A raw `SSLSocket` does not do it on its own,
|
|
202
|
+
* and `SSLParameters.setEndpointIdentificationAlgorithm` is API 24+, so the platform's default
|
|
203
|
+
* verifier is applied against the negotiated session instead — which works on every supported
|
|
204
|
+
* level and is the same check `HttpsURLConnection` would have made.
|
|
205
|
+
*/
|
|
206
|
+
private fun connect(host: String, port: Int, secure: Boolean, deadlineNanos: Long): Connection {
|
|
207
|
+
val plain = Socket()
|
|
208
|
+
// Everything from here on closes `plain` if it throws. Android's Socket.connect, unlike
|
|
209
|
+
// modern OpenJDK's, does not close itself on failure once the descriptor is allocated, so
|
|
210
|
+
// every unreachable AIA host would otherwise leak one until finalization.
|
|
211
|
+
var wrapper: SSLSocket? = null
|
|
212
|
+
return try {
|
|
213
|
+
plain.connect(InetSocketAddress(host, port), timeoutMs(deadlineNanos, connectTimeoutMs))
|
|
214
|
+
if (!secure) return Connection(plain, plain)
|
|
215
|
+
// The handshake is bounded by a watchdog, not by soTimeout: soTimeout is per-read, so a
|
|
216
|
+
// server drip-feeding handshake bytes would stretch it indefinitely. The watchdog closes
|
|
217
|
+
// `plain`, the transport — closing the SSLSocket instead would write a close_notify that
|
|
218
|
+
// can block against the very peer that caused the stall.
|
|
219
|
+
withWatchdog(
|
|
220
|
+
minOf(remainingMs(deadlineNanos), connectTimeoutMs + MAX_RESPONSE_MILLIS),
|
|
221
|
+
abort = { plain.close() },
|
|
222
|
+
) {
|
|
223
|
+
val ssl = plainSslSocketFactory.createSocket(plain, host, port, true) as SSLSocket
|
|
224
|
+
wrapper = ssl
|
|
225
|
+
ssl.soTimeout = timeoutMs(deadlineNanos, readTimeoutMs)
|
|
226
|
+
ssl.startHandshake()
|
|
227
|
+
require(HttpsURLConnection.getDefaultHostnameVerifier().verify(host, ssl.session)) {
|
|
228
|
+
"AIA host $host does not match its certificate"
|
|
229
|
+
}
|
|
230
|
+
Connection(ssl, plain)
|
|
231
|
+
}
|
|
232
|
+
} catch (e: Exception) {
|
|
233
|
+
runCatching { plain.close() }
|
|
234
|
+
// The orphaned TLS wrapper too — with the transport already gone its close_notify attempt
|
|
235
|
+
// fails fast, and this releases the wrapper's own buffers rather than waiting on the GC.
|
|
236
|
+
runCatching { wrapper?.close() }
|
|
237
|
+
throw e
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* A per-operation socket timeout: [perOperationMs] clipped to the deadline, and never 0 ("no
|
|
243
|
+
* timeout" to a socket) — an already-expired deadline still gets a 1 ms timeout and fails at
|
|
244
|
+
* once.
|
|
245
|
+
*/
|
|
246
|
+
private fun timeoutMs(deadlineNanos: Long, perOperationMs: Int): Int =
|
|
247
|
+
minOf(remainingMs(deadlineNanos), perOperationMs.toLong())
|
|
248
|
+
.coerceIn(1, Int.MAX_VALUE.toLong())
|
|
249
|
+
.toInt()
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Runs [body] with a watchdog thread that invokes [abort] once [budgetMs] elapses — unless the
|
|
253
|
+
* body has already finished, which the done flag makes visible to a watchdog waking at just that
|
|
254
|
+
* moment, so a successful exchange cannot have its socket closed out from under it.
|
|
255
|
+
*
|
|
256
|
+
* The socket timeouts are per-operation and so cannot bound a peer that keeps every individual
|
|
257
|
+
* read just inside them; [abort] — closing the underlying transport — is what actually ends a
|
|
258
|
+
* stalled handshake or exchange: the blocked call throws, and a failed fetch is already handled
|
|
259
|
+
* as "no AIA".
|
|
260
|
+
*/
|
|
261
|
+
private inline fun <T> withWatchdog(
|
|
262
|
+
budgetMs: Long,
|
|
263
|
+
crossinline abort: () -> Unit,
|
|
264
|
+
body: () -> T,
|
|
265
|
+
): T {
|
|
266
|
+
val done = AtomicBoolean(false)
|
|
267
|
+
val watchdog =
|
|
268
|
+
thread(isDaemon = true, name = "aia-fetch-watchdog") {
|
|
269
|
+
runCatching { Thread.sleep(budgetMs.coerceAtLeast(0)) }
|
|
270
|
+
.onSuccess { if (!done.get()) runCatching { abort() } }
|
|
271
|
+
}
|
|
272
|
+
try {
|
|
273
|
+
return body()
|
|
274
|
+
} finally {
|
|
275
|
+
done.set(true)
|
|
276
|
+
watchdog.interrupt()
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
126
280
|
companion object {
|
|
127
281
|
private const val MAX_REDIRECTS = 5
|
|
128
282
|
|
|
283
|
+
/** Ceiling on how many fetched issuer certificates are retained. */
|
|
284
|
+
const val MAX_CACHE_ENTRIES = 32
|
|
285
|
+
|
|
286
|
+
/** Ceiling on a single AIA response, headers included. */
|
|
287
|
+
const val MAX_RESPONSE_BYTES = 1 shl 20 // 1 MiB
|
|
288
|
+
|
|
289
|
+
/** Ceiling on the wall-clock time spent reading one AIA response. */
|
|
290
|
+
const val MAX_RESPONSE_MILLIS = 10_000L
|
|
291
|
+
|
|
129
292
|
/**
|
|
130
293
|
* Whether [url] is free of characters that have no business in a URL.
|
|
131
294
|
*
|
|
132
|
-
* The
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
295
|
+
* The fetch writes the path into a request line on a raw socket, and the URL is an IA5String
|
|
296
|
+
* copied verbatim out of a certificate a hostile server presented. `java.net.URL` preserves
|
|
297
|
+
* control characters — `URL("http://host:6379/\r\nSET foo bar")` parses to that host and port
|
|
298
|
+
* with the CRLF intact in `file` — so without this check a certificate could inject request
|
|
299
|
+
* lines of its own, to any host and port the device can reach. Space is excluded too: it would
|
|
300
|
+
* split the request line's target from its HTTP version.
|
|
138
301
|
*/
|
|
139
302
|
fun isSafeUrl(url: String): Boolean = url.none { it.code <= 0x20 || it.code == 0x7F }
|
|
140
303
|
|
|
141
|
-
/**
|
|
142
|
-
|
|
304
|
+
/** [text] with anything non-printable replaced, so a log line cannot be forged. */
|
|
305
|
+
fun sanitizeForLog(text: String): String =
|
|
306
|
+
text.map { if (it.code <= 0x20 || it.code == 0x7F) '?' else it }.joinToString("")
|
|
143
307
|
|
|
144
|
-
/**
|
|
145
|
-
|
|
308
|
+
/**
|
|
309
|
+
* Parses every certificate from the bytes a CA-Issuers URL serves — a single DER certificate
|
|
310
|
+
* (e.g. Let's Encrypt), a PEM file, or a PKCS#7 "certs-only" bundle. Empty if the bytes are not
|
|
311
|
+
* parseable certificates.
|
|
312
|
+
*/
|
|
313
|
+
fun parseCertificates(bytes: ByteArray): List<X509Certificate> =
|
|
314
|
+
try {
|
|
315
|
+
CertificateFactory.getInstance("X.509")
|
|
316
|
+
.generateCertificates(ByteArrayInputStream(bytes))
|
|
317
|
+
.filterIsInstance<X509Certificate>()
|
|
318
|
+
} catch (e: Exception) {
|
|
319
|
+
emptyList()
|
|
320
|
+
}
|
|
146
321
|
|
|
147
322
|
/**
|
|
148
323
|
* Reads [input] to EOF, or returns null once it exceeds [limit] bytes or [budgetMs] of
|
|
@@ -175,20 +350,6 @@ class CachingCertificateFetcher(
|
|
|
175
350
|
}
|
|
176
351
|
}
|
|
177
352
|
|
|
178
|
-
/**
|
|
179
|
-
* Parses every certificate from the bytes a CA-Issuers URL serves — a single DER certificate
|
|
180
|
-
* (e.g. Let's Encrypt), a PEM file, or a PKCS#7 "certs-only" bundle. Empty if the bytes are not
|
|
181
|
-
* parseable certificates.
|
|
182
|
-
*/
|
|
183
|
-
fun parseCertificates(bytes: ByteArray): List<X509Certificate> =
|
|
184
|
-
try {
|
|
185
|
-
CertificateFactory.getInstance("X.509")
|
|
186
|
-
.generateCertificates(ByteArrayInputStream(bytes))
|
|
187
|
-
.filterIsInstance<X509Certificate>()
|
|
188
|
-
} catch (e: Exception) {
|
|
189
|
-
emptyList()
|
|
190
|
-
}
|
|
191
|
-
|
|
192
353
|
/** Status code, `Location` header (if any), and decoded body of a raw HTTP response. */
|
|
193
354
|
data class HttpResponse(val status: Int, val location: String?, val body: ByteArray)
|
|
194
355
|
|
|
@@ -231,7 +392,9 @@ class CachingCertificateFetcher(
|
|
|
231
392
|
.substringBefore(';')
|
|
232
393
|
.trim()
|
|
233
394
|
.toIntOrNull(16) ?: break
|
|
234
|
-
|
|
395
|
+
// `<= 0`, not `== 0`: toIntOrNull accepts a leading `-`, and a negative size would move
|
|
396
|
+
// `pos` backwards — the same rewinding-cursor loop the DER reader had.
|
|
397
|
+
if (size <= 0) break // last chunk, or malformed
|
|
235
398
|
val dataStart = eol + 2
|
|
236
399
|
if (dataStart + size > body.size) break
|
|
237
400
|
out.write(body, dataStart, size)
|
|
@@ -6,6 +6,8 @@ import com.margelo.nitro.audiobrowser.ImageQueryParams
|
|
|
6
6
|
import com.margelo.nitro.audiobrowser.ImageRowItem
|
|
7
7
|
import com.margelo.nitro.audiobrowser.ImageSource
|
|
8
8
|
import com.margelo.nitro.audiobrowser.MediaRequestConfig
|
|
9
|
+
import com.margelo.nitro.audiobrowser.NativeRouteEntry
|
|
10
|
+
import com.margelo.nitro.audiobrowser.ResolvedTrack
|
|
9
11
|
import com.margelo.nitro.audiobrowser.Track
|
|
10
12
|
import com.margelo.nitro.audiobrowser.TransformableRequestConfig
|
|
11
13
|
|
|
@@ -141,6 +143,56 @@ object TestFixtures {
|
|
|
141
143
|
imageRow = null,
|
|
142
144
|
)
|
|
143
145
|
|
|
146
|
+
/** A container [ResolvedTrack] — what a browse route hands back for a path. */
|
|
147
|
+
fun resolvedTrack(
|
|
148
|
+
url: String = "/container",
|
|
149
|
+
title: String = "Container",
|
|
150
|
+
children: Array<Track>? = null,
|
|
151
|
+
imageRow: Array<ImageRowItem>? = null,
|
|
152
|
+
) =
|
|
153
|
+
ResolvedTrack(
|
|
154
|
+
url = url,
|
|
155
|
+
children = children,
|
|
156
|
+
carPlaySiriListButton = null,
|
|
157
|
+
id = null,
|
|
158
|
+
src = null,
|
|
159
|
+
artwork = null,
|
|
160
|
+
artworkSource = null,
|
|
161
|
+
request = null,
|
|
162
|
+
artworkCarPlayTinted = null,
|
|
163
|
+
title = title,
|
|
164
|
+
subtitle = null,
|
|
165
|
+
artist = null,
|
|
166
|
+
albumUrl = null,
|
|
167
|
+
album = null,
|
|
168
|
+
description = null,
|
|
169
|
+
genre = null,
|
|
170
|
+
duration = null,
|
|
171
|
+
style = null,
|
|
172
|
+
childrenStyle = null,
|
|
173
|
+
favorited = null,
|
|
174
|
+
groupTitle = null,
|
|
175
|
+
live = null,
|
|
176
|
+
imageRow = imageRow,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* A route serving [browseStatic] verbatim. `browseStatic` is the only one of the three browse
|
|
181
|
+
* arms (callback > config > static) that needs neither the JNI bridge nor HTTP, so it is how a
|
|
182
|
+
* unit test gives [com.audiobrowser.browser.BrowserManager] page content to resolve.
|
|
183
|
+
*/
|
|
184
|
+
fun staticRoute(path: String, browseStatic: ResolvedTrack) =
|
|
185
|
+
NativeRouteEntry(
|
|
186
|
+
path = path,
|
|
187
|
+
browseCallback = null,
|
|
188
|
+
browseConfig = null,
|
|
189
|
+
browseStatic = browseStatic,
|
|
190
|
+
searchCallback = null,
|
|
191
|
+
searchConfig = null,
|
|
192
|
+
media = null,
|
|
193
|
+
artwork = null,
|
|
194
|
+
)
|
|
195
|
+
|
|
144
196
|
fun artworkConfig(
|
|
145
197
|
path: String? = null,
|
|
146
198
|
query: Map<String, String>? = null,
|
|
@@ -8,6 +8,8 @@ import org.junit.Assert.assertNull
|
|
|
8
8
|
import org.junit.Assert.assertSame
|
|
9
9
|
import org.junit.Before
|
|
10
10
|
import org.junit.Test
|
|
11
|
+
import org.junit.runner.RunWith
|
|
12
|
+
import org.robolectric.RobolectricTestRunner
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Tests the request/browse layer resolution + caching introduced for resolver thunks.
|
|
@@ -26,7 +28,11 @@ import org.junit.Test
|
|
|
26
28
|
* bump, no in-flight cache) is identical for the static and resolver paths — `resolveLayer` only
|
|
27
29
|
* differs in how it produces the `TransformableRequestConfig` — so the guard is fully covered here.
|
|
28
30
|
* The resolver-await path is covered by the TS/web-stub tests and on iOS.
|
|
31
|
+
*
|
|
32
|
+
* Runs under Robolectric for [BrowserManager]'s `android.util.LruCache` fields: `clearContentCache`
|
|
33
|
+
* calls `evictAll()`, which a plain JVM test cannot execute.
|
|
29
34
|
*/
|
|
35
|
+
@RunWith(RobolectricTestRunner::class)
|
|
30
36
|
class BrowserManagerLayerResolutionTest {
|
|
31
37
|
|
|
32
38
|
private lateinit var browserManager: BrowserManager
|