Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: chrome/browser/extensions/active_script_controller_browsertest.cc

Issue 288053002: Block content scripts from executing until user grants permission (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CQ Time! Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/active_script_controller.h" 8 #include "chrome/browser/extensions/active_script_controller.h"
9 #include "chrome/browser/extensions/extension_action.h" 9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_browsertest.h" 10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h" 11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/extensions/location_bar_controller.h" 12 #include "chrome/browser/extensions/location_bar_controller.h"
13 #include "chrome/browser/extensions/tab_helper.h" 13 #include "chrome/browser/extensions/tab_helper.h"
14 #include "chrome/browser/extensions/test_extension_dir.h" 14 #include "chrome/browser/extensions/test_extension_dir.h"
15 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/ui_test_utils.h" 17 #include "chrome/test/base/ui_test_utils.h"
18 #include "extensions/common/feature_switch.h" 18 #include "extensions/common/feature_switch.h"
19 #include "extensions/common/switches.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h" 20 #include "net/test/embedded_test_server/embedded_test_server.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 namespace extensions { 23 namespace extensions {
23 24
24 namespace { 25 namespace {
25 26
26 const char kAllHostsScheme[] = "*://*/*"; 27 const char kAllHostsScheme[] = "*://*/*";
27 const char kExplicitHostsScheme[] = "http://127.0.0.1/*"; 28 const char kExplicitHostsScheme[] = "http://127.0.0.1/*";
28 const char kBackgroundScript[] = 29 const char kBackgroundScript[] =
(...skipping 25 matching lines...) Expand all
54 55
55 enum RequiresConsent { 56 enum RequiresConsent {
56 REQUIRES_CONSENT, 57 REQUIRES_CONSENT,
57 DOES_NOT_REQUIRE_CONSENT 58 DOES_NOT_REQUIRE_CONSENT
58 }; 59 };
59 60
60 } // namespace 61 } // namespace
61 62
62 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest { 63 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest {
63 public: 64 public:
64 ActiveScriptControllerBrowserTest() 65 ActiveScriptControllerBrowserTest() {}
65 : feature_override_(FeatureSwitch::scripts_require_action(),
66 FeatureSwitch::OVERRIDE_ENABLED) {}
67 66
67 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE;
68 virtual void CleanUpOnMainThread() OVERRIDE; 68 virtual void CleanUpOnMainThread() OVERRIDE;
69 69
70 // Returns an extension with the given |host_type| and |injection_type|. If 70 // Returns an extension with the given |host_type| and |injection_type|. If
71 // one already exists, the existing extension will be returned. Othewrwise, 71 // one already exists, the existing extension will be returned. Othewrwise,
72 // one will be created. 72 // one will be created.
73 // This could potentially return NULL if LoadExtension() fails. 73 // This could potentially return NULL if LoadExtension() fails.
74 const Extension* GetOrCreateExtension(HostType host_type, 74 const Extension* GetOrCreateExtension(HostType host_type,
75 InjectionType injection_type); 75 InjectionType injection_type);
76 76
77 private: 77 private:
78 FeatureSwitch::ScopedOverride feature_override_;
79 ScopedVector<TestExtensionDir> test_extension_dirs_; 78 ScopedVector<TestExtensionDir> test_extension_dirs_;
80 std::vector<const Extension*> extensions_; 79 std::vector<const Extension*> extensions_;
81 }; 80 };
82 81
82 void ActiveScriptControllerBrowserTest::SetUpCommandLine(
83 base::CommandLine* command_line) {
84 ExtensionBrowserTest::SetUpCommandLine(command_line);
85 // We append the actual switch to the commandline because it needs to be
86 // passed over to the renderer, which a FeatureSwitch::ScopedOverride will
87 // not do.
88 command_line->AppendSwitch(switches::kEnableScriptsRequireAction);
89 }
90
83 void ActiveScriptControllerBrowserTest::CleanUpOnMainThread() { 91 void ActiveScriptControllerBrowserTest::CleanUpOnMainThread() {
84 test_extension_dirs_.clear(); 92 test_extension_dirs_.clear();
85 } 93 }
86 94
87 const Extension* ActiveScriptControllerBrowserTest::GetOrCreateExtension( 95 const Extension* ActiveScriptControllerBrowserTest::GetOrCreateExtension(
88 HostType host_type, InjectionType injection_type) { 96 HostType host_type, InjectionType injection_type) {
89 std::string name = 97 std::string name =
90 base::StringPrintf( 98 base::StringPrintf(
91 "%s %s", 99 "%s %s",
92 injection_type == CONTENT_SCRIPT ? 100 injection_type == CONTENT_SCRIPT ?
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 267
260 // If the extension has permission, we should be able to simply wait for it 268 // If the extension has permission, we should be able to simply wait for it
261 // to execute. 269 // to execute.
262 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) { 270 if (requires_consent_ == DOES_NOT_REQUIRE_CONSENT) {
263 inject_success_listener_->WaitUntilSatisfied(); 271 inject_success_listener_->WaitUntilSatisfied();
264 return testing::AssertionSuccess(); 272 return testing::AssertionSuccess();
265 } 273 }
266 274
267 // Otherwise, we don't have permission, and have to grant it. Ensure the 275 // Otherwise, we don't have permission, and have to grant it. Ensure the
268 // script has *not* already executed. 276 // script has *not* already executed.
269 // Currently, it's okay for content scripts to execute, because we don't 277 if (inject_success_listener_->was_satisfied()) {
270 // block them.
271 // TODO(rdevlin.cronin): Fix this.
272 if (inject_success_listener_->was_satisfied() && type_ != CONTENT_SCRIPT) {
273 return testing::AssertionFailure() << 278 return testing::AssertionFailure() <<
274 name_ << "'s script ran without permission."; 279 name_ << "'s script ran without permission.";
275 } 280 }
276 281
277 // If we reach this point, we should always have an action. 282 // If we reach this point, we should always have an action.
278 DCHECK(action); 283 DCHECK(action);
279 284
280 // Grant permission by clicking on the extension action. 285 // Grant permission by clicking on the extension action.
281 location_bar_controller->OnClicked(action); 286 location_bar_controller->OnClicked(action);
282 287
(...skipping 26 matching lines...) Expand all
309 return location_bar_controller ? 314 return location_bar_controller ?
310 location_bar_controller->active_script_controller() : NULL; 315 location_bar_controller->active_script_controller() : NULL;
311 } 316 }
312 317
313 ExtensionAction* ActiveScriptTester::GetAction() { 318 ExtensionAction* ActiveScriptTester::GetAction() {
314 ActiveScriptController* controller = GetActiveScriptController(); 319 ActiveScriptController* controller = GetActiveScriptController();
315 return controller ? controller->GetActionForExtension(extension_) : NULL; 320 return controller ? controller->GetActionForExtension(extension_) : NULL;
316 } 321 }
317 322
318 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest, 323 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest,
319 ActiveScriptsAreDisplayed) { 324 ActiveScriptsAreDisplayedAndDelayExecution) {
320 base::FilePath active_script_path = 325 base::FilePath active_script_path =
321 test_data_dir_.AppendASCII("active_script"); 326 test_data_dir_.AppendASCII("active_script");
322 327
323 const char* kExtensionNames[] = { 328 const char* kExtensionNames[] = {
324 "inject_scripts_all_hosts", 329 "inject_scripts_all_hosts",
325 "inject_scripts_explicit_hosts", 330 "inject_scripts_explicit_hosts",
326 "content_scripts_all_hosts", 331 "content_scripts_all_hosts",
327 "content_scripts_explicit_hosts" 332 "content_scripts_explicit_hosts"
328 }; 333 };
329 334
(...skipping 25 matching lines...) Expand all
355 CONTENT_SCRIPT), 360 CONTENT_SCRIPT),
356 ActiveScriptTester( 361 ActiveScriptTester(
357 kExtensionNames[3], 362 kExtensionNames[3],
358 GetOrCreateExtension(EXPLICIT_HOSTS, CONTENT_SCRIPT), 363 GetOrCreateExtension(EXPLICIT_HOSTS, CONTENT_SCRIPT),
359 browser(), 364 browser(),
360 DOES_NOT_REQUIRE_CONSENT, 365 DOES_NOT_REQUIRE_CONSENT,
361 CONTENT_SCRIPT), 366 CONTENT_SCRIPT),
362 }; 367 };
363 368
364 // Navigate to an URL (which matches the explicit host specified in the 369 // Navigate to an URL (which matches the explicit host specified in the
365 // extension content_scripts_explicit_hosts). All three extensions should 370 // extension content_scripts_explicit_hosts). All four extensions should
366 // inject the script. 371 // inject the script.
367 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 372 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
368 ui_test_utils::NavigateToURL( 373 ui_test_utils::NavigateToURL(
369 browser(), embedded_test_server()->GetURL("/extensions/test_file.html")); 374 browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
370 375
371 for (size_t i = 0u; i < arraysize(testers); ++i) 376 for (size_t i = 0u; i < arraysize(testers); ++i)
372 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i]; 377 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i];
373 } 378 }
374 379
380 // A version of the test with the flag off, in order to test that everything
381 // still works as expected.
382 class FlagOffActiveScriptControllerBrowserTest
383 : public ActiveScriptControllerBrowserTest {
384 private:
385 // Simply don't append the flag.
386 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE {
387 ExtensionBrowserTest::SetUpCommandLine(command_line);
388 }
389 };
390
391 IN_PROC_BROWSER_TEST_F(FlagOffActiveScriptControllerBrowserTest,
392 ScriptsExecuteWhenFlagAbsent) {
393 const char* kExtensionNames[] = {
394 "content_scripts_all_hosts",
395 "inject_scripts_all_hosts",
396 };
397 ActiveScriptTester testers[] = {
398 ActiveScriptTester(
399 kExtensionNames[0],
400 GetOrCreateExtension(ALL_HOSTS, CONTENT_SCRIPT),
401 browser(),
402 DOES_NOT_REQUIRE_CONSENT,
403 CONTENT_SCRIPT),
404 ActiveScriptTester(
405 kExtensionNames[1],
406 GetOrCreateExtension(ALL_HOSTS, EXECUTE_SCRIPT),
407 browser(),
408 DOES_NOT_REQUIRE_CONSENT,
409 EXECUTE_SCRIPT),
410 };
411
412 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
413 ui_test_utils::NavigateToURL(
414 browser(), embedded_test_server()->GetURL("/extensions/test_file.html"));
415
416 for (size_t i = 0u; i < arraysize(testers); ++i)
417 EXPECT_TRUE(testers[i].Verify()) << kExtensionNames[i];
418 }
419
375 } // namespace extensions 420 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/active_script_controller.cc ('k') | chrome/browser/extensions/user_script_master.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698