Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android filename validation when downloading from browser #7

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,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
Expand All @@ -27,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()
Expand Down Expand Up @@ -106,33 +110,46 @@ 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)
}
//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) {
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
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()
)
) {
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)
Expand Down