| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); | 213 EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); |
| 214 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); | 214 EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); |
| 215 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); | 215 EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); |
| 216 EXPECT_TRUE(extension->permissions_data()->HasEffectiveAccessToAllHosts()); | 216 EXPECT_TRUE(extension->permissions_data()->HasEffectiveAccessToAllHosts()); |
| 217 | 217 |
| 218 // Tab-specific permissions should be included in the effective hosts. | 218 // Tab-specific permissions should be included in the effective hosts. |
| 219 GURL tab_url("http://www.example.com/"); | 219 GURL tab_url("http://www.example.com/"); |
| 220 URLPatternSet new_hosts; | 220 URLPatternSet new_hosts; |
| 221 new_hosts.AddOrigin(URLPattern::SCHEME_ALL, tab_url); | 221 new_hosts.AddOrigin(URLPattern::SCHEME_ALL, tab_url); |
| 222 extension->permissions_data()->UpdateTabSpecificPermissions( | 222 extension->permissions_data()->UpdateTabSpecificPermissions( |
| 223 1, | 223 1, PermissionSet(APIPermissionSet(), ManifestPermissionSet(), new_hosts, |
| 224 new PermissionSet(APIPermissionSet(), | 224 URLPatternSet())); |
| 225 ManifestPermissionSet(), | |
| 226 new_hosts, | |
| 227 URLPatternSet())); | |
| 228 EXPECT_TRUE(extension->permissions_data()->GetEffectiveHostPermissions(). | 225 EXPECT_TRUE(extension->permissions_data()->GetEffectiveHostPermissions(). |
| 229 MatchesURL(tab_url)); | 226 MatchesURL(tab_url)); |
| 230 extension->permissions_data()->ClearTabSpecificPermissions(1); | 227 extension->permissions_data()->ClearTabSpecificPermissions(1); |
| 231 EXPECT_FALSE(extension->permissions_data()->GetEffectiveHostPermissions(). | 228 EXPECT_FALSE(extension->permissions_data()->GetEffectiveHostPermissions(). |
| 232 MatchesURL(tab_url)); | 229 MatchesURL(tab_url)); |
| 233 } | 230 } |
| 234 | 231 |
| 235 TEST(PermissionsDataTest, SocketPermissions) { | 232 TEST(PermissionsDataTest, SocketPermissions) { |
| 236 scoped_refptr<Extension> extension; | 233 scoped_refptr<Extension> extension; |
| 237 std::string error; | 234 std::string error; |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 EXPECT_TRUE(Blocked(extension.get(), about_url)); | 685 EXPECT_TRUE(Blocked(extension.get(), about_url)); |
| 689 EXPECT_TRUE(Blocked(extension.get(), extension_url)); | 686 EXPECT_TRUE(Blocked(extension.get(), extension_url)); |
| 690 EXPECT_FALSE(extension->permissions_data()->HasHostPermission(settings_url)); | 687 EXPECT_FALSE(extension->permissions_data()->HasHostPermission(settings_url)); |
| 691 } | 688 } |
| 692 | 689 |
| 693 TEST_F(ExtensionScriptAndCaptureVisibleTest, TabSpecific) { | 690 TEST_F(ExtensionScriptAndCaptureVisibleTest, TabSpecific) { |
| 694 scoped_refptr<Extension> extension = | 691 scoped_refptr<Extension> extension = |
| 695 LoadManifestStrict("script_and_capture", "tab_specific.json"); | 692 LoadManifestStrict("script_and_capture", "tab_specific.json"); |
| 696 | 693 |
| 697 const PermissionsData* permissions_data = extension->permissions_data(); | 694 const PermissionsData* permissions_data = extension->permissions_data(); |
| 698 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(0).get()); | 695 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(0)); |
| 699 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(1).get()); | 696 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(1)); |
| 700 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(2).get()); | 697 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(2)); |
| 701 | 698 |
| 702 std::set<GURL> no_urls; | 699 std::set<GURL> no_urls; |
| 703 | 700 |
| 704 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); | 701 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); |
| 705 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); | 702 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); |
| 706 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); | 703 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); |
| 707 | 704 |
| 708 URLPatternSet allowed_hosts; | 705 URLPatternSet allowed_hosts; |
| 709 allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, | 706 allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, |
| 710 http_url.spec())); | 707 http_url.spec())); |
| 711 std::set<GURL> allowed_urls; | 708 std::set<GURL> allowed_urls; |
| 712 allowed_urls.insert(http_url); | 709 allowed_urls.insert(http_url); |
| 713 // http_url_with_path() will also be allowed, because Extension should be | 710 // http_url_with_path() will also be allowed, because Extension should be |
| 714 // considering the security origin of the URL not the URL itself, and | 711 // considering the security origin of the URL not the URL itself, and |
| 715 // http_url is in allowed_hosts. | 712 // http_url is in allowed_hosts. |
| 716 allowed_urls.insert(http_url_with_path); | 713 allowed_urls.insert(http_url_with_path); |
| 717 | 714 |
| 718 { | 715 { |
| 719 scoped_refptr<PermissionSet> permissions( | 716 PermissionSet permissions(APIPermissionSet(), ManifestPermissionSet(), |
| 720 new PermissionSet(APIPermissionSet(), ManifestPermissionSet(), | 717 allowed_hosts, URLPatternSet()); |
| 721 allowed_hosts, URLPatternSet())); | |
| 722 permissions_data->UpdateTabSpecificPermissions(0, permissions); | 718 permissions_data->UpdateTabSpecificPermissions(0, permissions); |
| 723 EXPECT_EQ(permissions->explicit_hosts(), | 719 EXPECT_EQ(permissions.explicit_hosts(), |
| 724 permissions_data->GetTabSpecificPermissionsForTesting(0) | 720 permissions_data->GetTabSpecificPermissionsForTesting(0) |
| 725 ->explicit_hosts()); | 721 ->explicit_hosts()); |
| 726 } | 722 } |
| 727 | 723 |
| 728 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), allowed_urls, 0)); | 724 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), allowed_urls, 0)); |
| 729 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); | 725 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); |
| 730 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); | 726 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); |
| 731 | 727 |
| 732 permissions_data->ClearTabSpecificPermissions(0); | 728 permissions_data->ClearTabSpecificPermissions(0); |
| 733 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(0).get()); | 729 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(0)); |
| 734 | 730 |
| 735 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); | 731 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); |
| 736 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); | 732 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); |
| 737 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); | 733 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); |
| 738 | 734 |
| 739 std::set<GURL> more_allowed_urls = allowed_urls; | 735 std::set<GURL> more_allowed_urls = allowed_urls; |
| 740 more_allowed_urls.insert(https_url); | 736 more_allowed_urls.insert(https_url); |
| 741 URLPatternSet more_allowed_hosts = allowed_hosts; | 737 URLPatternSet more_allowed_hosts = allowed_hosts; |
| 742 more_allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, | 738 more_allowed_hosts.AddPattern(URLPattern(URLPattern::SCHEME_ALL, |
| 743 https_url.spec())); | 739 https_url.spec())); |
| 744 | 740 |
| 745 { | 741 { |
| 746 scoped_refptr<PermissionSet> permissions( | 742 PermissionSet permissions1(APIPermissionSet(), ManifestPermissionSet(), |
| 747 new PermissionSet(APIPermissionSet(), ManifestPermissionSet(), | 743 allowed_hosts, URLPatternSet()); |
| 748 allowed_hosts, URLPatternSet())); | 744 permissions_data->UpdateTabSpecificPermissions(0, permissions1); |
| 749 permissions_data->UpdateTabSpecificPermissions(0, permissions); | 745 EXPECT_EQ(permissions1.explicit_hosts(), |
| 750 EXPECT_EQ(permissions->explicit_hosts(), | |
| 751 permissions_data->GetTabSpecificPermissionsForTesting(0) | 746 permissions_data->GetTabSpecificPermissionsForTesting(0) |
| 752 ->explicit_hosts()); | 747 ->explicit_hosts()); |
| 753 | 748 |
| 754 permissions = new PermissionSet(APIPermissionSet(), | 749 PermissionSet permissions2(APIPermissionSet(), ManifestPermissionSet(), |
| 755 ManifestPermissionSet(), | 750 more_allowed_hosts, URLPatternSet()); |
| 756 more_allowed_hosts, | 751 permissions_data->UpdateTabSpecificPermissions(1, permissions2); |
| 757 URLPatternSet()); | 752 EXPECT_EQ(permissions2.explicit_hosts(), |
| 758 permissions_data->UpdateTabSpecificPermissions(1, permissions); | |
| 759 EXPECT_EQ(permissions->explicit_hosts(), | |
| 760 permissions_data->GetTabSpecificPermissionsForTesting(1) | 753 permissions_data->GetTabSpecificPermissionsForTesting(1) |
| 761 ->explicit_hosts()); | 754 ->explicit_hosts()); |
| 762 } | 755 } |
| 763 | 756 |
| 764 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), allowed_urls, 0)); | 757 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), allowed_urls, 0)); |
| 765 EXPECT_TRUE( | 758 EXPECT_TRUE( |
| 766 ScriptAllowedExclusivelyOnTab(extension.get(), more_allowed_urls, 1)); | 759 ScriptAllowedExclusivelyOnTab(extension.get(), more_allowed_urls, 1)); |
| 767 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); | 760 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); |
| 768 | 761 |
| 769 permissions_data->ClearTabSpecificPermissions(0); | 762 permissions_data->ClearTabSpecificPermissions(0); |
| 770 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(0).get()); | 763 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(0)); |
| 771 | 764 |
| 772 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); | 765 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); |
| 773 EXPECT_TRUE( | 766 EXPECT_TRUE( |
| 774 ScriptAllowedExclusivelyOnTab(extension.get(), more_allowed_urls, 1)); | 767 ScriptAllowedExclusivelyOnTab(extension.get(), more_allowed_urls, 1)); |
| 775 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); | 768 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); |
| 776 | 769 |
| 777 permissions_data->ClearTabSpecificPermissions(1); | 770 permissions_data->ClearTabSpecificPermissions(1); |
| 778 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(1).get()); | 771 EXPECT_FALSE(permissions_data->GetTabSpecificPermissionsForTesting(1)); |
| 779 | 772 |
| 780 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); | 773 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 0)); |
| 781 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); | 774 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 1)); |
| 782 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); | 775 EXPECT_TRUE(ScriptAllowedExclusivelyOnTab(extension.get(), no_urls, 2)); |
| 783 } | 776 } |
| 784 | 777 |
| 785 } // namespace extensions | 778 } // namespace extensions |
| OLD | NEW |