mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-05-03 23:15:44 +01:00
Add redirect validation for link previews.
This commit is contained in:
committed by
jeffrey-signal
parent
7665ae1464
commit
969635d942
@@ -38,6 +38,7 @@ import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph;
|
||||
import org.thoughtcrime.securesms.mms.PushMediaConstraints;
|
||||
import org.thoughtcrime.securesms.net.CallRequestController;
|
||||
import org.thoughtcrime.securesms.net.CompositeRequestController;
|
||||
import org.thoughtcrime.securesms.net.LinkPreviewRedirectValidationInterceptor;
|
||||
import org.thoughtcrime.securesms.net.RequestController;
|
||||
import org.thoughtcrime.securesms.net.UserAgentInterceptor;
|
||||
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
||||
@@ -93,6 +94,7 @@ public class LinkPreviewRepository {
|
||||
this.client = new OkHttpClient.Builder()
|
||||
.cache(null)
|
||||
.addInterceptor(new UserAgentInterceptor("WhatsApp/2"))
|
||||
.addNetworkInterceptor(new LinkPreviewRedirectValidationInterceptor())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.thoughtcrime.securesms.net
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.signal.core.util.logging.Log.tag
|
||||
import org.thoughtcrime.securesms.util.LinkUtil.isValidPreviewUrl
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* Validates redirects for link preview requests to ensure they all meet the link criteria.
|
||||
*/
|
||||
class LinkPreviewRedirectValidationInterceptor : Interceptor {
|
||||
|
||||
companion object {
|
||||
private val TAG = tag(LinkPreviewRedirectValidationInterceptor::class)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val url = chain.request().url.toString()
|
||||
|
||||
if (!isValidPreviewUrl(url)) {
|
||||
Log.w(TAG, "Redirect target failed link preview URL validation.")
|
||||
chain.call().cancel()
|
||||
throw IOException("Redirect target is not a valid preview URL.")
|
||||
}
|
||||
|
||||
return chain.proceed(chain.request())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user