Skip to content

Commit

Permalink
Merge branch 'main' into location-permission-dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcloureiro committed Mar 26, 2024
2 parents a3ceec6 + 1629a52 commit f868b86
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 30 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/security-code-scanner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: 'MetaMask Security Code Scanner'

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
run-security-scan:
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
steps:
- name: MetaMask Security Code Scanner
uses: MetaMask/Security-Code-Scanner@main
with:
repo: ${{ github.repository }}
paths_ignored: |
.storybook/
'**/__snapshots__/'
'**/*.snap'
'**/*.stories.js'
'**/*.stories.tsx'
'**/*.test.browser.ts*'
'**/*.test.js*'
'**/*.test.ts*'
'**/fixtures/'
'**/jest.config.js'
'**/jest.environment.js'
'**/mocks/'
'**/test*/'
docs/
e2e/
merged-packages/
node_modules
storybook/
test*/
rules_excluded: example
mixpanel_project_token: ${{ secrets.SECURITY_CODE_SCANNER_MIXPANEL_TOKEN }}
slack_webhook: ${{ secrets.APPSEC_BOT_SLACK_WEBHOOK }}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -151,11 +152,14 @@ public void onPermissionRequest(final PermissionRequest request) {
ArrayList<String> requestedAndroidPermissions = new ArrayList<>();
for (String requestedResource : request.getResources()) {
String androidPermission = null;
String requestPermissionIdentifier = null;

if (requestedResource.equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE)) {
androidPermission = Manifest.permission.RECORD_AUDIO;
requestPermissionIdentifier = "microphone";
} else if (requestedResource.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
androidPermission = Manifest.permission.CAMERA;
requestPermissionIdentifier = "camera";
} else if(requestedResource.equals(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID)) {
if (mAllowsProtectedMedia) {
grantedPermissions.add(requestedResource);
Expand All @@ -170,9 +174,23 @@ public void onPermissionRequest(final PermissionRequest request) {
androidPermission = PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID;
} }
// TODO: RESOURCE_MIDI_SYSEX, RESOURCE_PROTECTED_MEDIA_ID.
String alertMessage = String.format("Allow this app to use your " + requestPermissionIdentifier + "?");
if (androidPermission != null) {
if (ContextCompat.checkSelfPermission(this.mWebView.getThemedReactContext(), androidPermission) == PackageManager.PERMISSION_GRANTED) {
grantedPermissions.add(requestedResource);
AlertDialog.Builder builder = new AlertDialog.Builder(this.mWebView.getContext());
builder.setMessage(alertMessage);
builder.setCancelable(false);
String finalAndroidPermission = androidPermission;
builder.setPositiveButton("Allow", (dialog, which) -> {
permissionRequest = request;
grantedPermissions.add(finalAndroidPermission);
requestPermissions(grantedPermissions);
});
builder.setNegativeButton("Don't allow", (dialog, which) -> {
request.deny();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
} else {
requestedAndroidPermissions.add(androidPermission);
}
Expand All @@ -181,8 +199,10 @@ public void onPermissionRequest(final PermissionRequest request) {

// If all the permissions are already granted, send the response to the WebView synchronously
if (requestedAndroidPermissions.isEmpty()) {
request.grant(grantedPermissions.toArray(new String[0]));
grantedPermissions = null;
if (!grantedPermissions.isEmpty()) {
request.grant(grantedPermissions.toArray(new String[0]));
grantedPermissions = null;
}
return;
}

Expand Down Expand Up @@ -381,4 +401,3 @@ public void setAllowsProtectedMedia(boolean enabled) {
public void setHasOnOpenWindowEvent(boolean hasEvent) {
mHasOnOpenWindowEvent = hasEvent;
}
}
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 Down Expand Up @@ -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)
}
Expand Down

0 comments on commit f868b86

Please sign in to comment.