Use URL for S3.

Thanks to Oscar Mira <valldrac@molly.im> for bringing this to our attention.
This commit is contained in:
Cody Henthorne
2024-06-20 12:15:49 -04:00
committed by GitHub
parent 4b8546a151
commit 230de7e9dc
3 changed files with 59 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.s3
import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.annotation.WorkerThread
import okhttp3.Request
import okhttp3.Response
@@ -20,6 +21,9 @@ import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream
import java.net.URI
import java.net.URISyntaxException
import java.net.URL
import java.nio.charset.Charset
import java.security.MessageDigest
import java.util.regex.Matcher
@@ -31,9 +35,8 @@ import java.util.regex.Pattern
object S3 {
private val TAG = Log.tag(S3::class.java)
private val okHttpClient = AppDependencies.signalOkHttpClient
private val okHttpClient by lazy { AppDependencies.signalOkHttpClient }
private const val S3_BASE = "https://updates2.signal.org"
const val DYNAMIC_PATH = "/dynamic"
const val STATIC_PATH = "/static"
@@ -78,6 +81,8 @@ object S3 {
/**
* Retrieves an S3 object from the given endpoint.
*
* @param endpoint Must be an absolute path to the resource
*/
@WorkerThread
@JvmStatic
@@ -85,7 +90,7 @@ object S3 {
fun getObject(endpoint: String): Response {
val request = Request.Builder()
.get()
.url("$S3_BASE$endpoint")
.url(s3Url(endpoint))
.build()
return okHttpClient.newCall(request).execute()
@@ -191,12 +196,14 @@ object S3 {
/**
* Downloads and parses the ETAG from an S3 object, utilizing a HEAD request.
*
* @param endpoint Must be an absolute path to the resource
*/
@WorkerThread
fun getObjectMD5(endpoint: String): ByteArray? {
val request = Request.Builder()
.head()
.url("$S3_BASE$endpoint")
.url(s3Url(endpoint))
.build()
try {
@@ -229,5 +236,15 @@ object S3 {
}
}
@Throws(IOException::class)
@VisibleForTesting
fun s3Url(path: String): URL {
try {
return URI("https", "updates2.signal.org", path, null).toURL()
} catch (e: URISyntaxException) {
throw IOException(e)
}
}
class Md5FailureException : IOException("Failed to getting or comparing MD5")
}