blob: 47067d774c0490e3f0daf1f0d7ee60e4f51fc110 [file] [log] [blame]
[email protected]144a8102012-01-14 01:05:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]b6583592012-01-25 19:52:335#include "content/browser/site_instance_impl.h"
initial.commit09911bf2008-07-26 23:55:296
Lukasz Anforowicz48097c42017-12-15 00:23:387#include <string>
8
creis0b49fa12017-07-10 16:31:119#include "base/command_line.h"
Alex Moshchuk4479b97c2017-10-17 23:20:3210#include "base/debug/crash_logging.h"
Aaron Colwell6fee3f52019-05-08 21:35:0211#include "base/lazy_instance.h"
alexmos3b9ad102017-05-26 23:41:0812#include "base/macros.h"
[email protected]39365212011-02-24 01:01:0013#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:5914#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5315#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0716#include "content/browser/frame_host/frame_tree_node.h"
Alex Moshchuk07e1bb42019-03-08 04:44:0817#include "content/browser/isolated_origin_util.h"
Alex Moshchuk8e5c1952019-01-15 03:39:5018#include "content/browser/isolation_context.h"
[email protected]f3b1a082011-11-18 00:34:3019#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3420#include "content/browser/storage_partition_impl.h"
Aaron Colwellea6921f2019-01-29 16:50:3921#include "content/public/browser/browser_or_resource_context.h"
[email protected]87f3c082011-10-19 18:07:4422#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3023#include "content/public/browser/render_process_host_factory.h"
Nick Carterbf6264a52018-04-06 02:39:3324#include "content/public/browser/site_isolation_policy.h"
[email protected]41fb79a52012-06-29 16:34:3325#include "content/public/browser/web_ui_controller_factory.h"
Nate Chapin71da03c2019-02-05 01:21:4126#include "content/public/common/content_features.h"
creis0b49fa12017-07-10 16:31:1127#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0328#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4329#include "content/public/common/url_utils.h"
[email protected]be28b5f42012-07-20 11:31:2530#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2931
[email protected]46488322012-10-30 03:22:2032namespace content {
[email protected]b6583592012-01-25 19:52:3333
Aaron Colwellf45e3a852019-06-06 00:48:2734namespace {
35
36// Returns true if CreateForURL() and related functions should be allowed to
37// return a default SiteInstance.
38bool ShouldAllowDefaultSiteInstance() {
Aaron Colwellc2ab84e2019-07-24 01:31:0039 return base::FeatureList::IsEnabled(
40 features::kProcessSharingWithDefaultSiteInstances);
Aaron Colwellf45e3a852019-06-06 00:48:2741}
42
43} // namespace
44
avib7348942015-12-25 20:57:1045int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1546
Aaron Colwell6fee3f52019-05-08 21:35:0247// static
48const GURL& SiteInstanceImpl::GetDefaultSiteURL() {
49 struct DefaultSiteURL {
50 const GURL url = GURL("http://unisolated.invalid");
51 };
52 static base::LazyInstance<DefaultSiteURL>::Leaky default_site_url =
53 LAZY_INSTANCE_INITIALIZER;
54
55 return default_site_url.Get().url;
56}
57
[email protected]b6583592012-01-25 19:52:3358SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1559 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2260 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1561 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4462 process_(nullptr),
Lukasz Anforowicz3caa8372018-06-05 17:22:0763 can_associate_with_spare_process_(true),
nickd5bbd0b2016-03-31 19:52:4464 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4165 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
66 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1367 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1368}
69
[email protected]b6583592012-01-25 19:52:3370SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2071 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5572
Charlie Reiscf6aa512019-04-16 17:27:2973 if (process_) {
[email protected]40bfaf32013-11-19 01:35:4374 process_->RemoveObserver(this);
75
Charlie Reiscf6aa512019-04-16 17:27:2976 // Ensure the RenderProcessHost gets deleted if this SiteInstance created a
77 // process which was never used by any listeners.
78 process_->Cleanup();
79 }
80
initial.commit09911bf2008-07-26 23:55:2981 // Now that no one is referencing us, we can safely remove ourselves from
82 // the BrowsingInstance. Any future visits to a page from this site
83 // (within the same BrowsingInstance) can safely create a new SiteInstance.
84 if (has_site_)
dchengbccd6b82016-03-30 16:24:1985 browsing_instance_->UnregisterSiteInstance(this);
86}
87
Nasko Oskovdeb6c7ea2017-11-30 18:18:1188// static
dchengbccd6b82016-03-30 16:24:1989scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
90 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:1091 DCHECK(browser_context);
kylecharda69d882017-10-04 05:49:5292 return base::WrapRefCounted(
dchengbccd6b82016-03-30 16:24:1993 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
94}
95
Nasko Oskovdeb6c7ea2017-11-30 18:18:1196// static
dchengbccd6b82016-03-30 16:24:1997scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
98 BrowserContext* browser_context,
99 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10100 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19101 // This will create a new SiteInstance and BrowsingInstance.
102 scoped_refptr<BrowsingInstance> instance(
103 new BrowsingInstance(browser_context));
Aaron Colwellf45e3a852019-06-06 00:48:27104 return instance->GetSiteInstanceForURL(url, ShouldAllowDefaultSiteInstance());
initial.commit09911bf2008-07-26 23:55:29105}
106
Nasko Oskovdeb6c7ea2017-11-30 18:18:11107// static
Aaron Colwellecc72fb2019-04-30 17:41:11108scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForServiceWorker(
109 BrowserContext* browser_context,
Aaron Colwell369968ee2019-05-14 00:32:38110 const GURL& url,
111 bool can_reuse_process) {
Aaron Colwellecc72fb2019-04-30 17:41:11112 // This will create a new SiteInstance and BrowsingInstance.
113 scoped_refptr<BrowsingInstance> instance(
114 new BrowsingInstance(browser_context));
115
116 // We do NOT want to allow the default site instance here because workers
117 // need to be kept separate from other sites.
118 scoped_refptr<SiteInstanceImpl> site_instance =
119 instance->GetSiteInstanceForURL(url, /* allow_default_instance */ false);
120 site_instance->is_for_service_worker_ = true;
Aaron Colwell369968ee2019-05-14 00:32:38121
122 // Attempt to reuse a renderer process if possible. Note that in the
123 // <webview> case, process reuse isn't currently supported and a new
124 // process will always be created (https://crbug.com/752667).
125 DCHECK(site_instance->process_reuse_policy() ==
126 SiteInstanceImpl::ProcessReusePolicy::DEFAULT ||
127 site_instance->process_reuse_policy() ==
128 SiteInstanceImpl::ProcessReusePolicy::PROCESS_PER_SITE);
129 if (can_reuse_process) {
130 site_instance->set_process_reuse_policy(
131 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE);
132 }
133 return site_instance;
134}
135
136// static
137scoped_refptr<SiteInstanceImpl>
138SiteInstanceImpl::CreateReusableInstanceForTesting(
139 BrowserContext* browser_context,
140 const GURL& url) {
141 DCHECK(browser_context);
142 // This will create a new SiteInstance and BrowsingInstance.
143 scoped_refptr<BrowsingInstance> instance(
144 new BrowsingInstance(browser_context));
145 auto site_instance =
146 instance->GetSiteInstanceForURL(url,
147 /* allow_default_instance */ false);
148 site_instance->set_process_reuse_policy(
149 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE);
Aaron Colwellecc72fb2019-04-30 17:41:11150 return site_instance;
151}
152
153// static
Nasko Oskovdeb6c7ea2017-11-30 18:18:11154bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) {
155 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
156 // still be used for a normal web site.
Mohamed Abdelhalim9e868ca42019-04-25 16:53:29157 if (url.IsAboutBlank())
Nasko Oskovdeb6c7ea2017-11-30 18:18:11158 return false;
159
160 // The embedder will then have the opportunity to determine if the URL
161 // should "use up" the SiteInstance.
162 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
163}
164
Nicolas Pena7c7847f2018-05-30 01:36:05165// static
166bool SiteInstanceImpl::IsOriginLockASite(const GURL& lock_url) {
167 return lock_url.has_scheme() && lock_url.has_host();
168}
169
avib7348942015-12-25 20:57:10170int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:33171 return id_;
172}
173
Chris Hamilton2ed5d5572019-05-23 22:47:58174int32_t SiteInstanceImpl::GetBrowsingInstanceId() {
175 // This is being vended out as an opaque ID, and it is always defined for
176 // a BrowsingInstance affiliated IsolationContext, so it's safe to call
177 // "GetUnsafeValue" and expose the inner value directly.
178 return browsing_instance_->isolation_context()
179 .browsing_instance_id()
180 .GetUnsafeValue();
181}
182
Alex Moshchuk8e5c1952019-01-15 03:39:50183const IsolationContext& SiteInstanceImpl::GetIsolationContext() {
184 return browsing_instance_->isolation_context();
185}
186
Nate Chapin71da03c2019-02-05 01:21:41187RenderProcessHost* SiteInstanceImpl::GetDefaultProcessIfUsable() {
188 if (!base::FeatureList::IsEnabled(
189 features::kProcessSharingWithStrictSiteInstances)) {
190 return nullptr;
191 }
192 if (RequiresDedicatedProcess())
193 return nullptr;
194 return browsing_instance_->default_process();
195}
196
Aaron Colwell0447cd02019-06-26 22:04:20197bool SiteInstanceImpl::IsDefaultSiteInstance() const {
Aaron Colwellddeccbdb2019-03-08 01:11:03198 return browsing_instance_->IsDefaultSiteInstance(this);
199}
200
Aaron Colwell0447cd02019-06-26 22:04:20201bool SiteInstanceImpl::IsSiteInDefaultSiteInstance(const GURL& site_url) const {
202 return browsing_instance_->IsSiteInDefaultSiteInstance(site_url);
203}
204
Nate Chapin71da03c2019-02-05 01:21:41205void SiteInstanceImpl::MaybeSetBrowsingInstanceDefaultProcess() {
206 if (!base::FeatureList::IsEnabled(
207 features::kProcessSharingWithStrictSiteInstances)) {
208 return;
209 }
210 // Wait until this SiteInstance both has a site and a process
211 // assigned, so that we can be sure that RequiresDedicatedProcess()
212 // is accurate and we actually have a process to set.
213 if (!process_ || !has_site_ || RequiresDedicatedProcess())
214 return;
215 if (browsing_instance_->default_process()) {
216 DCHECK_EQ(process_, browsing_instance_->default_process());
217 return;
218 }
219 browsing_instance_->SetDefaultProcess(process_);
220}
221
Alex Moshchuk8e5c1952019-01-15 03:39:50222// static
223BrowsingInstanceId SiteInstanceImpl::NextBrowsingInstanceId() {
224 return BrowsingInstance::NextBrowsingInstanceId();
225}
226
227bool SiteInstanceImpl::HasProcess() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28228 if (process_ != nullptr)
[email protected]41fb79a52012-06-29 16:34:33229 return true;
230
231 // If we would use process-per-site for this site, also check if there is an
232 // existing process that we would use if GetProcess() were called.
Alex Moshchukf7488792019-03-11 22:37:57233 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
[email protected]41fb79a52012-06-29 16:34:33234 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:56235 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
Alex Moshchuk8e5c1952019-01-15 03:39:50236 RenderProcessHostImpl::GetSoleProcessHostForSite(
237 browser_context, GetIsolationContext(), site_, lock_url_)) {
[email protected]41fb79a52012-06-29 16:34:33238 return true;
239 }
240
241 return false;
[email protected]5ab79b02010-04-26 16:47:11242}
243
[email protected]41fb79a52012-06-29 16:34:33244RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:48245 // TODO(erikkay) It would be nice to ensure that the renderer type had been
246 // properly set before we get here. The default tab creation case winds up
247 // with no site set at this point, so it will default to TYPE_NORMAL. This
248 // may not be correct, so we'll wind up potentially creating a process that
249 // we then throw away, or worse sharing a process with the wrong process type.
250 // See crbug.com/43448.
251
initial.commit09911bf2008-07-26 23:55:29252 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:13253 if (!process_) {
Alex Moshchukf7488792019-03-11 22:37:57254 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
[email protected]41fb79a52012-06-29 16:34:33255
clamy63960c32017-05-10 22:57:07256 // Check if the ProcessReusePolicy should be updated.
257 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42258 has_site_ &&
259 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07260 if (should_use_process_per_site) {
261 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
262 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
263 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33264 }
265
Alex Moshchuk5f926a52018-08-29 20:57:30266 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(this);
avi6d686db2017-01-06 02:28:57267
[email protected]41fb79a52012-06-29 16:34:33268 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43269 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33270
Nate Chapin71da03c2019-02-05 01:21:41271 MaybeSetBrowsingInstanceDefaultProcess();
272
[email protected]41fb79a52012-06-29 16:34:33273 // If we are using process-per-site, we need to register this process
274 // for the current site so that we can find it again. (If no site is set
275 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07276 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
277 has_site_) {
Alex Moshchuk5f926a52018-08-29 20:57:30278 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
279 process_, this);
[email protected]41fb79a52012-06-29 16:34:33280 }
initial.commit09911bf2008-07-26 23:55:29281
naskob8744d22014-08-28 17:07:43282 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
283 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20284 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46285
[email protected]313b80bd2011-11-23 03:49:10286 if (has_site_)
alexmos13fe1962017-06-28 04:25:12287 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29288 }
[email protected]4566f132009-03-12 01:55:13289 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29290
[email protected]4566f132009-03-12 01:55:13291 return process_;
initial.commit09911bf2008-07-26 23:55:29292}
293
Lukasz Anforowicz3caa8372018-06-05 17:22:07294bool SiteInstanceImpl::CanAssociateWithSpareProcess() {
295 return can_associate_with_spare_process_;
296}
297
298void SiteInstanceImpl::PreventAssociationWithSpareProcess() {
299 can_associate_with_spare_process_ = false;
300}
301
[email protected]b6583592012-01-25 19:52:33302void SiteInstanceImpl::SetSite(const GURL& url) {
Charlie Reisc2099752019-04-06 01:21:12303 // TODO(creis): Consider calling ShouldAssignSiteForURL internally, rather
304 // than before multiple call sites. See https://crbug.com/949220.
naskob8744d22014-08-28 17:07:43305 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
306 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29307 // A SiteInstance's site should not change.
308 // TODO(creis): When following links or script navigations, we can currently
309 // render pages from other sites in this SiteInstance. This will eventually
310 // be fixed, but until then, we should still not set the site of a
311 // SiteInstance more than once.
312 DCHECK(!has_site_);
313
314 // Remember that this SiteInstance has been used to load a URL, even if the
315 // URL is invalid.
316 has_site_ = true;
Alex Moshchukf7488792019-03-11 22:37:57317 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
Alex Moshchuk25c64bb2017-12-02 02:50:11318 original_url_ = url;
Aaron Colwellddeccbdb2019-03-08 01:11:03319 browsing_instance_->GetSiteAndLockForURL(
320 url, /* allow_default_instance */ false, &site_, &lock_url_);
initial.commit09911bf2008-07-26 23:55:29321
322 // Now that we have a site, register it with the BrowsingInstance. This
323 // ensures that we won't create another SiteInstance for this site within
324 // the same BrowsingInstance, because all same-site pages within a
325 // BrowsingInstance can script each other.
326 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10327
clamy63960c32017-05-10 22:57:07328 // Update the process reuse policy based on the site.
329 bool should_use_process_per_site =
330 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
331 if (should_use_process_per_site) {
332 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
333 }
334
[email protected]41fb79a52012-06-29 16:34:33335 if (process_) {
alexmos13fe1962017-06-28 04:25:12336 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33337
338 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07339 if (should_use_process_per_site) {
Alex Moshchuk5f926a52018-08-29 20:57:30340 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
341 process_, this);
[email protected]41fb79a52012-06-29 16:34:33342 }
Nate Chapin71da03c2019-02-05 01:21:41343 MaybeSetBrowsingInstanceDefaultProcess();
[email protected]41fb79a52012-06-29 16:34:33344 }
initial.commit09911bf2008-07-26 23:55:29345}
346
Aaron Colwelle953e562019-07-24 16:47:36347void SiteInstanceImpl::ConvertToDefaultOrSetSite(const GURL& url) {
348 DCHECK(!has_site_);
349
350 if (ShouldAllowDefaultSiteInstance() &&
351 browsing_instance_->TrySettingDefaultSiteInstance(this, url)) {
352 return;
353 }
354
355 SetSite(url);
356}
357
Lucas Furukawa Gadanid726e1e2019-05-08 16:20:03358const GURL& SiteInstanceImpl::GetSiteURL() {
[email protected]b6583592012-01-25 19:52:33359 return site_;
360}
361
362bool SiteInstanceImpl::HasSite() const {
363 return has_site_;
364}
365
366bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29367 return browsing_instance_->HasSiteInstance(url);
368}
369
dchengbccd6b82016-03-30 16:24:19370scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
371 const GURL& url) {
Aaron Colwellddeccbdb2019-03-08 01:11:03372 return browsing_instance_->GetSiteInstanceForURL(
373 url, /* allow_default_instance */ true);
initial.commit09911bf2008-07-26 23:55:29374}
375
[email protected]14392a52012-05-02 20:28:44376bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46377 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
378 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44379}
380
[email protected]d5072a82014-05-15 05:50:18381size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
382 return browsing_instance_->active_contents_count();
383}
384
[email protected]f88628d02012-11-11 17:58:59385bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
Clark DuVall4e1f0a72019-08-07 22:41:42386 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]144a8102012-01-14 01:05:31387 // If the URL to navigate to can be associated with any site instance,
388 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53389 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31390 return false;
391
Nasko Oskov978c16b2018-08-03 22:17:06392 // Any process can host an about:blank URL, except the one used for error
393 // pages, which should not commit successful navigations. This check avoids a
394 // process transfer for browser-initiated navigations to about:blank in a
395 // dedicated process; without it, IsSuitableHost would consider this process
396 // unsuitable for about:blank when it compares origin locks.
397 // Renderer-initiated navigations will handle about:blank navigations
398 // elsewhere and leave them in the source SiteInstance, along with
399 // about:srcdoc and data:.
400 if (url.IsAboutBlank() && site_ != GURL(kUnreachableWebDataURL))
alexmos13fe1962017-06-28 04:25:12401 return false;
402
[email protected]88aae972011-12-16 01:14:18403 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11404 // process is not (or vice versa), make sure we notice and fix it.
Aaron Colwellddeccbdb2019-03-08 01:11:03405 GURL site_url;
406 GURL origin_lock;
407
408 // Note: This call must return information that is identical to what
409 // would be reported in the SiteInstance returned by
410 // GetRelatedSiteInstance(url).
411 browsing_instance_->GetSiteAndLockForURL(
412 url, /* allow_default_instance */ true, &site_url, &origin_lock);
Charlie Reisc2099752019-04-06 01:21:12413
Aaron Colwell7292aec2019-07-25 17:23:43414 // If this is a default SiteInstance and the BrowsingInstance gives us a
415 // non-default site URL even when we explicitly allow the default SiteInstance
416 // to be considered, then |url| does not belong in the same process as this
417 // SiteInstance. This can happen when the
418 // kProcessSharingWithDefaultSiteInstances feature is not enabled and the
419 // site URL is explicitly set on a SiteInstance for a URL that would normally
420 // be directed to the default SiteInstance (e.g. a site not requiring a
421 // dedicated process). This situation typically happens when the top-level
422 // frame is a site that should be in the default SiteInstance and the
423 // SiteInstance associated with that frame is initially a SiteInstance with
424 // no site URL set.
425 if (IsDefaultSiteInstance() && site_url != GetSiteURL())
426 return true;
427
Charlie Reisc2099752019-04-06 01:21:12428 // Note that HasProcess() may return true if process_ is null, in
429 // process-per-site cases where there's an existing process available.
430 // We want to use such a process in the IsSuitableHost check, so we
431 // may end up assigning process_ in the GetProcess() call below.
432 if (!HasProcess()) {
433 // If there is no process or site, then this is a new SiteInstance that can
434 // be used for anything.
435 if (!HasSite())
436 return false;
437
438 // If there is no process but there is a site, then the process must have
439 // been discarded after we navigated away. If the site URLs match, then it
440 // is safe to use this SiteInstance.
441 if (GetSiteURL() == site_url)
442 return false;
443
444 // If the site URLs do not match, but neither this SiteInstance nor the
445 // destination site_url require dedicated processes, then it is safe to use
446 // this SiteInstance.
447 if (!RequiresDedicatedProcess() &&
Aaron Colwell5080de62019-05-06 23:02:03448 !DoesSiteURLRequireDedicatedProcess(GetIsolationContext(), site_url)) {
Charlie Reisc2099752019-04-06 01:21:12449 return false;
450 }
451
452 // Otherwise, there's no process, the site URLs don't match, and at least
453 // one of them requires a dedicated process, so it is not safe to use this
454 // SiteInstance.
455 return true;
456 }
457
[email protected]88aae972011-12-16 01:14:18458 return !RenderProcessHostImpl::IsSuitableHost(
Alex Moshchukf7488792019-03-11 22:37:57459 GetProcess(), browsing_instance_->GetBrowserContext(),
Alex Moshchuk8e5c1952019-01-15 03:39:50460 GetIsolationContext(), site_url, origin_lock);
[email protected]d292d8a2011-05-25 03:47:11461}
462
nickd30fd962015-07-27 21:51:08463bool SiteInstanceImpl::RequiresDedicatedProcess() {
Clark DuVall4e1f0a72019-08-07 22:41:42464 DCHECK_CURRENTLY_ON(BrowserThread::UI);
nickd30fd962015-07-27 21:51:08465 if (!has_site_)
466 return false;
nickd5bbd0b2016-03-31 19:52:44467
Aaron Colwell5080de62019-05-06 23:02:03468 return DoesSiteURLRequireDedicatedProcess(GetIsolationContext(), site_);
nickd30fd962015-07-27 21:51:08469}
470
nick9f34e2892016-01-12 21:01:07471void SiteInstanceImpl::IncrementActiveFrameCount() {
472 active_frame_count_++;
473}
474
475void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31476 if (--active_frame_count_ == 0) {
477 for (auto& observer : observers_)
478 observer.ActiveFrameCountIsZero(this);
479 }
nick9f34e2892016-01-12 21:01:07480}
481
[email protected]d5072a82014-05-15 05:50:18482void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
483 browsing_instance_->increment_active_contents_count();
484}
485
486void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
487 browsing_instance_->decrement_active_contents_count();
488}
489
nick9f34e2892016-01-12 21:01:07490void SiteInstanceImpl::AddObserver(Observer* observer) {
491 observers_.AddObserver(observer);
492}
493
494void SiteInstanceImpl::RemoveObserver(Observer* observer) {
495 observers_.RemoveObserver(observer);
496}
497
Lucas Furukawa Gadanid726e1e2019-05-08 16:20:03498BrowserContext* SiteInstanceImpl::GetBrowserContext() {
Alex Moshchukf7488792019-03-11 22:37:57499 return browsing_instance_->GetBrowserContext();
[email protected]72daaa92012-01-18 13:39:02500}
501
nickcc0d9142015-10-14 16:27:10502// static
dchengbccd6b82016-03-30 16:24:19503scoped_refptr<SiteInstance> SiteInstance::Create(
504 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:10505 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19506 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29507}
508
nickcc0d9142015-10-14 16:27:10509// static
dchengbccd6b82016-03-30 16:24:19510scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
511 BrowserContext* browser_context,
512 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10513 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19514 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16515}
516
nickcc0d9142015-10-14 16:27:10517// static
Nasko Oskovdeb6c7ea2017-11-30 18:18:11518bool SiteInstance::ShouldAssignSiteForURL(const GURL& url) {
519 return SiteInstanceImpl::ShouldAssignSiteForURL(url);
520}
521
Alex Moshchuk78cf66bda2018-11-30 01:49:30522bool SiteInstanceImpl::IsSameSiteWithURL(const GURL& url) {
Aaron Colwell6fee3f52019-05-08 21:35:02523 if (IsDefaultSiteInstance()) {
Aaron Colwelle953e562019-07-24 16:47:36524 // about:blank URLs should always be considered same site just like they are
525 // in IsSameWebSite().
526 if (url.IsAboutBlank())
527 return true;
528
Aaron Colwell6fee3f52019-05-08 21:35:02529 // Consider |url| the same site if it could be handled by the
530 // default SiteInstance and we don't already have a SiteInstance for
531 // this URL.
532 // TODO(acolwell): Remove HasSiteInstance() call once we have a way to
533 // prevent SiteInstances with no site URL from being used for URLs
534 // that should be routed to the default SiteInstance.
535 DCHECK_EQ(site_, GetDefaultSiteURL());
536 return site_ == GetSiteForURLInternal(GetIsolationContext(), url,
537 true /* should_use_effective_urls */,
538 true /* allow_default_site_url */) &&
539 !browsing_instance_->HasSiteInstance(url);
540 }
541
Alex Moshchuk78cf66bda2018-11-30 01:49:30542 return SiteInstanceImpl::IsSameWebSite(
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02543 GetIsolationContext(), site_, url,
544 true /* should_compare_effective_urls */);
Alex Moshchuk25c64bb2017-12-02 02:50:11545}
546
Aaron Colwell6fee3f52019-05-08 21:35:02547bool SiteInstanceImpl::IsOriginalUrlSameSite(
548 const GURL& dest_url,
549 bool should_compare_effective_urls) {
550 if (IsDefaultSiteInstance())
551 return IsSameSiteWithURL(dest_url);
552
553 return IsSameWebSite(GetIsolationContext(), original_url_, dest_url,
554 should_compare_effective_urls);
555}
556
Alex Moshchuk78cf66bda2018-11-30 01:49:30557// static
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02558bool SiteInstanceImpl::IsSameWebSite(const IsolationContext& isolation_context,
Alex Moshchuk25c64bb2017-12-02 02:50:11559 const GURL& real_src_url,
560 const GURL& real_dest_url,
561 bool should_compare_effective_urls) {
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02562 DCHECK_CURRENTLY_ON(BrowserThread::UI);
563 BrowserContext* browser_context =
564 isolation_context.browser_or_resource_context().ToBrowserContext();
Lukasz Anforowicz4726a172018-10-15 21:25:10565 DCHECK(browser_context);
Aaron Colwell6fee3f52019-05-08 21:35:02566 DCHECK_NE(real_src_url, GetDefaultSiteURL());
Lukasz Anforowicz4726a172018-10-15 21:25:10567
Alex Moshchuk25c64bb2017-12-02 02:50:11568 GURL src_url =
569 should_compare_effective_urls
570 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_src_url)
571 : real_src_url;
572 GURL dest_url =
573 should_compare_effective_urls
574 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_dest_url)
575 : real_dest_url;
[email protected]399583b2012-12-11 09:33:42576
577 // We infer web site boundaries based on the registered domain name of the
578 // top-level page and the scheme. We do not pay attention to the port if
579 // one is present, because pages served from different ports can still
580 // access each other if they change their document.domain variable.
581
582 // Some special URLs will match the site instance of any other URL. This is
583 // done before checking both of them for validity, since we want these URLs
584 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17585 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42586 return true;
587
588 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17589 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42590 return false;
591
[email protected]855d7d572014-08-02 11:18:17592 // If the destination url is just a blank page, we treat them as part of the
593 // same site.
Mohamed Abdelhalim9e868ca42019-04-25 16:53:29594 if (dest_url.IsAboutBlank())
[email protected]855d7d572014-08-02 11:18:17595 return true;
596
Alex Moshchuka308c9b2018-02-08 20:58:14597 // If the source and destination URLs are equal excluding the hash, they have
598 // the same site. This matters for file URLs, where SameDomainOrHost() would
599 // otherwise return false below.
600 if (src_url.EqualsIgnoringRef(dest_url))
601 return true;
602
Daniel Cheng88186bd52017-10-20 08:14:46603 url::Origin src_origin = url::Origin::Create(src_url);
604 url::Origin dest_origin = url::Origin::Create(dest_url);
alexmos3b9ad102017-05-26 23:41:08605
[email protected]399583b2012-12-11 09:33:42606 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13607 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42608 return false;
609
W. James MacLeanf79c97e2019-05-02 20:35:46610 if (SiteIsolationPolicy::IsStrictOriginIsolationEnabled())
611 return src_origin == dest_origin;
612
alexmos4bc26322017-07-01 00:57:14613 if (!net::registry_controlled_domains::SameDomainOrHost(
614 src_origin, dest_origin,
615 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
616 return false;
617 }
618
619 // If the sites are the same, check isolated origins. If either URL matches
Alex Moshchuk4e19b362018-09-10 21:14:36620 // an isolated origin, compare origins rather than sites. As an optimization
621 // to avoid unneeded isolated origin lookups, shortcut this check if the two
622 // origins are the same.
623 if (src_origin == dest_origin)
624 return true;
alexmos4bc26322017-07-01 00:57:14625 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
626 url::Origin src_isolated_origin;
627 url::Origin dest_isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50628 bool src_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
629 isolation_context, src_origin, &src_isolated_origin);
630 bool dest_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
631 isolation_context, dest_origin, &dest_isolated_origin);
alexmos4bc26322017-07-01 00:57:14632 if (src_origin_is_isolated || dest_origin_is_isolated) {
633 // Compare most specific matching origins to ensure that a subdomain of an
634 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
635 // the isolated origin's site URL (e.g., https://isolated.foo.com).
636 return src_isolated_origin == dest_isolated_origin;
637 }
638
639 return true;
[email protected]399583b2012-12-11 09:33:42640}
641
Aaron Colwellf45e3a852019-06-06 00:48:27642bool SiteInstanceImpl::DoesSiteForURLMatch(const GURL& url) {
643 // Note: The |allow_default_site_url| value used here MUST match the value
644 // used in CreateForURL().
645 return site_ == GetSiteForURLInternal(GetIsolationContext(), url,
646 true /* should_use_effective_urls */,
647 ShouldAllowDefaultSiteInstance());
648}
649
nickcc0d9142015-10-14 16:27:10650// static
[email protected]399583b2012-12-11 09:33:42651GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
Alex Moshchuk5f926a52018-08-29 20:57:30652 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39653 DCHECK_CURRENTLY_ON(BrowserThread::UI);
654 DCHECK(browser_context);
655
Aaron Colwell3acea722019-04-24 21:54:43656 // By default, GetSiteForURL will resolve |url| to an effective URL
657 // before computing its site.
Alex Moshchuk8e5c1952019-01-15 03:39:50658 //
659 // TODO(alexmos): Callers inside content/ should already be using the
660 // internal SiteInstanceImpl version and providing a proper IsolationContext.
661 // For callers outside content/, plumb the applicable IsolationContext here,
662 // where needed. Eventually, GetSiteForURL should always require an
663 // IsolationContext to be passed in, and this implementation should just
664 // become SiteInstanceImpl::GetSiteForURL.
Aaron Colwell3acea722019-04-24 21:54:43665 return SiteInstanceImpl::GetSiteForURL(IsolationContext(browser_context),
666 url);
Alex Moshchuk5f926a52018-08-29 20:57:30667}
668
669// static
Alex Moshchuk8e5c1952019-01-15 03:39:50670GURL SiteInstanceImpl::DetermineProcessLockURL(
Alex Moshchuk8e5c1952019-01-15 03:39:50671 const IsolationContext& isolation_context,
672 const GURL& url) {
Alex Moshchuk5f926a52018-08-29 20:57:30673 // For the process lock URL, convert |url| to a site without resolving |url|
674 // to an effective URL.
Aaron Colwell3acea722019-04-24 21:54:43675 return SiteInstanceImpl::GetSiteForURLInternal(
Aaron Colwell6fee3f52019-05-08 21:35:02676 isolation_context, url, false /* should_use_effective_urls */,
677 false /* allow_default_site_url */);
Alex Moshchuk5f926a52018-08-29 20:57:30678}
679
Aaron Colwellea6921f2019-01-29 16:50:39680// static
Alex Moshchukf7488792019-03-11 22:37:57681GURL SiteInstanceImpl::GetSiteForURL(const IsolationContext& isolation_context,
Aaron Colwell3acea722019-04-24 21:54:43682 const GURL& real_url) {
683 return GetSiteForURLInternal(isolation_context, real_url,
Aaron Colwell6fee3f52019-05-08 21:35:02684 true /* should_use_effective_urls */,
685 false /* allow_default_site_url */);
Aaron Colwell3acea722019-04-24 21:54:43686}
687
688// static
689GURL SiteInstanceImpl::GetSiteForURLInternal(
690 const IsolationContext& isolation_context,
691 const GURL& real_url,
Aaron Colwell6fee3f52019-05-08 21:35:02692 bool should_use_effective_urls,
693 bool allow_default_site_url) {
[email protected]25a9e8e532012-06-22 06:16:28694 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37695 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28696 return real_url;
697
Aaron Colwellea6921f2019-01-29 16:50:39698 if (should_use_effective_urls)
699 DCHECK_CURRENTLY_ON(BrowserThread::UI);
700
Alex Moshchuk5f926a52018-08-29 20:57:30701 GURL url = should_use_effective_urls
Alex Moshchukf7488792019-03-11 22:37:57702 ? SiteInstanceImpl::GetEffectiveURL(
703 isolation_context.browser_or_resource_context()
704 .ToBrowserContext(),
705 real_url)
Alex Moshchuk5f926a52018-08-29 20:57:30706 : real_url;
Daniel Cheng88186bd52017-10-20 08:14:46707 url::Origin origin = url::Origin::Create(url);
[email protected]3a8eecb2010-04-22 23:56:30708
Lukasz Anforowicz48097c42017-12-15 00:23:38709 // If the url has a host, then determine the site. Skip file URLs to avoid a
710 // situation where site URL of file://localhost/ would mismatch Blink's origin
711 // (which ignores the hostname in this case - see https://crbug.com/776160).
Aaron Colwell6fee3f52019-05-08 21:35:02712 GURL site_url;
Lukasz Anforowicz48097c42017-12-15 00:23:38713 if (!origin.host().empty() && origin.scheme() != url::kFileScheme) {
Alex Moshchuk12ce6c792019-05-10 00:32:09714 // For Strict Origin Isolation, use the full origin instead of site for all
715 // HTTP/HTTPS URLs. Note that the HTTP/HTTPS restriction guarantees that
716 // we won't hit this for hosted app effective URLs, which would otherwise
717 // need to append a non-translated site URL to the hash below (see
718 // https://crbug.com/961386).
719 if (SiteIsolationPolicy::IsStrictOriginIsolationEnabled() &&
720 origin.GetURL().SchemeIsHTTPOrHTTPS())
W. James MacLeanf79c97e2019-05-02 20:35:46721 return origin.GetURL();
722
Aaron Colwell6fee3f52019-05-08 21:35:02723 site_url = GetSiteForOrigin(origin);
Alex Moshchuk4e19b362018-09-10 21:14:36724
725 // Isolated origins should use the full origin as their site URL. A
726 // subdomain of an isolated origin should also use that isolated origin's
727 // site URL. It is important to check |origin| (based on |url|) rather than
728 // |real_url| here, since some effective URLs (such as for NTP) need to be
729 // resolved prior to the isolated origin lookup.
730 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
731 url::Origin isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50732 if (policy->GetMatchingIsolatedOrigin(isolation_context, origin, site_url,
733 &isolated_origin))
Alex Moshchuk4e19b362018-09-10 21:14:36734 return isolated_origin.GetURL();
Alex Moshchuk5f926a52018-08-29 20:57:30735
736 // If an effective URL was used, augment the effective site URL with the
737 // underlying web site in the hash. This is needed to keep
738 // navigations across sites covered by one hosted app in separate
739 // SiteInstances. See https://crbug.com/791796.
740 //
741 // TODO(https://crbug.com/734722): Consider replacing this hack with
742 // a proper security principal.
743 if (should_use_effective_urls && url != real_url) {
Alex Moshchuk4e19b362018-09-10 21:14:36744 std::string non_translated_site_url(
Aaron Colwell3acea722019-04-24 21:54:43745 GetSiteForURLInternal(isolation_context, real_url,
Aaron Colwell6fee3f52019-05-08 21:35:02746 false /* should_use_effective_urls */,
747 allow_default_site_url)
Alex Moshchuk4e19b362018-09-10 21:14:36748 .spec());
749 GURL::Replacements replacements;
750 replacements.SetRefStr(non_translated_site_url.c_str());
751 site_url = site_url.ReplaceComponents(replacements);
Alex Moshchuk5f926a52018-08-29 20:57:30752 }
Aaron Colwell6fee3f52019-05-08 21:35:02753 } else {
754 // If there is no host but there is a scheme, return the scheme.
755 // This is useful for cases like file URLs.
756 if (!origin.opaque()) {
757 // Prefer to use the scheme of |origin| rather than |url|, to correctly
758 // cover blob:file: and filesystem:file: URIs (see also
759 // https://crbug.com/697111).
760 DCHECK(!origin.scheme().empty());
761 site_url = GURL(origin.scheme() + ":");
762 } else if (url.has_scheme()) {
763 // In some cases, it is not safe to use just the scheme as a site URL, as
764 // that might allow two URLs created by different sites to share a
765 // process. See https://crbug.com/863623 and https://crbug.com/863069.
766 //
767 // TODO(alexmos,creis): This should eventually be expanded to certain
768 // other schemes, such as file:.
769 // TODO(creis): This currently causes problems with tests on Android and
770 // Android WebView. For now, skip it when Site Isolation is not enabled,
771 // since there's no need to isolate data and blob URLs from each other in
772 // that case.
773 bool is_site_isolation_enabled =
774 SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
775 SiteIsolationPolicy::AreIsolatedOriginsEnabled();
776 if (is_site_isolation_enabled &&
777 (url.SchemeIsBlob() || url.scheme() == url::kDataScheme)) {
778 // We get here for blob URLs of form blob:null/guid. Use the full URL
779 // with the guid in that case, which isolates all blob URLs with unique
780 // origins from each other. We also get here for browser-initiated
781 // navigations to data URLs, which have a unique origin and should only
782 // share a process when they are identical. Remove hash from the URL in
783 // either case, since same-document navigations shouldn't use a
784 // different site URL.
785 if (url.has_ref()) {
786 GURL::Replacements replacements;
787 replacements.ClearRef();
788 url = url.ReplaceComponents(replacements);
789 }
790 site_url = url;
791 } else {
792 DCHECK(!url.scheme().empty());
793 site_url = GURL(url.scheme() + ":");
Alex Moshchukb1f87482018-07-19 01:51:51794 }
Aaron Colwell6fee3f52019-05-08 21:35:02795 } else {
796 // Otherwise the URL should be invalid; return an empty site.
797 DCHECK(!url.is_valid()) << url;
798 return GURL();
Alex Moshchukb1f87482018-07-19 01:51:51799 }
Lukasz Anforowiczd926a842018-03-09 01:50:45800 }
creisf60c2cd2014-12-18 00:41:02801
Aaron Colwell6fee3f52019-05-08 21:35:02802 if (allow_default_site_url &&
Aaron Colwell8bb0ada2019-07-18 22:41:23803 CanBePlacedInDefaultSiteInstance(isolation_context, url, site_url)) {
Aaron Colwell6fee3f52019-05-08 21:35:02804 return GetDefaultSiteURL();
805 }
806 return site_url;
initial.commit09911bf2008-07-26 23:55:29807}
808
nickcc0d9142015-10-14 16:27:10809// static
Aaron Colwell8bb0ada2019-07-18 22:41:23810bool SiteInstanceImpl::CanBePlacedInDefaultSiteInstance(
811 const IsolationContext& isolation_context,
812 const GURL& url,
813 const GURL& site_url) {
Clark DuVall4e1f0a72019-08-07 22:41:42814 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Aaron Colwell8bb0ada2019-07-18 22:41:23815 // Exclude "chrome-guest:" URLs from the default SiteInstance to ensure that
816 // guest specific process selection, process swapping, and storage partition
817 // behavior is preserved.
818 if (url.SchemeIs(kGuestScheme))
819 return false;
820
821 // Exclude "file://" URLs from the default SiteInstance to prevent the
822 // default SiteInstance process from accumulating file access grants that
823 // could be exploited by other non-isolated sites.
824 if (url.SchemeIs(url::kFileScheme))
825 return false;
826
827 // Don't use the default SiteInstance when
828 // kProcessSharingWithStrictSiteInstances is enabled because we want each
829 // site to have its own SiteInstance object and logic elsewhere ensures
830 // that those SiteInstances share a process.
831 if (base::FeatureList::IsEnabled(
832 features::kProcessSharingWithStrictSiteInstances)) {
833 return false;
834 }
835
836 // Don't use the default SiteInstance when SiteInstance doesn't assign a
837 // site URL for |url|, since in that case the SiteInstance should remain
838 // unused, and a subsequent navigation should always be able to reuse it,
839 // whether or not it's to a site requiring a dedicated process or to a site
840 // that will use the default SiteInstance.
841 if (!ShouldAssignSiteForURL(url))
842 return false;
843
844 // Allow the default SiteInstance to be used for sites that don't need to be
845 // isolated in their own process.
846 return !DoesSiteURLRequireDedicatedProcess(isolation_context, site_url);
847}
848
849// static
Alex Moshchuk4e19b362018-09-10 21:14:36850GURL SiteInstanceImpl::GetSiteForOrigin(const url::Origin& origin) {
851 // Only keep the scheme and registered domain of |origin|.
852 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
853 origin.host(),
854 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
855 std::string site = origin.scheme();
856 site += url::kStandardSchemeSeparator;
857 site += domain.empty() ? origin.host() : domain;
858 return GURL(site);
859}
860
861// static
[email protected]4c3a23582012-08-18 08:54:34862GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
863 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39864 DCHECK(browser_context);
Alex Moshchuka31c7882018-01-17 00:57:30865 return GetContentClient()->browser()->GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30866}
867
nickcc0d9142015-10-14 16:27:10868// static
Alex Moshchuk25c64bb2017-12-02 02:50:11869bool SiteInstanceImpl::HasEffectiveURL(BrowserContext* browser_context,
870 const GURL& url) {
871 return GetEffectiveURL(browser_context, url) != url;
872}
873
874// static
nickcc0d9142015-10-14 16:27:10875bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
Alex Moshchuk8e5c1952019-01-15 03:39:50876 const IsolationContext& isolation_context,
nickdb193a12016-09-09 23:09:23877 const GURL& url) {
Clark DuVall4e1f0a72019-08-07 22:41:42878 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Aaron Colwell5080de62019-05-06 23:02:03879 return SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
880 DoesSiteURLRequireDedicatedProcess(
881 isolation_context,
882 SiteInstanceImpl::GetSiteForURL(isolation_context, url));
883}
884
885// static
886bool SiteInstanceImpl::DoesSiteURLRequireDedicatedProcess(
887 const IsolationContext& isolation_context,
888 const GURL& site_url) {
Clark DuVall4e1f0a72019-08-07 22:41:42889 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Lukasz Anforowicz4aff3b82019-04-04 16:00:33890 DCHECK(isolation_context.browser_or_resource_context());
Lukasz Anforowicz4726a172018-10-15 21:25:10891
nickcc0d9142015-10-14 16:27:10892 // If --site-per-process is enabled, site isolation is enabled everywhere.
893 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
894 return true;
895
alexmos3b9ad102017-05-26 23:41:08896 // Always require a dedicated process for isolated origins.
alexmos3b9ad102017-05-26 23:41:08897 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk8e5c1952019-01-15 03:39:50898 if (policy->IsIsolatedOrigin(isolation_context,
899 url::Origin::Create(site_url)))
alexmos3b9ad102017-05-26 23:41:08900 return true;
901
Lukasz Anforowicz1f5ad402018-09-28 23:57:54902 // Error pages in main frames do require isolation, however since this is
903 // missing the context whether this is for a main frame or not, that part
904 // is enforced in RenderFrameHostManager.
905 if (site_url.SchemeIs(kChromeErrorScheme))
906 return true;
907
908 // Isolate kChromeUIScheme pages from one another and from other kinds of
909 // schemes.
910 if (site_url.SchemeIs(content::kChromeUIScheme))
911 return true;
912
nickdb193a12016-09-09 23:09:23913 // Let the content embedder enable site isolation for specific URLs. Use the
914 // canonical site url for this check, so that schemes with nested origins
915 // (blob and filesystem) work properly.
Avi Drissman8c36df72017-12-11 21:19:20916 if (GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
Clark DuVall4e1f0a72019-08-07 22:41:42917 isolation_context.browser_or_resource_context().ToBrowserContext(),
918 site_url)) {
nickcc0d9142015-10-14 16:27:10919 return true;
920 }
921
922 return false;
923}
924
alexmos13fe1962017-06-28 04:25:12925// static
Alex Moshchuk8e5c1952019-01-15 03:39:50926bool SiteInstanceImpl::ShouldLockToOrigin(
Alex Moshchuk8e5c1952019-01-15 03:39:50927 const IsolationContext& isolation_context,
928 GURL site_url) {
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02929 DCHECK_CURRENTLY_ON(BrowserThread::UI);
930 BrowserContext* browser_context =
931 isolation_context.browser_or_resource_context().ToBrowserContext();
Lukasz Anforowicz4726a172018-10-15 21:25:10932 DCHECK(browser_context);
933
creis0b49fa12017-07-10 16:31:11934 // Don't lock to origin in --single-process mode, since this mode puts
935 // cross-site pages into the same process.
Alex Moshchukd4c4ca32018-08-15 20:36:51936 if (RenderProcessHost::run_renderer_in_process())
creis0b49fa12017-07-10 16:31:11937 return false;
938
Aaron Colwell5080de62019-05-06 23:02:03939 if (!DoesSiteURLRequireDedicatedProcess(isolation_context, site_url))
alexmos13fe1962017-06-28 04:25:12940 return false;
941
942 // Guest processes cannot be locked to their site because guests always have
943 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
944 // SiteInstance. So we skip locking the guest process to the site.
945 // TODO(ncarter): Remove this exclusion once we can make origin lock per
946 // RenderFrame routing id.
947 if (site_url.SchemeIs(content::kGuestScheme))
948 return false;
949
alexmos13fe1962017-06-28 04:25:12950 // TODO(creis, nick): Until we can handle sites with effective URLs at the
951 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
952 // must give the embedder a chance to exempt some sites to avoid process
953 // kills.
954 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
955 site_url)) {
956 return false;
957 }
958
959 return true;
960}
961
Lukasz Anforowicz27851e62018-12-18 23:04:08962// static
963base::Optional<url::Origin> SiteInstanceImpl::GetRequestInitiatorSiteLock(
Lukasz Anforowicz27851e62018-12-18 23:04:08964 GURL site_url) {
Lukasz Anforowicz27851e62018-12-18 23:04:08965 // The following schemes are safe for sites that require a process lock:
966 // - data: - locking |request_initiator| to an opaque origin
967 // - http/https - requiring |request_initiator| to match |site_url| with
968 // DomainIs (i.e. suffix-based) comparison.
969 if (site_url.SchemeIsHTTPOrHTTPS() || site_url.SchemeIs(url::kDataScheme))
970 return url::Origin::Create(site_url);
971
972 // Other schemes might not be safe to use as |request_initiator_site_lock|.
973 // One example is chrome-guest://...
974 return base::nullopt;
975}
976
[email protected]40bfaf32013-11-19 01:35:43977void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
978 DCHECK_EQ(process_, host);
979 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44980 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13981}
[email protected]313b80bd2011-11-23 03:49:10982
Bo Liu2a489402018-04-24 23:41:27983void SiteInstanceImpl::RenderProcessExited(
984 RenderProcessHost* host,
985 const ChildProcessTerminationInfo& info) {
ericwilligers254597b2016-10-17 10:32:31986 for (auto& observer : observers_)
987 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07988}
989
alexmos13fe1962017-06-28 04:25:12990void SiteInstanceImpl::LockToOriginIfNeeded() {
991 DCHECK(HasSite());
992
993 // From now on, this process should be considered "tainted" for future
994 // process reuse decisions:
995 // (1) If |site_| required a dedicated process, this SiteInstance's process
996 // can only host URLs for the same site.
997 // (2) Even if |site_| does not require a dedicated process, this
998 // SiteInstance's process still cannot be reused to host other sites
999 // requiring dedicated sites in the future.
1000 // We can get here either when we commit a URL into a SiteInstance that does
1001 // not yet have a site, or when we create a process for a SiteInstance with a
1002 // preassigned site.
1003 process_->SetIsUsed();
1004
Alex Moshchuk47dd00f2017-10-10 17:15:231005 ChildProcessSecurityPolicyImpl* policy =
1006 ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk58225c82019-04-18 00:45:011007 GURL process_lock = policy->GetOriginLock(process_->GetID());
Lukasz Anforowicz1a0a89a2019-03-29 22:31:021008 if (ShouldLockToOrigin(GetIsolationContext(), site_)) {
Alex Moshchuk9cf5a45d2017-08-14 18:50:571009 // Sanity check that this won't try to assign an origin lock to a <webview>
1010 // process, which can't be locked.
1011 CHECK(!process_->IsForGuestsOnly());
1012
Alex Moshchuk58225c82019-04-18 00:45:011013 if (process_lock.is_empty()) {
1014 // TODO(nick): When all sites are isolated, this operation provides
1015 // strong protection. If only some sites are isolated, we need
1016 // additional logic to prevent the non-isolated sites from requesting
1017 // resources for isolated sites. https://crbug.com/509125
1018 TRACE_EVENT2("navigation", "SiteInstanceImpl::LockToOrigin", "site id",
1019 id_, "lock", lock_url().possibly_invalid_spec());
1020 process_->LockToOrigin(GetIsolationContext(), lock_url());
1021 } else if (process_lock != lock_url()) {
1022 // We should never attempt to reassign a different origin lock to a
1023 // process.
1024 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
1025 site_.spec());
1026 base::debug::SetCrashKeyString(
1027 bad_message::GetKilledProcessOriginLockKey(), process_lock.spec());
1028 CHECK(false) << "Trying to lock a process to " << lock_url()
1029 << " but the process is already locked to " << process_lock;
1030 } else {
1031 // Process already has the right origin lock assigned. This case will
1032 // happen for commits to |site_| after the first one.
Alex Moshchuk9cf5a45d2017-08-14 18:50:571033 }
Alex Moshchuk47dd00f2017-10-10 17:15:231034 } else {
1035 // If the site that we've just committed doesn't require a dedicated
1036 // process, make sure we aren't putting it in a process for a site that
1037 // does.
Alex Moshchuk58225c82019-04-18 00:45:011038 if (!process_lock.is_empty()) {
Alex Moshchuk1656c672018-04-28 01:48:341039 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
1040 site_.spec());
1041 base::debug::SetCrashKeyString(
Alex Moshchuk58225c82019-04-18 00:45:011042 bad_message::GetKilledProcessOriginLockKey(), process_lock.spec());
Alex Moshchuk1656c672018-04-28 01:48:341043 CHECK(false) << "Trying to commit non-isolated site " << site_
Alex Moshchuk58225c82019-04-18 00:45:011044 << " in process locked to " << process_lock;
Alex Moshchuk1656c672018-04-28 01:48:341045 }
[email protected]313b80bd2011-11-23 03:49:101046 }
1047}
[email protected]46488322012-10-30 03:22:201048
Alex Moshchuk07e1bb42019-03-08 04:44:081049// static
1050void SiteInstance::StartIsolatingSite(BrowserContext* context,
1051 const GURL& url) {
1052 if (!SiteIsolationPolicy::AreDynamicIsolatedOriginsEnabled())
1053 return;
1054
1055 // Ignore attempts to isolate origins that are not supported. Do this here
1056 // instead of relying on AddIsolatedOrigins()'s internal validation, to avoid
1057 // the runtime warning generated by the latter.
1058 url::Origin origin(url::Origin::Create(url));
1059 if (!IsolatedOriginUtil::IsValidIsolatedOrigin(origin))
1060 return;
1061
1062 // Convert |url| to a site, to avoid breaking document.domain. Note that
1063 // this doesn't use effective URL resolution or other special cases from
1064 // GetSiteForURL() and simply converts |origin| to a scheme and eTLD+1.
1065 GURL site(SiteInstanceImpl::GetSiteForOrigin(origin));
1066
1067 ChildProcessSecurityPolicyImpl* policy =
1068 ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuke256d562019-04-26 21:43:571069 url::Origin site_origin(url::Origin::Create(site));
Alex Moshchukc4679422019-06-11 17:04:481070 policy->AddIsolatedOrigins(
1071 {site_origin},
1072 ChildProcessSecurityPolicy::IsolatedOriginSource::USER_TRIGGERED,
1073 context);
Alex Moshchuke256d562019-04-26 21:43:571074
1075 // This function currently assumes the new isolated site should persist
1076 // across restarts, so ask the embedder to save it, excluding off-the-record
1077 // profiles.
1078 if (!context->IsOffTheRecord())
1079 GetContentClient()->browser()->PersistIsolatedOrigin(context, site_origin);
Alex Moshchuk07e1bb42019-03-08 04:44:081080}
1081
[email protected]46488322012-10-30 03:22:201082} // namespace content