From c41b213808ebe26a5eb00b09a4ee06d978e8778e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Loureiro?= <1649425+jpcloureiro@users.noreply.github.com> Date: Wed, 14 Feb 2024 20:35:40 +0000 Subject: [PATCH 1/3] show prompt before download starts --- .../webview/RNCWebViewManagerImpl.kt | 62 +++++++++++-------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt index 1b6ff7aa5..b015aa445 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt @@ -1,6 +1,8 @@ package com.reactnativecommunity.webview +import android.app.AlertDialog import android.app.DownloadManager +import android.content.DialogInterface import android.content.pm.ActivityInfo import android.graphics.Bitmap import android.graphics.Color @@ -106,34 +108,42 @@ class RNCWebViewManagerImpl { val downloadMessage = "Downloading $fileName" - //Attempt to add cookie, if it exists - var urlObj: URL? = null - try { - urlObj = URL(url) - val baseUrl = urlObj.protocol + "://" + urlObj.host - val cookie = CookieManager.getInstance().getCookie(baseUrl) - request.addRequestHeader("Cookie", cookie) - } catch (e: MalformedURLException) { - Log.w(TAG, "Error getting cookie for DownloadManager", e) - } + val builder = AlertDialog.Builder(webView.context) + builder.setMessage("Do you want to download \n$fileName?") + builder.setCancelable(false) + builder.setPositiveButton("Download") { _, _ -> + //Attempt to add cookie, if it exists + var urlObj: URL? = null + try { + urlObj = URL(url) + val baseUrl = urlObj.protocol + "://" + urlObj.host + val cookie = CookieManager.getInstance().getCookie(baseUrl) + request.addRequestHeader("Cookie", cookie) + } catch (e: MalformedURLException) { + Log.w(TAG, "Error getting cookie for DownloadManager", e) + } - //Finish setting up request - request.addRequestHeader("User-Agent", userAgent) - request.setTitle(fileName) - request.setDescription(downloadMessage) - request.allowScanningByMediaScanner() - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) - module.setDownloadRequest(request) - if (module.grantFileDownloaderPermissions( - getDownloadingMessageOrDefault(), - getLackPermissionToDownloadMessageOrDefault() - ) - ) { - module.downloadFile( - getDownloadingMessageOrDefault() - ) + //Finish setting up request + request.addRequestHeader("User-Agent", userAgent) + request.setTitle(fileName) + request.setDescription(downloadMessage) + request.allowScanningByMediaScanner() + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) + module.setDownloadRequest(request) + if (module.grantFileDownloaderPermissions( + getDownloadingMessageOrDefault(), + getLackPermissionToDownloadMessageOrDefault() + ) + ) { + module.downloadFile( + getDownloadingMessageOrDefault() + ) + } } + builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> } + val alertDialog = builder.create() + alertDialog.show() }) return RNCWebViewWrapper(context, webView) } From c9242686e32c876e8028c447c2fe94ad0529968a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Loureiro?= <1649425+jpcloureiro@users.noreply.github.com> Date: Wed, 21 Feb 2024 15:24:47 +0000 Subject: [PATCH 2/3] rtlo filename protection --- .../webview/RNCWebViewManagerImpl.kt | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt index b015aa445..fc9db2ceb 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt @@ -17,6 +17,7 @@ import android.webkit.CookieManager import android.webkit.DownloadListener import android.webkit.WebSettings import android.webkit.WebView +import android.widget.Toast import androidx.webkit.WebSettingsCompat import androidx.webkit.WebViewFeature import com.facebook.react.bridge.ReadableArray @@ -29,6 +30,7 @@ import org.json.JSONObject import java.io.UnsupportedEncodingException import java.net.MalformedURLException import java.net.URL +import java.text.Bidi import java.util.Locale val invalidCharRegex = "[\\\\/%\"]".toRegex() @@ -108,42 +110,47 @@ class RNCWebViewManagerImpl { val downloadMessage = "Downloading $fileName" - val builder = AlertDialog.Builder(webView.context) - builder.setMessage("Do you want to download \n$fileName?") - builder.setCancelable(false) - builder.setPositiveButton("Download") { _, _ -> - //Attempt to add cookie, if it exists - var urlObj: URL? = null - try { - urlObj = URL(url) - val baseUrl = urlObj.protocol + "://" + urlObj.host - val cookie = CookieManager.getInstance().getCookie(baseUrl) - request.addRequestHeader("Cookie", cookie) - } catch (e: MalformedURLException) { - Log.w(TAG, "Error getting cookie for DownloadManager", e) - } + //Filename validation checking for files that use RTL characters and do not allow those types + if (Bidi(fileName, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT).isMixed) { + Toast.makeText(webView.context, "Invalid filename or type", Toast.LENGTH_LONG).show() + } else { + val builder = AlertDialog.Builder(webView.context) + builder.setMessage("Do you want to download \n$fileName?") + builder.setCancelable(false) + builder.setPositiveButton("Download") { _, _ -> + //Attempt to add cookie, if it exists + var urlObj: URL? = null + try { + urlObj = URL(url) + val baseUrl = urlObj.protocol + "://" + urlObj.host + val cookie = CookieManager.getInstance().getCookie(baseUrl) + request.addRequestHeader("Cookie", cookie) + } catch (e: MalformedURLException) { + Log.w(TAG, "Error getting cookie for DownloadManager", e) + } - //Finish setting up request - request.addRequestHeader("User-Agent", userAgent) - request.setTitle(fileName) - request.setDescription(downloadMessage) - request.allowScanningByMediaScanner() - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) - module.setDownloadRequest(request) - if (module.grantFileDownloaderPermissions( - getDownloadingMessageOrDefault(), - getLackPermissionToDownloadMessageOrDefault() + //Finish setting up request + request.addRequestHeader("User-Agent", userAgent) + request.setTitle(fileName) + request.setDescription(downloadMessage) + request.allowScanningByMediaScanner() + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) + module.setDownloadRequest(request) + if (module.grantFileDownloaderPermissions( + getDownloadingMessageOrDefault(), + getLackPermissionToDownloadMessageOrDefault() + ) + ) { + module.downloadFile( + getDownloadingMessageOrDefault() ) - ) { - module.downloadFile( - getDownloadingMessageOrDefault() - ) + } } + builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> } + val alertDialog = builder.create() + alertDialog.show() } - builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> } - val alertDialog = builder.create() - alertDialog.show() }) return RNCWebViewWrapper(context, webView) } From 0e850dba135eaae9dcb5f781b301a979233b6739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Loureiro?= <1649425+jpcloureiro@users.noreply.github.com> Date: Tue, 26 Mar 2024 16:58:17 +0000 Subject: [PATCH 3/3] remove bad code from previous merge --- .../reactnativecommunity/webview/RNCWebViewManagerImpl.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt index 869bad851..a77c57203 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt @@ -151,9 +151,6 @@ class RNCWebViewManagerImpl { val alertDialog = builder.create() alertDialog.show() } - builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> } - val alertDialog = builder.create() - alertDialog.show() }) return RNCWebViewWrapper(context, webView) } @@ -721,4 +718,4 @@ class RNCWebViewManagerImpl { fun setWebviewDebuggingEnabled(viewWrapper: RNCWebViewWrapper, enabled: Boolean) { RNCWebView.setWebContentsDebuggingEnabled(enabled) - } + } \ No newline at end of file