| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/extensions/active_script_controller.h" | 8 #include "chrome/browser/extensions/active_script_controller.h" |
| 9 #include "chrome/browser/extensions/active_tab_permission_granter.h" | 9 #include "chrome/browser/extensions/active_tab_permission_granter.h" |
| 10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 // Unittests for the ActiveScriptController mostly test the internal logic | 35 // Unittests for the ActiveScriptController mostly test the internal logic |
| 36 // of the controller itself (when to allow/deny extension script injection). | 36 // of the controller itself (when to allow/deny extension script injection). |
| 37 // Testing real injection is allowed/denied as expected (i.e., that the | 37 // Testing real injection is allowed/denied as expected (i.e., that the |
| 38 // ActiveScriptController correctly interfaces in the system) is done in the | 38 // ActiveScriptController correctly interfaces in the system) is done in the |
| 39 // ActiveScriptControllerBrowserTests. | 39 // ActiveScriptControllerBrowserTests. |
| 40 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { | 40 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { |
| 41 protected: | 41 protected: |
| 42 ActiveScriptControllerUnitTest(); | 42 ActiveScriptControllerUnitTest(); |
| 43 virtual ~ActiveScriptControllerUnitTest(); | 43 virtual ~ActiveScriptControllerUnitTest(); |
| 44 | 44 |
| 45 // Creates an extension with all hosts permission and adds it to the registry. | 45 // Creates an extension with a generated id and adds it to the registry. |
| 46 const Extension* AddExtension(); | 46 const Extension* AddExtension(); |
| 47 | 47 |
| 48 // Creates an extension with |id| and all host permission and adds it to the |
| 49 // registry. |
| 50 const Extension* AddExtension(const std::string& id); |
| 51 |
| 52 // Reloads the extension with |id| by removing it from the registry and |
| 53 // recreating a new extension with the same |id|. |
| 54 const Extension* ReloadExtension(const std::string& id); |
| 55 |
| 48 // Returns true if the |extension| requires user consent before injecting | 56 // Returns true if the |extension| requires user consent before injecting |
| 49 // a script. | 57 // a script. |
| 50 bool RequiresUserConsent(const Extension* extension) const; | 58 bool RequiresUserConsent(const Extension* extension) const; |
| 51 | 59 |
| 52 // Request an injection for the given |extension|. | 60 // Request an injection for the given |extension|. |
| 53 void RequestInjection(const Extension* extension); | 61 void RequestInjection(const Extension* extension); |
| 54 | 62 |
| 63 // Simulate clicking "always run" menu item for |extension|. |
| 64 void AlwaysRun(const Extension* extension); |
| 65 |
| 55 // Returns the number of times a given extension has had a script execute. | 66 // Returns the number of times a given extension has had a script execute. |
| 56 size_t GetExecutionCountForExtension(const std::string& extension_id) const; | 67 size_t GetExecutionCountForExtension(const std::string& extension_id) const; |
| 57 | 68 |
| 58 ActiveScriptController* controller() const { | 69 ActiveScriptController* controller() const { |
| 59 return active_script_controller_; | 70 return active_script_controller_; |
| 60 } | 71 } |
| 61 | 72 |
| 62 private: | 73 private: |
| 63 // Returns a closure to use as a script execution for a given extension. | 74 // Returns a closure to use as a script execution for a given extension. |
| 64 base::Closure GetExecutionCallbackForExtension( | 75 base::Closure GetExecutionCallbackForExtension( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 83 ActiveScriptControllerUnitTest::ActiveScriptControllerUnitTest() | 94 ActiveScriptControllerUnitTest::ActiveScriptControllerUnitTest() |
| 84 : feature_override_(FeatureSwitch::scripts_require_action(), | 95 : feature_override_(FeatureSwitch::scripts_require_action(), |
| 85 FeatureSwitch::OVERRIDE_ENABLED), | 96 FeatureSwitch::OVERRIDE_ENABLED), |
| 86 active_script_controller_(NULL) { | 97 active_script_controller_(NULL) { |
| 87 } | 98 } |
| 88 | 99 |
| 89 ActiveScriptControllerUnitTest::~ActiveScriptControllerUnitTest() { | 100 ActiveScriptControllerUnitTest::~ActiveScriptControllerUnitTest() { |
| 90 } | 101 } |
| 91 | 102 |
| 92 const Extension* ActiveScriptControllerUnitTest::AddExtension() { | 103 const Extension* ActiveScriptControllerUnitTest::AddExtension() { |
| 93 const std::string kId = id_util::GenerateId("all_hosts_extension"); | 104 return AddExtension(id_util::GenerateId("all_hosts_extension")); |
| 105 } |
| 106 |
| 107 const Extension* ActiveScriptControllerUnitTest::AddExtension( |
| 108 const std::string& id) { |
| 94 scoped_refptr<const Extension> extension = | 109 scoped_refptr<const Extension> extension = |
| 95 ExtensionBuilder() | 110 ExtensionBuilder() |
| 96 .SetManifest( | 111 .SetManifest( |
| 97 DictionaryBuilder() | 112 DictionaryBuilder() |
| 98 .Set("name", "all_hosts_extension") | 113 .Set("name", "all_hosts_extension") |
| 99 .Set("description", "an extension") | 114 .Set("description", "an extension") |
| 100 .Set("manifest_version", 2) | 115 .Set("manifest_version", 2) |
| 101 .Set("version", "1.0.0") | 116 .Set("version", "1.0.0") |
| 102 .Set("permissions", | 117 .Set("permissions", |
| 103 ListBuilder().Append(kAllHostsPermission))) | 118 ListBuilder().Append(kAllHostsPermission))) |
| 104 .SetLocation(Manifest::INTERNAL) | 119 .SetLocation(Manifest::INTERNAL) |
| 105 .SetID(kId) | 120 .SetID(id) |
| 106 .Build(); | 121 .Build(); |
| 107 | 122 |
| 108 ExtensionRegistry::Get(profile())->AddEnabled(extension); | 123 ExtensionRegistry::Get(profile())->AddEnabled(extension); |
| 109 PermissionsUpdater(profile()).InitializePermissions(extension); | 124 PermissionsUpdater(profile()).InitializePermissions(extension); |
| 110 return extension; | 125 return extension; |
| 111 } | 126 } |
| 112 | 127 |
| 128 const Extension* ActiveScriptControllerUnitTest::ReloadExtension( |
| 129 const std::string& id) { |
| 130 std::string extension_id = id; |
| 131 ExtensionRegistry::Get(profile())->RemoveEnabled(extension_id); |
| 132 return AddExtension(extension_id); |
| 133 } |
| 134 |
| 113 bool ActiveScriptControllerUnitTest::RequiresUserConsent( | 135 bool ActiveScriptControllerUnitTest::RequiresUserConsent( |
| 114 const Extension* extension) const { | 136 const Extension* extension) const { |
| 115 PermissionsData::AccessType access_type = | 137 PermissionsData::AccessType access_type = |
| 116 controller()->RequiresUserConsentForScriptInjectionForTesting( | 138 controller()->RequiresUserConsentForScriptInjectionForTesting( |
| 117 extension, UserScript::PROGRAMMATIC_SCRIPT); | 139 extension, UserScript::PROGRAMMATIC_SCRIPT); |
| 118 // We should never downright refuse access in these tests. | 140 // We should never downright refuse access in these tests. |
| 119 DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type); | 141 DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type); |
| 120 return access_type == PermissionsData::ACCESS_WITHHELD; | 142 return access_type == PermissionsData::ACCESS_WITHHELD; |
| 121 } | 143 } |
| 122 | 144 |
| 123 void ActiveScriptControllerUnitTest::RequestInjection( | 145 void ActiveScriptControllerUnitTest::RequestInjection( |
| 124 const Extension* extension) { | 146 const Extension* extension) { |
| 125 controller()->RequestScriptInjectionForTesting( | 147 controller()->RequestScriptInjectionForTesting( |
| 126 extension, | 148 extension, |
| 127 GetExecutionCallbackForExtension(extension->id())); | 149 GetExecutionCallbackForExtension(extension->id())); |
| 128 } | 150 } |
| 129 | 151 |
| 152 void ActiveScriptControllerUnitTest::AlwaysRun(const Extension* extension) { |
| 153 controller()->AlwaysRunOnVisibleHost(extension); |
| 154 } |
| 155 |
| 130 size_t ActiveScriptControllerUnitTest::GetExecutionCountForExtension( | 156 size_t ActiveScriptControllerUnitTest::GetExecutionCountForExtension( |
| 131 const std::string& extension_id) const { | 157 const std::string& extension_id) const { |
| 132 std::map<std::string, int>::const_iterator iter = | 158 std::map<std::string, int>::const_iterator iter = |
| 133 extension_executions_.find(extension_id); | 159 extension_executions_.find(extension_id); |
| 134 if (iter != extension_executions_.end()) | 160 if (iter != extension_executions_.end()) |
| 135 return iter->second; | 161 return iter->second; |
| 136 return 0u; | 162 return 0u; |
| 137 } | 163 } |
| 138 | 164 |
| 139 base::Closure ActiveScriptControllerUnitTest::GetExecutionCallbackForExtension( | 165 base::Closure ActiveScriptControllerUnitTest::GetExecutionCallbackForExtension( |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 | 341 |
| 316 // Turning off the preference should have instant effect. | 342 // Turning off the preference should have instant effect. |
| 317 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false); | 343 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false); |
| 318 EXPECT_TRUE(RequiresUserConsent(extension)); | 344 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 319 | 345 |
| 320 // And should also persist across navigations and websites. | 346 // And should also persist across navigations and websites. |
| 321 NavigateAndCommit(GURL("http://www.bar.com")); | 347 NavigateAndCommit(GURL("http://www.bar.com")); |
| 322 EXPECT_TRUE(RequiresUserConsent(extension)); | 348 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 323 } | 349 } |
| 324 | 350 |
| 351 TEST_F(ActiveScriptControllerUnitTest, TestAlwaysRun) { |
| 352 const Extension* extension = AddExtension(); |
| 353 ASSERT_TRUE(extension); |
| 354 |
| 355 NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl")); |
| 356 |
| 357 // Ensure that there aren't any executions pending. |
| 358 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 359 ASSERT_FALSE(controller()->GetActionForExtension(extension)); |
| 360 |
| 361 // Since the extension requests all_hosts, we should require user consent. |
| 362 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 363 |
| 364 // Request an injection. There should be an action visible, but no executions. |
| 365 RequestInjection(extension); |
| 366 EXPECT_TRUE(controller()->GetActionForExtension(extension)); |
| 367 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
| 368 |
| 369 AlwaysRun(extension); |
| 370 |
| 371 // The extension should execute, and the action should go away. |
| 372 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
| 373 EXPECT_FALSE(controller()->GetActionForExtension(extension)); |
| 374 |
| 375 // Since we already executed on the given page, we shouldn't need permission |
| 376 // for a second time. |
| 377 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 378 |
| 379 // Navigating to another site that hasn't been granted a persisted permission |
| 380 // should necessitate user consent. |
| 381 NavigateAndCommit(GURL("https://www.foo.com/bar")); |
| 382 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 383 |
| 384 // We shouldn't need user permission upon returning to the original origin. |
| 385 NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
| 386 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 387 |
| 388 // Reloading the extension should clear active permissions, but not persisted |
| 389 // permissions. |
| 390 extension = ReloadExtension(extension->id()); |
| 391 Reload(); |
| 392 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 393 |
| 394 // Different host... |
| 395 NavigateAndCommit(GURL("https://www.foo.com/bar")); |
| 396 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 397 // Different scheme... |
| 398 NavigateAndCommit(GURL("http://www.google.com/foo/bar")); |
| 399 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 400 // Different subdomain... |
| 401 NavigateAndCommit(GURL("https://en.google.com/foo/bar")); |
| 402 EXPECT_TRUE(RequiresUserConsent(extension)); |
| 403 // Only the "always run" origin should be allowed to run without user consent. |
| 404 NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
| 405 EXPECT_FALSE(RequiresUserConsent(extension)); |
| 406 } |
| 407 |
| 325 } // namespace extensions | 408 } // namespace extensions |
| OLD | NEW |