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

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

Issue 2766263003: Extensions: Only load incognito-enabled extensions in an incognito renderer. (Closed)
Patch Set: Rebase. Created 3 years, 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_piece.h" 18 #include "base/strings/string_piece.h"
19 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
21 #include "base/synchronization/waitable_event.h" 21 #include "base/synchronization/waitable_event.h"
22 #include "base/values.h" 22 #include "base/values.h"
23 #include "build/build_config.h" 23 #include "build/build_config.h"
24 #include "chrome/browser/chrome_notification_types.h" 24 #include "chrome/browser/chrome_notification_types.h"
25 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" 25 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h"
26 #include "chrome/browser/extensions/extension_apitest.h" 26 #include "chrome/browser/extensions/extension_apitest.h"
27 #include "chrome/browser/extensions/extension_util.h"
27 #include "chrome/browser/extensions/test_extension_dir.h" 28 #include "chrome/browser/extensions/test_extension_dir.h"
28 #include "chrome/browser/infobars/infobar_service.h" 29 #include "chrome/browser/infobars/infobar_service.h"
29 #include "chrome/browser/profiles/profile.h" 30 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/ui/browser.h" 31 #include "chrome/browser/ui/browser.h"
31 #include "chrome/browser/ui/browser_commands.h" 32 #include "chrome/browser/ui/browser_commands.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h" 33 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/common/chrome_paths.h" 34 #include "chrome/common/chrome_paths.h"
34 #include "chrome/common/chrome_switches.h" 35 #include "chrome/common/chrome_switches.h"
35 #include "chrome/test/base/ui_test_utils.h" 36 #include "chrome/test/base/ui_test_utils.h"
36 #include "content/public/browser/notification_registrar.h" 37 #include "content/public/browser/notification_registrar.h"
37 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/notification_service.h"
38 #include "content/public/test/browser_test_utils.h" 39 #include "content/public/test/browser_test_utils.h"
39 #include "content/public/test/test_utils.h" 40 #include "content/public/test/test_utils.h"
40 #include "extensions/browser/event_router.h" 41 #include "extensions/browser/event_router.h"
41 #include "extensions/browser/extension_prefs.h" 42 #include "extensions/browser/extension_prefs.h"
43 #include "extensions/browser/extension_registry.h"
42 #include "extensions/browser/extension_system.h" 44 #include "extensions/browser/extension_system.h"
43 #include "extensions/browser/process_manager.h" 45 #include "extensions/browser/process_manager.h"
46 #include "extensions/browser/test_extension_registry_observer.h"
44 #include "extensions/common/api/runtime.h" 47 #include "extensions/common/api/runtime.h"
45 #include "extensions/common/extension_builder.h" 48 #include "extensions/common/extension_builder.h"
46 #include "extensions/common/value_builder.h" 49 #include "extensions/common/value_builder.h"
47 #include "extensions/test/extension_test_message_listener.h" 50 #include "extensions/test/extension_test_message_listener.h"
48 #include "extensions/test/result_catcher.h" 51 #include "extensions/test/result_catcher.h"
49 #include "net/cert/asn1_util.h" 52 #include "net/cert/asn1_util.h"
50 #include "net/cert/jwk_serializer.h" 53 #include "net/cert/jwk_serializer.h"
51 #include "net/dns/mock_host_resolver.h" 54 #include "net/dns/mock_host_resolver.h"
52 #include "net/ssl/channel_id_service.h" 55 #include "net/ssl/channel_id_service.h"
53 #include "net/test/embedded_test_server/embedded_test_server.h" 56 #include "net/test/embedded_test_server/embedded_test_server.h"
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 344
342 GURL popup_opener_url() { 345 GURL popup_opener_url() {
343 return GetURLForPath("www.chromium.org", "/popup_opener.html"); 346 return GetURLForPath("www.chromium.org", "/popup_opener.html");
344 } 347 }
345 348
346 GURL google_com_url() { 349 GURL google_com_url() {
347 return GetURLForPath("www.google.com", "/google.com.html"); 350 return GetURLForPath("www.google.com", "/google.com.html");
348 } 351 }
349 352
350 scoped_refptr<const Extension> LoadChromiumConnectableExtension() { 353 scoped_refptr<const Extension> LoadChromiumConnectableExtension() {
351 scoped_refptr<const Extension> extension = 354 scoped_refptr<const Extension> extension = LoadExtensionIntoDir(
352 LoadExtensionIntoDir(&web_connectable_dir_, 355 &web_connectable_dir_extension_,
353 base::StringPrintf( 356 base::StringPrintf("{"
354 "{" 357 " \"name\": \"chromium_connectable\","
355 " \"name\": \"chromium_connectable\"," 358 " %s,"
356 " %s," 359 " \"externally_connectable\": {"
357 " \"externally_connectable\": {" 360 " \"matches\": [\"*://*.chromium.org:*/*\"]"
358 " \"matches\": [\"*://*.chromium.org:*/*\"]" 361 " }"
359 " }" 362 "}",
360 "}", 363 common_manifest()));
361 common_manifest()));
362 CHECK(extension.get()); 364 CHECK(extension.get());
363 return extension; 365 return extension;
364 } 366 }
365 367
366 scoped_refptr<const Extension> LoadChromiumConnectableApp( 368 scoped_refptr<const Extension> LoadChromiumConnectableApp(
367 bool with_event_handlers = true) { 369 bool with_event_handlers = true) {
368 scoped_refptr<const Extension> extension = 370 scoped_refptr<const Extension> extension =
369 LoadExtensionIntoDir(&web_connectable_dir_, 371 LoadExtensionIntoDir(&web_connectable_dir_app_,
370 "{" 372 "{"
371 " \"app\": {" 373 " \"app\": {"
372 " \"background\": {" 374 " \"background\": {"
373 " \"scripts\": [\"background.js\"]" 375 " \"scripts\": [\"background.js\"]"
374 " }" 376 " }"
375 " }," 377 " },"
376 " \"externally_connectable\": {" 378 " \"externally_connectable\": {"
377 " \"matches\": [\"*://*.chromium.org:*/*\"]" 379 " \"matches\": [\"*://*.chromium.org:*/*\"]"
378 " }," 380 " },"
379 " \"manifest_version\": 2," 381 " \"manifest_version\": 2,"
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 args += include_tls_channel_id ? "true" : "false"; 504 args += include_tls_channel_id ? "true" : "false";
503 if (message) 505 if (message)
504 args += std::string(", '") + message + "'"; 506 args += std::string(", '") + message + "'";
505 CHECK(content::ExecuteScriptAndExtractString( 507 CHECK(content::ExecuteScriptAndExtractString(
506 browser()->tab_strip_model()->GetActiveWebContents(), 508 browser()->tab_strip_model()->GetActiveWebContents(),
507 base::StringPrintf("assertions.%s(%s)", method, args.c_str()), 509 base::StringPrintf("assertions.%s(%s)", method, args.c_str()),
508 &result)); 510 &result));
509 return result; 511 return result;
510 } 512 }
511 513
512 TestExtensionDir web_connectable_dir_; 514 TestExtensionDir web_connectable_dir_extension_;
karandeepb 2017/04/04 03:44:16 Necessary so that they don't get the same extensio
515 TestExtensionDir web_connectable_dir_app_;
513 TestExtensionDir not_connectable_dir_; 516 TestExtensionDir not_connectable_dir_;
514 TestExtensionDir tls_channel_id_connectable_dir_; 517 TestExtensionDir tls_channel_id_connectable_dir_;
515 TestExtensionDir hosted_app_dir_; 518 TestExtensionDir hosted_app_dir_;
516 }; 519 };
517 520
518 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, NotInstalled) { 521 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, NotInstalled) {
519 InitializeTestServer(); 522 InitializeTestServer();
520 523
521 scoped_refptr<const Extension> extension = 524 scoped_refptr<const Extension> extension =
522 ExtensionBuilder() 525 ExtensionBuilder()
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount()); 767 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
765 } 768 }
766 769
767 // It's not possible to allow an app in incognito. 770 // It's not possible to allow an app in incognito.
768 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(app->id(), true); 771 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(app->id(), true);
769 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, 772 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
770 CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL)); 773 CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
771 } 774 }
772 775
773 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, 776 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
774 FromIncognitoDenyExtension) { 777 FromIncognitoDenyExtensionAndApp) {
775 InitializeTestServer(); 778 InitializeTestServer();
776 779
777 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension(); 780 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
778 781
779 Browser* incognito_browser = OpenURLOffTheRecord( 782 Browser* incognito_browser = OpenURLOffTheRecord(
780 profile()->GetOffTheRecordProfile(), chromium_org_url()); 783 profile()->GetOffTheRecordProfile(), chromium_org_url());
781 content::RenderFrameHost* incognito_frame = 784 content::RenderFrameHost* incognito_frame =
782 incognito_browser->tab_strip_model() 785 incognito_browser->tab_strip_model()
783 ->GetActiveWebContents() 786 ->GetActiveWebContents()
784 ->GetMainFrame(); 787 ->GetMainFrame();
785 788
786 { 789 IncognitoConnectability::ScopedAlertTracker alert_tracker(
787 IncognitoConnectability::ScopedAlertTracker alert_tracker( 790 IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY);
788 IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY);
789 791
790 // The alert doesn't show for extensions. 792 // Since there is no externally connectible extension loaded into the
791 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, 793 // incognito renderer, the chrome.runtime API won't be defined.
792 CanConnectAndSendMessagesToFrame( 794 EXPECT_EQ(NAMESPACE_NOT_DEFINED,
793 incognito_frame, extension.get(), NULL)); 795 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(),
794 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount()); 796 nullptr));
795 }
796 797
797 // Allowing the extension in incognito mode will bypass the deny. 798 // Loading a platform app in the renderer should cause the chrome.runtime
798 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true); 799 // bindings to be generated in the renderer.
799 EXPECT_EQ( 800 LoadChromiumConnectableApp();
800 OK, 801 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
801 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL)); 802 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(),
803 nullptr));
804
805 // Allowing the extension in incognito mode loads the extension in the
806 // incognito renderer, allowing it to receive connections.
807 TestExtensionRegistryObserver observer(
808 ExtensionRegistry::Get(profile()->GetOffTheRecordProfile()),
809 extension->id());
810 util::SetIsIncognitoEnabled(extension->id(),
811 profile()->GetOffTheRecordProfile(), true);
812 const Extension* loaded_extension = observer.WaitForExtensionLoaded();
813 EXPECT_EQ(OK, CanConnectAndSendMessagesToFrame(incognito_frame,
814 loaded_extension, nullptr));
815
816 // No alert is shown for extensions which support being enabled in incognito
817 // mode.
818 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
802 } 819 }
803 820
804 // Tests connection from incognito tabs when the extension doesn't have an event 821 // Tests connection from incognito tabs when the extension doesn't have an event
805 // handler for the connection event. 822 // handler for the connection event.
806 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, 823 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
807 FromIncognitoNoEventHandlerInApp) { 824 FromIncognitoNoEventHandlerInApp) {
808 InitializeTestServer(); 825 InitializeTestServer();
809 826
810 scoped_refptr<const Extension> app = LoadChromiumConnectableApp(false); 827 scoped_refptr<const Extension> app = LoadChromiumConnectableApp(false);
811 ASSERT_TRUE(app->is_platform_app()); 828 ASSERT_TRUE(app->is_platform_app());
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 968
952 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension(); 969 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
953 970
954 Browser* incognito_browser = OpenURLOffTheRecord( 971 Browser* incognito_browser = OpenURLOffTheRecord(
955 profile()->GetOffTheRecordProfile(), chromium_org_url()); 972 profile()->GetOffTheRecordProfile(), chromium_org_url());
956 content::RenderFrameHost* incognito_frame = 973 content::RenderFrameHost* incognito_frame =
957 incognito_browser->tab_strip_model() 974 incognito_browser->tab_strip_model()
958 ->GetActiveWebContents() 975 ->GetActiveWebContents()
959 ->GetMainFrame(); 976 ->GetMainFrame();
960 977
961 { 978 IncognitoConnectability::ScopedAlertTracker alert_tracker(
962 IncognitoConnectability::ScopedAlertTracker alert_tracker( 979 IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
963 IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
964 980
965 // No alert is shown. 981 // Since there is no externally connectible extension loaded into the
966 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, 982 // incognito renderer, the chrome.runtime API won't be defined.
967 CanConnectAndSendMessagesToFrame( 983 EXPECT_EQ(NAMESPACE_NOT_DEFINED,
968 incognito_frame, extension.get(), NULL)); 984 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(),
969 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount()); 985 nullptr));
970 }
971 986
972 // Allowing the extension in incognito mode is what allows connections. 987 // Allowing the extension in incognito mode loads the extension in the
973 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true); 988 // incognito renderer, causing the chrome.runtime bindings to be generated in
974 EXPECT_EQ( 989 // the renderer and allowing the extension to receive connections.
975 OK, 990 TestExtensionRegistryObserver observer(
976 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL)); 991 ExtensionRegistry::Get(profile()->GetOffTheRecordProfile()),
992 extension->id());
993 util::SetIsIncognitoEnabled(extension->id(),
994 profile()->GetOffTheRecordProfile(), true);
995 const Extension* loaded_extension = observer.WaitForExtensionLoaded();
996 EXPECT_EQ(OK, CanConnectAndSendMessagesToFrame(incognito_frame,
997 loaded_extension, nullptr));
998
999 // No alert is shown for extensions which support being enabled in incognito
1000 // mode.
1001 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
977 } 1002 }
978 1003
979 // Tests a connection from an iframe within a tab which doesn't have 1004 // Tests a connection from an iframe within a tab which doesn't have
980 // permission. Iframe should work. 1005 // permission. Iframe should work.
981 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, 1006 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
982 FromIframeWithPermission) { 1007 FromIframeWithPermission) {
983 InitializeTestServer(); 1008 InitializeTestServer();
984 1009
985 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension(); 1010 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
986 1011
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1324 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 1349 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
1325 background_contents, 1350 background_contents,
1326 "window.domAutomationController.send(window.messageCount);", 1351 "window.domAutomationController.send(window.messageCount);",
1327 &message_count)); 1352 &message_count));
1328 EXPECT_EQ(1, message_count); 1353 EXPECT_EQ(1, message_count);
1329 } 1354 }
1330 1355
1331 } // namespace 1356 } // namespace
1332 1357
1333 }; // namespace extensions 1358 }; // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/messaging/message_service.cc ('k') | content/public/test/test_browser_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698