blob: 88e7ffcd622886329632f215bd4f0e646c4de00c [file] [log] [blame]
David Bertoni38d9d192018-07-11 16:46:411// 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 Bertoni38d9d192018-07-11 16:46:418#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 Kasting919ce652020-05-07 10:22:3615#include "content/public/test/browser_test.h"
David Bertoni38d9d192018-07-11 16:46:4116#include "content/public/test/browser_test_utils.h"
Devlin Cronin5911e742020-04-28 15:56:3417#include "extensions/browser/extension_action.h"
Devlin Cronin94832fc2020-08-04 18:39:4118#include "extensions/browser/extension_action_manager.h"
David Bertoni38d9d192018-07-11 16:46:4119#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
24namespace extensions {
25namespace {
26
27// Tests that the chrome-extension scheme disallows running Javascript URLs.
28IN_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 Anforowiczb78290c2021-09-08 04:31:3838 ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_file_url));
David Bertoni38d9d192018-07-11 16:46:4139
40 content::WebContents* web_contents =
41 browser()->tab_strip_model()->GetActiveWebContents();
Jan Wilken Dörrie78e88d82e2021-03-23 15:24:2242 const std::u16string expected_title = u"foo";
David Bertoni38d9d192018-07-11 16:46:4143 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