-
Notifications
You must be signed in to change notification settings - Fork 904
/
Copy pathbrave_shields_util.cc
152 lines (137 loc) · 6.08 KB
/
brave_shields_util.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
/* 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/components/brave_shields/browser/brave_shields_util.h"
#include "base/task/post_task.h"
#include "brave/common/shield_exceptions.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "content/public/common/referrer.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/websocket_handshake_request_info.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/gurl.h"
using content::ResourceContext;
using content::BrowserThread;
using content::Referrer;
using content::ResourceRequestInfo;
using net::URLRequest;
using namespace net::registry_controlled_domains;
namespace brave_shields {
bool GetDefaultFromResourceIdentifier(const std::string& resource_identifier,
const GURL& primary_url, const GURL& secondary_url) {
if (resource_identifier == brave_shields::kAds) {
return false;
} else if (resource_identifier == brave_shields::kTrackers) {
return false;
} else if (resource_identifier == brave_shields::kHTTPUpgradableResources) {
return false;
} else if (resource_identifier == brave_shields::kBraveShields) {
return true;
} else if (resource_identifier == brave_shields::kReferrers) {
return false;
} else if (resource_identifier == brave_shields::kCookies) {
return secondary_url == GURL("https://firstParty/");
}
return false;
}
bool IsAllowContentSettingFromIO(const net::URLRequest* request,
const GURL& primary_url, const GURL& secondary_url,
ContentSettingsType setting_type,
const std::string& resource_identifier) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
const content::ResourceRequestInfo* resource_info =
content::ResourceRequestInfo::ForRequest(request);
if (!resource_info) {
return GetDefaultFromResourceIdentifier(resource_identifier, primary_url,
secondary_url);
}
ProfileIOData* io_data =
ProfileIOData::FromResourceContext(resource_info->GetContext());
return IsAllowContentSettingWithIOData(io_data, primary_url,
secondary_url, setting_type, resource_identifier);
}
bool IsAllowContentSettingWithIOData(ProfileIOData* io_data,
const GURL& primary_url, const GURL& secondary_url,
ContentSettingsType setting_type,
const std::string& resource_identifier) {
if (!io_data) {
return GetDefaultFromResourceIdentifier(resource_identifier, primary_url,
secondary_url);
}
content_settings::SettingInfo setting_info;
std::unique_ptr<base::Value> value =
io_data->GetHostContentSettingsMap()->GetWebsiteSetting(
primary_url, secondary_url,
setting_type,
resource_identifier, &setting_info);
ContentSetting setting =
content_settings::ValueToContentSetting(value.get());
// TODO(bbondy): Add a static RegisterUserPrefs method for shields and use
// prefs instead of simply returning true / false below.
if (setting == CONTENT_SETTING_DEFAULT) {
return GetDefaultFromResourceIdentifier(resource_identifier, primary_url,
secondary_url);
}
return setting == CONTENT_SETTING_ALLOW;
}
void GetRenderFrameInfo(const URLRequest* request,
int* render_frame_id,
int* render_process_id,
int* frame_tree_node_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
*render_frame_id = -1;
*render_process_id = -1;
*frame_tree_node_id = -1;
// PlzNavigate requests have a frame_tree_node_id, but no render_process_id
auto* request_info = content::ResourceRequestInfo::ForRequest(request);
if (request_info) {
*frame_tree_node_id = request_info->GetFrameTreeNodeId();
}
extensions::ExtensionApiFrameIdMap::FrameData frame_data;
if (!content::ResourceRequestInfo::GetRenderFrameForRequest(
request, render_process_id, render_frame_id)) {
const content::WebSocketHandshakeRequestInfo* websocket_info =
content::WebSocketHandshakeRequestInfo::ForRequest(request);
if (websocket_info) {
*render_frame_id = websocket_info->GetRenderFrameId();
*render_process_id = websocket_info->GetChildId();
}
}
}
void DispatchBlockedEventFromIO(const GURL &request_url, int render_frame_id,
int render_process_id, int frame_tree_node_id,
const std::string& block_type) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&BraveShieldsWebContentsObserver::DispatchBlockedEvent,
block_type, request_url.spec(),
render_process_id, render_frame_id, frame_tree_node_id));
}
bool ShouldSetReferrer(bool allow_referrers, bool shields_up,
const GURL& original_referrer, const GURL& tab_origin,
const GURL& target_url, const GURL& new_referrer_url,
blink::WebReferrerPolicy policy, Referrer *output_referrer) {
if (!output_referrer ||
allow_referrers ||
!shields_up ||
original_referrer.is_empty() ||
// Same TLD+1 whouldn't set the referrer
SameDomainOrHost(target_url, original_referrer,
INCLUDE_PRIVATE_REGISTRIES) ||
// Whitelisted referrers shoud never set the referrer
brave::IsWhitelistedReferrer(tab_origin, target_url.GetOrigin())) {
return false;
}
*output_referrer = Referrer::SanitizeForRequest(target_url,
Referrer(new_referrer_url, policy));
return true;
}
} // namespace brave_shields