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 all 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
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
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()
//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)
}
Expand Down Expand Up @@ -711,5 +718,4 @@ class RNCWebViewManagerImpl {

fun setWebviewDebuggingEnabled(viewWrapper: RNCWebViewWrapper, enabled: Boolean) {
RNCWebView.setWebContentsDebuggingEnabled(enabled)
}
}
}
Loading