[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 1 | // Copyright 2014 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 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 7 | #include <map> |
limasdf | 3d10254 | 2015-12-09 03:58:45 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 9 | |
| 10 | #include "base/values.h" |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 11 | #include "chrome/browser/extensions/active_tab_permission_granter.h" |
rdevlin.cronin | 699ca6ff | 2014-09-29 23:59:57 | [diff] [blame] | 12 | #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_action_runner.h" |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_sync_service_factory.h" |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 15 | #include "chrome/browser/extensions/permissions_updater.h" |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 16 | #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 17 | #include "chrome/browser/extensions/tab_helper.h" |
| 18 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 19 | #include "chrome/test/base/testing_profile.h" |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 20 | #include "components/crx_file/id_util.h" |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 21 | #include "content/public/browser/navigation_controller.h" |
| 22 | #include "content/public/browser/navigation_entry.h" |
| 23 | #include "content/public/browser/web_contents.h" |
clamy | f205303 | 2017-10-20 16:01:59 | [diff] [blame] | 24 | #include "content/public/test/navigation_simulator.h" |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 25 | #include "extensions/browser/extension_registry.h" |
| 26 | #include "extensions/common/extension.h" |
| 27 | #include "extensions/common/extension_builder.h" |
| 28 | #include "extensions/common/feature_switch.h" |
| 29 | #include "extensions/common/manifest.h" |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 30 | #include "extensions/common/user_script.h" |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 31 | #include "extensions/common/value_builder.h" |
| 32 | |
| 33 | namespace extensions { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | const char kAllHostsPermission[] = "*://*/*"; |
| 38 | |
| 39 | } // namespace |
| 40 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 41 | // Unittests for the ExtensionActionRunner mostly test the internal logic |
| 42 | // of the runner itself (when to allow/deny extension script injection). |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 43 | // Testing real injection is allowed/denied as expected (i.e., that the |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 44 | // ExtensionActionRunner correctly interfaces in the system) is done in the |
| 45 | // ExtensionActionRunnerBrowserTests. |
| 46 | class ExtensionActionRunnerUnitTest : public ChromeRenderViewHostTestHarness { |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 47 | protected: |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 48 | ExtensionActionRunnerUnitTest(); |
| 49 | ~ExtensionActionRunnerUnitTest() override; |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 50 | |
| 51 | // Creates an extension with all hosts permission and adds it to the registry. |
| 52 | const Extension* AddExtension(); |
| 53 | |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 54 | // Reloads |extension_| by removing it from the registry and recreating it. |
| 55 | const Extension* ReloadExtension(); |
| 56 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 57 | // Returns true if the |extension| requires user consent before injecting |
| 58 | // a script. |
| 59 | bool RequiresUserConsent(const Extension* extension) const; |
| 60 | |
| 61 | // Request an injection for the given |extension|. |
| 62 | void RequestInjection(const Extension* extension); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 63 | void RequestInjection(const Extension* extension, |
| 64 | UserScript::RunLocation run_location); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 65 | |
| 66 | // Returns the number of times a given extension has had a script execute. |
| 67 | size_t GetExecutionCountForExtension(const std::string& extension_id) const; |
| 68 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 69 | ExtensionActionRunner* runner() const { return extension_action_runner_; } |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 70 | |
| 71 | private: |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 72 | // Returns a closure to use as a script execution for a given extension. |
| 73 | base::Closure GetExecutionCallbackForExtension( |
| 74 | const std::string& extension_id); |
| 75 | |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 76 | // Increment the number of executions for the given |extension_id|. |
| 77 | void IncrementExecutionCount(const std::string& extension_id); |
| 78 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 79 | void SetUp() override; |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 80 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 81 | // Since ExtensionActionRunner's behavior is behind a flag, override the |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 82 | // feature switch. |
| 83 | FeatureSwitch::ScopedOverride feature_override_; |
| 84 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 85 | // The associated ExtensionActionRunner. |
| 86 | ExtensionActionRunner* extension_action_runner_; |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 87 | |
| 88 | // The map of observed executions, keyed by extension id. |
| 89 | std::map<std::string, int> extension_executions_; |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 90 | |
| 91 | scoped_refptr<const Extension> extension_; |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 92 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(ExtensionActionRunnerUnitTest); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 94 | }; |
| 95 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 96 | ExtensionActionRunnerUnitTest::ExtensionActionRunnerUnitTest() |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 97 | : feature_override_(FeatureSwitch::scripts_require_action(), |
| 98 | FeatureSwitch::OVERRIDE_ENABLED), |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 99 | extension_action_runner_(nullptr) {} |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 100 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 101 | ExtensionActionRunnerUnitTest::~ExtensionActionRunnerUnitTest() {} |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 102 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 103 | const Extension* ExtensionActionRunnerUnitTest::AddExtension() { |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 104 | const std::string kId = crx_file::id_util::GenerateId("all_hosts_extension"); |
limasdf | 3d10254 | 2015-12-09 03:58:45 | [diff] [blame] | 105 | extension_ = |
| 106 | ExtensionBuilder() |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 107 | .SetManifest( |
limasdf | 21d67e6 | 2015-12-19 12:04:49 | [diff] [blame] | 108 | DictionaryBuilder() |
| 109 | .Set("name", "all_hosts_extension") |
| 110 | .Set("description", "an extension") |
| 111 | .Set("manifest_version", 2) |
| 112 | .Set("version", "1.0.0") |
| 113 | .Set("permissions", |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 114 | ListBuilder().Append(kAllHostsPermission).Build()) |
| 115 | .Build()) |
limasdf | 3d10254 | 2015-12-09 03:58:45 | [diff] [blame] | 116 | .SetLocation(Manifest::INTERNAL) |
| 117 | .SetID(kId) |
| 118 | .Build(); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 119 | |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 120 | ExtensionRegistry::Get(profile())->AddEnabled(extension_); |
dcheng | c704794 | 2014-08-26 05:05:31 | [diff] [blame] | 121 | PermissionsUpdater(profile()).InitializePermissions(extension_.get()); |
| 122 | return extension_.get(); |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 123 | } |
| 124 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 125 | const Extension* ExtensionActionRunnerUnitTest::ReloadExtension() { |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 126 | ExtensionRegistry::Get(profile())->RemoveEnabled(extension_->id()); |
| 127 | return AddExtension(); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 128 | } |
| 129 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 130 | bool ExtensionActionRunnerUnitTest::RequiresUserConsent( |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 131 | const Extension* extension) const { |
| 132 | PermissionsData::AccessType access_type = |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 133 | runner()->RequiresUserConsentForScriptInjectionForTesting( |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 134 | extension, UserScript::PROGRAMMATIC_SCRIPT); |
| 135 | // We should never downright refuse access in these tests. |
| 136 | DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type); |
| 137 | return access_type == PermissionsData::ACCESS_WITHHELD; |
| 138 | } |
| 139 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 140 | void ExtensionActionRunnerUnitTest::RequestInjection( |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 141 | const Extension* extension) { |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 142 | RequestInjection(extension, UserScript::DOCUMENT_IDLE); |
| 143 | } |
| 144 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 145 | void ExtensionActionRunnerUnitTest::RequestInjection( |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 146 | const Extension* extension, |
| 147 | UserScript::RunLocation run_location) { |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 148 | runner()->RequestScriptInjectionForTesting( |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 149 | extension, run_location, |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 150 | GetExecutionCallbackForExtension(extension->id())); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 151 | } |
| 152 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 153 | size_t ExtensionActionRunnerUnitTest::GetExecutionCountForExtension( |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 154 | const std::string& extension_id) const { |
| 155 | std::map<std::string, int>::const_iterator iter = |
| 156 | extension_executions_.find(extension_id); |
| 157 | if (iter != extension_executions_.end()) |
| 158 | return iter->second; |
| 159 | return 0u; |
| 160 | } |
| 161 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 162 | base::Closure ExtensionActionRunnerUnitTest::GetExecutionCallbackForExtension( |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 163 | const std::string& extension_id) { |
| 164 | // We use base unretained here, but if this ever gets executed outside of |
| 165 | // this test's lifetime, we have a major problem anyway. |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 166 | return base::Bind(&ExtensionActionRunnerUnitTest::IncrementExecutionCount, |
| 167 | base::Unretained(this), extension_id); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 168 | } |
| 169 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 170 | void ExtensionActionRunnerUnitTest::IncrementExecutionCount( |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 171 | const std::string& extension_id) { |
| 172 | ++extension_executions_[extension_id]; |
| 173 | } |
| 174 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 175 | void ExtensionActionRunnerUnitTest::SetUp() { |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 176 | ChromeRenderViewHostTestHarness::SetUp(); |
| 177 | |
isherman | 30fa851a | 2015-06-09 23:32:10 | [diff] [blame] | 178 | // Skip syncing for testing purposes. |
| 179 | ExtensionSyncServiceFactory::GetInstance()->SetTestingFactory(profile(), |
| 180 | nullptr); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 181 | |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 182 | TabHelper::CreateForWebContents(web_contents()); |
| 183 | TabHelper* tab_helper = TabHelper::FromWebContents(web_contents()); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 184 | // These should never be null. |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 185 | DCHECK(tab_helper); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 186 | extension_action_runner_ = tab_helper->extension_action_runner(); |
| 187 | DCHECK(extension_action_runner_); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Test that extensions with all_hosts require permission to execute, and, once |
| 191 | // that permission is granted, do execute. |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 192 | TEST_F(ExtensionActionRunnerUnitTest, RequestPermissionAndExecute) { |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 193 | const Extension* extension = AddExtension(); |
| 194 | ASSERT_TRUE(extension); |
| 195 | |
| 196 | NavigateAndCommit(GURL("https://www.google.com")); |
| 197 | |
| 198 | // Ensure that there aren't any executions pending. |
| 199 | ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 200 | ASSERT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 201 | |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 202 | // Since the extension requests all_hosts, we should require user consent. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 203 | EXPECT_TRUE(RequiresUserConsent(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 204 | |
rdevlin.cronin | 91f162a1 | 2014-09-03 16:48:40 | [diff] [blame] | 205 | // Request an injection. The extension should want to run, but should not have |
| 206 | // executed. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 207 | RequestInjection(extension); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 208 | EXPECT_TRUE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 209 | EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 210 | |
| 211 | // Click to accept the extension executing. |
rdevlin.cronin | 4a78c48b | 2016-03-24 00:02:29 | [diff] [blame] | 212 | runner()->RunForTesting(extension); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 213 | |
rdevlin.cronin | 91f162a1 | 2014-09-03 16:48:40 | [diff] [blame] | 214 | // The extension should execute, and the extension shouldn't want to run. |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 215 | EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 216 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 217 | |
| 218 | // Since we already executed on the given page, we shouldn't need permission |
| 219 | // for a second time. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 220 | EXPECT_FALSE(RequiresUserConsent(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 221 | |
[email protected] | 4b8d1c6 | 2014-08-16 01:22:21 | [diff] [blame] | 222 | // Reloading and same-origin navigations shouldn't clear those permissions, |
| 223 | // and we shouldn't require user constent again. |
clamy | f205303 | 2017-10-20 16:01:59 | [diff] [blame] | 224 | content::NavigationSimulator::Reload(web_contents()); |
[email protected] | 4b8d1c6 | 2014-08-16 01:22:21 | [diff] [blame] | 225 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 226 | NavigateAndCommit(GURL("https://www.google.com/foo")); |
| 227 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 228 | NavigateAndCommit(GURL("https://www.google.com/bar")); |
| 229 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 230 | |
| 231 | // Cross-origin navigations should clear permissions. |
| 232 | NavigateAndCommit(GURL("https://otherdomain.google.com")); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 233 | EXPECT_TRUE(RequiresUserConsent(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 234 | |
| 235 | // Grant access. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 236 | RequestInjection(extension); |
rdevlin.cronin | 4a78c48b | 2016-03-24 00:02:29 | [diff] [blame] | 237 | runner()->RunForTesting(extension); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 238 | EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 239 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 240 | |
| 241 | // Navigating to another site should also clear the permissions. |
| 242 | NavigateAndCommit(GURL("https://www.foo.com")); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 243 | EXPECT_TRUE(RequiresUserConsent(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Test that injections that are not executed by the time the user navigates are |
| 247 | // ignored and never execute. |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 248 | TEST_F(ExtensionActionRunnerUnitTest, PendingInjectionsRemovedAtNavigation) { |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 249 | const Extension* extension = AddExtension(); |
| 250 | ASSERT_TRUE(extension); |
| 251 | |
| 252 | NavigateAndCommit(GURL("https://www.google.com")); |
| 253 | |
| 254 | ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 255 | |
rdevlin.cronin | 91f162a1 | 2014-09-03 16:48:40 | [diff] [blame] | 256 | // Request an injection. The extension should want to run, but not execute. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 257 | RequestInjection(extension); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 258 | EXPECT_TRUE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 259 | EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 260 | |
[email protected] | 8d5cb21 | 2014-06-04 09:00:39 | [diff] [blame] | 261 | // Reload. This should remove the pending injection, and we should not |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 262 | // execute anything. |
clamy | f205303 | 2017-10-20 16:01:59 | [diff] [blame] | 263 | content::NavigationSimulator::Reload(web_contents()); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 264 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 265 | EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 266 | |
| 267 | // Request and accept a new injection. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 268 | RequestInjection(extension); |
rdevlin.cronin | 4a78c48b | 2016-03-24 00:02:29 | [diff] [blame] | 269 | runner()->RunForTesting(extension); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 270 | |
| 271 | // The extension should only have executed once, even though a grand total |
| 272 | // of two executions were requested. |
| 273 | EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 274 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // Test that queueing multiple pending injections, and then accepting, triggers |
| 278 | // them all. |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 279 | TEST_F(ExtensionActionRunnerUnitTest, MultiplePendingInjection) { |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 280 | const Extension* extension = AddExtension(); |
| 281 | ASSERT_TRUE(extension); |
| 282 | NavigateAndCommit(GURL("https://www.google.com")); |
| 283 | |
| 284 | ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 285 | |
| 286 | const size_t kNumInjections = 3u; |
| 287 | // Queue multiple pending injections. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 288 | for (size_t i = 0u; i < kNumInjections; ++i) |
| 289 | RequestInjection(extension); |
| 290 | |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 291 | EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 292 | |
rdevlin.cronin | 4a78c48b | 2016-03-24 00:02:29 | [diff] [blame] | 293 | runner()->RunForTesting(extension); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 294 | |
| 295 | // All pending injections should have executed. |
| 296 | EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 297 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 298 | } |
| 299 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 300 | TEST_F(ExtensionActionRunnerUnitTest, ActiveScriptsUseActiveTabPermissions) { |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 301 | const Extension* extension = AddExtension(); |
| 302 | NavigateAndCommit(GURL("https://www.google.com")); |
| 303 | |
| 304 | ActiveTabPermissionGranter* active_tab_permission_granter = |
| 305 | TabHelper::FromWebContents(web_contents()) |
| 306 | ->active_tab_permission_granter(); |
| 307 | ASSERT_TRUE(active_tab_permission_granter); |
| 308 | // Grant the extension active tab permissions. This normally happens, e.g., |
| 309 | // if the user clicks on a browser action. |
| 310 | active_tab_permission_granter->GrantIfRequested(extension); |
| 311 | |
| 312 | // Since we have active tab permissions, we shouldn't need user consent |
| 313 | // anymore. |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 314 | EXPECT_FALSE(RequiresUserConsent(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 315 | |
[email protected] | 4b8d1c6 | 2014-08-16 01:22:21 | [diff] [blame] | 316 | // Reloading and other same-origin navigations maintain the permission to |
| 317 | // execute. |
clamy | f205303 | 2017-10-20 16:01:59 | [diff] [blame] | 318 | content::NavigationSimulator::Reload(web_contents()); |
[email protected] | 4b8d1c6 | 2014-08-16 01:22:21 | [diff] [blame] | 319 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 320 | NavigateAndCommit(GURL("https://www.google.com/foo")); |
| 321 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 322 | NavigateAndCommit(GURL("https://www.google.com/bar")); |
| 323 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 324 | |
| 325 | // Navigating to a different origin will require user consent again. |
| 326 | NavigateAndCommit(GURL("https://yahoo.com")); |
| 327 | EXPECT_TRUE(RequiresUserConsent(extension)); |
| 328 | |
| 329 | // Back to the original origin should also re-require constent. |
| 330 | NavigateAndCommit(GURL("https://www.google.com")); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 331 | EXPECT_TRUE(RequiresUserConsent(extension)); |
[email protected] | 11814f5 | 2014-05-23 06:50:35 | [diff] [blame] | 332 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 333 | RequestInjection(extension); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 334 | EXPECT_TRUE(runner()->WantsToRun(extension)); |
[email protected] | 11814f5 | 2014-05-23 06:50:35 | [diff] [blame] | 335 | EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 336 | |
| 337 | // Grant active tab. |
| 338 | active_tab_permission_granter->GrantIfRequested(extension); |
| 339 | |
| 340 | // The pending injections should have run since active tab permission was |
| 341 | // granted. |
| 342 | EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 343 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 344 | } |
| 345 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 346 | TEST_F(ExtensionActionRunnerUnitTest, ActiveScriptsCanHaveAllUrlsPref) { |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 347 | const Extension* extension = AddExtension(); |
| 348 | ASSERT_TRUE(extension); |
| 349 | |
| 350 | NavigateAndCommit(GURL("https://www.google.com")); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 351 | EXPECT_TRUE(RequiresUserConsent(extension)); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 352 | |
| 353 | // Enable the extension on all urls. |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 354 | ScriptingPermissionsModifier permissions_modifier(profile(), extension); |
| 355 | permissions_modifier.SetAllowedOnAllUrls(true); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 356 | |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 357 | EXPECT_FALSE(RequiresUserConsent(extension)); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 358 | // This should carry across navigations, and websites. |
| 359 | NavigateAndCommit(GURL("http://www.foo.com")); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 360 | EXPECT_FALSE(RequiresUserConsent(extension)); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 361 | |
| 362 | // Turning off the preference should have instant effect. |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 363 | permissions_modifier.SetAllowedOnAllUrls(false); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 364 | EXPECT_TRUE(RequiresUserConsent(extension)); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 365 | |
| 366 | // And should also persist across navigations and websites. |
| 367 | NavigateAndCommit(GURL("http://www.bar.com")); |
[email protected] | 23a8536 | 2014-07-07 23:26:19 | [diff] [blame] | 368 | EXPECT_TRUE(RequiresUserConsent(extension)); |
[email protected] | b33c8c2 | 2014-05-29 19:51:08 | [diff] [blame] | 369 | } |
| 370 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 371 | TEST_F(ExtensionActionRunnerUnitTest, TestAlwaysRun) { |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 372 | const Extension* extension = AddExtension(); |
| 373 | ASSERT_TRUE(extension); |
| 374 | |
| 375 | NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl")); |
| 376 | |
| 377 | // Ensure that there aren't any executions pending. |
| 378 | ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 379 | ASSERT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 380 | |
| 381 | // Since the extension requests all_hosts, we should require user consent. |
| 382 | EXPECT_TRUE(RequiresUserConsent(extension)); |
| 383 | |
rdevlin.cronin | 91f162a1 | 2014-09-03 16:48:40 | [diff] [blame] | 384 | // Request an injection. The extension should want to run, but not execute. |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 385 | RequestInjection(extension); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 386 | EXPECT_TRUE(runner()->WantsToRun(extension)); |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 387 | EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 388 | |
| 389 | // Allow the extension to always run on this origin. |
rdevlin.cronin | cb9f86e | 2015-10-15 15:13:42 | [diff] [blame] | 390 | ScriptingPermissionsModifier modifier(profile(), extension); |
| 391 | modifier.GrantHostPermission(web_contents()->GetLastCommittedURL()); |
rdevlin.cronin | 4a78c48b | 2016-03-24 00:02:29 | [diff] [blame] | 392 | runner()->RunForTesting(extension); |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 393 | |
rdevlin.cronin | 91f162a1 | 2014-09-03 16:48:40 | [diff] [blame] | 394 | // The extension should execute, and the extension shouldn't want to run. |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 395 | EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 396 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 397 | |
| 398 | // Since we already executed on the given page, we shouldn't need permission |
| 399 | // for a second time. |
| 400 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 401 | |
| 402 | // Navigating to another site that hasn't been granted a persisted permission |
| 403 | // should necessitate user consent. |
| 404 | NavigateAndCommit(GURL("https://www.foo.com/bar")); |
| 405 | EXPECT_TRUE(RequiresUserConsent(extension)); |
| 406 | |
| 407 | // We shouldn't need user permission upon returning to the original origin. |
| 408 | NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
| 409 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 410 | |
| 411 | // Reloading the extension should not clear any granted host permissions. |
| 412 | extension = ReloadExtension(); |
clamy | f205303 | 2017-10-20 16:01:59 | [diff] [blame] | 413 | content::NavigationSimulator::Reload(web_contents()); |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 414 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 415 | |
| 416 | // Different host... |
| 417 | NavigateAndCommit(GURL("https://www.foo.com/bar")); |
| 418 | EXPECT_TRUE(RequiresUserConsent(extension)); |
| 419 | // Different scheme... |
| 420 | NavigateAndCommit(GURL("http://www.google.com/foo/bar")); |
| 421 | EXPECT_TRUE(RequiresUserConsent(extension)); |
| 422 | // Different subdomain... |
| 423 | NavigateAndCommit(GURL("https://en.google.com/foo/bar")); |
| 424 | EXPECT_TRUE(RequiresUserConsent(extension)); |
| 425 | // Only the "always run" origin should be allowed to run without user consent. |
| 426 | NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
| 427 | EXPECT_FALSE(RequiresUserConsent(extension)); |
| 428 | } |
| 429 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 430 | TEST_F(ExtensionActionRunnerUnitTest, TestDifferentScriptRunLocations) { |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 431 | const Extension* extension = AddExtension(); |
| 432 | ASSERT_TRUE(extension); |
| 433 | |
| 434 | NavigateAndCommit(GURL("https://www.foo.com")); |
| 435 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 436 | EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 437 | |
| 438 | RequestInjection(extension, UserScript::DOCUMENT_END); |
| 439 | EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER, |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 440 | runner()->GetBlockedActions(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 441 | RequestInjection(extension, UserScript::DOCUMENT_IDLE); |
| 442 | EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER, |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 443 | runner()->GetBlockedActions(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 444 | RequestInjection(extension, UserScript::DOCUMENT_START); |
| 445 | EXPECT_EQ(BLOCKED_ACTION_SCRIPT_AT_START | BLOCKED_ACTION_SCRIPT_OTHER, |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 446 | runner()->GetBlockedActions(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 447 | |
rdevlin.cronin | 4a78c48b | 2016-03-24 00:02:29 | [diff] [blame] | 448 | runner()->RunForTesting(extension); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 449 | EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 450 | } |
| 451 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 452 | TEST_F(ExtensionActionRunnerUnitTest, TestWebRequestBlocked) { |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 453 | const Extension* extension = AddExtension(); |
| 454 | ASSERT_TRUE(extension); |
| 455 | |
| 456 | NavigateAndCommit(GURL("https://www.foo.com")); |
| 457 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 458 | EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); |
| 459 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 460 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 461 | runner()->OnWebRequestBlocked(extension); |
| 462 | EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner()->GetBlockedActions(extension)); |
| 463 | EXPECT_TRUE(runner()->WantsToRun(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 464 | |
| 465 | RequestInjection(extension); |
| 466 | EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST | BLOCKED_ACTION_SCRIPT_OTHER, |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 467 | runner()->GetBlockedActions(extension)); |
| 468 | EXPECT_TRUE(runner()->WantsToRun(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 469 | |
| 470 | NavigateAndCommit(GURL("https://www.bar.com")); |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 471 | EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); |
| 472 | EXPECT_FALSE(runner()->WantsToRun(extension)); |
rdevlin.cronin | 8d034e5 | 2016-02-02 22:46:32 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | 78cd68e | 2014-05-22 20:33:52 | [diff] [blame] | 475 | } // namespace extensions |