-
Notifications
You must be signed in to change notification settings - Fork 894
/
brave_content_settings_agent_impl.cc
372 lines (321 loc) · 13.4 KB
/
brave_content_settings_agent_impl.cc
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
/* Copyright 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/renderer/brave_content_settings_agent_impl.h"
#include <string>
#include <utility>
#include <vector>
#include "base/bind_helpers.h"
#include "base/feature_list.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/common/render_messages.h"
#include "brave/common/shield_exceptions.h"
#include "brave/components/brave_shields/common/features.h"
#include "brave/content/common/frame_messages.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "content/public/renderer/render_frame.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom-blink.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom-blink-forward.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "url/url_constants.h"
namespace {
GURL GetOriginOrURL(
const blink::WebFrame* frame) {
url::Origin top_origin = url::Origin(frame->Top()->GetSecurityOrigin());
// The |top_origin| is unique ("null") e.g., for file:// URLs. Use the
// document URL as the primary URL in those cases.
// TODO(alexmos): This is broken for --site-per-process, since top() can be a
// WebRemoteFrame which does not have a document(), and the WebRemoteFrame's
// URL is not replicated. See https://crbug.com/628759.
if (top_origin.opaque() && frame->Top()->IsWebLocalFrame())
return frame->Top()->ToWebLocalFrame()->GetDocument().Url();
return top_origin.GetURL();
}
bool IsBraveShieldsDown(const blink::WebFrame* frame,
const GURL& secondary_url,
const ContentSettingsForOneType& rules) {
ContentSetting setting = CONTENT_SETTING_DEFAULT;
const GURL& primary_url = GetOriginOrURL(frame);
for (const auto& rule : rules) {
if (rule.primary_pattern.Matches(primary_url) &&
rule.secondary_pattern.Matches(secondary_url)) {
setting = rule.GetContentSetting();
break;
}
}
return setting == CONTENT_SETTING_BLOCK;
}
// This method can only be used for brave plugin content settings because
// they are implemented incorrectly and swap primary/secondary url
template <typename URL>
ContentSetting GetBraveContentSettingFromRules(
const ContentSettingsForOneType& shield_rules,
const ContentSettingsForOneType& rules,
const blink::WebFrame* frame,
const URL& secondary_url) {
// if shields is down, allow everything
if (IsBraveShieldsDown(frame, secondary_url, shield_rules))
return CONTENT_SETTING_ALLOW;
const GURL& primary_url = GetOriginOrURL(frame);
const GURL& secondary_gurl = secondary_url;
for (const auto& rule : rules) {
// this swap is intentional - see comment at the beginning of the method
if (rule.primary_pattern.Matches(secondary_gurl) &&
rule.secondary_pattern.Matches(primary_url)) {
return rule.GetContentSetting();
}
}
return CONTENT_SETTING_DEFAULT;
}
} // namespace
BraveContentSettingsAgentImpl::BraveContentSettingsAgentImpl(
content::RenderFrame* render_frame,
bool should_whitelist,
service_manager::BinderRegistry* registry)
: ContentSettingsAgentImpl(render_frame, should_whitelist, registry) {
}
BraveContentSettingsAgentImpl::~BraveContentSettingsAgentImpl() {
}
bool BraveContentSettingsAgentImpl::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(BraveContentSettingsAgentImpl, message)
IPC_MESSAGE_HANDLER(BraveFrameMsg_AllowScriptsOnce, OnAllowScriptsOnce)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
if (handled) return true;
return ContentSettingsAgentImpl::OnMessageReceived(message);
}
void BraveContentSettingsAgentImpl::OnAllowScriptsOnce(
const std::vector<std::string>& origins) {
preloaded_temporarily_allowed_scripts_ = std::move(origins);
}
void BraveContentSettingsAgentImpl::DidCommitProvisionalLoad(
bool is_same_document_navigation, ui::PageTransition transition) {
if (!is_same_document_navigation) {
temporarily_allowed_scripts_ =
std::move(preloaded_temporarily_allowed_scripts_);
}
ContentSettingsAgentImpl::DidCommitProvisionalLoad(
is_same_document_navigation, transition);
}
bool BraveContentSettingsAgentImpl::IsScriptTemporilyAllowed(
const GURL& script_url) {
// Check if scripts from this origin are temporily allowed or not.
// Also matches the full script URL to support data URL cases which we use
// the full URL to allow it.
return base::Contains(
temporarily_allowed_scripts_, script_url.GetOrigin().spec()) ||
base::Contains(temporarily_allowed_scripts_, script_url.spec());
}
void BraveContentSettingsAgentImpl::BraveSpecificDidBlockJavaScript(
const base::string16& details) {
Send(new BraveViewHostMsg_JavaScriptBlocked(routing_id(), details));
}
bool BraveContentSettingsAgentImpl::AllowScript(
bool enabled_per_settings) {
// clear cached url for other flow like directly calling `DidNotAllowScript`
// without calling `AllowScriptFromSource` first
blocked_script_url_ = GURL::EmptyGURL();
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
const GURL secondary_url(
url::Origin(frame->GetDocument().GetSecurityOrigin()).GetURL());
bool allow = ContentSettingsAgentImpl::AllowScript(enabled_per_settings);
allow = allow ||
IsBraveShieldsDown(frame, secondary_url) ||
IsScriptTemporilyAllowed(secondary_url);
return allow;
}
void BraveContentSettingsAgentImpl::DidNotAllowScript() {
if (!blocked_script_url_.is_empty()) {
BraveSpecificDidBlockJavaScript(
base::UTF8ToUTF16(blocked_script_url_.spec()));
blocked_script_url_ = GURL::EmptyGURL();
}
ContentSettingsAgentImpl::DidNotAllowScript();
}
bool BraveContentSettingsAgentImpl::AllowScriptFromSource(
bool enabled_per_settings,
const blink::WebURL& script_url) {
const GURL secondary_url(script_url);
bool allow = ContentSettingsAgentImpl::AllowScriptFromSource(
enabled_per_settings, script_url);
// scripts with whitelisted protocols, such as chrome://extensions should
// be allowed
bool should_white_list = IsWhitelistedForContentSettings(
blink::WebSecurityOrigin::Create(script_url),
render_frame()->GetWebFrame()->GetDocument().Url());
allow = allow ||
should_white_list ||
IsBraveShieldsDown(render_frame()->GetWebFrame(), secondary_url) ||
IsScriptTemporilyAllowed(secondary_url);
if (!allow) {
blocked_script_url_ = secondary_url;
}
return allow;
}
void BraveContentSettingsAgentImpl::DidBlockFingerprinting(
const base::string16& details) {
Send(new BraveViewHostMsg_FingerprintingBlocked(routing_id(), details));
}
ContentSetting BraveContentSettingsAgentImpl::GetFPContentSettingFromRules(
const ContentSettingsForOneType& rules,
const blink::WebFrame* frame,
const GURL& secondary_url) {
if (rules.size() == 0)
return CONTENT_SETTING_DEFAULT;
const GURL& primary_url = GetOriginOrURL(frame);
for (const auto& rule : rules) {
ContentSettingsPattern secondary_pattern = rule.secondary_pattern;
if (rule.secondary_pattern ==
ContentSettingsPattern::FromString("https://firstParty/*")) {
secondary_pattern = ContentSettingsPattern::FromString(
"[*.]" + GetOriginOrURL(frame).HostNoBrackets());
}
if (rule.primary_pattern.Matches(primary_url) &&
(secondary_pattern == ContentSettingsPattern::Wildcard() ||
secondary_pattern.Matches(secondary_url))) {
return rule.GetContentSetting();
}
}
// for cases which are third party resources and doesn't match any existing
// rules, block them by default
return CONTENT_SETTING_BLOCK;
}
bool BraveContentSettingsAgentImpl::IsBraveShieldsDown(
const blink::WebFrame* frame,
const GURL& secondary_url) {
return !content_setting_rules_ ||
::IsBraveShieldsDown(frame, secondary_url,
content_setting_rules_->brave_shields_rules);
}
bool BraveContentSettingsAgentImpl::AllowFingerprinting(
bool enabled_per_settings) {
if (!enabled_per_settings)
return false;
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
const GURL secondary_url(
url::Origin(frame->GetDocument().GetSecurityOrigin()).GetURL());
if (IsBraveShieldsDown(frame, secondary_url)) {
return true;
}
const GURL& primary_url = GetOriginOrURL(frame);
if (brave::IsWhitelistedFingerprintingException(primary_url, secondary_url)) {
return true;
}
if (base::FeatureList::IsEnabled(
brave_shields::features::kFingerprintingProtectionV2)) {
return GetBraveFarblingLevel() != BraveFarblingLevel::MAXIMUM;
}
ContentSettingsForOneType rules;
if (content_setting_rules_) {
rules = content_setting_rules_->fingerprinting_rules;
}
ContentSettingPatternSource default_rule = ContentSettingPatternSource(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString("https://firstParty/*"),
base::Value::FromUniquePtrValue(
content_settings::ContentSettingToValue(CONTENT_SETTING_ALLOW)),
std::string(), false);
rules.push_back(default_rule);
ContentSetting setting =
GetFPContentSettingFromRules(rules, frame, secondary_url);
rules.pop_back();
bool allow = setting != CONTENT_SETTING_BLOCK;
allow = allow || IsWhitelistedForContentSettings();
if (!allow) {
DidBlockFingerprinting(base::UTF8ToUTF16(secondary_url.spec()));
}
return allow;
}
BraveFarblingLevel BraveContentSettingsAgentImpl::GetBraveFarblingLevel() {
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
ContentSetting setting = CONTENT_SETTING_DEFAULT;
if (content_setting_rules_) {
setting = GetBraveContentSettingFromRules(
content_setting_rules_->brave_shields_rules,
content_setting_rules_->fingerprinting_rules, frame,
url::Origin(frame->GetDocument().GetSecurityOrigin()).GetURL());
}
if (base::FeatureList::IsEnabled(
brave_shields::features::kFingerprintingProtectionV2)) {
if (setting == CONTENT_SETTING_BLOCK) {
return BraveFarblingLevel::MAXIMUM;
} else if (setting == CONTENT_SETTING_ALLOW) {
return BraveFarblingLevel::OFF;
} else {
return BraveFarblingLevel::BALANCED;
}
} else {
if (setting == CONTENT_SETTING_ALLOW) {
return BraveFarblingLevel::OFF;
} else {
return BraveFarblingLevel::BALANCED;
}
}
}
bool BraveContentSettingsAgentImpl::AllowAutoplay(bool default_value) {
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
auto origin = frame->GetDocument().GetSecurityOrigin();
// default allow local files
if (origin.IsNull() || origin.Protocol().Ascii() == url::kFileScheme) {
VLOG(1) << "AllowAutoplay=true because no origin or file scheme";
return true;
}
// respect user's site blocklist, if any
bool ask = false;
const GURL& primary_url = GetOriginOrURL(frame);
const GURL& secondary_url =
url::Origin(frame->GetDocument().GetSecurityOrigin()).GetURL();
for (const auto& rule : content_setting_rules_->autoplay_rules) {
if (rule.primary_pattern == ContentSettingsPattern::Wildcard())
continue;
if (rule.primary_pattern.Matches(primary_url) &&
(rule.secondary_pattern == ContentSettingsPattern::Wildcard() ||
rule.secondary_pattern.Matches(secondary_url))) {
if (rule.GetContentSetting() == CONTENT_SETTING_BLOCK) {
VLOG(1) << "AllowAutoplay=false because rule=CONTENT_SETTING_BLOCK";
return false;
} else if (rule.GetContentSetting() == CONTENT_SETTING_ASK) {
VLOG(1) << "AllowAutoplay=ask because rule=CONTENT_SETTING_ASK";
ask = true;
}
}
}
if (ask) {
mojo::Remote<blink::mojom::PermissionService> permission_service;
render_frame()->GetBrowserInterfaceBroker()->GetInterface(
permission_service.BindNewPipeAndPassReceiver());
if (permission_service.get()) {
// Request permission (asynchronously) but exit this function without
// allowing autoplay. Depending on settings and previous user choices,
// this may display visible permissions UI, or an "autoplay blocked"
// message, or nothing. In any case, we can't wait for it now.
auto request_permission_descriptor =
blink::mojom::PermissionDescriptor::New();
request_permission_descriptor->name =
blink::mojom::PermissionName::AUTOPLAY;
permission_service->RequestPermission(
std::move(request_permission_descriptor), true, base::DoNothing());
}
return false;
}
bool allow = ContentSettingsAgentImpl::AllowAutoplay(default_value);
if (allow)
VLOG(1) << "AllowAutoplay=true because "
"ContentSettingsAgentImpl::AllowAutoplay says so";
else
VLOG(1) << "AllowAutoplay=false because "
"ContentSettingsAgentImpl::AllowAutoplay says so";
return allow;
}