OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/browsing_data/clear_site_data_throttle.h" | 5 #include "content/browser/browsing_data/clear_site_data_throttle.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "content/browser/frame_host/navigation_handle_impl.h" | 15 #include "content/browser/service_worker/service_worker_response_info.h" |
16 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
17 #include "content/public/browser/browser_thread.h" | |
17 #include "content/public/browser/content_browser_client.h" | 18 #include "content/public/browser/content_browser_client.h" |
18 #include "content/public/browser/navigation_handle.h" | 19 #include "content/public/browser/render_frame_host.h" |
20 #include "content/public/browser/resource_controller.h" | |
21 #include "content/public/browser/storage_partition.h" | |
19 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
20 #include "content/public/common/content_client.h" | 23 #include "content/public/common/content_client.h" |
21 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
22 #include "content/public/common/origin_util.h" | 25 #include "content/public/common/origin_util.h" |
26 #include "content/public/common/resource_response_info.h" | |
27 #include "net/base/load_flags.h" | |
23 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
29 #include "net/url_request/redirect_info.h" | |
24 #include "url/gurl.h" | 30 #include "url/gurl.h" |
25 #include "url/origin.h" | 31 #include "url/origin.h" |
26 | 32 |
27 namespace content { | 33 namespace content { |
28 | 34 |
29 namespace { | 35 namespace { |
30 | 36 |
37 static const char* kNameForLogging = "ClearSiteDataThrottle"; | |
38 | |
31 static const char* kClearSiteDataHeader = "Clear-Site-Data"; | 39 static const char* kClearSiteDataHeader = "Clear-Site-Data"; |
32 | 40 |
33 static const char* kTypesKey = "types"; | 41 static const char* kTypesKey = "types"; |
34 | 42 |
35 // Pretty-printed log output. | 43 // Pretty-printed log output. |
36 static const char* kConsoleMessagePrefix = "Clear-Site-Data header on '%s': %s"; | 44 static const char* kConsoleMessagePrefix = "Clear-Site-Data header on '%s': %s"; |
37 static const char* kClearingOneType = "Clearing %s."; | 45 static const char* kClearingOneType = "Clearing %s."; |
38 static const char* kClearingTwoTypes = "Clearing %s and %s."; | 46 static const char* kClearingTwoTypes = "Clearing %s and %s."; |
39 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s."; | 47 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s."; |
40 | 48 |
(...skipping 10 matching lines...) Expand all Loading... | |
51 switches::kEnableExperimentalWebPlatformFeatures); | 59 switches::kEnableExperimentalWebPlatformFeatures); |
52 } | 60 } |
53 | 61 |
54 // Represents the parameters as a single number to be recorded in a histogram. | 62 // Represents the parameters as a single number to be recorded in a histogram. |
55 int ParametersMask(bool clear_cookies, bool clear_storage, bool clear_cache) { | 63 int ParametersMask(bool clear_cookies, bool clear_storage, bool clear_cache) { |
56 return static_cast<int>(clear_cookies) * (1 << 0) + | 64 return static_cast<int>(clear_cookies) * (1 << 0) + |
57 static_cast<int>(clear_storage) * (1 << 1) + | 65 static_cast<int>(clear_storage) * (1 << 1) + |
58 static_cast<int>(clear_cache) * (1 << 2); | 66 static_cast<int>(clear_cache) * (1 << 2); |
59 } | 67 } |
60 | 68 |
61 } // namespace | 69 // A helper function to pass an IO thread callback to a method called on |
62 | 70 // the UI thread. |
63 // static | 71 void JumpFromUIToIOThread(const base::Closure& callback) { |
64 std::unique_ptr<NavigationThrottle> | 72 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
65 ClearSiteDataThrottle::CreateThrottleForNavigation(NavigationHandle* handle) { | 73 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback); |
66 if (AreExperimentalFeaturesEnabled()) | |
67 return base::WrapUnique(new ClearSiteDataThrottle(handle)); | |
68 | |
69 return std::unique_ptr<NavigationThrottle>(); | |
70 } | 74 } |
71 | 75 |
72 ClearSiteDataThrottle::ClearSiteDataThrottle( | 76 // Finds the BrowserContext associated with the request and requests |
73 NavigationHandle* navigation_handle) | 77 // the actual clearing of data for |origin|. The datatypes to be deleted |
74 : NavigationThrottle(navigation_handle), | 78 // are determined by |clear_cookies|, |clear_storage|, and |clear_cache|. |
75 clearing_in_progress_(false), | 79 // |web_contents_getter| identifies the WebContents from which the request |
76 weak_ptr_factory_(this) {} | 80 // originated. Must be run on the UI thread. The |callback| will be executed |
81 // on the IO thread. | |
82 void ClearSiteDataOnUIThread( | |
83 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
84 url::Origin origin, | |
85 bool clear_cookies, | |
86 bool clear_storage, | |
87 bool clear_cache, | |
88 const base::Closure& callback) { | |
89 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
77 | 90 |
78 ClearSiteDataThrottle::~ClearSiteDataThrottle() { | 91 WebContents* web_contents = web_contents_getter.Run(); |
79 // At the end of the navigation we finally have access to the correct | 92 if (!web_contents) |
80 // RenderFrameHost. Output the cached console messages. Prefix each sequence | 93 return; |
81 // of messages belonging to the same URL with |kConsoleMessagePrefix|. | 94 |
95 BrowserContext* browser_context = web_contents->GetBrowserContext(); | |
96 | |
97 GetContentClient()->browser()->ClearSiteData( | |
98 browser_context, origin, clear_cookies, clear_storage, clear_cache, | |
99 base::Bind(&JumpFromUIToIOThread, callback)); | |
100 } | |
101 | |
102 // Outputs |messages| to the console of WebContents retrieved from | |
103 // |web_contents_getter|. Must be run on the UI thread. | |
104 void OutputConsoleMessagesOnUIThread( | |
105 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
106 const std::vector<ClearSiteDataThrottle::ConsoleMessage>& messages) { | |
107 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
108 | |
109 WebContents* web_contents = web_contents_getter.Run(); | |
110 if (!web_contents) | |
111 return; | |
112 | |
82 GURL last_seen_url; | 113 GURL last_seen_url; |
83 for (const ConsoleMessage& message : messages_) { | 114 for (const ClearSiteDataThrottle::ConsoleMessage& message : messages) { |
84 if (message.url == last_seen_url) { | 115 if (message.url == last_seen_url) { |
85 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | 116 web_contents->GetMainFrame()->AddMessageToConsole(message.level, |
86 message.level, message.text); | 117 message.text); |
87 } else { | 118 } else { |
88 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | 119 web_contents->GetMainFrame()->AddMessageToConsole( |
89 message.level, | 120 message.level, |
90 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), | 121 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), |
91 message.text.c_str())); | 122 message.text.c_str())); |
92 } | 123 } |
93 | 124 |
94 last_seen_url = message.url; | 125 last_seen_url = message.url; |
95 } | 126 } |
96 } | 127 } |
97 | 128 |
98 ClearSiteDataThrottle::ThrottleCheckResult | 129 } // namespace |
99 ClearSiteDataThrottle::WillStartRequest() { | 130 |
100 current_url_ = navigation_handle()->GetURL(); | 131 // static |
101 return PROCEED; | 132 std::unique_ptr<ResourceThrottle> |
133 ClearSiteDataThrottle::CreateThrottleForRequest(net::URLRequest* request) { | |
134 // This is an experimental feature. | |
135 if (!AreExperimentalFeaturesEnabled()) | |
136 return std::unique_ptr<ResourceThrottle>(); | |
137 | |
138 // The throttle has no purpose if the request has no ResourceRequestInfo, | |
139 // because we won't be able to determine whose data should be deleted. | |
140 if (!ResourceRequestInfo::ForRequest(request)) | |
141 return std::unique_ptr<ResourceThrottle>(); | |
142 | |
143 return base::WrapUnique(new ClearSiteDataThrottle(request)); | |
102 } | 144 } |
103 | 145 |
104 ClearSiteDataThrottle::ThrottleCheckResult | 146 ClearSiteDataThrottle::ClearSiteDataThrottle(net::URLRequest* request) |
105 ClearSiteDataThrottle::WillRedirectRequest() { | 147 : request_(request), weak_ptr_factory_(this) {} |
148 | |
149 ClearSiteDataThrottle::~ClearSiteDataThrottle() { | |
150 // Output the cached console messages. Prefix each sequence of messages | |
151 // belonging to the same URL with |kConsoleMessagePrefix|. We output console | |
152 // messages when the request is finished rather than in real time, since in | |
153 // case of navigations swapping RenderFrameHost would cause the outputs to | |
154 // disappear. | |
155 if (messages_.empty()) | |
156 return; | |
157 | |
158 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
159 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
160 base::Bind(&OutputConsoleMessagesOnUIThread, | |
161 ResourceRequestInfo::ForRequest(request_) | |
162 ->GetWebContentsGetterForRequest(), | |
163 std::move(messages_))); | |
164 } | |
165 | |
166 void ClearSiteDataThrottle::WillStartRequest(bool* defer) { | |
167 current_url_ = request_->original_url(); | |
168 *defer = false; | |
169 } | |
170 | |
171 void ClearSiteDataThrottle::WillRedirectRequest( | |
172 const net::RedirectInfo& redirect_info, | |
173 bool* defer) { | |
106 // We are processing a redirect from url1 to url2. GetResponseHeaders() | 174 // We are processing a redirect from url1 to url2. GetResponseHeaders() |
107 // contains headers from url1, but GetURL() is already equal to url2. Handle | 175 // contains headers from url1, but GetURL() is already equal to url2. Handle |
108 // the headers before updating the URL, so that |current_url_| corresponds | 176 // the headers before updating the URL, so that |current_url_| corresponds |
109 // to the URL that sent the headers. | 177 // to the URL that sent the headers. |
110 HandleHeader(); | 178 *defer = HandleHeader(); |
111 current_url_ = navigation_handle()->GetURL(); | 179 current_url_ = redirect_info.new_url; |
112 | |
113 return clearing_in_progress_ ? DEFER : PROCEED; | |
114 } | 180 } |
115 | 181 |
116 ClearSiteDataThrottle::ThrottleCheckResult | 182 void ClearSiteDataThrottle::WillProcessResponse(bool* defer) { |
117 ClearSiteDataThrottle::WillProcessResponse() { | 183 *defer = HandleHeader(); |
118 HandleHeader(); | |
119 return clearing_in_progress_ ? DEFER : PROCEED; | |
120 } | 184 } |
121 | 185 |
122 void ClearSiteDataThrottle::HandleHeader() { | 186 const char* ClearSiteDataThrottle::GetNameForLogging() const { |
123 NavigationHandleImpl* handle = | 187 return kNameForLogging; |
124 static_cast<NavigationHandleImpl*>(navigation_handle()); | 188 } |
125 const net::HttpResponseHeaders* headers = handle->GetResponseHeaders(); | |
126 | 189 |
127 if (!headers || !headers->HasHeader(kClearSiteDataHeader)) | 190 bool ClearSiteDataThrottle::HandleHeader() { |
128 return; | 191 const net::HttpResponseHeaders* headers = request_->response_headers(); |
129 | 192 |
130 // Only accept the header on secure origins. | 193 std::string header_value; |
194 if (!headers || | |
195 !headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value)) { | |
mmenke
2016/10/21 15:16:20
Should the kClearSiteDataHeader be added to kCooki
msramek
2016/10/31 19:23:35
Yes, that's a good point. This header should be ne
| |
196 return false; | |
197 } | |
198 | |
199 // Only accept the header on secure non-unique origins. | |
131 if (!IsOriginSecure(current_url_)) { | 200 if (!IsOriginSecure(current_url_)) { |
mmenke
2016/10/21 15:16:20
There's a lot of logic here which it seems like we
msramek
2016/10/31 19:23:35
Added a few tests:
ClearSiteDataThrottleTest.Inva
| |
132 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.", | 201 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.", |
133 CONSOLE_MESSAGE_LEVEL_ERROR); | 202 CONSOLE_MESSAGE_LEVEL_ERROR); |
mmenke
2016/10/21 15:16:20
Do we care about console messages enough to test t
msramek
2016/10/31 19:23:35
We don't care that much, but I tried to test them.
| |
134 return; | 203 return false; |
135 } | 204 } |
136 | 205 |
137 std::string header_value; | 206 url::Origin origin(current_url_); |
138 headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value); | 207 if (origin.unique()) { |
208 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.", | |
209 CONSOLE_MESSAGE_LEVEL_ERROR); | |
210 return false; | |
211 } | |
212 | |
213 // The LOAD_DO_NOT_SAVE_COOKIES flag prohibits the request from doing any | |
214 // modification to cookies. Clear-Site-Data applies this restriction to other | |
215 // datatypes as well. | |
216 if (request_->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES) { | |
217 ConsoleLog(&messages_, current_url_, | |
218 "The request's credentials mode prohibits modifying cookies " | |
219 "and other local data.", | |
220 CONSOLE_MESSAGE_LEVEL_ERROR); | |
221 return false; | |
222 } | |
223 | |
224 // Service workers can handle fetches of third-party resources and inject | |
225 // arbitrary headers. Ignore responses that came from a service worker, | |
226 // as supporting Clear-Site-Data would give them the power to delete data from | |
227 // any website. | |
228 const ServiceWorkerResponseInfo* response_info = | |
229 ServiceWorkerResponseInfo::ForRequest(request_); | |
230 if (response_info) { | |
231 ResourceResponseInfo extra_response_info; | |
232 response_info->GetExtraResponseInfo(&extra_response_info); | |
233 | |
234 if (extra_response_info.was_fetched_via_service_worker) { | |
235 ConsoleLog(&messages_, current_url_, | |
236 "Ignoring, as the response came from a service worker.", | |
237 CONSOLE_MESSAGE_LEVEL_ERROR); | |
238 return false; | |
239 } | |
240 } | |
139 | 241 |
140 bool clear_cookies; | 242 bool clear_cookies; |
141 bool clear_storage; | 243 bool clear_storage; |
142 bool clear_cache; | 244 bool clear_cache; |
143 | 245 |
144 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, | 246 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, |
145 &messages_)) { | 247 &messages_)) { |
146 return; | 248 return false; |
147 } | 249 } |
148 | 250 |
149 // Record the call parameters. | 251 // Record the call parameters. |
150 UMA_HISTOGRAM_ENUMERATION( | 252 UMA_HISTOGRAM_ENUMERATION( |
151 "Navigation.ClearSiteData.Parameters", | 253 "Navigation.ClearSiteData.Parameters", |
152 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3)); | 254 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3)); |
153 | 255 |
154 // If the header is valid, clear the data for this browser context and origin. | 256 // If the header is valid, clear the data for this browser context and origin. |
155 BrowserContext* browser_context = | 257 clearing_started_ = base::TimeTicks::Now(); |
156 navigation_handle()->GetWebContents()->GetBrowserContext(); | |
157 url::Origin origin(current_url_); | |
158 | 258 |
159 if (origin.unique()) { | 259 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
mmenke
2016/10/21 15:16:20
optional: May want to put this at the start of th
msramek
2016/10/31 19:23:35
I extracted everything that knows about threads to
| |
160 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.", | 260 BrowserThread::PostTask( |
161 CONSOLE_MESSAGE_LEVEL_ERROR); | 261 BrowserThread::UI, FROM_HERE, |
162 return; | 262 base::Bind(&ClearSiteDataOnUIThread, |
163 } | 263 ResourceRequestInfo::ForRequest(request_) |
264 ->GetWebContentsGetterForRequest(), | |
265 origin, clear_cookies, clear_storage, clear_cache, | |
266 base::Bind(&ClearSiteDataThrottle::TaskFinished, | |
267 weak_ptr_factory_.GetWeakPtr()))); | |
164 | 268 |
165 clearing_in_progress_ = true; | 269 return true; |
166 clearing_started_ = base::TimeTicks::Now(); | |
167 GetContentClient()->browser()->ClearSiteData( | |
168 browser_context, origin, clear_cookies, clear_storage, clear_cache, | |
169 base::Bind(&ClearSiteDataThrottle::TaskFinished, | |
170 weak_ptr_factory_.GetWeakPtr())); | |
171 } | 270 } |
172 | 271 |
173 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, | 272 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, |
174 bool* clear_cookies, | 273 bool* clear_cookies, |
175 bool* clear_storage, | 274 bool* clear_storage, |
176 bool* clear_cache, | 275 bool* clear_cache, |
177 std::vector<ConsoleMessage>* messages) { | 276 std::vector<ConsoleMessage>* messages) { |
178 if (!base::IsStringASCII(header)) { | 277 if (!base::IsStringASCII(header)) { |
179 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.", | 278 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.", |
mmenke
2016/10/21 15:16:20
I think it's cleaner to make more sense to make th
msramek
2016/10/31 19:23:35
Done. I actually had that in mind when writing thi
| |
180 CONSOLE_MESSAGE_LEVEL_ERROR); | 279 CONSOLE_MESSAGE_LEVEL_ERROR); |
181 return false; | 280 return false; |
182 } | 281 } |
183 | 282 |
184 std::unique_ptr<base::Value> parsed_header = base::JSONReader::Read(header); | 283 std::unique_ptr<base::Value> parsed_header = base::JSONReader::Read(header); |
185 | 284 |
186 if (!parsed_header) { | 285 if (!parsed_header) { |
187 ConsoleLog(messages, current_url_, "Not a valid JSON.", | 286 ConsoleLog(messages, current_url_, "Not a valid JSON.", |
188 CONSOLE_MESSAGE_LEVEL_ERROR); | 287 CONSOLE_MESSAGE_LEVEL_ERROR); |
189 return false; | 288 return false; |
(...skipping 27 matching lines...) Expand all Loading... | |
217 } else if (type == "storage") { | 316 } else if (type == "storage") { |
218 datatype = clear_storage; | 317 datatype = clear_storage; |
219 } else if (type == "cache") { | 318 } else if (type == "cache") { |
220 datatype = clear_cache; | 319 datatype = clear_cache; |
221 } else { | 320 } else { |
222 std::string serialized_type; | 321 std::string serialized_type; |
223 JSONStringValueSerializer serializer(&serialized_type); | 322 JSONStringValueSerializer serializer(&serialized_type); |
224 serializer.Serialize(*value); | 323 serializer.Serialize(*value); |
225 ConsoleLog( | 324 ConsoleLog( |
226 messages, current_url_, | 325 messages, current_url_, |
227 base::StringPrintf("Invalid type: %s.", serialized_type.c_str()), | 326 base::StringPrintf("Invalid type: %s.", serialized_type.c_str()), |
mmenke
2016/10/21 15:16:20
Not really related to this CL, but while I'm readi
msramek
2016/10/31 19:23:35
Done. Here and below.
Makes sense. This is in fac
| |
228 CONSOLE_MESSAGE_LEVEL_ERROR); | 327 CONSOLE_MESSAGE_LEVEL_ERROR); |
229 continue; | 328 continue; |
230 } | 329 } |
231 | 330 |
232 // Each data type should only be processed once. | 331 // Each data type should only be processed once. |
233 DCHECK(datatype); | 332 DCHECK(datatype); |
234 if (*datatype) | 333 if (*datatype) |
235 continue; | 334 continue; |
236 | 335 |
237 *datatype = true; | 336 *datatype = true; |
(...skipping 23 matching lines...) Expand all Loading... | |
261 break; | 360 break; |
262 default: | 361 default: |
263 NOTREACHED(); | 362 NOTREACHED(); |
264 } | 363 } |
265 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG); | 364 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG); |
266 | 365 |
267 return true; | 366 return true; |
268 } | 367 } |
269 | 368 |
270 void ClearSiteDataThrottle::TaskFinished() { | 369 void ClearSiteDataThrottle::TaskFinished() { |
271 DCHECK(clearing_in_progress_); | 370 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
272 clearing_in_progress_ = false; | |
273 | 371 |
274 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration", | 372 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration", |
275 base::TimeTicks::Now() - clearing_started_, | 373 base::TimeTicks::Now() - clearing_started_, |
276 base::TimeDelta::FromMilliseconds(1), | 374 base::TimeDelta::FromMilliseconds(1), |
277 base::TimeDelta::FromSeconds(1), 50); | 375 base::TimeDelta::FromSeconds(1), 50); |
278 | 376 |
279 navigation_handle()->Resume(); | 377 controller()->Resume(); |
280 } | 378 } |
281 | 379 |
282 } // namespace content | 380 } // namespace content |
OLD | NEW |