electron/shell/browser/electron_permission_manager.cc
electron-roller[bot] e86d1cba75
chore: bump chromium to 105.0.5129.0 (main) (#34403)
* chore: bump chromium in DEPS to 104.0.5096.0

* 3651284: Use the entry settings object for window.open navigation

https://chromium-review.googlesource.com/c/chromium/src/+/3651284

* 3644598: Make RenderFrameHost used for notification permission decision

https://chromium-review.googlesource.com/c/chromium/src/+/3644598

* 3642842: Window Placement: Prototype Fullscreen Capability Delegation - Part 2

https://chromium-review.googlesource.com/c/chromium/src/+/3642842

* 3652785: [sandbox] Enable sandboxed pointers on sanitizer builds

https://chromium-review.googlesource.com/c/v8/v8/+/3652785

* 3611967: webhid: Migrate HidDelegate to use BrowserContext and Origin

https://chromium-review.googlesource.com/c/chromium/src/+/3611967

* 3665762: Remove RenderFrameHost::IsRenderFrameCreated from //content/.

https://chromium-review.googlesource.com/c/chromium/src/+/3665762

* 3659375: Fold x509_util_ios and most of x509_util_mac into x509_util_apple

https://chromium-review.googlesource.com/c/chromium/src/+/3659375

* 3656234: [CodeHealth] Remove uses of base::ListValue::Append() (Final, take 2)

https://chromium-review.googlesource.com/c/chromium/src/+/3656234

* chore: update patch indices

* chore: fix lint

* 3644598: Make RenderFrameHost used for notification permission decision

https://chromium-review.googlesource.com/c/chromium/src/+/3644598

* webhid: Migrate HidDelegate to use BrowserContext and Origin

This is a temporary fix for  https://chromium-review.googlesource.com/c/chromium/src/+/3611967 to get the build compiling, but we need to either patch around  https://chromium-review.googlesource.com/c/chromium/src/+/3611967 or move our device permissioning to BrowserContext

* chore: fix lint

* build: run electron/script/gen-libc++-filenames.js

fixes gn check

* chore: bump chromium in DEPS to 104.0.5098.0

* chore: disable flaking tests

* 3682394: Change pipes surrounding code references in comments to backticks

https://chromium-review.googlesource.com/c/chromium/src/+/3682394

* 3652749: Delete GLRenderer and related classes

https://chromium-review.googlesource.com/c/chromium/src/+/3652749

* chore: fixup patch indices

* 3671199: Remove ContentMainDelegate::PostFieldTrialInitialization

https://chromium-review.googlesource.com/c/chromium/src/+/3671199

* 3607963: hid: Do not exclude HID device with no collections

https://chromium-review.googlesource.com/c/chromium/src/+/3607963

* refactor: use ElectronBrowserContext instead of WebContents to persist devices

due to changes like https://chromium-review.googlesource.com/c/chromium/src/+/3611967,
we can no longer use WebContents to store device permissions so this commit
moves device permission storage to live in memory in ElectronBrowserContext
instead.

* 3557253: Deprecate some signature checks

https://chromium-review.googlesource.com/c/v8/v8/+/3557253

* chore: bump chromium in DEPS to 104.0.5100.0

* 3680781: Add policy for Renderer App Container.

https://chromium-review.googlesource.com/c/chromium/src/+/3680781

* chore: update patch indices

* 3675465: Update NetLog network service API to use mojom::DictionaryValue.

https://chromium-review.googlesource.com/c/chromium/src/+/3675465

* chore: bump chromium in DEPS to 104.0.5102.0

* chore: update patches

* chore: bump chromium in DEPS to 104.0.5103.0

* chore: update patches

* chore: bump chromium in DEPS to 104.0.5104.0

* chore: update patches

* fix: add patch for DCHECK in fullscreen test

* build: fix nan build

* build: make the nan spec runner work on macOS

* chore: bump chromium in DEPS to 104.0.5106.0

* chore: update patches

* chore: bump chromium in DEPS to 104.0.5108.0

* chore: update patches

* chore: bump chromium in DEPS to 104.0.5110.0

* chore: update patches

* chore: bump chromium in DEPS to 104.0.5112.0

* chore: bump chromium in DEPS to 105.0.5113.0

* chore: bump chromium in DEPS to 105.0.5115.0

* chore: bump chromium in DEPS to 105.0.5117.0

* chore: update patches

* chore: update libcpp patch

* 3693745: Delete base::LowerCaseEqualsASCII()

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3693745

* 3669226: Remove printing PostTask usage of IO thread

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3669226

* 3687395: Remove DictionaryValue::HasKey().

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3687395

* 3691014: Prevent unsafe narrowing: ui/accessibility, part 2

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3691014

* 3560567: [MSC] Porting GenerateStreams clients to handle stream vectors.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3560567

* 3684873: [Bluetooth][Win/Linux] Add bluetooth pair confirmation prompt

https://chromium-review.googlesource.com/c/chromium/src/+/3684873

* chore: bump chromium in DEPS to 105.0.5119.0

* chore: missing includes in desktop_notification_controller

* chore: update patches

* 3685951: Reland "Make sure screen object is created once in tests."

https://chromium-review.googlesource.com/c/chromium/src/+/3685951

* fixup: Reland "Make sure screen object is created once in tests."

* 3646014: [API] Deprecate LegacyOOMErrorCallback

Ref: https://chromium-review.googlesource.com/c/v8/v8/+/3646014

* chore: bump chromium in DEPS to 105.0.5121.0

* chore: update patches

* 3699085: [cleanup] update PrintBackend::EnumeratePrinters to use reference

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3699085

* chore: bump chromium in DEPS to 105.0.5123.0

* chore: update patches

* chore: bump chromium in DEPS to 105.0.5125.0

* chore: update patches

* 3630082: [sandbox] Also enable the sandbox outside of Chromium builds

Ref: https://chromium-review.googlesource.com/c/v8/v8/+/3630082

* chore: bump chromium in DEPS to 105.0.5127.0

* chore: update patches

* chore: bump chromium in DEPS to 105.0.5129.0

* chore: update patches

* 3703741: Remove WebContents::GetMainFrame.

https://chromium-review.googlesource.com/c/chromium/src/+/3703741

* chore: update patches

* fixup! 3703741: Remove WebContents::GetMainFrame.

* fix lint

* more lint

* chore: document breaking change

* 3687671: [v8] Freeze flags after initialization

https://chromium-review.googlesource.com/c/chromium/src/+/3687671

* fixup! 3560567: [MSC] Porting GenerateStreams clients to handle stream vectors.

* use the v8 allocator for node serdes

* chore: update patches

* remove faulty non-v8-sandbox-compatible code

* make NodeArrayBufferAllocator use the v8 allocator under the hood

* fixup! 3560567: [MSC] Porting GenerateStreams clients to handle stream vectors.

* fix build on windows

* 3691954: [Reland][Extensions Bindings] Validate arguments sent to API events

https://chromium-review.googlesource.com/c/chromium/src/+/3691954

* chore: remove deprecated AccessorSignatures

https://github.com/nodejs/nan/pull/941

* Update patches/chromium/notification_provenance.patch

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* remove chore_expose_v8_initialization_isolate_callbacks.patch

* add link to nodejs/nan#941

* 52026: Do not allow md4 or md5 based signatures in X.509 certificates.

https://boringssl-review.googlesource.com/c/boringssl/+/52026

* chore: update patches

* disable nan buffer-test

* disable sandboxed pointers for now

* force sandboxed pointers off

* improve node allocation patch

* restore accidentally dropped node crypto test patch

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: Samuel Attard <sattard@salesforce.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: VerteDinde <keeleymhammond@gmail.com>
Co-authored-by: VerteDinde <vertedinde@electronjs.org>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
Co-authored-by: Jeremy Rose <japthorp@slack-corp.com>
2022-06-27 15:50:08 -05:00

361 lines
13 KiB
C++

// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/electron_permission_manager.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/values.h"
#include "content/browser/permissions/permission_util.h" // nogncheck
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "gin/data_object_builder.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/electron_browser_client.h"
#include "shell/browser/electron_browser_main_parts.h"
#include "shell/browser/web_contents_permission_helper.h"
#include "shell/browser/web_contents_preferences.h"
#include "shell/common/gin_converters/content_converter.h"
#include "shell/common/gin_converters/frame_converter.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
namespace electron {
namespace {
bool WebContentsDestroyed(content::RenderFrameHost* rfh) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(rfh);
if (!web_contents)
return true;
return web_contents->IsBeingDestroyed();
}
void PermissionRequestResponseCallbackWrapper(
ElectronPermissionManager::StatusCallback callback,
const std::vector<blink::mojom::PermissionStatus>& vector) {
std::move(callback).Run(vector[0]);
}
} // namespace
class ElectronPermissionManager::PendingRequest {
public:
PendingRequest(content::RenderFrameHost* render_frame_host,
const std::vector<blink::PermissionType>& permissions,
StatusesCallback callback)
: render_process_id_(render_frame_host->GetProcess()->GetID()),
render_frame_id_(render_frame_host->GetGlobalId()),
callback_(std::move(callback)),
permissions_(permissions),
results_(permissions.size(), blink::mojom::PermissionStatus::DENIED),
remaining_results_(permissions.size()) {}
void SetPermissionStatus(int permission_id,
blink::mojom::PermissionStatus status) {
DCHECK(!IsComplete());
if (status == blink::mojom::PermissionStatus::GRANTED) {
const auto permission = permissions_[permission_id];
if (permission == blink::PermissionType::MIDI_SYSEX) {
content::ChildProcessSecurityPolicy::GetInstance()
->GrantSendMidiSysExMessage(render_process_id_);
} else if (permission == blink::PermissionType::GEOLOCATION) {
ElectronBrowserMainParts::Get()
->GetGeolocationControl()
->UserDidOptIntoLocationServices();
}
}
results_[permission_id] = status;
--remaining_results_;
}
content::RenderFrameHost* GetRenderFrameHost() {
return content::RenderFrameHost::FromID(render_frame_id_);
}
bool IsComplete() const { return remaining_results_ == 0; }
void RunCallback() {
if (!callback_.is_null()) {
std::move(callback_).Run(results_);
}
}
private:
int render_process_id_;
content::GlobalRenderFrameHostId render_frame_id_;
StatusesCallback callback_;
std::vector<blink::PermissionType> permissions_;
std::vector<blink::mojom::PermissionStatus> results_;
size_t remaining_results_;
};
ElectronPermissionManager::ElectronPermissionManager() = default;
ElectronPermissionManager::~ElectronPermissionManager() = default;
void ElectronPermissionManager::SetPermissionRequestHandler(
const RequestHandler& handler) {
if (handler.is_null() && !pending_requests_.IsEmpty()) {
for (PendingRequestsMap::iterator iter(&pending_requests_); !iter.IsAtEnd();
iter.Advance()) {
auto* request = iter.GetCurrentValue();
if (!WebContentsDestroyed(request->GetRenderFrameHost()))
request->RunCallback();
}
pending_requests_.Clear();
}
request_handler_ = handler;
}
void ElectronPermissionManager::SetPermissionCheckHandler(
const CheckHandler& handler) {
check_handler_ = handler;
}
void ElectronPermissionManager::SetDevicePermissionHandler(
const DeviceCheckHandler& handler) {
device_permission_handler_ = handler;
}
void ElectronPermissionManager::RequestPermission(
blink::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
StatusCallback response_callback) {
RequestPermissionWithDetails(permission, render_frame_host, requesting_origin,
user_gesture, nullptr,
std::move(response_callback));
}
void ElectronPermissionManager::RequestPermissionWithDetails(
blink::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
const base::DictionaryValue* details,
StatusCallback response_callback) {
RequestPermissionsWithDetails(
std::vector<blink::PermissionType>(1, permission), render_frame_host,
user_gesture, details,
base::BindOnce(PermissionRequestResponseCallbackWrapper,
std::move(response_callback)));
}
void ElectronPermissionManager::RequestPermissions(
const std::vector<blink::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
StatusesCallback response_callback) {
RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture,
nullptr, std::move(response_callback));
}
void ElectronPermissionManager::RequestPermissionsWithDetails(
const std::vector<blink::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host,
bool user_gesture,
const base::DictionaryValue* details,
StatusesCallback response_callback) {
if (permissions.empty()) {
std::move(response_callback).Run({});
return;
}
if (request_handler_.is_null()) {
std::vector<blink::mojom::PermissionStatus> statuses;
for (auto permission : permissions) {
if (permission == blink::PermissionType::MIDI_SYSEX) {
content::ChildProcessSecurityPolicy::GetInstance()
->GrantSendMidiSysExMessage(
render_frame_host->GetProcess()->GetID());
} else if (permission == blink::PermissionType::GEOLOCATION) {
ElectronBrowserMainParts::Get()
->GetGeolocationControl()
->UserDidOptIntoLocationServices();
}
statuses.push_back(blink::mojom::PermissionStatus::GRANTED);
}
std::move(response_callback).Run(statuses);
return;
}
auto* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
int request_id = pending_requests_.Add(std::make_unique<PendingRequest>(
render_frame_host, permissions, std::move(response_callback)));
for (size_t i = 0; i < permissions.size(); ++i) {
auto permission = permissions[i];
const auto callback =
base::BindRepeating(&ElectronPermissionManager::OnPermissionResponse,
base::Unretained(this), request_id, i);
auto mutable_details =
details == nullptr ? base::DictionaryValue() : details->Clone();
mutable_details.SetStringKey(
"requestingUrl", render_frame_host->GetLastCommittedURL().spec());
mutable_details.SetBoolKey("isMainFrame",
render_frame_host->GetParent() == nullptr);
request_handler_.Run(web_contents, permission, callback, mutable_details);
}
}
void ElectronPermissionManager::OnPermissionResponse(
int request_id,
int permission_id,
blink::mojom::PermissionStatus status) {
auto* pending_request = pending_requests_.Lookup(request_id);
if (!pending_request)
return;
pending_request->SetPermissionStatus(permission_id, status);
if (pending_request->IsComplete()) {
pending_request->RunCallback();
pending_requests_.Remove(request_id);
}
}
void ElectronPermissionManager::ResetPermission(
blink::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {}
void ElectronPermissionManager::RequestPermissionsFromCurrentDocument(
const std::vector<blink::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host,
bool user_gesture,
base::OnceCallback<void(const std::vector<blink::mojom::PermissionStatus>&)>
callback) {
RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture,
nullptr, std::move(callback));
}
blink::mojom::PermissionStatus ElectronPermissionManager::GetPermissionStatus(
blink::PermissionType permission,
const GURL& requesting_origin,
const GURL& embedding_origin) {
base::DictionaryValue details;
details.SetString("embeddingOrigin", embedding_origin.spec());
bool granted = CheckPermissionWithDetails(permission, nullptr,
requesting_origin, &details);
return granted ? blink::mojom::PermissionStatus::GRANTED
: blink::mojom::PermissionStatus::DENIED;
}
ElectronPermissionManager::SubscriptionId
ElectronPermissionManager::SubscribePermissionStatusChange(
blink::PermissionType permission,
content::RenderProcessHost* render_process_host,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback) {
return SubscriptionId(-1);
}
void ElectronPermissionManager::UnsubscribePermissionStatusChange(
SubscriptionId id) {}
bool ElectronPermissionManager::CheckPermissionWithDetails(
blink::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
const base::DictionaryValue* details) const {
if (check_handler_.is_null()) {
return true;
}
auto* web_contents =
render_frame_host
? content::WebContents::FromRenderFrameHost(render_frame_host)
: nullptr;
auto mutable_details =
details == nullptr ? base::DictionaryValue() : details->Clone();
if (render_frame_host) {
mutable_details.SetStringKey(
"requestingUrl", render_frame_host->GetLastCommittedURL().spec());
}
mutable_details.SetBoolKey(
"isMainFrame",
render_frame_host && render_frame_host->GetParent() == nullptr);
switch (permission) {
case blink::PermissionType::AUDIO_CAPTURE:
mutable_details.SetStringKey("mediaType", "audio");
break;
case blink::PermissionType::VIDEO_CAPTURE:
mutable_details.SetStringKey("mediaType", "video");
break;
default:
break;
}
return check_handler_.Run(web_contents, permission, requesting_origin,
mutable_details);
}
bool ElectronPermissionManager::CheckDevicePermission(
blink::PermissionType permission,
const url::Origin& origin,
const base::Value& device,
ElectronBrowserContext* browser_context) const {
if (device_permission_handler_.is_null()) {
return browser_context->CheckDevicePermission(origin, device, permission);
} else {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
v8::Local<v8::Object> details = gin::DataObjectBuilder(isolate)
.Set("deviceType", permission)
.Set("origin", origin.Serialize())
.Set("device", device.Clone())
.Build();
return device_permission_handler_.Run(details);
}
}
void ElectronPermissionManager::GrantDevicePermission(
blink::PermissionType permission,
const url::Origin& origin,
const base::Value& device,
ElectronBrowserContext* browser_context) const {
if (device_permission_handler_.is_null()) {
browser_context->GrantDevicePermission(origin, device, permission);
}
}
void ElectronPermissionManager::RevokeDevicePermission(
blink::PermissionType permission,
const url::Origin& origin,
const base::Value& device,
ElectronBrowserContext* browser_context) const {
browser_context->RevokeDevicePermission(origin, device, permission);
}
blink::mojom::PermissionStatus
ElectronPermissionManager::GetPermissionStatusForCurrentDocument(
blink::PermissionType permission,
content::RenderFrameHost* render_frame_host) {
return GetPermissionStatus(
permission, render_frame_host->GetLastCommittedOrigin().GetURL(),
content::PermissionUtil::GetLastCommittedOriginAsURL(render_frame_host));
}
blink::mojom::PermissionStatus
ElectronPermissionManager::GetPermissionStatusForWorker(
blink::PermissionType permission,
content::RenderProcessHost* render_process_host,
const GURL& worker_origin) {
return GetPermissionStatus(permission, worker_origin, worker_origin);
}
} // namespace electron