David Bertoni | 38d9d19 | 2018-07-11 16:46:41 | [diff] [blame] | 1 | // Copyright (c) 2018 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 | |
| 5 | #include <stddef.h> |
| 6 | |
| 7 | #include "base/strings/utf_string_conversions.h" |
David Bertoni | 38d9d19 | 2018-07-11 16:46:41 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_action_test_util.h" |
| 9 | #include "chrome/browser/extensions/extension_browsertest.h" |
| 10 | #include "chrome/browser/extensions/extension_tab_util.h" |
| 11 | #include "chrome/browser/ui/browser.h" |
| 12 | #include "chrome/browser/ui/browser_window.h" |
| 13 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 | #include "chrome/test/base/ui_test_utils.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 15 | #include "content/public/test/browser_test.h" |
David Bertoni | 38d9d19 | 2018-07-11 16:46:41 | [diff] [blame] | 16 | #include "content/public/test/browser_test_utils.h" |
Devlin Cronin | 5911e74 | 2020-04-28 15:56:34 | [diff] [blame] | 17 | #include "extensions/browser/extension_action.h" |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 18 | #include "extensions/browser/extension_action_manager.h" |
David Bertoni | 38d9d19 | 2018-07-11 16:46:41 | [diff] [blame] | 19 | #include "extensions/browser/extension_registry.h" |
| 20 | #include "extensions/common/constants.h" |
| 21 | #include "extensions/common/extension.h" |
| 22 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 23 | |
| 24 | namespace extensions { |
| 25 | namespace { |
| 26 | |
| 27 | // Tests that the chrome-extension scheme disallows running Javascript URLs. |
| 28 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, |
| 29 | ChromeExtensionSchemeNotAllowJavascript) { |
| 30 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 31 | |
| 32 | const Extension* extension = |
| 33 | LoadExtension(test_data_dir_.AppendASCII("simple_with_file")); |
| 34 | ASSERT_TRUE(extension); |
| 35 | |
| 36 | // Navigate to the extension's page. |
| 37 | const GURL extension_file_url(extension->GetResourceURL("file.html")); |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 38 | ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_file_url)); |
David Bertoni | 38d9d19 | 2018-07-11 16:46:41 | [diff] [blame] | 39 | |
| 40 | content::WebContents* web_contents = |
| 41 | browser()->tab_strip_model()->GetActiveWebContents(); |
Jan Wilken Dörrie | 78e88d82e | 2021-03-23 15:24:22 | [diff] [blame] | 42 | const std::u16string expected_title = u"foo"; |
David Bertoni | 38d9d19 | 2018-07-11 16:46:41 | [diff] [blame] | 43 | ASSERT_EQ(expected_title, web_contents->GetTitle()); |
| 44 | |
| 45 | // Attempt to set the page title via Javascript. Don't try to block since |
| 46 | // the javascript URL won't actually navigate anywhere. |
| 47 | const GURL script_url("javascript:void(document.title='Bad Title')"); |
| 48 | NavigateToURLWithDisposition(browser(), script_url, |
| 49 | WindowOpenDisposition::CURRENT_TAB, |
| 50 | ui_test_utils::BROWSER_TEST_NONE); |
| 51 | // Force serialization with the renderer by executing a no-op script. |
| 52 | bool result = false; |
| 53 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 54 | web_contents, "domAutomationController.send(true)", &result)); |
| 55 | EXPECT_TRUE(result); |
| 56 | |
| 57 | // Expect the title hasn't changed since the javascript URL was blocked |
| 58 | // from executing. |
| 59 | EXPECT_EQ(expected_title, web_contents->GetTitle()); |
| 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | } // namespace extensions |