blob: 12995294e3ad49a951610f9b5776df1d897aac40 [file] [log] [blame]
[email protected]7f474212013-11-05 04:26:161// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avia2f4804a2015-12-24 23:11:135#include <stddef.h>
dchengc963c7142016-04-08 03:55:226
7#include <memory>
dcheng1fc00f12015-12-26 22:18:038#include <utility>
avia2f4804a2015-12-24 23:11:139
Sebastien Marchandf1349f52019-01-25 03:16:4110#include "base/bind.h"
robcdcc4b82015-12-06 12:39:4511#include "base/callback.h"
alexmosf7af1352016-10-21 20:00:4112#include "base/path_service.h"
robcdcc4b82015-12-06 12:39:4513#include "base/run_loop.h"
Avi Drissman5f0fb8c2018-12-25 23:20:4914#include "base/stl_util.h"
Daniel Cheng4ebba552018-07-06 21:43:1615#include "base/strings/strcat.h"
Lei Zhangfe5b86932019-02-01 17:26:5916#include "base/strings/stringprintf.h"
Devlin Cronin7a282e32017-08-10 01:54:1017#include "base/strings/utf_string_conversions.h"
Devlin Cronin626d80c2018-06-01 01:08:3618#include "base/test/metrics/histogram_tester.h"
vitaliii20d3e4e2018-01-29 12:45:1419#include "build/build_config.h"
steelf65a72a2016-10-19 22:16:4520#include "chrome/browser/chrome_notification_types.h"
[email protected]7f474212013-11-05 04:26:1621#include "chrome/browser/extensions/extension_browsertest.h"
22#include "chrome/browser/extensions/extension_service.h"
Jochen Eisinger68fb0642018-01-17 21:56:4523#include "chrome/browser/permissions/permission_request_manager.h"
alexmosf7af1352016-10-21 20:00:4124#include "chrome/browser/ui/browser_commands.h"
Devlin Cronine458a222019-12-30 22:39:3725#include "chrome/browser/ui/extensions/extension_action_test_helper.h"
[email protected]6703c15d2014-04-03 19:03:4526#include "chrome/browser/ui/tabs/tab_strip_model.h"
alexmosf7af1352016-10-21 20:00:4127#include "chrome/common/chrome_paths.h"
alexmos4bfdc922016-09-15 22:40:5428#include "chrome/common/pref_names.h"
[email protected]7f474212013-11-05 04:26:1629#include "chrome/test/base/in_process_browser_test.h"
[email protected]6703c15d2014-04-03 19:03:4530#include "chrome/test/base/ui_test_utils.h"
alexmosf7af1352016-10-21 20:00:4131#include "components/guest_view/browser/test_guest_view_manager.h"
Evan Stade7220e472020-01-31 17:06:5732#include "components/javascript_dialogs/app_modal_dialog_manager.h"
nick2a8ba8c2016-10-03 18:51:3933#include "content/public/browser/child_process_security_policy.h"
nasko68e5e6d2017-05-24 23:19:3434#include "content/public/browser/navigation_entry.h"
[email protected]7f474212013-11-05 04:26:1635#include "content/public/browser/notification_service.h"
robcdcc4b82015-12-06 12:39:4536#include "content/public/browser/render_frame_host.h"
nick92b04582016-04-08 02:54:0437#include "content/public/browser/render_process_host.h"
[email protected]6703c15d2014-04-03 19:03:4538#include "content/public/browser/web_contents.h"
Arthur Sonzognif21fb512018-11-06 09:31:5839#include "content/public/common/navigation_policy.h"
robcdcc4b82015-12-06 12:39:4540#include "content/public/test/browser_test_utils.h"
Jochen Eisinger68fb0642018-01-17 21:56:4541#include "content/public/test/download_test_observer.h"
alexmos4bfdc922016-09-15 22:40:5442#include "content/public/test/test_navigation_observer.h"
[email protected]7f474212013-11-05 04:26:1643#include "content/public/test/test_utils.h"
alexmosf7af1352016-10-21 20:00:4144#include "extensions/browser/app_window/app_window.h"
45#include "extensions/browser/app_window/app_window_registry.h"
dchengc963c7142016-04-08 03:55:2246#include "extensions/browser/process_manager.h"
wez714dde12017-02-14 22:26:0347#include "extensions/common/manifest_handlers/background_info.h"
Nick Carter1cb9c33c2018-03-30 18:40:0748#include "extensions/common/manifest_handlers/web_accessible_resources_info.h"
alexmosf7af1352016-10-21 20:00:4149#include "extensions/common/permissions/permissions_data.h"
robcdcc4b82015-12-06 12:39:4550#include "extensions/common/value_builder.h"
51#include "extensions/test/background_page_watcher.h"
Devlin Cronin4f455a22018-01-25 01:36:4552#include "extensions/test/test_extension_dir.h"
[email protected]6703c15d2014-04-03 19:03:4553#include "net/dns/mock_host_resolver.h"
54#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]7f474212013-11-05 04:26:1655
Hiroki Satobe244d92020-01-28 00:12:4856#if defined(OS_CHROMEOS)
achuithcb6fe98c2017-03-22 08:57:5157#include "chrome/browser/chromeos/profiles/profile_helper.h"
Steven Bennetts38e9bde22019-01-03 17:31:1058#include "chromeos/constants/chromeos_switches.h"
achuithcb6fe98c2017-03-22 08:57:5159#endif
60
[email protected]98b6d942013-11-10 00:34:0761namespace extensions {
[email protected]7f474212013-11-05 04:26:1662
robcdcc4b82015-12-06 12:39:4563namespace {
64
Nick Carterbf6264a52018-04-06 02:39:3365bool IsExtensionProcessSharingAllowed() {
66 // TODO(nick): Currently, process sharing is allowed even in
67 // --site-per-process. Lock this down. https://crbug.com/766267
68 return true;
69}
70
robcdcc4b82015-12-06 12:39:4571void AddFrameToSet(std::set<content::RenderFrameHost*>* frames,
72 content::RenderFrameHost* rfh) {
73 if (rfh->IsRenderFrameLive())
74 frames->insert(rfh);
75}
76
alexmos4bfdc922016-09-15 22:40:5477GURL CreateBlobURL(content::RenderFrameHost* frame,
78 const std::string& content) {
79 std::string blob_url_string;
80 EXPECT_TRUE(ExecuteScriptAndExtractString(
81 frame,
82 "var blob = new Blob(['<html><body>" + content + "</body></html>'],\n"
83 " {type: 'text/html'});\n"
84 "domAutomationController.send(URL.createObjectURL(blob));\n",
85 &blob_url_string));
86 GURL blob_url(blob_url_string);
87 EXPECT_TRUE(blob_url.is_valid());
88 EXPECT_TRUE(blob_url.SchemeIsBlob());
89 return blob_url;
90}
91
92GURL CreateFileSystemURL(content::RenderFrameHost* frame,
93 const std::string& content) {
94 std::string filesystem_url_string;
95 EXPECT_TRUE(ExecuteScriptAndExtractString(
96 frame,
97 "var blob = new Blob(['<html><body>" + content + "</body></html>'],\n"
98 " {type: 'text/html'});\n"
99 "window.webkitRequestFileSystem(TEMPORARY, blob.size, fs => {\n"
100 " fs.root.getFile('foo.html', {create: true}, file => {\n"
101 " file.createWriter(writer => {\n"
102 " writer.write(blob);\n"
103 " writer.onwriteend = () => {\n"
104 " domAutomationController.send(file.toURL());\n"
105 " }\n"
106 " });\n"
107 " });\n"
108 "});\n",
109 &filesystem_url_string));
110 GURL filesystem_url(filesystem_url_string);
111 EXPECT_TRUE(filesystem_url.is_valid());
112 EXPECT_TRUE(filesystem_url.SchemeIsFileSystem());
113 return filesystem_url;
114}
115
116std::string GetTextContent(content::RenderFrameHost* frame) {
117 std::string result;
118 EXPECT_TRUE(ExecuteScriptAndExtractString(
119 frame, "domAutomationController.send(document.body.innerText)", &result));
120 return result;
121}
122
alexmos1f48ef62016-10-06 00:51:50123// Helper to send a postMessage from |sender| to |opener| via window.opener,
124// wait for a reply, and verify the response. Defines its own message event
125// handlers.
126void VerifyPostMessageToOpener(content::RenderFrameHost* sender,
127 content::RenderFrameHost* opener) {
128 EXPECT_TRUE(
129 ExecuteScript(opener,
130 "window.addEventListener('message', function(event) {\n"
131 " event.source.postMessage(event.data, '*');\n"
132 "});"));
133
134 EXPECT_TRUE(
135 ExecuteScript(sender,
136 "window.addEventListener('message', function(event) {\n"
137 " window.domAutomationController.send(event.data);\n"
138 "});"));
139
140 std::string result;
141 EXPECT_TRUE(ExecuteScriptAndExtractString(
142 sender, "opener.postMessage('foo', '*');", &result));
143 EXPECT_EQ("foo", result);
144}
145
robcdcc4b82015-12-06 12:39:45146// Takes a snapshot of all frames upon construction. When Wait() is called, a
147// MessageLoop is created and Quit when all previously recorded frames are
148// either present in the tab, or deleted. If a navigation happens between the
149// construction and the Wait() call, then this logic ensures that all obsolete
150// RenderFrameHosts have been destructed when Wait() returns.
151// See also the comment at ProcessManagerBrowserTest::NavigateToURL.
152class NavigationCompletedObserver : public content::WebContentsObserver {
153 public:
154 explicit NavigationCompletedObserver(content::WebContents* web_contents)
155 : content::WebContentsObserver(web_contents),
156 message_loop_runner_(new content::MessageLoopRunner) {
157 web_contents->ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:45158 base::BindRepeating(&AddFrameToSet, base::Unretained(&frames_)));
robcdcc4b82015-12-06 12:39:45159 }
160
161 void Wait() {
162 if (!AreAllFramesInTab())
163 message_loop_runner_->Run();
164 }
165
166 void RenderFrameDeleted(content::RenderFrameHost* rfh) override {
167 if (frames_.erase(rfh) != 0 && message_loop_runner_->loop_running() &&
Lei Zhangebcc6302018-01-12 19:46:45168 AreAllFramesInTab()) {
robcdcc4b82015-12-06 12:39:45169 message_loop_runner_->Quit();
Lei Zhangebcc6302018-01-12 19:46:45170 }
robcdcc4b82015-12-06 12:39:45171 }
172
173 private:
174 // Check whether all frames that were recorded at the construction of this
175 // class are still part of the tab.
176 bool AreAllFramesInTab() {
177 std::set<content::RenderFrameHost*> current_frames;
178 web_contents()->ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:45179 base::BindRepeating(&AddFrameToSet, base::Unretained(&current_frames)));
robcdcc4b82015-12-06 12:39:45180 for (content::RenderFrameHost* frame : frames_) {
Jan Wilken Dörrieade79222019-06-06 19:01:12181 if (!base::Contains(current_frames, frame))
robcdcc4b82015-12-06 12:39:45182 return false;
183 }
184 return true;
185 }
186
187 std::set<content::RenderFrameHost*> frames_;
188 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
189
190 DISALLOW_COPY_AND_ASSIGN(NavigationCompletedObserver);
191};
192
[email protected]7f474212013-11-05 04:26:16193// Exists as a browser test because ExtensionHosts are hard to create without
194// a real browser.
robcdcc4b82015-12-06 12:39:45195class ProcessManagerBrowserTest : public ExtensionBrowserTest {
196 public:
krasinaf85e4c42016-10-25 17:54:26197 ProcessManagerBrowserTest() {
198 guest_view::GuestViewManager::set_factory_for_testing(&factory_);
199 }
jama9cfcb52017-05-01 21:49:13200
201 void SetUpOnMainThread() override {
202 ExtensionBrowserTest::SetUpOnMainThread();
203 host_resolver()->AddRule("*", "127.0.0.1");
204 }
205
robcdcc4b82015-12-06 12:39:45206 // Create an extension with web-accessible frames and an optional background
207 // page.
208 const Extension* CreateExtension(const std::string& name,
209 bool has_background_process) {
dchengc963c7142016-04-08 03:55:22210 std::unique_ptr<TestExtensionDir> dir(new TestExtensionDir());
robcdcc4b82015-12-06 12:39:45211
212 DictionaryBuilder manifest;
213 manifest.Set("name", name)
214 .Set("version", "1")
215 .Set("manifest_version", 2)
216 // To allow ExecuteScript* to work.
217 .Set("content_security_policy",
218 "script-src 'self' 'unsafe-eval'; object-src 'self'")
limasdf3d102542015-12-09 03:58:45219 .Set("sandbox",
dcheng794d2bd2016-02-27 03:51:32220 DictionaryBuilder()
221 .Set("pages", ListBuilder().Append("sandboxed.html").Build())
222 .Build())
Nick Carter1cb9c33c2018-03-30 18:40:07223 .Set("web_accessible_resources",
224 ListBuilder().Append("*.html").Build());
robcdcc4b82015-12-06 12:39:45225
226 if (has_background_process) {
limasdf21d67e62015-12-19 12:04:49227 manifest.Set("background",
dcheng794d2bd2016-02-27 03:51:32228 DictionaryBuilder().Set("page", "bg.html").Build());
robcdcc4b82015-12-06 12:39:45229 dir->WriteFile(FILE_PATH_LITERAL("bg.html"),
230 "<iframe id='bgframe' src='empty.html'></iframe>");
231 }
232
233 dir->WriteFile(FILE_PATH_LITERAL("blank_iframe.html"),
234 "<iframe id='frame0' src='about:blank'></iframe>");
235
236 dir->WriteFile(FILE_PATH_LITERAL("srcdoc_iframe.html"),
237 "<iframe id='frame0' srcdoc='Hello world'></iframe>");
238
239 dir->WriteFile(FILE_PATH_LITERAL("two_iframes.html"),
240 "<iframe id='frame1' src='empty.html'></iframe>"
241 "<iframe id='frame2' src='empty.html'></iframe>");
242
243 dir->WriteFile(FILE_PATH_LITERAL("sandboxed.html"), "Some sandboxed page");
244
245 dir->WriteFile(FILE_PATH_LITERAL("empty.html"), "");
246
247 dir->WriteManifest(manifest.ToJSON());
248
vabr9142fe22016-09-08 13:19:22249 const Extension* extension = LoadExtension(dir->UnpackedPath());
robcdcc4b82015-12-06 12:39:45250 EXPECT_TRUE(extension);
dcheng1fc00f12015-12-26 22:18:03251 temp_dirs_.push_back(std::move(dir));
robcdcc4b82015-12-06 12:39:45252 return extension;
253 }
254
255 // ui_test_utils::NavigateToURL sometimes returns too early: It returns as
256 // soon as the StopLoading notification has been triggered. This does not
257 // imply that RenderFrameDeleted was called, so the test may continue too
258 // early and fail when ProcessManager::GetAllFrames() returns too many frames
259 // (namely frames that are in the process of being deleted). To work around
260 // this problem, we also wait until all previous frames have been deleted.
261 void NavigateToURL(const GURL& url) {
262 NavigationCompletedObserver observer(
263 browser()->tab_strip_model()->GetActiveWebContents());
264
265 ui_test_utils::NavigateToURL(browser(), url);
266
267 // Wait until the last RenderFrameHosts are deleted. This wait doesn't take
268 // long.
269 observer.Wait();
270 }
271
alexmos4bfdc922016-09-15 22:40:54272 content::WebContents* OpenPopup(content::RenderFrameHost* opener,
Nick Carter1cb9c33c2018-03-30 18:40:07273 const GURL& url,
274 bool expect_success = true) {
Evan Stadeacbfc3de2019-07-18 19:44:17275 ui_test_utils::TabAddedWaiter waiter(browser());
alexmos1c3da4f2016-11-03 06:06:11276 EXPECT_TRUE(ExecuteScript(
277 opener, "window.popup = window.open('" + url.spec() + "')"));
Evan Stadeacbfc3de2019-07-18 19:44:17278 waiter.Wait();
alexmos4bfdc922016-09-15 22:40:54279 content::WebContents* popup =
280 browser()->tab_strip_model()->GetActiveWebContents();
281 WaitForLoadStop(popup);
Nick Carter1cb9c33c2018-03-30 18:40:07282 if (expect_success)
283 EXPECT_EQ(url, popup->GetMainFrame()->GetLastCommittedURL());
alexmos4bfdc922016-09-15 22:40:54284 return popup;
285 }
286
Nick Cartera6102ee2018-03-14 18:49:13287 content::WebContents* OpenPopupNoOpener(content::RenderFrameHost* opener,
288 const GURL& url) {
289 content::WebContentsAddedObserver popup_observer;
290 EXPECT_TRUE(ExecuteScript(
291 opener, "window.open('" + url.spec() + "', '', 'noopener')"));
292 content::WebContents* popup = popup_observer.GetWebContents();
293 WaitForLoadStop(popup);
294 return popup;
295 }
296
robcdcc4b82015-12-06 12:39:45297 private:
krasinaf85e4c42016-10-25 17:54:26298 guest_view::TestGuestViewManagerFactory factory_;
dchengc963c7142016-04-08 03:55:22299 std::vector<std::unique_ptr<TestExtensionDir>> temp_dirs_;
robcdcc4b82015-12-06 12:39:45300};
[email protected]7f474212013-11-05 04:26:16301
achuithcb6fe98c2017-03-22 08:57:51302class DefaultProfileExtensionBrowserTest : public ExtensionBrowserTest {
303 protected:
304 DefaultProfileExtensionBrowserTest() {
305#if defined(OS_CHROMEOS)
306 // We want signin profile on ChromeOS, not logged in user profile.
307 set_chromeos_user_ = false;
308#endif
309 }
310
311 private:
312 void SetUpCommandLine(base::CommandLine* command_line) override {
313 ExtensionBrowserTest::SetUpCommandLine(command_line);
314#if defined(OS_CHROMEOS)
315 command_line->AppendSwitch(chromeos::switches::kLoginManager);
316 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
317#endif
318 }
319};
320
321} // namespace
322
323// By default, no extension hosts should be present in the profile;
324// they should only be present if non-component extensions are loaded
325// or if the user takes some action to trigger a component extension.
326// TODO(achuith): Expand this testing to include more in-depth
327// testing for the signin profile, where we explicitly disallow all
328// extension hosts unless it's the off-the-record profile.
329IN_PROC_BROWSER_TEST_F(DefaultProfileExtensionBrowserTest, NoExtensionHosts) {
330 // Explicitly get the original and off-the-record-profiles, since on CrOS,
331 // the signin profile (profile()) is the off-the-record version.
332 Profile* original = profile()->GetOriginalProfile();
333 Profile* otr = original->GetOffTheRecordProfile();
334#if defined(OS_CHROMEOS)
335 EXPECT_EQ(profile(), otr);
336 EXPECT_TRUE(chromeos::ProfileHelper::IsSigninProfile(original));
337#endif
338
339 ProcessManager* pm = ProcessManager::Get(original);
340 EXPECT_EQ(0u, pm->background_hosts().size());
341
342 pm = ProcessManager::Get(otr);
343 EXPECT_EQ(0u, pm->background_hosts().size());
344}
345
[email protected]7f474212013-11-05 04:26:16346// Test that basic extension loading creates the appropriate ExtensionHosts
347// and background pages.
[email protected]98b6d942013-11-10 00:34:07348IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
[email protected]7f474212013-11-05 04:26:16349 ExtensionHostCreation) {
reillyg0ea3fa902014-10-28 15:30:23350 ProcessManager* pm = ProcessManager::Get(profile());
[email protected]7f474212013-11-05 04:26:16351
352 // We start with no background hosts.
353 ASSERT_EQ(0u, pm->background_hosts().size());
rdevlin.cronin6ae04a012015-04-03 20:19:40354 ASSERT_EQ(0u, pm->GetAllFrames().size());
[email protected]7f474212013-11-05 04:26:16355
356 // Load an extension with a background page.
357 scoped_refptr<const Extension> extension =
358 LoadExtension(test_data_dir_.AppendASCII("api_test")
359 .AppendASCII("browser_action")
360 .AppendASCII("none"));
wez714dde12017-02-14 22:26:03361 ASSERT_TRUE(extension);
362
363 EXPECT_TRUE(BackgroundInfo::HasPersistentBackgroundPage(extension.get()));
364 EXPECT_EQ(-1, pm->GetLazyKeepaliveCount(extension.get()));
David Bertoni3e1e9fa2018-08-29 20:39:30365 EXPECT_TRUE(pm->GetLazyKeepaliveActivities(extension.get()).empty());
[email protected]7f474212013-11-05 04:26:16366
367 // Process manager gains a background host.
368 EXPECT_EQ(1u, pm->background_hosts().size());
rdevlin.cronin6ae04a012015-04-03 20:19:40369 EXPECT_EQ(1u, pm->GetAllFrames().size());
[email protected]7f474212013-11-05 04:26:16370 EXPECT_TRUE(pm->GetBackgroundHostForExtension(extension->id()));
371 EXPECT_TRUE(pm->GetSiteInstanceForURL(extension->url()));
rdevlin.cronin6ae04a012015-04-03 20:19:40372 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
[email protected]7f474212013-11-05 04:26:16373 EXPECT_FALSE(pm->IsBackgroundHostClosing(extension->id()));
[email protected]7f474212013-11-05 04:26:16374
375 // Unload the extension.
376 UnloadExtension(extension->id());
377
378 // Background host disappears.
379 EXPECT_EQ(0u, pm->background_hosts().size());
rdevlin.cronin6ae04a012015-04-03 20:19:40380 EXPECT_EQ(0u, pm->GetAllFrames().size());
[email protected]7f474212013-11-05 04:26:16381 EXPECT_FALSE(pm->GetBackgroundHostForExtension(extension->id()));
382 EXPECT_TRUE(pm->GetSiteInstanceForURL(extension->url()));
rdevlin.cronin6ae04a012015-04-03 20:19:40383 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
[email protected]7f474212013-11-05 04:26:16384 EXPECT_FALSE(pm->IsBackgroundHostClosing(extension->id()));
wez714dde12017-02-14 22:26:03385 EXPECT_EQ(-1, pm->GetLazyKeepaliveCount(extension.get()));
David Bertoni3e1e9fa2018-08-29 20:39:30386 EXPECT_TRUE(pm->GetLazyKeepaliveActivities(extension.get()).empty());
[email protected]7f474212013-11-05 04:26:16387}
388
[email protected]7f474212013-11-05 04:26:16389// Test that loading an extension with a browser action does not create a
390// background page and that clicking on the action creates the appropriate
391// ExtensionHost.
[email protected]1df312d2013-11-05 21:20:39392// Disabled due to flake, see http://crbug.com/315242
[email protected]98b6d942013-11-10 00:34:07393IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
[email protected]1df312d2013-11-05 21:20:39394 DISABLED_PopupHostCreation) {
reillyg0ea3fa902014-10-28 15:30:23395 ProcessManager* pm = ProcessManager::Get(profile());
[email protected]7f474212013-11-05 04:26:16396
397 // Load an extension with the ability to open a popup but no background
398 // page.
399 scoped_refptr<const Extension> popup =
400 LoadExtension(test_data_dir_.AppendASCII("api_test")
401 .AppendASCII("browser_action")
402 .AppendASCII("popup"));
wez714dde12017-02-14 22:26:03403 ASSERT_TRUE(popup);
404
405 EXPECT_FALSE(BackgroundInfo::HasBackgroundPage(popup.get()));
406 EXPECT_EQ(-1, pm->GetLazyKeepaliveCount(popup.get()));
David Bertoni3e1e9fa2018-08-29 20:39:30407 EXPECT_TRUE(pm->GetLazyKeepaliveActivities(popup.get()).empty());
[email protected]7f474212013-11-05 04:26:16408
409 // No background host was added.
410 EXPECT_EQ(0u, pm->background_hosts().size());
rdevlin.cronin6ae04a012015-04-03 20:19:40411 EXPECT_EQ(0u, pm->GetAllFrames().size());
[email protected]7f474212013-11-05 04:26:16412 EXPECT_FALSE(pm->GetBackgroundHostForExtension(popup->id()));
rdevlin.cronin6ae04a012015-04-03 20:19:40413 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(popup->id()).size());
[email protected]7f474212013-11-05 04:26:16414 EXPECT_TRUE(pm->GetSiteInstanceForURL(popup->url()));
415 EXPECT_FALSE(pm->IsBackgroundHostClosing(popup->id()));
[email protected]7f474212013-11-05 04:26:16416
417 // Simulate clicking on the action to open a popup.
Devlin Cronine458a222019-12-30 22:39:37418 auto test_util = ExtensionActionTestHelper::Create(browser());
[email protected]7f474212013-11-05 04:26:16419 content::WindowedNotificationObserver frame_observer(
420 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
421 content::NotificationService::AllSources());
422 // Open popup in the first extension.
Elly Fong-Jones914e0d52018-03-09 16:50:14423 test_util->Press(0);
[email protected]7f474212013-11-05 04:26:16424 frame_observer.Wait();
Elly Fong-Jones914e0d52018-03-09 16:50:14425 ASSERT_TRUE(test_util->HasPopup());
[email protected]7f474212013-11-05 04:26:16426
427 // We now have a view, but still no background hosts.
428 EXPECT_EQ(0u, pm->background_hosts().size());
rdevlin.cronin6ae04a012015-04-03 20:19:40429 EXPECT_EQ(1u, pm->GetAllFrames().size());
[email protected]7f474212013-11-05 04:26:16430 EXPECT_FALSE(pm->GetBackgroundHostForExtension(popup->id()));
rdevlin.cronin6ae04a012015-04-03 20:19:40431 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(popup->id()).size());
[email protected]7f474212013-11-05 04:26:16432 EXPECT_TRUE(pm->GetSiteInstanceForURL(popup->url()));
433 EXPECT_FALSE(pm->IsBackgroundHostClosing(popup->id()));
wez714dde12017-02-14 22:26:03434 EXPECT_EQ(-1, pm->GetLazyKeepaliveCount(popup.get()));
David Bertoni3e1e9fa2018-08-29 20:39:30435 EXPECT_TRUE(pm->GetLazyKeepaliveActivities(popup.get()).empty());
436 EXPECT_TRUE(pm->GetLazyKeepaliveActivities(popup.get()).empty());
[email protected]7f474212013-11-05 04:26:16437}
[email protected]98b6d942013-11-10 00:34:07438
[email protected]6703c15d2014-04-03 19:03:45439// Content loaded from http://hlogonemlfkgpejgnedahbkiabcdhnnn should not
440// interact with an installed extension with that ID. Regression test
441// for bug 357382.
442IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest, HttpHostMatchingExtensionId) {
reillyg0ea3fa902014-10-28 15:30:23443 ProcessManager* pm = ProcessManager::Get(profile());
[email protected]6703c15d2014-04-03 19:03:45444
445 // We start with no background hosts.
446 ASSERT_EQ(0u, pm->background_hosts().size());
rdevlin.cronin6ae04a012015-04-03 20:19:40447 ASSERT_EQ(0u, pm->GetAllFrames().size());
[email protected]6703c15d2014-04-03 19:03:45448
449 // Load an extension with a background page.
450 scoped_refptr<const Extension> extension =
451 LoadExtension(test_data_dir_.AppendASCII("api_test")
452 .AppendASCII("browser_action")
453 .AppendASCII("none"));
454
455 // Set up a test server running at http://[extension-id]
456 ASSERT_TRUE(extension.get());
mgiuca77752c32015-02-05 07:31:18457 const std::string& aliased_host = extension->id();
svaldeza01f7d92015-11-18 17:47:56458 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]6703c15d2014-04-03 19:03:45459 GURL url =
460 embedded_test_server()->GetURL("/extensions/test_file_with_body.html");
461 GURL::Replacements replace_host;
462 replace_host.SetHostStr(aliased_host);
463 url = url.ReplaceComponents(replace_host);
464
465 // Load a page from the test host in a new tab.
466 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19467 browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
Fergal Dalyffa9bba2020-01-27 23:45:02468 ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
[email protected]6703c15d2014-04-03 19:03:45469
470 // Sanity check that there's no bleeding between the extension and the tab.
471 content::WebContents* tab_web_contents =
472 browser()->tab_strip_model()->GetActiveWebContents();
473 EXPECT_EQ(url, tab_web_contents->GetVisibleURL());
rdevlin.cronin6ae04a012015-04-03 20:19:40474 EXPECT_FALSE(pm->GetExtensionForWebContents(tab_web_contents))
[email protected]6703c15d2014-04-03 19:03:45475 << "Non-extension content must not have an associated extension";
rdevlin.cronin6ae04a012015-04-03 20:19:40476 ASSERT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
[email protected]6703c15d2014-04-03 19:03:45477 content::WebContents* extension_web_contents =
rdevlin.cronin6ae04a012015-04-03 20:19:40478 content::WebContents::FromRenderFrameHost(
479 *pm->GetRenderFrameHostsForExtension(extension->id()).begin());
[email protected]6703c15d2014-04-03 19:03:45480 EXPECT_TRUE(extension_web_contents->GetSiteInstance() !=
481 tab_web_contents->GetSiteInstance());
482 EXPECT_TRUE(pm->GetSiteInstanceForURL(extension->url()) !=
483 tab_web_contents->GetSiteInstance());
484 EXPECT_TRUE(pm->GetBackgroundHostForExtension(extension->id()));
485}
486
robcdcc4b82015-12-06 12:39:45487IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest, NoBackgroundPage) {
488 ASSERT_TRUE(embedded_test_server()->Start());
489
490 ProcessManager* pm = ProcessManager::Get(profile());
491 const Extension* extension =
492 LoadExtension(test_data_dir_.AppendASCII("api_test")
493 .AppendASCII("messaging")
494 .AppendASCII("connect_nobackground"));
495 ASSERT_TRUE(extension);
496
497 // The extension has no background page.
498 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
499
500 // Start in a non-extension process, then navigate to an extension process.
501 NavigateToURL(embedded_test_server()->GetURL("/empty.html"));
502 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
503
504 const GURL extension_url = extension->url().Resolve("manifest.json");
505 NavigateToURL(extension_url);
506 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
507
508 NavigateToURL(GURL("about:blank"));
509 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
510
511 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19512 browser(), extension_url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
Fergal Dalyffa9bba2020-01-27 23:45:02513 ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
robcdcc4b82015-12-06 12:39:45514 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
515}
516
517// Tests whether frames are correctly classified. Non-extension frames should
518// never appear in the list. Top-level extension frames should always appear.
519// Child extension frames should only appear if it is hosted in an extension
520// process (i.e. if the top-level frame is an extension page, or if OOP frames
521// are enabled for extensions).
mgiucaf5c1ca22017-02-20 02:16:21522// Disabled due to flake: https://crbug.com/693287.
523IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
524 DISABLED_FrameClassification) {
robcdcc4b82015-12-06 12:39:45525 const Extension* extension1 = CreateExtension("Extension 1", false);
526 const Extension* extension2 = CreateExtension("Extension 2", true);
527 embedded_test_server()->ServeFilesFromDirectory(extension1->path());
528 ASSERT_TRUE(embedded_test_server()->Start());
529
530 const GURL kExt1TwoFramesUrl(extension1->url().Resolve("two_iframes.html"));
531 const GURL kExt1EmptyUrl(extension1->url().Resolve("empty.html"));
532 const GURL kExt2TwoFramesUrl(extension2->url().Resolve("two_iframes.html"));
533 const GURL kExt2EmptyUrl(extension2->url().Resolve("empty.html"));
534
535 ProcessManager* pm = ProcessManager::Get(profile());
536
537 // 1 background page + 1 frame in background page from Extension 2.
538 BackgroundPageWatcher(pm, extension2).WaitForOpen();
539 EXPECT_EQ(2u, pm->GetAllFrames().size());
540 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
541 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
542
543 ExecuteScriptInBackgroundPageNoWait(extension2->id(),
544 "setTimeout(window.close, 0)");
545 BackgroundPageWatcher(pm, extension2).WaitForClose();
546 EXPECT_EQ(0u, pm->GetAllFrames().size());
547 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
548
549 NavigateToURL(embedded_test_server()->GetURL("/two_iframes.html"));
550 EXPECT_EQ(0u, pm->GetAllFrames().size());
551
552 content::WebContents* tab =
553 browser()->tab_strip_model()->GetActiveWebContents();
554
555 // Tests extension frames in non-extension page.
rob57afbca2015-12-21 17:52:11556 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", kExt1EmptyUrl));
naskoabed2a52017-05-03 05:10:17557 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
558 EXPECT_EQ(1u, pm->GetAllFrames().size());
robcdcc4b82015-12-06 12:39:45559
rob57afbca2015-12-21 17:52:11560 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame2", kExt2EmptyUrl));
naskoabed2a52017-05-03 05:10:17561 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
562 EXPECT_EQ(2u, pm->GetAllFrames().size());
robcdcc4b82015-12-06 12:39:45563
564 // Tests non-extension page in extension frame.
565 NavigateToURL(kExt1TwoFramesUrl);
566 // 1 top-level + 2 child frames from Extension 1.
567 EXPECT_EQ(3u, pm->GetAllFrames().size());
568 EXPECT_EQ(3u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
569 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
570
rob57afbca2015-12-21 17:52:11571 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1",
572 embedded_test_server()
573 ->GetURL("/empty.html")));
robcdcc4b82015-12-06 12:39:45574 // 1 top-level + 1 child frame from Extension 1.
575 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
576 EXPECT_EQ(2u, pm->GetAllFrames().size());
577
rob57afbca2015-12-21 17:52:11578 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", kExt1EmptyUrl));
robcdcc4b82015-12-06 12:39:45579 // 1 top-level + 2 child frames from Extension 1.
580 EXPECT_EQ(3u, pm->GetAllFrames().size());
581 EXPECT_EQ(3u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
582
583 // Load a frame from another extension.
rob57afbca2015-12-21 17:52:11584 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", kExt2EmptyUrl));
robcdcc4b82015-12-06 12:39:45585 // 1 top-level + 1 child frame from Extension 1,
586 // 1 child frame from Extension 2.
naskoabed2a52017-05-03 05:10:17587 EXPECT_EQ(3u, pm->GetAllFrames().size());
robcdcc4b82015-12-06 12:39:45588 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
naskoabed2a52017-05-03 05:10:17589 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
robcdcc4b82015-12-06 12:39:45590
591 // Destroy all existing frames by navigating to another extension.
592 NavigateToURL(extension2->url().Resolve("empty.html"));
593 EXPECT_EQ(1u, pm->GetAllFrames().size());
594 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
595 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
596
597 // Test about:blank and about:srcdoc child frames.
598 NavigateToURL(extension2->url().Resolve("srcdoc_iframe.html"));
599 // 1 top-level frame + 1 child frame from Extension 2.
600 EXPECT_EQ(2u, pm->GetAllFrames().size());
601 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
602
603 NavigateToURL(extension2->url().Resolve("blank_iframe.html"));
604 // 1 top-level frame + 1 child frame from Extension 2.
605 EXPECT_EQ(2u, pm->GetAllFrames().size());
606 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
607
608 // Sandboxed frames are not viewed as extension frames.
rob57afbca2015-12-21 17:52:11609 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame0",
610 extension2->url()
611 .Resolve("sandboxed.html")));
robcdcc4b82015-12-06 12:39:45612 // 1 top-level frame from Extension 2.
613 EXPECT_EQ(1u, pm->GetAllFrames().size());
614 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
615
616 NavigateToURL(extension2->url().Resolve("sandboxed.html"));
617 EXPECT_EQ(0u, pm->GetAllFrames().size());
618 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
619
620 // Test nested frames (same extension).
621 NavigateToURL(kExt2TwoFramesUrl);
622 // 1 top-level + 2 child frames from Extension 2.
623 EXPECT_EQ(3u, pm->GetAllFrames().size());
624 EXPECT_EQ(3u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
625
rob57afbca2015-12-21 17:52:11626 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", kExt2TwoFramesUrl));
robcdcc4b82015-12-06 12:39:45627 // 1 top-level + 2 child frames from Extension 1,
628 // 2 child frames in frame1 from Extension 2.
629 EXPECT_EQ(5u, pm->GetAllFrames().size());
630 EXPECT_EQ(5u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
631
632 // The extension frame from the other extension should not be classified as an
633 // extension (unless out-of-process frames are enabled).
rob57afbca2015-12-21 17:52:11634 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", kExt1EmptyUrl));
robcdcc4b82015-12-06 12:39:45635 // 1 top-level + 1 child frames from Extension 2,
636 // 1 child frame from Extension 1.
naskoabed2a52017-05-03 05:10:17637 EXPECT_EQ(3u, pm->GetAllFrames().size());
638 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
robcdcc4b82015-12-06 12:39:45639 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
640
rob57afbca2015-12-21 17:52:11641 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame2", kExt1TwoFramesUrl));
robcdcc4b82015-12-06 12:39:45642 // 1 top-level + 1 child frames from Extension 2,
643 // 1 child frame + 2 child frames in frame2 from Extension 1.
naskoabed2a52017-05-03 05:10:17644 EXPECT_EQ(5u, pm->GetAllFrames().size());
645 EXPECT_EQ(4u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
robcdcc4b82015-12-06 12:39:45646 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
647
648 // Crash tab where the top-level frame is an extension frame.
649 content::CrashTab(tab);
650 EXPECT_EQ(0u, pm->GetAllFrames().size());
651 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
652 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
653
654 // Now load an extension page and a non-extension page...
655 ui_test_utils::NavigateToURLWithDisposition(
nick3b04f322016-08-31 19:29:19656 browser(), kExt1EmptyUrl, WindowOpenDisposition::NEW_BACKGROUND_TAB,
Fergal Dalyffa9bba2020-01-27 23:45:02657 ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
robcdcc4b82015-12-06 12:39:45658 NavigateToURL(embedded_test_server()->GetURL("/two_iframes.html"));
659 EXPECT_EQ(1u, pm->GetAllFrames().size());
660
661 // ... load an extension frame in the non-extension process
rob57afbca2015-12-21 17:52:11662 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", kExt1EmptyUrl));
naskoabed2a52017-05-03 05:10:17663 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
robcdcc4b82015-12-06 12:39:45664
665 // ... and take down the tab. The extension process is not part of the tab,
666 // so it should be kept alive (minus the frames that died).
667 content::CrashTab(tab);
668 EXPECT_EQ(1u, pm->GetAllFrames().size());
669 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
670}
671
rockote5703292015-09-28 23:23:09672// Verify correct keepalive count behavior on network request events.
673// Regression test for http://crbug.com/535716.
Takumi Fujimoto574147e02019-12-03 23:26:05674// Disabled on Linux for flakiness: http://crbug.com/1030435.
675#if defined(OS_LINUX)
676#define MAYBE_KeepaliveOnNetworkRequest DISABLED_KeepaliveOnNetworkRequest
677#else
678#define MAYBE_KeepaliveOnNetworkRequest KeepaliveOnNetworkRequest
679#endif
680IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
681 MAYBE_KeepaliveOnNetworkRequest) {
rockote5703292015-09-28 23:23:09682 // Load an extension with a lazy background page.
683 scoped_refptr<const Extension> extension =
684 LoadExtension(test_data_dir_.AppendASCII("api_test")
685 .AppendASCII("lazy_background_page")
686 .AppendASCII("broadcast_event"));
687 ASSERT_TRUE(extension.get());
688
689 ProcessManager* pm = ProcessManager::Get(profile());
690 ProcessManager::FrameSet frames =
691 pm->GetRenderFrameHostsForExtension(extension->id());
692 ASSERT_EQ(1u, frames.size());
693
694 // Keepalive count at this point is unpredictable as there may be an
695 // outstanding event dispatch. We use the current keepalive count as a
696 // reliable baseline for future expectations.
wez714dde12017-02-14 22:26:03697 EXPECT_TRUE(BackgroundInfo::HasLazyBackgroundPage(extension.get()));
rockote5703292015-09-28 23:23:09698 int baseline_keepalive = pm->GetLazyKeepaliveCount(extension.get());
David Bertoni3e1e9fa2018-08-29 20:39:30699 size_t baseline_activities_count =
700 pm->GetLazyKeepaliveActivities(extension.get()).size();
wez714dde12017-02-14 22:26:03701 EXPECT_LE(0, baseline_keepalive);
David Bertoni3e1e9fa2018-08-29 20:39:30702 EXPECT_LE(0u, baseline_activities_count);
rockote5703292015-09-28 23:23:09703
704 // Simulate some network events. This test assumes no other network requests
705 // are pending, i.e., that there are no conflicts with the fake request IDs
706 // we're using. This should be a safe assumption because LoadExtension should
707 // wait for loads to complete, and we don't run the message loop otherwise.
708 content::RenderFrameHost* frame_host = *frames.begin();
David Bertoni3e1e9fa2018-08-29 20:39:30709 constexpr int kRequestId = 1;
710 const auto activity =
711 std::make_pair(Activity::NETWORK, base::NumberToString(kRequestId));
712
713 pm->OnNetworkRequestStarted(frame_host, kRequestId);
rockote5703292015-09-28 23:23:09714 EXPECT_EQ(baseline_keepalive + 1, pm->GetLazyKeepaliveCount(extension.get()));
David Bertoni3e1e9fa2018-08-29 20:39:30715 EXPECT_EQ(1u,
716 pm->GetLazyKeepaliveActivities(extension.get()).count(activity));
717 pm->OnNetworkRequestDone(frame_host, kRequestId);
rockote5703292015-09-28 23:23:09718 EXPECT_EQ(baseline_keepalive, pm->GetLazyKeepaliveCount(extension.get()));
David Bertoni3e1e9fa2018-08-29 20:39:30719 EXPECT_EQ(0u,
720 pm->GetLazyKeepaliveActivities(extension.get()).count(activity));
rockote5703292015-09-28 23:23:09721
722 // Simulate only a request completion for this ID and ensure it doesn't result
723 // in keepalive decrement.
724 pm->OnNetworkRequestDone(frame_host, 2);
725 EXPECT_EQ(baseline_keepalive, pm->GetLazyKeepaliveCount(extension.get()));
David Bertoni3e1e9fa2018-08-29 20:39:30726 EXPECT_EQ(baseline_activities_count,
727 pm->GetLazyKeepaliveActivities(extension.get()).size());
rockote5703292015-09-28 23:23:09728}
729
nick92b04582016-04-08 02:54:04730IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest, ExtensionProcessReuse) {
731 const size_t kNumExtensions = 3;
732 content::RenderProcessHost::SetMaxRendererProcessCount(kNumExtensions - 1);
733 ProcessManager* pm = ProcessManager::Get(profile());
734
735 std::set<int> processes;
736 std::set<const Extension*> installed_extensions;
737
738 // Create 3 extensions, which is more than the process limit.
739 for (int i = 1; i <= static_cast<int>(kNumExtensions); ++i) {
740 const Extension* extension =
741 CreateExtension(base::StringPrintf("Extension %d", i), true);
742 installed_extensions.insert(extension);
743 ExtensionHost* extension_host =
744 pm->GetBackgroundHostForExtension(extension->id());
745
746 EXPECT_EQ(extension->url(),
747 extension_host->host_contents()->GetSiteInstance()->GetSiteURL());
748
749 processes.insert(extension_host->render_process_host()->GetID());
750 }
751
752 EXPECT_EQ(kNumExtensions, installed_extensions.size());
753
Nick Carterbf6264a52018-04-06 02:39:33754 if (!IsExtensionProcessSharingAllowed()) {
nick92b04582016-04-08 02:54:04755 EXPECT_EQ(kNumExtensions, processes.size()) << "Extension process reuse is "
Nick Carterbf6264a52018-04-06 02:39:33756 "expected to be disabled.";
nick92b04582016-04-08 02:54:04757 } else {
758 EXPECT_LT(processes.size(), kNumExtensions)
759 << "Expected extension process reuse, but none happened.";
760 }
761
762 // Interact with each extension background page by setting and reading back
763 // the cookie. This would fail for one of the two extensions in a shared
764 // process, if that process is locked to a single origin. This is a regression
765 // test for http://crbug.com/600441.
766 for (const Extension* extension : installed_extensions) {
767 content::DOMMessageQueue queue;
768 ExecuteScriptInBackgroundPageNoWait(
769 extension->id(),
770 "document.cookie = 'extension_cookie';"
771 "window.domAutomationController.send(document.cookie);");
772 std::string message;
773 ASSERT_TRUE(queue.WaitForMessage(&message));
774 EXPECT_EQ(message, "\"extension_cookie\"");
775 }
776}
777
alexmos4bfdc922016-09-15 22:40:54778// Test that navigations to blob: and filesystem: URLs with extension origins
779// are disallowed when initiated from non-extension processes. See
780// https://crbug.com/645028 and https://crbug.com/644426.
781IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
Alex Moshchukd2a81fe22018-05-07 17:35:44782 NestedURLNavigationsToExtensionBlocked) {
alexmos4bfdc922016-09-15 22:40:54783 // Disabling web security is necessary to test the browser enforcement;
784 // without it, the loads in this test would be blocked by
785 // SecurityOrigin::canDisplay() as invalid local resource loads.
786 PrefService* prefs = browser()->profile()->GetPrefs();
787 prefs->SetBoolean(prefs::kWebKitWebSecurityEnabled, false);
788
789 // Create a simple extension without a background page.
790 const Extension* extension = CreateExtension("Extension", false);
791 embedded_test_server()->ServeFilesFromDirectory(extension->path());
792 ASSERT_TRUE(embedded_test_server()->Start());
793
794 // Navigate main tab to a web page with two web iframes. There should be no
795 // extension frames yet.
796 NavigateToURL(embedded_test_server()->GetURL("/two_iframes.html"));
797 ProcessManager* pm = ProcessManager::Get(profile());
798 EXPECT_EQ(0u, pm->GetAllFrames().size());
799 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
800
801 content::WebContents* tab =
802 browser()->tab_strip_model()->GetActiveWebContents();
803
naskoabed2a52017-05-03 05:10:17804 // Navigate first subframe to an extension URL. This will go into a new
805 // extension process.
Daniel Cheng4ebba552018-07-06 21:43:16806 const GURL extension_empty_resource(extension->url().Resolve("empty.html"));
807 EXPECT_TRUE(
808 content::NavigateIframeToURL(tab, "frame1", extension_empty_resource));
naskoabed2a52017-05-03 05:10:17809 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
810 EXPECT_EQ(1u, pm->GetAllFrames().size());
alexmos4bfdc922016-09-15 22:40:54811
812 content::RenderFrameHost* main_frame = tab->GetMainFrame();
813 content::RenderFrameHost* extension_frame = ChildFrameAt(main_frame, 0);
814
Daniel Cheng4ebba552018-07-06 21:43:16815 // Ideally, this would be a GURL, but it's easier to compose the rest of the
816 // URLs if this is a std::string. Meh.
817 const std::string extension_base_url =
818 base::StrCat({"chrome-extension://", extension->id()});
819 const GURL extension_blob_url =
820 GURL(base::StrCat({"blob:", extension_base_url, "/some-guid"}));
821 const GURL extension_file_system_url =
822 GURL(base::StrCat({"filesystem:", extension_base_url, "/some-path"}));
823 const GURL extension_url =
824 GURL(base::StrCat({extension_base_url, "/some-path"}));
825
nick2a8ba8c2016-10-03 18:51:39826 // Validate that permissions have been granted for the extension scheme
827 // to the process of the extension iframe.
828 content::ChildProcessSecurityPolicy* policy =
829 content::ChildProcessSecurityPolicy::GetInstance();
Daniel Cheng4ebba552018-07-06 21:43:16830 EXPECT_TRUE(policy->CanRequestURL(extension_frame->GetProcess()->GetID(),
831 extension_blob_url));
832 EXPECT_TRUE(policy->CanRequestURL(main_frame->GetProcess()->GetID(),
833 extension_blob_url));
834 EXPECT_TRUE(policy->CanRequestURL(extension_frame->GetProcess()->GetID(),
835 extension_file_system_url));
836 EXPECT_TRUE(policy->CanRequestURL(main_frame->GetProcess()->GetID(),
837 extension_file_system_url));
838 EXPECT_TRUE(policy->CanRequestURL(extension_frame->GetProcess()->GetID(),
839 extension_url));
840 EXPECT_TRUE(
841 policy->CanRequestURL(main_frame->GetProcess()->GetID(), extension_url));
nick2a8ba8c2016-10-03 18:51:39842
Daniel Cheng4ebba552018-07-06 21:43:16843 EXPECT_TRUE(policy->CanCommitURL(extension_frame->GetProcess()->GetID(),
844 extension_blob_url));
845 EXPECT_FALSE(policy->CanCommitURL(main_frame->GetProcess()->GetID(),
846 extension_blob_url));
847 EXPECT_TRUE(policy->CanCommitURL(extension_frame->GetProcess()->GetID(),
848 extension_file_system_url));
849 EXPECT_FALSE(policy->CanCommitURL(main_frame->GetProcess()->GetID(),
850 extension_file_system_url));
851 EXPECT_TRUE(policy->CanCommitURL(extension_frame->GetProcess()->GetID(),
852 extension_url));
853 EXPECT_FALSE(
854 policy->CanCommitURL(main_frame->GetProcess()->GetID(), extension_url));
nick2a8ba8c2016-10-03 18:51:39855
alexmos4bfdc922016-09-15 22:40:54856 // Open a new about:blank popup from main frame. This should stay in the web
857 // process.
858 content::WebContents* popup =
859 OpenPopup(main_frame, GURL(url::kAboutBlankURL));
860 EXPECT_NE(popup, tab);
861 ASSERT_EQ(2, browser()->tab_strip_model()->count());
naskoabed2a52017-05-03 05:10:17862 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
863 EXPECT_EQ(1u, pm->GetAllFrames().size());
alexmos4bfdc922016-09-15 22:40:54864
865 // Create valid blob and filesystem URLs in the extension's origin.
866 url::Origin extension_origin(extension_frame->GetLastCommittedOrigin());
867 GURL blob_url(CreateBlobURL(extension_frame, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:46868 EXPECT_EQ(extension_origin, url::Origin::Create(blob_url));
alexmos4bfdc922016-09-15 22:40:54869 GURL filesystem_url(CreateFileSystemURL(extension_frame, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:46870 EXPECT_EQ(extension_origin, url::Origin::Create(filesystem_url));
alexmos4bfdc922016-09-15 22:40:54871
872 // Navigate the popup to each nested URL with extension origin.
873 GURL nested_urls[] = {blob_url, filesystem_url};
Avi Drissman5f0fb8c2018-12-25 23:20:49874 for (size_t i = 0; i < base::size(nested_urls); i++) {
alexmos4bfdc922016-09-15 22:40:54875 EXPECT_TRUE(ExecuteScript(
876 popup, "location.href = '" + nested_urls[i].spec() + "';"));
Alex Moshchukd2a81fe22018-05-07 17:35:44877
878 // If a navigation was started, wait for it to finish. This can't just use
879 // a TestNavigationObserver, since after https://crbug.com/811558 blob: and
880 // filesystem: navigations have different failure modes: blob URLs will be
881 // blocked on the browser side, and filesystem URLs on the renderer side,
882 // without notifying the browser. Since these navigations are scheduled in
883 // Blink, run a dummy script on the renderer to ensure that the navigation,
884 // if started, has made it to the browser process before we call
885 // WaitForLoadStop().
886 EXPECT_TRUE(ExecuteScript(popup, "true"));
887 EXPECT_TRUE(content::WaitForLoadStop(popup));
alexmos4bfdc922016-09-15 22:40:54888
889 // This is a top-level navigation that should be blocked since it
890 // originates from a non-extension process. Ensure that the error page
891 // doesn't commit an extension URL or origin.
892 EXPECT_NE(nested_urls[i], popup->GetLastCommittedURL());
893 EXPECT_FALSE(extension_origin.IsSameOriginWith(
894 popup->GetMainFrame()->GetLastCommittedOrigin()));
895 EXPECT_NE("foo", GetTextContent(popup->GetMainFrame()));
896
naskoabed2a52017-05-03 05:10:17897 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
898 EXPECT_EQ(1u, pm->GetAllFrames().size());
alexmos4bfdc922016-09-15 22:40:54899 }
900
Alex Moshchukd2a81fe22018-05-07 17:35:44901 // Close the popup. It won't be needed anymore, and bringing the original
902 // page back into foreground makes the remainder of this test a bit faster.
903 popup->Close();
904
alexmos4bfdc922016-09-15 22:40:54905 // Navigate second subframe to each nested URL from the main frame (i.e.,
nick6cfe5c72017-05-22 22:00:42906 // from non-extension process). These should be canceled.
Avi Drissman5f0fb8c2018-12-25 23:20:49907 for (size_t i = 0; i < base::size(nested_urls); i++) {
alexmos4bfdc922016-09-15 22:40:54908 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame2", nested_urls[i]));
909 content::RenderFrameHost* second_frame = ChildFrameAt(main_frame, 1);
nick6cfe5c72017-05-22 22:00:42910
911 EXPECT_NE(nested_urls[i], second_frame->GetLastCommittedURL());
912 EXPECT_FALSE(extension_origin.IsSameOriginWith(
913 second_frame->GetLastCommittedOrigin()));
914 EXPECT_NE("foo", GetTextContent(second_frame));
915 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
916 EXPECT_EQ(1u, pm->GetAllFrames().size());
917
alexmos1c3da4f2016-11-03 06:06:11918 EXPECT_TRUE(
919 content::NavigateIframeToURL(tab, "frame2", GURL(url::kAboutBlankURL)));
alexmos4bfdc922016-09-15 22:40:54920 }
Alex Moshchukd2a81fe22018-05-07 17:35:44921}
Jochen Eisinger68fb0642018-01-17 21:56:45922
Alex Moshchukd8fb84f2019-06-07 22:53:55923// Check that browser-side restrictions on extension blob URLs allow
Alex Moshchukd2a81fe22018-05-07 17:35:44924// navigations that will result in downloads. See https://crbug.com/714373.
925IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
Alex Moshchukd8fb84f2019-06-07 22:53:55926 BlobURLDownloadsToExtensionAllowed) {
Alex Moshchukd2a81fe22018-05-07 17:35:44927 // Disabling web security is necessary to test the browser enforcement;
928 // without it, the loads in this test would be blocked by
929 // SecurityOrigin::CanDisplay() as invalid local resource loads.
930 PrefService* prefs = browser()->profile()->GetPrefs();
931 prefs->SetBoolean(prefs::kWebKitWebSecurityEnabled, false);
932
933 // Create a simple extension without a background page.
934 const Extension* extension = CreateExtension("Extension", false);
935 embedded_test_server()->ServeFilesFromDirectory(extension->path());
936 ASSERT_TRUE(embedded_test_server()->Start());
937
938 // Navigate main tab to a web page an iframe. There should be no extension
939 // frames yet.
940 NavigateToURL(embedded_test_server()->GetURL("/blank_iframe.html"));
941 ProcessManager* pm = ProcessManager::Get(profile());
942 EXPECT_EQ(0u, pm->GetAllFrames().size());
943 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
944
945 content::WebContents* tab =
946 browser()->tab_strip_model()->GetActiveWebContents();
947
948 // Navigate iframe to an extension URL.
949 const GURL extension_url(extension->url().Resolve("empty.html"));
950 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame0", extension_url));
951 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
952 EXPECT_EQ(1u, pm->GetAllFrames().size());
953
954 content::RenderFrameHost* main_frame = tab->GetMainFrame();
955 content::RenderFrameHost* extension_frame = ChildFrameAt(main_frame, 0);
956
Alex Moshchukd8fb84f2019-06-07 22:53:55957 // Create a valid blob URL in the extension's origin.
Alex Moshchukd2a81fe22018-05-07 17:35:44958 url::Origin extension_origin(extension_frame->GetLastCommittedOrigin());
959 GURL blob_url(CreateBlobURL(extension_frame, "foo"));
960 EXPECT_EQ(extension_origin, url::Origin::Create(blob_url));
Alex Moshchukd2a81fe22018-05-07 17:35:44961
Alex Moshchukd8fb84f2019-06-07 22:53:55962 // Check that extension blob URLs still can be downloaded via an HTML anchor
963 // tag with the download attribute (i.e., <a download>) (which starts out as
964 // a top-level navigation).
Jochen Eisinger68fb0642018-01-17 21:56:45965 PermissionRequestManager* permission_request_manager =
Alex Moshchukd2a81fe22018-05-07 17:35:44966 PermissionRequestManager::FromWebContents(tab);
Jochen Eisinger68fb0642018-01-17 21:56:45967 permission_request_manager->set_auto_response_for_test(
968 PermissionRequestManager::ACCEPT_ALL);
Jochen Eisinger68fb0642018-01-17 21:56:45969
Alex Moshchukd8fb84f2019-06-07 22:53:55970 content::DownloadTestObserverTerminal observer(
971 content::BrowserContext::GetDownloadManager(profile()), 1,
972 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
973 std::string script = base::StringPrintf(
974 R"(var anchor = document.createElement('a');
975 anchor.href = '%s';
976 anchor.download = '';
977 anchor.click();)",
978 blob_url.spec().c_str());
979 EXPECT_TRUE(ExecuteScript(tab, script));
980 observer.WaitForFinished();
981 EXPECT_EQ(1u,
982 observer.NumDownloadsSeenInState(download::DownloadItem::COMPLETE));
Jochen Eisinger68fb0642018-01-17 21:56:45983
Alex Moshchukd8fb84f2019-06-07 22:53:55984 // This is a top-level navigation that should have resulted in a download.
985 // Ensure that the tab stayed at its original location.
986 EXPECT_NE(blob_url, tab->GetLastCommittedURL());
987 EXPECT_FALSE(
988 extension_origin.IsSameOriginWith(main_frame->GetLastCommittedOrigin()));
989 EXPECT_NE("foo", GetTextContent(main_frame));
990
991 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
992 EXPECT_EQ(1u, pm->GetAllFrames().size());
alexmos4bfdc922016-09-15 22:40:54993}
994
995// Test that navigations to blob: and filesystem: URLs with extension origins
Charlie Reis76147902018-04-26 01:21:07996// are disallowed in subframes when initiated from non-extension processes, even
997// when the main frame lies about its origin. See https://crbug.com/836858.
998IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
999 NestedURLNavigationsToExtensionBlockedInSubframe) {
1000 // Disabling web security is necessary to test the browser enforcement;
1001 // without it, the loads in this test would be blocked by
1002 // SecurityOrigin::canDisplay() as invalid local resource loads.
1003 PrefService* prefs = browser()->profile()->GetPrefs();
1004 prefs->SetBoolean(prefs::kWebKitWebSecurityEnabled, false);
1005
1006 // Create a simple extension without a background page.
1007 const Extension* extension = CreateExtension("Extension", false);
1008 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1009 ASSERT_TRUE(embedded_test_server()->Start());
1010
1011 // Navigate main tab to a web page with two web iframes. There should be no
1012 // extension frames yet.
1013 NavigateToURL(embedded_test_server()->GetURL("/two_iframes.html"));
1014 ProcessManager* pm = ProcessManager::Get(profile());
1015 EXPECT_EQ(0u, pm->GetAllFrames().size());
1016 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1017
1018 content::WebContents* tab =
1019 browser()->tab_strip_model()->GetActiveWebContents();
1020
1021 // Navigate first subframe to an extension URL. This will go into a new
1022 // extension process.
1023 const GURL extension_url(extension->url().Resolve("empty.html"));
1024 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", extension_url));
1025 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1026 EXPECT_EQ(1u, pm->GetAllFrames().size());
1027
1028 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1029 content::RenderFrameHost* extension_frame = ChildFrameAt(main_frame, 0);
1030
1031 // Create valid blob and filesystem URLs in the extension's origin.
1032 url::Origin extension_origin(extension_frame->GetLastCommittedOrigin());
1033 GURL blob_url(CreateBlobURL(extension_frame, "foo"));
1034 EXPECT_EQ(extension_origin, url::Origin::Create(blob_url));
1035 GURL filesystem_url(CreateFileSystemURL(extension_frame, "foo"));
1036 EXPECT_EQ(extension_origin, url::Origin::Create(filesystem_url));
1037
1038 // Suppose that the main frame's origin incorrectly claims it is an extension,
1039 // even though it is not in an extension process. This used to bypass the
1040 // checks in ExtensionNavigationThrottle.
1041 OverrideLastCommittedOrigin(main_frame, extension_origin);
1042
1043 // Navigate second subframe to each nested URL from the main frame (i.e.,
1044 // from non-extension process). These should be canceled.
1045 GURL nested_urls[] = {blob_url, filesystem_url};
Avi Drissman5f0fb8c2018-12-25 23:20:491046 for (size_t i = 0; i < base::size(nested_urls); i++) {
Charlie Reis76147902018-04-26 01:21:071047 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame2", nested_urls[i]));
1048 content::RenderFrameHost* second_frame = ChildFrameAt(main_frame, 1);
1049
1050 EXPECT_NE(nested_urls[i], second_frame->GetLastCommittedURL());
1051 EXPECT_FALSE(extension_origin.IsSameOriginWith(
1052 second_frame->GetLastCommittedOrigin()));
1053 EXPECT_NE("foo", GetTextContent(second_frame));
1054 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1055 EXPECT_EQ(1u, pm->GetAllFrames().size());
1056
1057 EXPECT_TRUE(
1058 content::NavigateIframeToURL(tab, "frame2", GURL(url::kAboutBlankURL)));
1059 }
1060}
1061
1062// Test that navigations to blob: and filesystem: URLs with extension origins
alexmos4bfdc922016-09-15 22:40:541063// are allowed when initiated from extension processes. See
1064// https://crbug.com/645028 and https://crbug.com/644426.
1065IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
1066 NestedURLNavigationsToExtensionAllowed) {
1067 // Create a simple extension without a background page.
1068 const Extension* extension = CreateExtension("Extension", false);
1069 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1070 ASSERT_TRUE(embedded_test_server()->Start());
1071
1072 // Navigate main tab to an extension URL with a blank subframe.
1073 const GURL extension_url(extension->url().Resolve("blank_iframe.html"));
1074 NavigateToURL(extension_url);
1075 ProcessManager* pm = ProcessManager::Get(profile());
1076 EXPECT_EQ(2u, pm->GetAllFrames().size());
1077 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1078
1079 content::WebContents* tab =
1080 browser()->tab_strip_model()->GetActiveWebContents();
1081 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1082
1083 // Create blob and filesystem URLs in the extension's origin.
1084 url::Origin extension_origin(main_frame->GetLastCommittedOrigin());
1085 GURL blob_url(CreateBlobURL(main_frame, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:461086 EXPECT_EQ(extension_origin, url::Origin::Create(blob_url));
alexmos4bfdc922016-09-15 22:40:541087 GURL filesystem_url(CreateFileSystemURL(main_frame, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:461088 EXPECT_EQ(extension_origin, url::Origin::Create(filesystem_url));
alexmos4bfdc922016-09-15 22:40:541089
1090 // From the main frame, navigate its subframe to each nested URL. This
1091 // should be allowed and should stay in the extension process.
1092 GURL nested_urls[] = {blob_url, filesystem_url};
Avi Drissman5f0fb8c2018-12-25 23:20:491093 for (size_t i = 0; i < base::size(nested_urls); i++) {
alexmos4bfdc922016-09-15 22:40:541094 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame0", nested_urls[i]));
1095 content::RenderFrameHost* child = ChildFrameAt(main_frame, 0);
1096 EXPECT_EQ(nested_urls[i], child->GetLastCommittedURL());
1097 EXPECT_EQ(extension_origin, child->GetLastCommittedOrigin());
1098 EXPECT_EQ("foo", GetTextContent(child));
1099 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1100 EXPECT_EQ(2u, pm->GetAllFrames().size());
1101 }
1102
Mustafa Emre Acer54400202018-04-30 19:38:111103 // From the main frame, create a blank popup and navigate it to the nested
1104 // blob URL. This should also be allowed, since the navigation originated from
1105 // an extension process.
1106 {
alexmos4bfdc922016-09-15 22:40:541107 content::WebContents* popup =
1108 OpenPopup(main_frame, GURL(url::kAboutBlankURL));
1109 EXPECT_NE(popup, tab);
1110
1111 content::TestNavigationObserver observer(popup);
1112 EXPECT_TRUE(ExecuteScript(
Mustafa Emre Acer54400202018-04-30 19:38:111113 popup, "location.href = '" + nested_urls[0].spec() + "';"));
alexmos4bfdc922016-09-15 22:40:541114 observer.Wait();
1115
Mustafa Emre Acer54400202018-04-30 19:38:111116 EXPECT_EQ(nested_urls[0], popup->GetLastCommittedURL());
alexmos4bfdc922016-09-15 22:40:541117 EXPECT_EQ(extension_origin,
1118 popup->GetMainFrame()->GetLastCommittedOrigin());
1119 EXPECT_EQ("foo", GetTextContent(popup->GetMainFrame()));
1120
Mustafa Emre Acer54400202018-04-30 19:38:111121 EXPECT_EQ(3u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1122 EXPECT_EQ(3u, pm->GetAllFrames().size());
1123 }
1124
1125 // Same as above, but renderers cannot navigate top frame to filesystem URLs.
1126 // So this will result in a console message.
1127 {
1128 content::WebContents* popup =
1129 OpenPopup(main_frame, GURL(url::kAboutBlankURL));
1130 EXPECT_NE(popup, tab);
1131
1132 content::ConsoleObserverDelegate console_observer(
1133 popup, "Not allowed to navigate top frame to*");
1134 popup->SetDelegate(&console_observer);
1135 EXPECT_TRUE(ExecuteScript(
1136 popup, "location.href = '" + nested_urls[1].spec() + "';"));
1137 console_observer.Wait();
1138
1139 // about:blank URLs can be modified by their opener. In that case their
1140 // effective origin changes to that of the opener, but the page URL remains
1141 // about:blank. Here the popup is being modified by the extension page,
1142 // so it's origin will change to the extension URL.
1143 EXPECT_EQ(GURL(url::kAboutBlankURL), popup->GetLastCommittedURL());
1144 EXPECT_EQ(extension_origin,
1145 popup->GetMainFrame()->GetLastCommittedOrigin());
1146 EXPECT_EQ(std::string(), GetTextContent(popup->GetMainFrame()));
1147
1148 EXPECT_EQ(4u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1149 EXPECT_EQ(4u, pm->GetAllFrames().size());
alexmos4bfdc922016-09-15 22:40:541150 }
1151}
1152
alexmosf7af1352016-10-21 20:00:411153// Test that navigations to blob: and filesystem: URLs with extension origins
1154// are disallowed in an unprivileged, non-guest web process when the extension
1155// origin corresponds to a Chrome app with the "webview" permission. See
1156// https://crbug.com/656752. These requests should still be allowed inside
1157// actual <webview> guest processes created by a Chrome app; this is checked in
1158// WebViewTest.Shim_TestBlobURL.
Nasko Oskov83bb45e2019-01-04 22:46:081159// TODO(alexmos): Enable this test once checks are implemented in the
1160// extensions NavigationThrottle. See https://crbug.com/919194.
alexmosf7af1352016-10-21 20:00:411161IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
Nasko Oskov83bb45e2019-01-04 22:46:081162 DISABLED_NestedURLNavigationsToAppBlocked) {
alexmosf7af1352016-10-21 20:00:411163 // Disabling web security is necessary to test the browser enforcement;
1164 // without it, the loads in this test would be blocked by
1165 // SecurityOrigin::canDisplay() as invalid local resource loads.
1166 PrefService* prefs = browser()->profile()->GetPrefs();
1167 prefs->SetBoolean(prefs::kWebKitWebSecurityEnabled, false);
1168
1169 // Load a simple app that has the "webview" permission. The app will also
1170 // open a <webview> when it's loaded.
1171 ASSERT_TRUE(embedded_test_server()->Start());
1172 base::FilePath dir;
Avi Drissman9098f9002018-05-04 00:11:521173 base::PathService::Get(chrome::DIR_TEST_DATA, &dir);
alexmosf7af1352016-10-21 20:00:411174 dir = dir.AppendASCII("extensions")
1175 .AppendASCII("platform_apps")
1176 .AppendASCII("web_view")
1177 .AppendASCII("simple");
1178 const Extension* app = LoadAndLaunchApp(dir);
1179 EXPECT_TRUE(app->permissions_data()->HasAPIPermission(
1180 extensions::APIPermission::kWebView));
1181
1182 auto app_windows = AppWindowRegistry::Get(browser()->profile())
1183 ->GetAppWindowsForApp(app->id());
1184 EXPECT_EQ(1u, app_windows.size());
1185 content::WebContents* app_tab = (*app_windows.begin())->web_contents();
1186 content::RenderFrameHost* app_rfh = app_tab->GetMainFrame();
1187 url::Origin app_origin(app_rfh->GetLastCommittedOrigin());
Daniel Cheng88186bd52017-10-20 08:14:461188 EXPECT_EQ(url::Origin::Create(app->url()), app_rfh->GetLastCommittedOrigin());
alexmosf7af1352016-10-21 20:00:411189
1190 // Wait for the app's guest WebContents to load.
1191 guest_view::TestGuestViewManager* guest_manager =
1192 static_cast<guest_view::TestGuestViewManager*>(
1193 guest_view::TestGuestViewManager::FromBrowserContext(
1194 browser()->profile()));
1195 content::WebContents* guest = guest_manager->WaitForSingleGuestCreated();
Lucas Furukawa Gadani565f6572017-06-07 22:17:401196 guest_manager->WaitUntilAttached(guest);
alexmosf7af1352016-10-21 20:00:411197
1198 // There should be two extension frames in ProcessManager: the app's main
1199 // page and the background page.
1200 ProcessManager* pm = ProcessManager::Get(profile());
1201 EXPECT_EQ(2u, pm->GetAllFrames().size());
1202 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(app->id()).size());
1203
1204 // Create valid blob and filesystem URLs in the app's origin.
1205 GURL blob_url(CreateBlobURL(app_rfh, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:461206 EXPECT_EQ(app_origin, url::Origin::Create(blob_url));
alexmosf7af1352016-10-21 20:00:411207 GURL filesystem_url(CreateFileSystemURL(app_rfh, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:461208 EXPECT_EQ(app_origin, url::Origin::Create(filesystem_url));
alexmosf7af1352016-10-21 20:00:411209
1210 // Create a new tab, unrelated to the app, and navigate it to a web URL.
1211 chrome::NewTab(browser());
1212 content::WebContents* web_tab =
1213 browser()->tab_strip_model()->GetActiveWebContents();
1214 GURL web_url(embedded_test_server()->GetURL("/title1.html"));
1215 ui_test_utils::NavigateToURL(browser(), web_url);
1216 EXPECT_NE(web_tab, app_tab);
1217 EXPECT_NE(web_tab->GetMainFrame()->GetProcess(), app_rfh->GetProcess());
1218
1219 // The web process shouldn't have permission to request URLs in the app's
1220 // origin, but the guest process should.
1221 content::ChildProcessSecurityPolicy* policy =
1222 content::ChildProcessSecurityPolicy::GetInstance();
Daniel Cheng4ebba552018-07-06 21:43:161223 EXPECT_FALSE(policy->CanRequestURL(
1224 web_tab->GetMainFrame()->GetProcess()->GetID(), app_origin.GetURL()));
1225 EXPECT_TRUE(policy->CanRequestURL(
1226 guest->GetMainFrame()->GetProcess()->GetID(), app_origin.GetURL()));
alexmosf7af1352016-10-21 20:00:411227
1228 // Try navigating the web tab to each nested URL with the app's origin. This
1229 // should be blocked.
1230 GURL nested_urls[] = {blob_url, filesystem_url};
Avi Drissman5f0fb8c2018-12-25 23:20:491231 for (size_t i = 0; i < base::size(nested_urls); i++) {
alexmosf7af1352016-10-21 20:00:411232 content::TestNavigationObserver observer(web_tab);
1233 EXPECT_TRUE(ExecuteScript(
1234 web_tab, "location.href = '" + nested_urls[i].spec() + "';"));
1235 observer.Wait();
1236 EXPECT_NE(nested_urls[i], web_tab->GetLastCommittedURL());
1237 EXPECT_FALSE(app_origin.IsSameOriginWith(
1238 web_tab->GetMainFrame()->GetLastCommittedOrigin()));
1239 EXPECT_NE("foo", GetTextContent(web_tab->GetMainFrame()));
1240 EXPECT_NE(web_tab->GetMainFrame()->GetProcess(), app_rfh->GetProcess());
1241
1242 EXPECT_EQ(2u, pm->GetAllFrames().size());
1243 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(app->id()).size());
1244 }
1245}
1246
alexmos1c3da4f2016-11-03 06:06:111247// Test that a web frame can't navigate a proxy for an extension frame to a
1248// blob/filesystem extension URL. See https://crbug.com/656752.
1249IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
Nick Carterac31d432018-03-16 23:38:351250 NestedURLNavigationsViaProxyBlocked) {
Nick Cartera6102ee2018-03-14 18:49:131251 // Create a simple extension without a background page.
1252 const Extension* extension = CreateExtension("Extension", false);
1253 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1254 ASSERT_TRUE(embedded_test_server()->Start());
1255
1256 // Navigate main tab to an empty web page. There should be no extension
1257 // frames yet.
1258 NavigateToURL(embedded_test_server()->GetURL("/empty.html"));
1259 ProcessManager* pm = ProcessManager::Get(profile());
1260 EXPECT_EQ(0u, pm->GetAllFrames().size());
1261 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1262
1263 content::WebContents* tab =
1264 browser()->tab_strip_model()->GetActiveWebContents();
1265 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1266
1267 // Have the web page navigate the popup to each nested URL with extension
1268 // origin via the window reference it obtained earlier from window.open.
1269 const GURL extension_url(extension->url().Resolve("empty.html"));
1270 for (auto create_function : {&CreateBlobURL, &CreateFileSystemURL}) {
1271 // Setup the test by navigating popup to an extension page. This is allowed
1272 // because it's web accessible.
1273 content::WebContents* popup = OpenPopup(main_frame, extension_url);
1274
1275 // This frame should now be in an extension process.
1276 EXPECT_EQ(1u, pm->GetAllFrames().size());
1277 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1278
1279 // Create a valid blob or filesystem URL in the extension's origin.
1280 GURL nested_url = (*create_function)(popup->GetMainFrame(), "foo");
1281
1282 // Navigate via the proxy to |nested_url|. This should be blocked by
1283 // FilterURL.
1284 EXPECT_TRUE(ExecuteScript(
1285 tab, "window.popup.location.href = '" + nested_url.spec() + "';"));
1286 WaitForLoadStop(popup);
1287
Nick Carterd2e4acf2018-03-30 22:13:291288 // Because the navigation was blocked, the URL doesn't change.
Nick Cartera6102ee2018-03-14 18:49:131289 EXPECT_NE(nested_url, popup->GetLastCommittedURL());
Nick Carterd2e4acf2018-03-30 22:13:291290 EXPECT_EQ(extension_url, popup->GetLastCommittedURL().spec());
Nick Cartera6102ee2018-03-14 18:49:131291 EXPECT_NE("foo", GetTextContent(popup->GetMainFrame()));
Nick Carterd2e4acf2018-03-30 22:13:291292 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1293 EXPECT_EQ(1u, pm->GetAllFrames().size());
1294 popup->Close();
Nick Cartera6102ee2018-03-14 18:49:131295 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1296 EXPECT_EQ(0u, pm->GetAllFrames().size());
1297 }
1298}
1299
Elly Fong-Jonesd47cd17a2019-12-03 16:18:511300// TODO(crbug.com/909570): This test is flaky everywhere.
Nick Cartera6102ee2018-03-14 18:49:131301IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
Elly Fong-Jonesd47cd17a2019-12-03 16:18:511302 DISABLED_NestedURLNavigationsViaNoOpenerPopupBlocked) {
alexmos1c3da4f2016-11-03 06:06:111303 // Create a simple extension without a background page.
1304 const Extension* extension = CreateExtension("Extension", false);
1305 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1306 ASSERT_TRUE(embedded_test_server()->Start());
1307
1308 // Navigate main tab to an empty web page. There should be no extension
1309 // frames yet.
1310 NavigateToURL(embedded_test_server()->GetURL("/empty.html"));
1311 ProcessManager* pm = ProcessManager::Get(profile());
1312 EXPECT_EQ(0u, pm->GetAllFrames().size());
1313 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1314
1315 content::WebContents* tab =
1316 browser()->tab_strip_model()->GetActiveWebContents();
1317 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1318
1319 // Open a new about:blank popup from main frame. This should stay in the web
1320 // process.
1321 content::WebContents* popup =
1322 OpenPopup(main_frame, GURL(url::kAboutBlankURL));
1323 EXPECT_NE(popup, tab);
1324 ASSERT_EQ(2, browser()->tab_strip_model()->count());
1325 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1326 EXPECT_EQ(0u, pm->GetAllFrames().size());
1327
1328 // Navigate popup to an extension page.
1329 const GURL extension_url(extension->url().Resolve("empty.html"));
1330 content::TestNavigationObserver observer(popup);
1331 EXPECT_TRUE(
1332 ExecuteScript(popup, "location.href = '" + extension_url.spec() + "';"));
1333 observer.Wait();
1334 EXPECT_EQ(1u, pm->GetAllFrames().size());
1335 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1336 content::RenderFrameHost* extension_frame = popup->GetMainFrame();
1337
1338 // Create valid blob and filesystem URLs in the extension's origin.
1339 url::Origin extension_origin(extension_frame->GetLastCommittedOrigin());
1340 GURL blob_url(CreateBlobURL(extension_frame, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:461341 EXPECT_EQ(extension_origin, url::Origin::Create(blob_url));
alexmos1c3da4f2016-11-03 06:06:111342 GURL filesystem_url(CreateFileSystemURL(extension_frame, "foo"));
Daniel Cheng88186bd52017-10-20 08:14:461343 EXPECT_EQ(extension_origin, url::Origin::Create(filesystem_url));
alexmos1c3da4f2016-11-03 06:06:111344
Nick Cartera6102ee2018-03-14 18:49:131345 // Attempt opening the nested urls using window.open(url, '', 'noopener').
1346 // This should not be allowed.
alexmos1c3da4f2016-11-03 06:06:111347 GURL nested_urls[] = {blob_url, filesystem_url};
Avi Drissman5f0fb8c2018-12-25 23:20:491348 for (size_t i = 0; i < base::size(nested_urls); i++) {
Nick Cartera6102ee2018-03-14 18:49:131349 content::WebContents* new_popup =
1350 OpenPopupNoOpener(tab->GetMainFrame(), nested_urls[i]);
alexmos1c3da4f2016-11-03 06:06:111351
Nick Cartera6102ee2018-03-14 18:49:131352 // This is a top-level navigation to a local resource, that should be
1353 // blocked by FilterURL, since it originates from a non-extension process.
1354 EXPECT_NE(nested_urls[i], new_popup->GetLastCommittedURL());
1355 EXPECT_EQ("about:blank", new_popup->GetLastCommittedURL().spec());
1356 EXPECT_NE("foo", GetTextContent(new_popup->GetMainFrame()));
alexmos1c3da4f2016-11-03 06:06:111357
1358 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1359 EXPECT_EQ(1u, pm->GetAllFrames().size());
alexmos1c3da4f2016-11-03 06:06:111360
Nick Cartera6102ee2018-03-14 18:49:131361 new_popup->Close();
1362 }
alexmos1c3da4f2016-11-03 06:06:111363}
1364
Nick Carter1cb9c33c2018-03-30 18:40:071365IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
1366 ServerRedirectToNonWebAccessibleResource) {
1367 // Create a simple extension without a background page.
1368 const Extension* extension = CreateExtension("Extension", false);
1369 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1370 ASSERT_TRUE(embedded_test_server()->Start());
1371
1372 // Navigate main tab to an empty web page. There should be no extension
1373 // frames yet.
1374 NavigateToURL(embedded_test_server()->GetURL("/empty.html"));
1375 ProcessManager* pm = ProcessManager::Get(profile());
1376 EXPECT_EQ(0u, pm->GetAllFrames().size());
1377 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1378
1379 content::WebContents* tab =
1380 browser()->tab_strip_model()->GetActiveWebContents();
1381 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1382
1383 // For this extension, only "*.html" resources are listed as web accessible;
1384 // "manifest.json" doesn't match that pattern, so it shouldn't be possible for
1385 // a webpage to initiate such a navigation.
1386 const GURL inaccessible_extension_resource(
1387 extension->url().Resolve("manifest.json"));
1388 // This is an HTTP request that redirects to a non-webaccessible resource.
1389 const GURL redirect_to_inaccessible(embedded_test_server()->GetURL(
1390 "/server-redirect?" + inaccessible_extension_resource.spec()));
1391 content::WebContents* sneaky_popup =
1392 OpenPopup(main_frame, redirect_to_inaccessible, false);
Lukasz Anforowiczc8058062019-10-01 20:13:021393 EXPECT_EQ(inaccessible_extension_resource,
1394 sneaky_popup->GetLastCommittedURL());
Nick Carter1cb9c33c2018-03-30 18:40:071395 EXPECT_EQ(
1396 content::PAGE_TYPE_ERROR,
1397 sneaky_popup->GetController().GetLastCommittedEntry()->GetPageType());
1398 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1399 EXPECT_EQ(0u, pm->GetAllFrames().size());
1400
1401 // Adding "noopener" to the navigation shouldn't make it work either.
1402 content::WebContents* sneaky_noopener_popup =
1403 OpenPopupNoOpener(main_frame, redirect_to_inaccessible);
Lukasz Anforowiczc8058062019-10-01 20:13:021404 EXPECT_EQ(inaccessible_extension_resource,
1405 sneaky_noopener_popup->GetLastCommittedURL());
Nick Carter1cb9c33c2018-03-30 18:40:071406 EXPECT_EQ(content::PAGE_TYPE_ERROR, sneaky_noopener_popup->GetController()
1407 .GetLastCommittedEntry()
1408 ->GetPageType());
1409 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1410 EXPECT_EQ(0u, pm->GetAllFrames().size());
1411}
1412
1413IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
1414 CrossExtensionEmbeddingOfWebAccessibleResources) {
1415 // Create a simple extension without a background page.
1416 const Extension* extension1 = CreateExtension("Extension 1", false);
1417 const Extension* extension2 = CreateExtension("Extension 2", false);
1418 ASSERT_TRUE(embedded_test_server()->Start());
1419
1420 // Navigate to the "extension 1" page with two iframes.
1421 NavigateToURL(extension1->url().Resolve("two_iframes.html"));
1422
1423 ProcessManager* pm = ProcessManager::Get(profile());
1424 content::WebContents* tab =
1425 browser()->tab_strip_model()->GetActiveWebContents();
1426 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1427
1428 // Navigate the first iframe to a webaccessible resource of extension 2. This
1429 // should work.
1430 GURL extension2_empty = extension2->url().Resolve("/empty.html");
1431 EXPECT_TRUE(WebAccessibleResourcesInfo::IsResourceWebAccessible(
1432 extension2, extension2_empty.path()));
1433 {
1434 content::RenderFrameDeletedObserver frame_deleted_observer(
1435 ChildFrameAt(main_frame, 0));
1436 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame1", extension2_empty));
1437 EXPECT_EQ(extension2_empty,
1438 ChildFrameAt(main_frame, 0)->GetLastCommittedURL());
1439 frame_deleted_observer.WaitUntilDeleted();
1440 EXPECT_EQ(3u, pm->GetAllFrames().size());
1441 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
1442 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
1443 }
1444
1445 // Manifest.json is not a webaccessible resource. extension1 should not be
1446 // able to navigate to extension2's manifest.json.
1447 GURL extension2_manifest = extension2->url().Resolve("/manifest.json");
1448 EXPECT_FALSE(WebAccessibleResourcesInfo::IsResourceWebAccessible(
1449 extension2, extension2_manifest.path()));
1450 {
Lukasz Anforowiczfc417492019-09-25 19:53:281451 content::TestNavigationObserver nav_observer(tab, 1);
Nick Carter1cb9c33c2018-03-30 18:40:071452 EXPECT_TRUE(ExecuteScript(
1453 tab, base::StringPrintf("frames[0].location.href = '%s';",
1454 extension2_manifest.spec().c_str())));
Lukasz Anforowiczfc417492019-09-25 19:53:281455 nav_observer.Wait();
1456 EXPECT_FALSE(nav_observer.last_navigation_succeeded());
1457 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, nav_observer.last_net_error_code());
1458 EXPECT_EQ(extension2_manifest,
1459 ChildFrameAt(main_frame, 0)->GetLastCommittedURL());
1460 EXPECT_EQ(2u, pm->GetAllFrames().size());
Nick Carter1cb9c33c2018-03-30 18:40:071461 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
Lukasz Anforowiczfc417492019-09-25 19:53:281462 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
Nick Carter1cb9c33c2018-03-30 18:40:071463 }
1464
1465 // extension1 should not be able to navigate its second iframe to
1466 // extension2's manifest by bouncing off an HTTP redirect.
1467 const GURL sneaky_extension2_manifest(embedded_test_server()->GetURL(
1468 "/server-redirect?" + extension2_manifest.spec()));
1469 {
Lukasz Anforowiczc8058062019-10-01 20:13:021470 content::TestNavigationObserver nav_observer(tab, 1);
Nick Carter1cb9c33c2018-03-30 18:40:071471 EXPECT_TRUE(ExecuteScript(
1472 tab, base::StringPrintf("frames[1].location.href = '%s';",
1473 sneaky_extension2_manifest.spec().c_str())));
Lukasz Anforowiczc8058062019-10-01 20:13:021474 nav_observer.Wait();
1475 EXPECT_FALSE(nav_observer.last_navigation_succeeded())
Nate Chapine6fd9fb2019-04-22 18:06:021476 << "The initial navigation should be allowed, but not the server "
1477 "redirect to extension2's manifest";
Lukasz Anforowiczc8058062019-10-01 20:13:021478 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, nav_observer.last_net_error_code());
1479 EXPECT_EQ(extension2_manifest, nav_observer.last_navigation_url());
1480 EXPECT_EQ(extension2_manifest,
1481 ChildFrameAt(main_frame, 1)->GetLastCommittedURL());
Lukasz Anforowiczfc417492019-09-25 19:53:281482 EXPECT_EQ(1u, pm->GetAllFrames().size());
Nate Chapine6fd9fb2019-04-22 18:06:021483 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
Lukasz Anforowiczfc417492019-09-25 19:53:281484 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
Nick Carter1cb9c33c2018-03-30 18:40:071485 }
1486
1487 // extension1 can embed a webaccessible resource of extension2 by means of
1488 // an HTTP redirect.
1489 {
1490 content::RenderFrameDeletedObserver frame_deleted_observer(
1491 ChildFrameAt(main_frame, 1));
1492 const GURL extension2_accessible_redirect(embedded_test_server()->GetURL(
1493 "/server-redirect?" + extension2_empty.spec()));
1494 EXPECT_TRUE(ExecuteScript(
1495 tab,
1496 base::StringPrintf("frames[1].location.href = '%s';",
1497 extension2_accessible_redirect.spec().c_str())));
1498 WaitForLoadStop(tab);
1499 frame_deleted_observer.WaitUntilDeleted();
1500 EXPECT_EQ(extension2_empty,
1501 ChildFrameAt(main_frame, 1)->GetLastCommittedURL())
1502 << "The URL of frames[1] should have changed";
Lukasz Anforowiczfc417492019-09-25 19:53:281503 EXPECT_EQ(2u, pm->GetAllFrames().size());
Nick Carter1cb9c33c2018-03-30 18:40:071504 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension1->id()).size());
Lukasz Anforowiczfc417492019-09-25 19:53:281505 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension2->id()).size());
Nick Carter1cb9c33c2018-03-30 18:40:071506 }
1507}
1508
alexmos1f48ef62016-10-06 00:51:501509// Verify that a web popup created via window.open from an extension page can
1510// communicate with the extension page via window.opener. See
1511// https://crbug.com/590068.
1512IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
1513 WebPopupFromExtensionMainFrameHasValidOpener) {
1514 // Create a simple extension without a background page.
1515 const Extension* extension = CreateExtension("Extension", false);
1516 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1517 ASSERT_TRUE(embedded_test_server()->Start());
1518
1519 // Navigate main tab to an extension page.
1520 NavigateToURL(extension->GetResourceURL("empty.html"));
1521 ProcessManager* pm = ProcessManager::Get(profile());
1522 EXPECT_EQ(1u, pm->GetAllFrames().size());
1523 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1524
1525 content::WebContents* tab =
1526 browser()->tab_strip_model()->GetActiveWebContents();
1527
1528 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1529
1530 // Open a new web popup from the extension tab. The popup should go into a
1531 // new process.
1532 GURL popup_url(embedded_test_server()->GetURL("/empty.html"));
1533 content::WebContents* popup = OpenPopup(main_frame, popup_url);
1534 EXPECT_NE(popup, tab);
1535 ASSERT_EQ(2, browser()->tab_strip_model()->count());
Lukasz Anforowicz3f0b84ca2017-09-28 13:02:371536 EXPECT_NE(popup->GetMainFrame()->GetProcess(), main_frame->GetProcess());
alexmos1f48ef62016-10-06 00:51:501537
1538 // Ensure the popup's window.opener is defined.
1539 bool is_opener_defined = false;
1540 EXPECT_TRUE(ExecuteScriptAndExtractBool(
1541 popup, "window.domAutomationController.send(!!window.opener)",
1542 &is_opener_defined));
1543 EXPECT_TRUE(is_opener_defined);
1544
1545 // Verify that postMessage to window.opener works.
1546 VerifyPostMessageToOpener(popup->GetMainFrame(), main_frame);
1547}
1548
1549// Verify that a web popup created via window.open from an extension subframe
1550// can communicate with the extension page via window.opener. Similar to the
1551// test above, but for subframes. See https://crbug.com/590068.
1552IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
1553 WebPopupFromExtensionSubframeHasValidOpener) {
alexmos1f48ef62016-10-06 00:51:501554 // Create a simple extension without a background page.
1555 const Extension* extension = CreateExtension("Extension", false);
1556 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1557 ASSERT_TRUE(embedded_test_server()->Start());
1558
1559 // Navigate main tab to a web page with a blank iframe. There should be no
1560 // extension frames yet.
1561 NavigateToURL(embedded_test_server()->GetURL("/blank_iframe.html"));
1562 ProcessManager* pm = ProcessManager::Get(profile());
1563 EXPECT_EQ(0u, pm->GetAllFrames().size());
1564 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1565
1566 content::WebContents* tab =
1567 browser()->tab_strip_model()->GetActiveWebContents();
1568
1569 // Navigate first subframe to an extension URL.
1570 const GURL extension_url(extension->GetResourceURL("empty.html"));
1571 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame0", extension_url));
1572 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1573 EXPECT_EQ(1u, pm->GetAllFrames().size());
1574
1575 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1576 content::RenderFrameHost* extension_frame = ChildFrameAt(main_frame, 0);
1577
1578 // Open a new web popup from extension frame. The popup should go into main
1579 // frame's web process.
1580 GURL popup_url(embedded_test_server()->GetURL("/empty.html"));
1581 content::WebContents* popup = OpenPopup(extension_frame, popup_url);
1582 EXPECT_NE(popup, tab);
1583 ASSERT_EQ(2, browser()->tab_strip_model()->count());
Lukasz Anforowicz3f0b84ca2017-09-28 13:02:371584 EXPECT_NE(popup->GetMainFrame()->GetProcess(), extension_frame->GetProcess());
1585 EXPECT_EQ(popup->GetMainFrame()->GetProcess(), main_frame->GetProcess());
alexmos1f48ef62016-10-06 00:51:501586
1587 // Ensure the popup's window.opener is defined.
1588 bool is_opener_defined = false;
1589 EXPECT_TRUE(ExecuteScriptAndExtractBool(
1590 popup, "window.domAutomationController.send(!!window.opener)",
1591 &is_opener_defined));
1592 EXPECT_TRUE(is_opener_defined);
1593
1594 // Verify that postMessage to window.opener works.
1595 VerifyPostMessageToOpener(popup->GetMainFrame(), extension_frame);
1596}
1597
alexmos244097022017-04-17 23:13:311598// Test that when a web site has an extension iframe, navigating that iframe to
1599// a different web site without --site-per-process will place it in the parent
1600// frame's process. See https://crbug.com/711006.
1601IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
1602 ExtensionFrameNavigatesToParentSiteInstance) {
1603 // This test matters only *without* --site-per-process.
1604 if (content::AreAllSitesIsolatedForTesting())
1605 return;
1606
alexmos244097022017-04-17 23:13:311607 // Create a simple extension without a background page.
1608 const Extension* extension = CreateExtension("Extension", false);
1609 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1610 ASSERT_TRUE(embedded_test_server()->Start());
1611
1612 // Navigate main tab to a web page with a blank iframe. There should be no
1613 // extension frames yet.
1614 NavigateToURL(embedded_test_server()->GetURL("a.com", "/blank_iframe.html"));
1615 ProcessManager* pm = ProcessManager::Get(profile());
1616 EXPECT_EQ(0u, pm->GetAllFrames().size());
1617 EXPECT_EQ(0u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1618
1619 content::WebContents* tab =
1620 browser()->tab_strip_model()->GetActiveWebContents();
1621
1622 // Navigate subframe to an extension URL. This should go into a new
1623 // extension process.
1624 const GURL extension_url(extension->url().Resolve("empty.html"));
1625 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame0", extension_url));
1626 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1627 EXPECT_EQ(1u, pm->GetAllFrames().size());
1628
1629 content::RenderFrameHost* main_frame = tab->GetMainFrame();
1630 {
1631 content::RenderFrameHost* subframe = ChildFrameAt(main_frame, 0);
1632 EXPECT_NE(subframe->GetProcess(), main_frame->GetProcess());
1633 EXPECT_NE(subframe->GetSiteInstance(), main_frame->GetSiteInstance());
1634 }
1635
1636 // Navigate subframe to b.com. This should be brought back to the parent
1637 // frame's (a.com) process.
1638 GURL b_url(embedded_test_server()->GetURL("b.com", "/empty.html"));
1639 EXPECT_TRUE(content::NavigateIframeToURL(tab, "frame0", b_url));
1640 {
1641 content::RenderFrameHost* subframe = ChildFrameAt(main_frame, 0);
1642 EXPECT_EQ(subframe->GetProcess(), main_frame->GetProcess());
1643 EXPECT_EQ(subframe->GetSiteInstance(), main_frame->GetSiteInstance());
1644 }
1645}
1646
Alex Moshchuk9c9e3882018-11-02 19:57:031647// Verify that web iframes on extension frames do not attempt to aggressively
1648// reuse existing processes for the same site. This helps prevent a
1649// misbehaving web iframe on an extension from slowing down other processes.
1650// See https://crbug.com/899418.
1651IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
1652 WebSubframeOnExtensionDoesNotReuseExistingProcess) {
1653 // This test matters only *with* --site-per-process. It depends on process
1654 // reuse logic that subframes use to look for existing processes, but that
1655 // logic is only turned on for sites that require a dedicated process.
1656 if (!content::AreAllSitesIsolatedForTesting())
1657 return;
1658
1659 // Create a simple extension with a background page that has an empty iframe.
1660 const Extension* extension = CreateExtension("Extension", true);
1661 embedded_test_server()->ServeFilesFromDirectory(extension->path());
1662 ASSERT_TRUE(embedded_test_server()->Start());
1663
1664 // Navigate main tab to a web page on foo.com.
1665 GURL foo_url(embedded_test_server()->GetURL("foo.com", "/title1.html"));
1666 NavigateToURL(foo_url);
1667 content::WebContents* tab =
1668 browser()->tab_strip_model()->GetActiveWebContents();
1669 EXPECT_EQ(foo_url, tab->GetLastCommittedURL());
1670
1671 // So far, there should be two extension frames: one for the background page,
1672 // one for the empty subframe on it.
1673 ProcessManager* pm = ProcessManager::Get(profile());
1674 EXPECT_EQ(2u, pm->GetAllFrames().size());
1675 EXPECT_EQ(2u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1676
1677 // Navigate the subframe on the extension background page to foo.com, and
1678 // wait for the old subframe to go away.
1679 ExtensionHost* background_host =
1680 pm->GetBackgroundHostForExtension(extension->id());
1681 content::RenderFrameHost* background_rfh =
1682 background_host->host_contents()->GetMainFrame();
1683 content::RenderFrameHost* extension_subframe =
1684 ChildFrameAt(background_rfh, 0);
1685 content::RenderFrameDeletedObserver deleted_observer(extension_subframe);
1686 EXPECT_TRUE(
1687 content::ExecJs(extension_subframe,
1688 content::JsReplace("window.location = $1;", foo_url)));
1689 deleted_observer.WaitUntilDeleted();
1690
1691 // There should now only be one extension frame for the background page. The
1692 // subframe should've swapped processes and should now be a web frame.
1693 EXPECT_EQ(1u, pm->GetAllFrames().size());
1694 EXPECT_EQ(1u, pm->GetRenderFrameHostsForExtension(extension->id()).size());
1695 content::RenderFrameHost* subframe = ChildFrameAt(background_rfh, 0);
1696 EXPECT_EQ(foo_url, subframe->GetLastCommittedURL());
1697
1698 // Verify that the subframe did *not* reuse the existing foo.com process.
1699 EXPECT_NE(tab->GetMainFrame()->GetProcess(), subframe->GetProcess());
1700}
1701
nasko68e5e6d2017-05-24 23:19:341702// Test to verify that loading a resource other than an icon file is
1703// disallowed for hosted apps, while icons are allowed.
1704// See https://crbug.com/717626.
1705IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest, HostedAppFilesAccess) {
1706 // Load an extension with a background page.
1707 scoped_refptr<const Extension> extension =
1708 LoadExtension(test_data_dir_.AppendASCII("hosted_app"));
1709 ASSERT_TRUE(extension);
1710
1711 content::WebContents* tab =
1712 browser()->tab_strip_model()->GetActiveWebContents();
1713
1714 // Navigating to the manifest should be blocked with an error page.
1715 {
1716 content::TestNavigationObserver observer(tab);
1717 NavigateToURL(extension->GetResourceURL("/manifest.json"));
1718 EXPECT_FALSE(observer.last_navigation_succeeded());
1719 EXPECT_EQ(tab->GetController().GetLastCommittedEntry()->GetPageType(),
1720 content::PAGE_TYPE_ERROR);
1721 }
1722
1723 // Navigation to the icon file should succeed.
1724 {
1725 content::TestNavigationObserver observer(tab);
1726 NavigateToURL(extension->GetResourceURL("/icon.png"));
1727 EXPECT_TRUE(observer.last_navigation_succeeded());
1728 EXPECT_EQ(tab->GetController().GetLastCommittedEntry()->GetPageType(),
1729 content::PAGE_TYPE_NORMAL);
1730 }
1731}
1732
Devlin Cronin7a282e32017-08-10 01:54:101733// Tests that we correctly account for vanilla web URLs that may be in the
1734// same SiteInstance as a hosted app, and display alerts correctly.
1735// https://crbug.com/746517.
1736IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest, HostedAppAlerts) {
1737 ASSERT_TRUE(embedded_test_server()->Start());
1738 scoped_refptr<const Extension> extension =
1739 LoadExtension(test_data_dir_.AppendASCII("hosted_app"));
1740 ASSERT_TRUE(extension);
1741
1742 content::WebContents* tab =
1743 browser()->tab_strip_model()->GetActiveWebContents();
Nasko Oskov31c0204d2018-04-30 17:32:481744 GURL hosted_app_url(embedded_test_server()->GetURL(
1745 "localhost", "/extensions/hosted_app/main.html"));
1746 {
1747 content::TestNavigationObserver observer(tab);
1748 NavigateToURL(hosted_app_url);
1749 EXPECT_TRUE(observer.last_navigation_succeeded());
1750 }
Devlin Cronin7a282e32017-08-10 01:54:101751 EXPECT_EQ(hosted_app_url, tab->GetLastCommittedURL());
1752 ProcessManager* pm = ProcessManager::Get(profile());
1753 EXPECT_EQ(extension, pm->GetExtensionForWebContents(tab));
Evan Stade7220e472020-01-31 17:06:571754 javascript_dialogs::AppModalDialogManager* js_dialog_manager =
1755 javascript_dialogs::AppModalDialogManager::GetInstance();
Devlin Cronin7a282e32017-08-10 01:54:101756 base::string16 hosted_app_title = base::ASCIIToUTF16("hosted_app");
1757 EXPECT_EQ(hosted_app_title, js_dialog_manager->GetTitle(
1758 tab, tab->GetLastCommittedURL().GetOrigin()));
1759
1760 GURL web_url = embedded_test_server()->GetURL("/title1.html");
1761 ASSERT_TRUE(content::ExecuteScript(
1762 tab, base::StringPrintf("window.open('%s');", web_url.spec().c_str())));
1763 content::WebContents* new_tab =
1764 browser()->tab_strip_model()->GetActiveWebContents();
1765 EXPECT_NE(new_tab, tab);
1766 EXPECT_TRUE(content::WaitForLoadStop(new_tab));
1767 EXPECT_EQ(web_url, new_tab->GetLastCommittedURL());
1768 EXPECT_EQ(nullptr, pm->GetExtensionForWebContents(new_tab));
1769 EXPECT_NE(hosted_app_title,
1770 js_dialog_manager->GetTitle(
1771 new_tab, new_tab->GetLastCommittedURL().GetOrigin()));
1772}
1773
[email protected]98b6d942013-11-10 00:34:071774} // namespace extensions