blob: fbb26e79b4cf9b6fcfd5c0c5a6c6ba9b5fbc0900 [file] [log] [blame]
[email protected]c333e792012-01-06 16:57:391// 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
3// found in the LICENSE file.
4
[email protected]57999812013-02-24 05:40:525#include "base/files/file_path.h"
[email protected]ffbec692012-02-26 20:26:426#include "base/json/json_file_value_serializer.h"
[email protected]c333e792012-01-06 16:57:397#include "base/memory/ref_counted.h"
8#include "base/path_service.h"
[email protected]78089f02012-07-19 06:11:289#include "base/run_loop.h"
[email protected]23a85362014-07-07 23:26:1910#include "base/strings/stringprintf.h"
[email protected]c333e792012-01-06 16:57:3911#include "base/values.h"
[email protected]49a01e642013-07-12 00:29:4512#include "chrome/browser/chrome_notification_types.h"
[email protected]c333e792012-01-06 16:57:3913#include "chrome/browser/extensions/extension_service.h"
[email protected]f484f8d52014-06-12 08:38:1814#include "chrome/browser/extensions/extension_service_test_base.h"
[email protected]c333e792012-01-06 16:57:3915#include "chrome/browser/extensions/permissions_updater.h"
[email protected]c333e792012-01-06 16:57:3916#include "chrome/common/chrome_paths.h"
[email protected]04e4bbe2013-04-27 07:44:2417#include "chrome/common/extensions/extension_test_util.h"
[email protected]c333e792012-01-06 16:57:3918#include "chrome/test/base/testing_profile.h"
[email protected]c333e792012-01-06 16:57:3919#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
21#include "content/public/browser/notification_service.h"
[email protected]dccba4f82014-05-29 00:52:5622#include "extensions/browser/extension_prefs.h"
[email protected]e4452d32013-11-15 23:07:4123#include "extensions/common/extension.h"
[email protected]23a85362014-07-07 23:26:1924#include "extensions/common/extension_builder.h"
25#include "extensions/common/feature_switch.h"
[email protected]5a55f3f2013-10-29 01:08:2926#include "extensions/common/permissions/permission_set.h"
[email protected]076ebeda2014-06-06 21:47:2627#include "extensions/common/permissions/permissions_data.h"
[email protected]23a85362014-07-07 23:26:1928#include "extensions/common/value_builder.h"
[email protected]c333e792012-01-06 16:57:3929#include "testing/gtest/include/gtest/gtest.h"
30
[email protected]04e4bbe2013-04-27 07:44:2431using extension_test_util::LoadManifest;
32
[email protected]c333e792012-01-06 16:57:3933namespace extensions {
34
35namespace {
36
[email protected]23a85362014-07-07 23:26:1937scoped_refptr<const Extension> CreateExtensionWithPermissions(
38 const std::set<URLPattern>& scriptable_hosts,
39 const std::set<URLPattern>& explicit_hosts,
40 Manifest::Location location) {
41 ListBuilder scriptable_host_list;
42 for (std::set<URLPattern>::const_iterator pattern = scriptable_hosts.begin();
43 pattern != scriptable_hosts.end();
44 ++pattern) {
45 scriptable_host_list.Append(pattern->GetAsString());
46 }
47
48 ListBuilder explicit_host_list;
49 for (std::set<URLPattern>::const_iterator pattern = explicit_hosts.begin();
50 pattern != explicit_hosts.end();
51 ++pattern) {
52 explicit_host_list.Append(pattern->GetAsString());
53 }
54
55 DictionaryBuilder script;
56 script.Set("matches", scriptable_host_list.Pass())
57 .Set("js", ListBuilder().Append("foo.js"));
58
59 return ExtensionBuilder()
60 .SetLocation(location)
61 .SetManifest(
62 DictionaryBuilder()
63 .Set("name", "extension")
64 .Set("description", "foo")
65 .Set("manifest_version", 2)
66 .Set("version", "0.1.2.3")
67 .Set("content_scripts", ListBuilder().Append(script.Pass()))
68 .Set("permissions", explicit_host_list.Pass()))
69 .Build();
70}
71
72testing::AssertionResult SetsAreEqual(const std::set<URLPattern>& set1,
73 const std::set<URLPattern>& set2) {
74 // Take the (set1 - set2) U (set2 - set1). This is then the set of all
75 // elements which are in either set1 or set2, but not both.
76 // If the sets are equal, this is none.
77 std::set<URLPattern> difference = base::STLSetUnion<std::set<URLPattern> >(
78 base::STLSetDifference<std::set<URLPattern> >(set1, set2),
79 base::STLSetDifference<std::set<URLPattern> >(set2, set1));
80
81 std::string error;
82 for (std::set<URLPattern>::const_iterator iter = difference.begin();
83 iter != difference.end();
84 ++iter) {
85 if (iter->GetAsString() == "chrome://favicon/*")
86 continue; // Grr... This is auto-added for extensions with <all_urls>
87 error = base::StringPrintf("%s\n%s contains %s and the other does not.",
88 error.c_str(),
89 (set1.count(*iter) ? "Set1" : "Set2"),
90 iter->GetAsString().c_str());
91 }
92
93 if (!error.empty())
94 return testing::AssertionFailure() << error;
95 return testing::AssertionSuccess();
96}
97
[email protected]c333e792012-01-06 16:57:3998// A helper class that listens for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED.
99class PermissionsUpdaterListener : public content::NotificationObserver {
100 public:
101 PermissionsUpdaterListener()
102 : received_notification_(false), waiting_(false) {
103 registrar_.Add(this,
104 chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED,
105 content::NotificationService::AllSources());
106 }
107
108 void Reset() {
109 received_notification_ = false;
110 waiting_ = false;
111 extension_ = NULL;
112 permissions_ = NULL;
113 }
114
115 void Wait() {
116 if (received_notification_)
117 return;
118
119 waiting_ = true;
[email protected]78089f02012-07-19 06:11:28120 base::RunLoop run_loop;
121 run_loop.Run();
[email protected]c333e792012-01-06 16:57:39122 }
123
124 bool received_notification() const { return received_notification_; }
[email protected]dc24976f2013-06-02 21:15:09125 const Extension* extension() const { return extension_.get(); }
126 const PermissionSet* permissions() const { return permissions_.get(); }
127 UpdatedExtensionPermissionsInfo::Reason reason() const { return reason_; }
[email protected]c333e792012-01-06 16:57:39128
129 private:
130 virtual void Observe(int type,
131 const content::NotificationSource& source,
132 const content::NotificationDetails& details) OVERRIDE {
133 received_notification_ = true;
134 UpdatedExtensionPermissionsInfo* info =
135 content::Details<UpdatedExtensionPermissionsInfo>(details).ptr();
136
137 extension_ = info->extension;
138 permissions_ = info->permissions;
139 reason_ = info->reason;
140
141 if (waiting_) {
142 waiting_ = false;
[email protected]b3a25092013-05-28 22:08:16143 base::MessageLoopForUI::current()->Quit();
[email protected]c333e792012-01-06 16:57:39144 }
145 }
146
147 bool received_notification_;
148 bool waiting_;
149 content::NotificationRegistrar registrar_;
150 scoped_refptr<const Extension> extension_;
[email protected]c2e66e12012-06-27 06:27:06151 scoped_refptr<const PermissionSet> permissions_;
[email protected]c333e792012-01-06 16:57:39152 UpdatedExtensionPermissionsInfo::Reason reason_;
153};
154
155class PermissionsUpdaterTest : public ExtensionServiceTestBase {
156};
157
[email protected]04e4bbe2013-04-27 07:44:24158scoped_refptr<Extension> LoadOurManifest() {
[email protected]650b2d52013-02-10 03:41:45159 base::FilePath path;
[email protected]04e4bbe2013-04-27 07:44:24160 path = path.AppendASCII("api_test")
[email protected]c333e792012-01-06 16:57:39161 .AppendASCII("permissions")
[email protected]04e4bbe2013-04-27 07:44:24162 .AppendASCII("optional");
163 return LoadManifest(path.AsUTF8Unsafe(),
164 "manifest.json",
165 Manifest::INTERNAL,
166 Extension::NO_FLAGS);
[email protected]c333e792012-01-06 16:57:39167}
168
169void AddPattern(URLPatternSet* extent, const std::string& pattern) {
170 int schemes = URLPattern::SCHEME_ALL;
171 extent->AddPattern(URLPattern(schemes, pattern));
172}
173
174} // namespace
175
176// Test that the PermissionUpdater can correctly add and remove active
177// permissions. This tests all of PermissionsUpdater's public methods because
[email protected]23a85362014-07-07 23:26:19178// GrantActivePermissions and SetPermissions are used by AddPermissions.
[email protected]c333e792012-01-06 16:57:39179TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) {
180 InitializeEmptyExtensionService();
181
182 // Load the test extension.
[email protected]04e4bbe2013-04-27 07:44:24183 scoped_refptr<Extension> extension = LoadOurManifest();
184 ASSERT_TRUE(extension.get());
[email protected]c333e792012-01-06 16:57:39185
[email protected]c2e66e12012-06-27 06:27:06186 APIPermissionSet default_apis;
187 default_apis.insert(APIPermission::kManagement);
[email protected]e737c442013-11-15 15:55:24188 ManifestPermissionSet empty_manifest_permissions;
189
[email protected]c333e792012-01-06 16:57:39190 URLPatternSet default_hosts;
191 AddPattern(&default_hosts, "http://a.com/*");
[email protected]c2e66e12012-06-27 06:27:06192 scoped_refptr<PermissionSet> default_permissions =
[email protected]e737c442013-11-15 15:55:24193 new PermissionSet(default_apis, empty_manifest_permissions,
194 default_hosts, URLPatternSet());
[email protected]c333e792012-01-06 16:57:39195
196 // Make sure it loaded properly.
[email protected]c2e66e12012-06-27 06:27:06197 scoped_refptr<const PermissionSet> permissions =
[email protected]076ebeda2014-06-06 21:47:26198 extension->permissions_data()->active_permissions();
[email protected]cadac622013-06-11 16:46:36199 ASSERT_EQ(*default_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:26200 *extension->permissions_data()->active_permissions().get());
[email protected]c333e792012-01-06 16:57:39201
202 // Add a few permissions.
[email protected]c2e66e12012-06-27 06:27:06203 APIPermissionSet apis;
204 apis.insert(APIPermission::kTab);
205 apis.insert(APIPermission::kNotification);
[email protected]c333e792012-01-06 16:57:39206 URLPatternSet hosts;
207 AddPattern(&hosts, "http://*.c.com/*");
208
[email protected]c2e66e12012-06-27 06:27:06209 scoped_refptr<PermissionSet> delta =
[email protected]e737c442013-11-15 15:55:24210 new PermissionSet(apis, empty_manifest_permissions,
211 hosts, URLPatternSet());
[email protected]c333e792012-01-06 16:57:39212
213 PermissionsUpdaterListener listener;
214 PermissionsUpdater updater(profile_.get());
215 updater.AddPermissions(extension.get(), delta.get());
216
217 listener.Wait();
218
219 // Verify that the permission notification was sent correctly.
220 ASSERT_TRUE(listener.received_notification());
221 ASSERT_EQ(extension, listener.extension());
222 ASSERT_EQ(UpdatedExtensionPermissionsInfo::ADDED, listener.reason());
[email protected]dc24976f2013-06-02 21:15:09223 ASSERT_EQ(*delta.get(), *listener.permissions());
[email protected]c333e792012-01-06 16:57:39224
225 // Make sure the extension's active permissions reflect the change.
[email protected]c2e66e12012-06-27 06:27:06226 scoped_refptr<PermissionSet> active_permissions =
[email protected]dc24976f2013-06-02 21:15:09227 PermissionSet::CreateUnion(default_permissions.get(), delta.get());
[email protected]cadac622013-06-11 16:46:36228 ASSERT_EQ(*active_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:26229 *extension->permissions_data()->active_permissions().get());
[email protected]c333e792012-01-06 16:57:39230
231 // Verify that the new granted and active permissions were also stored
232 // in the extension preferences. In this case, the granted permissions should
233 // be equal to the active permissions.
[email protected]7c82539c2014-02-19 06:09:17234 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get());
[email protected]c2e66e12012-06-27 06:27:06235 scoped_refptr<PermissionSet> granted_permissions =
[email protected]c333e792012-01-06 16:57:39236 active_permissions;
237
[email protected]c2e66e12012-06-27 06:27:06238 scoped_refptr<PermissionSet> from_prefs =
[email protected]c333e792012-01-06 16:57:39239 prefs->GetActivePermissions(extension->id());
[email protected]dc24976f2013-06-02 21:15:09240 ASSERT_EQ(*active_permissions.get(), *from_prefs.get());
[email protected]c333e792012-01-06 16:57:39241
242 from_prefs = prefs->GetGrantedPermissions(extension->id());
[email protected]dc24976f2013-06-02 21:15:09243 ASSERT_EQ(*active_permissions.get(), *from_prefs.get());
[email protected]c333e792012-01-06 16:57:39244
245 // In the second part of the test, we'll remove the permissions that we
246 // just added except for 'notification'.
[email protected]c2e66e12012-06-27 06:27:06247 apis.erase(APIPermission::kNotification);
[email protected]e737c442013-11-15 15:55:24248 delta = new PermissionSet(apis, empty_manifest_permissions,
249 hosts, URLPatternSet());
[email protected]c333e792012-01-06 16:57:39250
251 listener.Reset();
[email protected]dc24976f2013-06-02 21:15:09252 updater.RemovePermissions(extension.get(), delta.get());
[email protected]c333e792012-01-06 16:57:39253 listener.Wait();
254
255 // Verify that the notification was correct.
256 ASSERT_TRUE(listener.received_notification());
257 ASSERT_EQ(extension, listener.extension());
258 ASSERT_EQ(UpdatedExtensionPermissionsInfo::REMOVED, listener.reason());
[email protected]dc24976f2013-06-02 21:15:09259 ASSERT_EQ(*delta.get(), *listener.permissions());
[email protected]c333e792012-01-06 16:57:39260
261 // Make sure the extension's active permissions reflect the change.
262 active_permissions =
[email protected]dc24976f2013-06-02 21:15:09263 PermissionSet::CreateDifference(active_permissions.get(), delta.get());
[email protected]cadac622013-06-11 16:46:36264 ASSERT_EQ(*active_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:26265 *extension->permissions_data()->active_permissions().get());
[email protected]c333e792012-01-06 16:57:39266
267 // Verify that the extension prefs hold the new active permissions and the
268 // same granted permissions.
269 from_prefs = prefs->GetActivePermissions(extension->id());
[email protected]dc24976f2013-06-02 21:15:09270 ASSERT_EQ(*active_permissions.get(), *from_prefs.get());
[email protected]c333e792012-01-06 16:57:39271
272 from_prefs = prefs->GetGrantedPermissions(extension->id());
[email protected]dc24976f2013-06-02 21:15:09273 ASSERT_EQ(*granted_permissions.get(), *from_prefs.get());
[email protected]c333e792012-01-06 16:57:39274}
275
[email protected]23a85362014-07-07 23:26:19276TEST_F(PermissionsUpdaterTest, WithholdAllHosts) {
277 InitializeEmptyExtensionService();
278
279 // Permissions are only withheld with the appropriate switch turned on.
280 scoped_ptr<FeatureSwitch::ScopedOverride> switch_override(
281 new FeatureSwitch::ScopedOverride(FeatureSwitch::scripts_require_action(),
282 FeatureSwitch::OVERRIDE_ENABLED));
283
284 URLPattern google(URLPattern::SCHEME_ALL, "http://www.google.com/*");
285 URLPattern sub_google(URLPattern::SCHEME_ALL, "http://*.google.com/*");
286 URLPattern all_http(URLPattern::SCHEME_ALL, "http://*/*");
287 URLPattern all_hosts(URLPattern::SCHEME_ALL, "<all_urls>");
288 URLPattern all_com(URLPattern::SCHEME_ALL, "http://*.com/*");
289
290 std::set<URLPattern> all_host_patterns;
291 std::set<URLPattern> safe_patterns;
292
293 all_host_patterns.insert(all_http);
294 all_host_patterns.insert(all_hosts);
295 all_host_patterns.insert(all_com);
296
297 safe_patterns.insert(google);
298 safe_patterns.insert(sub_google);
299
300 std::set<URLPattern> all_patterns = base::STLSetUnion<std::set<URLPattern> >(
301 all_host_patterns, safe_patterns);
302
303 scoped_refptr<const Extension> extension = CreateExtensionWithPermissions(
304 all_patterns, all_patterns, Manifest::INTERNAL);
305 const PermissionsData* permissions_data = extension->permissions_data();
306 PermissionsUpdater updater(profile_.get());
307 updater.InitializePermissions(extension);
308
309 // At first, the active permissions should have only the safe patterns and
310 // the withheld permissions should have only the all host patterns.
311 EXPECT_TRUE(SetsAreEqual(
312 permissions_data->active_permissions()->scriptable_hosts().patterns(),
313 safe_patterns));
314 EXPECT_TRUE(SetsAreEqual(
315 permissions_data->active_permissions()->explicit_hosts().patterns(),
316 safe_patterns));
317 EXPECT_TRUE(SetsAreEqual(
318 permissions_data->withheld_permissions()->scriptable_hosts().patterns(),
319 all_host_patterns));
320 EXPECT_TRUE(SetsAreEqual(
321 permissions_data->withheld_permissions()->explicit_hosts().patterns(),
322 all_host_patterns));
323
324 // Then, we grant the withheld all-hosts permissions.
325 updater.GrantWithheldImpliedAllHosts(extension);
326 // Now, active permissions should have all patterns, and withheld permissions
327 // should have none.
328 EXPECT_TRUE(SetsAreEqual(
329 permissions_data->active_permissions()->scriptable_hosts().patterns(),
330 all_patterns));
331 EXPECT_TRUE(permissions_data->withheld_permissions()
332 ->scriptable_hosts()
333 .patterns()
334 .empty());
335 EXPECT_TRUE(SetsAreEqual(
336 permissions_data->active_permissions()->explicit_hosts().patterns(),
337 all_patterns));
338 EXPECT_TRUE(permissions_data->withheld_permissions()
339 ->explicit_hosts()
340 .patterns()
341 .empty());
342
343 // Finally, we revoke the all hosts permissions.
344 updater.WithholdImpliedAllHosts(extension);
345
346 // We should be back to our initial state - all_hosts should be withheld, and
347 // the safe patterns should be granted.
348 EXPECT_TRUE(SetsAreEqual(
349 permissions_data->active_permissions()->scriptable_hosts().patterns(),
350 safe_patterns));
351 EXPECT_TRUE(SetsAreEqual(
352 permissions_data->active_permissions()->explicit_hosts().patterns(),
353 safe_patterns));
354 EXPECT_TRUE(SetsAreEqual(
355 permissions_data->withheld_permissions()->scriptable_hosts().patterns(),
356 all_host_patterns));
357 EXPECT_TRUE(SetsAreEqual(
358 permissions_data->withheld_permissions()->explicit_hosts().patterns(),
359 all_host_patterns));
360
361 // Creating a component extension should result in no withheld permissions.
362 extension = CreateExtensionWithPermissions(
363 all_patterns, all_patterns, Manifest::COMPONENT);
364 permissions_data = extension->permissions_data();
365 updater.InitializePermissions(extension);
366 EXPECT_TRUE(SetsAreEqual(
367 permissions_data->active_permissions()->scriptable_hosts().patterns(),
368 all_patterns));
369 EXPECT_TRUE(permissions_data->withheld_permissions()
370 ->scriptable_hosts()
371 .patterns()
372 .empty());
373 EXPECT_TRUE(SetsAreEqual(
374 permissions_data->active_permissions()->explicit_hosts().patterns(),
375 all_patterns));
376 EXPECT_TRUE(permissions_data->withheld_permissions()
377 ->explicit_hosts()
378 .patterns()
379 .empty());
380
381 // Without the switch, we shouldn't withhold anything.
382 switch_override.reset();
383 extension = CreateExtensionWithPermissions(
384 all_patterns, all_patterns, Manifest::INTERNAL);
385 permissions_data = extension->permissions_data();
386 updater.InitializePermissions(extension);
387 EXPECT_TRUE(SetsAreEqual(
388 permissions_data->active_permissions()->scriptable_hosts().patterns(),
389 all_patterns));
390 EXPECT_TRUE(permissions_data->withheld_permissions()
391 ->scriptable_hosts()
392 .patterns()
393 .empty());
394 EXPECT_TRUE(SetsAreEqual(
395 permissions_data->active_permissions()->explicit_hosts().patterns(),
396 all_patterns));
397 EXPECT_TRUE(permissions_data->withheld_permissions()
398 ->explicit_hosts()
399 .patterns()
400 .empty());
401}
402
[email protected]c333e792012-01-06 16:57:39403} // namespace extensions