1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/permissions/permission_context_base.h"
#include <stddef.h>
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_decision_auto_blocker.h"
#include "components/permissions/permission_request.h"
#include "components/permissions/permission_request_id.h"
#include "components/permissions/permission_request_impl.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/permission_uma_util.h"
#include "components/permissions/permission_util.h"
#include "components/permissions/permissions_client.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/origin_util.h"
#include "url/gurl.h"
namespace permissions {
namespace {
const char kPermissionBlockedKillSwitchMessage[] =
"%s permission has been blocked.";
#if defined(OS_ANDROID)
const char kPermissionBlockedRepeatedDismissalsMessage[] =
"%s permission has been blocked as the user has dismissed the permission "
"prompt several times. This can be reset in Site Settings. See "
"https://www.chromestatus.com/features/6443143280984064 for more "
"information.";
const char kPermissionBlockedRepeatedIgnoresMessage[] =
"%s permission has been blocked as the user has ignored the permission "
"prompt several times. This can be reset in Site Settings. See "
"https://www.chromestatus.com/features/6443143280984064 for more "
"information.";
#else
const char kPermissionBlockedRepeatedDismissalsMessage[] =
"%s permission has been blocked as the user has dismissed the permission "
"prompt several times. This can be reset in Page Info which can be "
"accessed by clicking the lock icon next to the URL. See "
"https://www.chromestatus.com/features/6443143280984064 for more "
"information.";
const char kPermissionBlockedRepeatedIgnoresMessage[] =
"%s permission has been blocked as the user has ignored the permission "
"prompt several times. This can be reset in Page Info which can be "
"accessed by clicking the lock icon next to the URL. See "
"https://www.chromestatus.com/features/6443143280984064 for more "
"information.";
#endif
const char kPermissionBlockedFeaturePolicyMessage[] =
"%s permission has been blocked because of a Feature Policy applied to the "
"current document. See https://goo.gl/EuHzyv for more details.";
const char kPermissionBlockedPortalsMessage[] =
"%s permission has been blocked because it was requested inside a portal. "
"Portals don't currently support permission requests.";
void LogPermissionBlockedMessage(content::WebContents* web_contents,
const char* message,
ContentSettingsType type) {
web_contents->GetMainFrame()->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kWarning,
base::StringPrintf(message,
PermissionUtil::GetPermissionString(type).c_str()));
}
} // namespace
// static
const char PermissionContextBase::kPermissionsKillSwitchFieldStudy[] =
"PermissionsKillSwitch";
// static
const char PermissionContextBase::kPermissionsKillSwitchBlockedValue[] =
"blocked";
PermissionContextBase::PermissionContextBase(
content::BrowserContext* browser_context,
ContentSettingsType content_settings_type,
blink::mojom::FeaturePolicyFeature feature_policy_feature)
: browser_context_(browser_context),
content_settings_type_(content_settings_type),
feature_policy_feature_(feature_policy_feature) {
PermissionDecisionAutoBlocker::UpdateFromVariations();
}
PermissionContextBase::~PermissionContextBase() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
void PermissionContextBase::RequestPermission(
content::WebContents* web_contents,
const PermissionRequestID& id,
const GURL& requesting_frame,
bool user_gesture,
BrowserPermissionCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
GURL requesting_origin = requesting_frame.GetOrigin();
GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin();
if (!requesting_origin.is_valid() || !embedding_origin.is_valid()) {
std::string type_name =
PermissionUtil::GetPermissionString(content_settings_type_);
DVLOG(1) << "Attempt to use " << type_name
<< " from an invalid URL: " << requesting_origin << ","
<< embedding_origin << " (" << type_name
<< " is not supported in popups)";
NotifyPermissionSet(id, requesting_origin, embedding_origin,
std::move(callback), false /* persist */,
CONTENT_SETTING_BLOCK);
return;
}
// Check the content setting to see if the user has already made a decision,
// or if the origin is under embargo. If so, respect that decision.
content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
id.render_process_id(), id.render_frame_id());
PermissionResult result =
GetPermissionStatus(rfh, requesting_origin, embedding_origin);
if (result.content_setting == CONTENT_SETTING_ALLOW ||
result.content_setting == CONTENT_SETTING_BLOCK) {
switch (result.source) {
case PermissionStatusSource::KILL_SWITCH:
// Block the request and log to the developer console.
LogPermissionBlockedMessage(web_contents,
kPermissionBlockedKillSwitchMessage,
content_settings_type_);
std::move(callback).Run(CONTENT_SETTING_BLOCK);
return;
case PermissionStatusSource::MULTIPLE_DISMISSALS:
LogPermissionBlockedMessage(web_contents,
kPermissionBlockedRepeatedDismissalsMessage,
content_settings_type_);
break;
case PermissionStatusSource::MULTIPLE_IGNORES:
LogPermissionBlockedMessage(web_contents,
kPermissionBlockedRepeatedIgnoresMessage,
content_settings_type_);
break;
case PermissionStatusSource::FEATURE_POLICY:
LogPermissionBlockedMessage(web_contents,
kPermissionBlockedFeaturePolicyMessage,
content_settings_type_);
break;
case PermissionStatusSource::PORTAL:
LogPermissionBlockedMessage(web_contents,
kPermissionBlockedPortalsMessage,
content_settings_type_);
break;
case PermissionStatusSource::INSECURE_ORIGIN:
case PermissionStatusSource::UNSPECIFIED:
case PermissionStatusSource::VIRTUAL_URL_DIFFERENT_ORIGIN:
break;
}
// If we are under embargo, record the embargo reason for which we have
// suppressed the prompt.
PermissionUmaUtil::RecordEmbargoPromptSuppressionFromSource(result.source);
NotifyPermissionSet(id, requesting_origin, embedding_origin,
std::move(callback), false /* persist */,
result.content_setting);
return;
}
// Make sure we do not show a UI for cached documents
if (content::BackForwardCache::EvictIfCached(
content::GlobalFrameRoutingId(id.render_process_id(),
id.render_frame_id()),
"PermissionContextBase::RequestPermission")) {
std::move(callback).Run(result.content_setting);
return;
}
// We are going to show a prompt now.
PermissionUmaUtil::PermissionRequested(content_settings_type_,
requesting_origin);
PermissionUmaUtil::RecordEmbargoPromptSuppression(
PermissionEmbargoStatus::NOT_EMBARGOED);
DecidePermission(web_contents, id, requesting_origin, embedding_origin,
user_gesture, std::move(callback));
}
void PermissionContextBase::UserMadePermissionDecision(
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
ContentSetting content_setting) {}
PermissionResult PermissionContextBase::GetPermissionStatus(
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
const GURL& embedding_origin) const {
// If the permission has been disabled through Finch, block all requests.
if (IsPermissionKillSwitchOn()) {
return PermissionResult(CONTENT_SETTING_BLOCK,
PermissionStatusSource::KILL_SWITCH);
}
if (!IsPermissionAvailableToOrigins(requesting_origin, embedding_origin)) {
return PermissionResult(CONTENT_SETTING_BLOCK,
PermissionStatusSource::INSECURE_ORIGIN);
}
// Check whether the feature is enabled for the frame by feature policy. We
// can only do this when a RenderFrameHost has been provided.
if (render_frame_host &&
!PermissionAllowedByFeaturePolicy(render_frame_host)) {
return PermissionResult(CONTENT_SETTING_BLOCK,
PermissionStatusSource::FEATURE_POLICY);
}
if (render_frame_host) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
// Permissions are denied for portals.
if (web_contents && web_contents->IsPortal()) {
return PermissionResult(CONTENT_SETTING_BLOCK,
PermissionStatusSource::PORTAL);
}
// Automatically deny all HTTP or HTTPS requests where the virtual URL and
// the loaded URL are for different origins. The loaded URL is the one
// actually in the renderer, but the virtual URL is the one
// seen by the user. This may be very confusing for a user to see in a
// permissions request.
content::NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
if (entry) {
const GURL virtual_url = entry->GetVirtualURL();
const GURL loaded_url = entry->GetURL();
if (virtual_url.SchemeIsHTTPOrHTTPS() &&
loaded_url.SchemeIsHTTPOrHTTPS() &&
!url::Origin::Create(virtual_url)
.IsSameOriginWith(url::Origin::Create(loaded_url))) {
return PermissionResult(
CONTENT_SETTING_BLOCK,
PermissionStatusSource::VIRTUAL_URL_DIFFERENT_ORIGIN);
}
}
}
ContentSetting content_setting = GetPermissionStatusInternal(
render_frame_host, requesting_origin, embedding_origin);
if (content_setting != CONTENT_SETTING_ASK) {
return PermissionResult(content_setting,
PermissionStatusSource::UNSPECIFIED);
}
PermissionResult result =
PermissionsClient::Get()
->GetPermissionDecisionAutoBlocker(browser_context_)
->GetEmbargoResult(requesting_origin, content_settings_type_);
DCHECK(result.content_setting == CONTENT_SETTING_ASK ||
result.content_setting == CONTENT_SETTING_BLOCK);
return result;
}
bool PermissionContextBase::IsPermissionAvailableToOrigins(
const GURL& requesting_origin,
const GURL& embedding_origin) const {
if (IsRestrictedToSecureOrigins()) {
if (!content::IsOriginSecure(requesting_origin))
return false;
// TODO(raymes): We should check the entire chain of embedders here whenever
// possible as this corresponds to the requirements of the secure contexts
// spec and matches what is implemented in blink. Right now we just check
// the top level and requesting origins.
if (!PermissionsClient::Get()->CanBypassEmbeddingOriginCheck(
requesting_origin, embedding_origin) &&
!content::IsOriginSecure(embedding_origin)) {
return false;
}
}
return true;
}
PermissionResult PermissionContextBase::UpdatePermissionStatusWithDeviceStatus(
PermissionResult result,
const GURL& requesting_origin,
const GURL& embedding_origin) const {
return result;
}
void PermissionContextBase::ResetPermission(const GURL& requesting_origin,
const GURL& embedding_origin) {
if (!content_settings::ContentSettingsRegistry::GetInstance()->Get(
content_settings_type_)) {
return;
}
PermissionsClient::Get()
->GetSettingsMap(browser_context_)
->SetContentSettingDefaultScope(requesting_origin, embedding_origin,
content_settings_type_, std::string(),
CONTENT_SETTING_DEFAULT);
}
bool PermissionContextBase::IsPermissionKillSwitchOn() const {
const std::string param = base::GetFieldTrialParamValue(
kPermissionsKillSwitchFieldStudy,
PermissionUtil::GetPermissionString(content_settings_type_));
return param == kPermissionsKillSwitchBlockedValue;
}
ContentSetting PermissionContextBase::GetPermissionStatusInternal(
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
const GURL& embedding_origin) const {
return PermissionsClient::Get()
->GetSettingsMap(browser_context_)
->GetContentSetting(requesting_origin, embedding_origin,
content_settings_type_, std::string());
}
void PermissionContextBase::DecidePermission(
content::WebContents* web_contents,
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
bool user_gesture,
BrowserPermissionCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Under permission delegation, when we display a permission prompt, the
// origin displayed in the prompt should never differ from the top-level
// origin. Storage access API requests are excluded as they are expected to
// request permissions from the frame origin needing access.
DCHECK(!base::FeatureList::IsEnabled(features::kPermissionDelegation) ||
PermissionsClient::Get()->CanBypassEmbeddingOriginCheck(
requesting_origin, embedding_origin) ||
requesting_origin == embedding_origin ||
content_settings_type_ == ContentSettingsType::STORAGE_ACCESS);
PermissionRequestManager* permission_request_manager =
PermissionRequestManager::FromWebContents(web_contents);
// TODO(felt): sometimes |permission_request_manager| is null. This check is
// meant to prevent crashes. See crbug.com/457091.
if (!permission_request_manager)
return;
std::unique_ptr<PermissionRequest> request_ptr =
std::make_unique<PermissionRequestImpl>(
embedding_origin, requesting_origin, content_settings_type_,
user_gesture,
base::BindOnce(&PermissionContextBase::PermissionDecided,
weak_factory_.GetWeakPtr(), id, requesting_origin,
embedding_origin, std::move(callback)),
base::BindOnce(&PermissionContextBase::CleanUpRequest,
weak_factory_.GetWeakPtr(), id));
PermissionRequest* request = request_ptr.get();
bool inserted =
pending_requests_
.insert(std::make_pair(id.ToString(), std::move(request_ptr)))
.second;
DCHECK(inserted) << "Duplicate id " << id.ToString();
permission_request_manager->AddRequest(request);
}
void PermissionContextBase::PermissionDecided(
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
BrowserPermissionCallback callback,
ContentSetting content_setting) {
DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
content_setting == CONTENT_SETTING_BLOCK ||
content_setting == CONTENT_SETTING_DEFAULT);
UserMadePermissionDecision(id, requesting_origin, embedding_origin,
content_setting);
bool persist = content_setting != CONTENT_SETTING_DEFAULT;
NotifyPermissionSet(id, requesting_origin, embedding_origin,
std::move(callback), persist, content_setting);
}
content::BrowserContext* PermissionContextBase::browser_context() const {
return browser_context_;
}
void PermissionContextBase::NotifyPermissionSet(
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
BrowserPermissionCallback callback,
bool persist,
ContentSetting content_setting) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (persist)
UpdateContentSetting(requesting_origin, embedding_origin, content_setting);
UpdateTabContext(id, requesting_origin,
content_setting == CONTENT_SETTING_ALLOW);
if (content_setting == CONTENT_SETTING_DEFAULT)
content_setting = CONTENT_SETTING_ASK;
std::move(callback).Run(content_setting);
}
void PermissionContextBase::CleanUpRequest(const PermissionRequestID& id) {
size_t success = pending_requests_.erase(id.ToString());
DCHECK(success == 1) << "Missing request " << id.ToString();
}
void PermissionContextBase::UpdateContentSetting(
const GURL& requesting_origin,
const GURL& embedding_origin,
ContentSetting content_setting) {
DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin());
DCHECK_EQ(embedding_origin, embedding_origin.GetOrigin());
DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
content_setting == CONTENT_SETTING_BLOCK);
DCHECK(!requesting_origin.SchemeIsFile());
DCHECK(!embedding_origin.SchemeIsFile());
PermissionsClient::Get()
->GetSettingsMap(browser_context_)
->SetContentSettingDefaultScope(requesting_origin, embedding_origin,
content_settings_type_, std::string(),
content_setting);
}
bool PermissionContextBase::PermissionAllowedByFeaturePolicy(
content::RenderFrameHost* rfh) const {
// Some features don't have an associated feature policy yet. Allow those.
if (feature_policy_feature_ == blink::mojom::FeaturePolicyFeature::kNotFound)
return true;
return rfh->IsFeatureEnabled(feature_policy_feature_);
}
} // namespace permissions
|