1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/extensions/manifest.h"
#include <algorithm>
#include <set>
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_error_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace keys = extension_manifest_keys;
namespace errors = extension_manifest_errors;
namespace extensions {
namespace {
// Keys that define types.
const char* kTypeKeys[] = {
keys::kApp,
keys::kTheme,
keys::kPlatformApp
};
// Keys that are not accesible by themes.
const char* kNotThemeKeys[] = {
keys::kBrowserAction,
keys::kPageAction,
keys::kPageActions,
keys::kChromeURLOverrides,
keys::kPermissions,
keys::kOptionalPermissions,
keys::kOptionsPage,
keys::kBackground,
keys::kOfflineEnabled,
keys::kMinimumChromeVersion,
keys::kRequirements,
keys::kConvertedFromUserScript,
keys::kNaClModules,
keys::kPlugins,
keys::kContentScripts,
keys::kOmnibox,
keys::kDevToolsPage,
keys::kSidebar,
keys::kHomepageURL,
keys::kContentSecurityPolicy,
keys::kFileBrowserHandlers,
keys::kIncognito,
keys::kInputComponents,
keys::kTtsEngine,
keys::kIntents
};
// Keys that are not accessible by hosted apps.
const char* kNotHostedAppKeys[] = {
keys::kBrowserAction,
keys::kPageAction,
keys::kPageActions,
keys::kChromeURLOverrides,
keys::kContentScripts,
keys::kOmnibox,
keys::kDevToolsPage,
keys::kSidebar,
keys::kHomepageURL,
keys::kContentSecurityPolicy,
keys::kFileBrowserHandlers,
keys::kIncognito,
keys::kInputComponents,
keys::kTtsEngine,
keys::kIntents
};
// Keys not accessible by packaged aps.
const char* kNotPackagedAppKeys[] = {
keys::kBrowserAction,
keys::kPageAction,
keys::kPageActions
};
// Keys not accessible by platform apps.
const char* kNotPlatformAppKeys[] = {
keys::kBrowserAction,
keys::kPageAction,
keys::kPageActions,
keys::kChromeURLOverrides,
keys::kContentScripts,
keys::kOmnibox,
keys::kDevToolsPage,
keys::kSidebar,
keys::kHomepageURL,
keys::kPlugins,
keys::kInputComponents,
keys::kTtsEngine,
keys::kFileBrowserHandlers
};
// Returns all the manifest keys not including those in |filtered| or kTypeKeys.
std::set<std::string> GetAccessibleKeys(const char* filtered[], size_t length) {
std::set<std::string> all_keys = Manifest::GetAllKnownKeys();
std::set<std::string> filtered_keys(filtered, filtered + length);
// Starting with all possible manfiest keys, remove the keys that aren't
// accessible for the given type.
std::set<std::string> intermediate;
std::set_difference(all_keys.begin(), all_keys.end(),
filtered_keys.begin(), filtered_keys.end(),
std::insert_iterator<std::set<std::string> >(
intermediate, intermediate.begin()));
// Then remove the keys that specify types (app, platform_app, etc.).
std::set<std::string> result;
std::set<std::string> type_keys(
kTypeKeys, kTypeKeys + ARRAYSIZE_UNSAFE(kTypeKeys));
std::set_difference(intermediate.begin(), intermediate.end(),
type_keys.begin(), type_keys.end(),
std::insert_iterator<std::set<std::string> >(
result, result.begin()));
return result;
}
} // namespace
class ManifestTest : public testing::Test {
public:
ManifestTest() : default_value_("test") {}
protected:
void AssertType(Manifest* manifest, Manifest::Type type) {
EXPECT_EQ(type, manifest->GetType());
EXPECT_EQ(type == Manifest::kTypeTheme, manifest->IsTheme());
EXPECT_EQ(type == Manifest::kTypePlatformApp, manifest->IsPlatformApp());
EXPECT_EQ(type == Manifest::kTypePackagedApp, manifest->IsPackagedApp());
EXPECT_EQ(type == Manifest::kTypeHostedApp, manifest->IsHostedApp());
}
void TestRestrictedKeys(Manifest* manifest,
const char* restricted_keys[],
size_t restricted_keys_length) {
// Verify that the keys on the restricted key list for the given manifest
// fail validation and are filtered out.
DictionaryValue* value = manifest->value();
for (size_t i = 0; i < restricted_keys_length; ++i) {
std::string str;
string16 error;
value->Set(restricted_keys[i], Value::CreateStringValue(default_value_));
EXPECT_FALSE(manifest->ValidateManifest(&error));
EXPECT_EQ(error, ExtensionErrorUtils::FormatErrorMessageUTF16(
errors::kFeatureNotAllowed, restricted_keys[i]));
EXPECT_FALSE(manifest->GetString(restricted_keys[i], &str));
EXPECT_TRUE(value->Remove(restricted_keys[i], NULL));
}
}
std::string default_value_;
};
// Verifies that extensions can access the correct keys.
TEST_F(ManifestTest, Extension) {
// Generate the list of keys accessible by extensions.
std::set<std::string> extension_keys = GetAccessibleKeys(NULL, 0u);
// Construct the underlying value using every single key other than those
// on the restricted list.. We can use the same value for every key because we
// validate only by checking the presence of the keys.
DictionaryValue* value = new DictionaryValue();
for (std::set<std::string>::iterator i = extension_keys.begin();
i != extension_keys.end(); ++i)
value->Set(*i, Value::CreateStringValue(default_value_));
scoped_ptr<Manifest> manifest(new Manifest(value));
string16 error;
EXPECT_TRUE(manifest->ValidateManifest(&error));
EXPECT_EQ(ASCIIToUTF16(""), error);
AssertType(manifest.get(), Manifest::kTypeExtension);
// Verify that all the extension keys are accessible.
for (std::set<std::string>::iterator i = extension_keys.begin();
i != extension_keys.end(); ++i) {
std::string value;
manifest->GetString(*i, &value);
EXPECT_EQ(default_value_, value) << *i;
}
// Test DeepCopy and Equals.
scoped_ptr<Manifest> manifest2(manifest->DeepCopy());
EXPECT_TRUE(manifest->Equals(manifest2.get()));
EXPECT_TRUE(manifest2->Equals(manifest.get()));
value->Set("foo", Value::CreateStringValue("blah"));
EXPECT_FALSE(manifest->Equals(manifest2.get()));
}
// Verifies that themes can access the right keys.
TEST_F(ManifestTest, Theme) {
std::set<std::string> theme_keys =
GetAccessibleKeys(kNotThemeKeys, ARRAYSIZE_UNSAFE(kNotThemeKeys));
DictionaryValue* value = new DictionaryValue();
for (std::set<std::string>::iterator i = theme_keys.begin();
i != theme_keys.end(); ++i)
value->Set(*i, Value::CreateStringValue(default_value_));
std::string theme_key = keys::kTheme + std::string(".test");
value->Set(theme_key, Value::CreateStringValue(default_value_));
scoped_ptr<Manifest> manifest(new Manifest(value));
string16 error;
EXPECT_TRUE(manifest->ValidateManifest(&error));
EXPECT_EQ(ASCIIToUTF16(""), error);
AssertType(manifest.get(), Manifest::kTypeTheme);
// Verify that all the theme keys are accessible.
std::string str;
for (std::set<std::string>::iterator i = theme_keys.begin();
i != theme_keys.end(); ++i) {
EXPECT_TRUE(manifest->GetString(*i, &str));
EXPECT_EQ(default_value_, str) << *i;
}
EXPECT_TRUE(manifest->GetString(theme_key, &str));
EXPECT_EQ(default_value_, str) << theme_key;
// And that all the other keys fail validation and are filtered out
TestRestrictedKeys(manifest.get(), kNotThemeKeys,
ARRAYSIZE_UNSAFE(kNotThemeKeys));
};
// Verifies that platform apps can access the right keys.
TEST_F(ManifestTest, PlatformApp) {
std::set<std::string> platform_keys = GetAccessibleKeys(
kNotPlatformAppKeys,
ARRAYSIZE_UNSAFE(kNotPlatformAppKeys));
DictionaryValue* value = new DictionaryValue();
for (std::set<std::string>::iterator i = platform_keys.begin();
i != platform_keys.end(); ++i)
value->Set(*i, Value::CreateStringValue(default_value_));
value->Set(keys::kPlatformApp, Value::CreateBooleanValue(true));
scoped_ptr<Manifest> manifest(new Manifest(value));
string16 error;
EXPECT_TRUE(manifest->ValidateManifest(&error));
EXPECT_EQ(ASCIIToUTF16(""), error);
AssertType(manifest.get(), Manifest::kTypePlatformApp);
// Verify that all the platform app keys are accessible.
std::string str;
for (std::set<std::string>::iterator i = platform_keys.begin();
i != platform_keys.end(); ++i) {
EXPECT_TRUE(manifest->GetString(*i, &str));
EXPECT_EQ(default_value_, str) << *i;
}
bool is_platform_app = false;
EXPECT_TRUE(manifest->GetBoolean(keys::kPlatformApp, &is_platform_app));
EXPECT_TRUE(is_platform_app) << keys::kPlatformApp;
// And that all the other keys fail validation and are filtered out.
TestRestrictedKeys(manifest.get(), kNotPlatformAppKeys,
ARRAYSIZE_UNSAFE(kNotPlatformAppKeys));
};
// Verifies that hosted apps can access the right keys.
TEST_F(ManifestTest, HostedApp) {
std::set<std::string> keys = GetAccessibleKeys(
kNotHostedAppKeys,
ARRAYSIZE_UNSAFE(kNotHostedAppKeys));
DictionaryValue* value = new DictionaryValue();
for (std::set<std::string>::iterator i = keys.begin();
i != keys.end(); ++i)
value->Set(*i, Value::CreateStringValue(default_value_));
value->Set(keys::kWebURLs, Value::CreateStringValue(default_value_));
scoped_ptr<Manifest> manifest(new Manifest(value));
string16 error;
EXPECT_TRUE(manifest->ValidateManifest(&error));
EXPECT_EQ(ASCIIToUTF16(""), error);
AssertType(manifest.get(), Manifest::kTypeHostedApp);
// Verify that all the hosted app keys are accessible.
std::string str;
for (std::set<std::string>::iterator i = keys.begin();
i != keys.end(); ++i) {
EXPECT_TRUE(manifest->GetString(*i, &str));
EXPECT_EQ(default_value_, str) << *i;
}
EXPECT_TRUE(manifest->GetString(keys::kWebURLs, &str));
EXPECT_EQ(default_value_, str) << keys::kWebURLs;
// And that all the other keys fail validation and are filtered out.
TestRestrictedKeys(manifest.get(), kNotHostedAppKeys,
ARRAYSIZE_UNSAFE(kNotHostedAppKeys));
};
// Verifies that packaged apps can access the right keys.
TEST_F(ManifestTest, PackagedApp) {
std::set<std::string> keys = GetAccessibleKeys(
kNotPackagedAppKeys,
ARRAYSIZE_UNSAFE(kNotPackagedAppKeys));
DictionaryValue* value = new DictionaryValue();
for (std::set<std::string>::iterator i = keys.begin();
i != keys.end(); ++i)
value->Set(*i, Value::CreateStringValue(default_value_));
value->Set(keys::kApp, Value::CreateStringValue(default_value_));
scoped_ptr<Manifest> manifest(new Manifest(value));
string16 error;
EXPECT_TRUE(manifest->ValidateManifest(&error));
EXPECT_EQ(ASCIIToUTF16(""), error);
AssertType(manifest.get(), Manifest::kTypePackagedApp);
// Verify that all the packaged app keys are accessible.
std::string str;
for (std::set<std::string>::iterator i = keys.begin();
i != keys.end(); ++i) {
EXPECT_TRUE(manifest->GetString(*i, &str));
EXPECT_EQ(default_value_, str) << *i;
}
EXPECT_TRUE(manifest->GetString(keys::kApp, &str));
EXPECT_EQ(default_value_, str) << keys::kApp;
// And that all the other keys fail validation and are filtered out.
TestRestrictedKeys(manifest.get(), kNotPackagedAppKeys,
ARRAYSIZE_UNSAFE(kNotPackagedAppKeys));
};
// Verifies that the various getters filter unknown and restricted keys.
TEST_F(ManifestTest, Getters) {
DictionaryValue* value = new DictionaryValue();
scoped_ptr<Manifest> manifest(new Manifest(value));
std::string unknown_key = "asdfaskldjf";
// Verify that the key filtering works for each of the getters.
// Get and GetBoolean
bool expected_bool = true, actual_bool = false;
value->Set(unknown_key, Value::CreateBooleanValue(expected_bool));
EXPECT_FALSE(manifest->HasKey(unknown_key));
EXPECT_FALSE(manifest->GetBoolean(unknown_key, &actual_bool));
EXPECT_FALSE(actual_bool);
Value* actual_value = NULL;
EXPECT_FALSE(manifest->Get(unknown_key, &actual_value));
EXPECT_TRUE(value->Remove(unknown_key, NULL));
// GetInteger
int expected_int = 5, actual_int = 0;
value->Set(unknown_key, Value::CreateIntegerValue(expected_int));
EXPECT_FALSE(manifest->GetInteger(unknown_key, &actual_int));
EXPECT_NE(expected_int, actual_int);
EXPECT_TRUE(value->Remove(unknown_key, NULL));
// GetString
std::string expected_str = "hello", actual_str;
value->Set(unknown_key, Value::CreateStringValue(expected_str));
EXPECT_FALSE(manifest->GetString(unknown_key, &actual_str));
EXPECT_NE(expected_str, actual_str);
EXPECT_TRUE(value->Remove(unknown_key, NULL));
// GetString (string16)
string16 expected_str16(UTF8ToUTF16("hello")), actual_str16;
value->Set(unknown_key, Value::CreateStringValue(expected_str16));
EXPECT_FALSE(manifest->GetString(unknown_key, &actual_str16));
EXPECT_NE(expected_str16, actual_str16);
EXPECT_TRUE(value->Remove(unknown_key, NULL));
// GetDictionary
DictionaryValue* expected_dict = new DictionaryValue();
DictionaryValue* actual_dict = NULL;
expected_dict->Set("foo", Value::CreateStringValue("bar"));
value->Set(unknown_key, expected_dict);
EXPECT_FALSE(manifest->GetDictionary(unknown_key, &actual_dict));
EXPECT_EQ(NULL, actual_dict);
std::string path = unknown_key + ".foo";
EXPECT_FALSE(manifest->GetString(path, &actual_str));
EXPECT_NE("bar", actual_str);
EXPECT_TRUE(value->Remove(unknown_key, NULL));
// GetList
ListValue* expected_list = new ListValue();
ListValue* actual_list = NULL;
expected_list->Append(Value::CreateStringValue("blah"));
value->Set(unknown_key, expected_list);
EXPECT_FALSE(manifest->GetList(unknown_key, &actual_list));
EXPECT_EQ(NULL, actual_list);
EXPECT_TRUE(value->Remove(unknown_key, NULL));
}
} // namespace extensions
|