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

Side by Side Diff: third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp

Issue 2636843003: Move most Feature Policy code into content/ (Closed)
Patch Set: Addressing review comments Created 3 years, 11 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "platform/feature_policy/FeaturePolicy.h" 5 #include "platform/feature_policy/FeaturePolicy.h"
6 6
7 #include "platform/RuntimeEnabledFeatures.h" 7 #include "platform/RuntimeEnabledFeatures.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 // Origin strings used for tests 10 // Origin strings used for tests
(...skipping 16 matching lines...) Expand all
27 "{\"feature1\": [\"" ORIGIN_A "\"]}, {\"feature2\": [\"self\"]}"}; 27 "{\"feature1\": [\"" ORIGIN_A "\"]}, {\"feature2\": [\"self\"]}"};
28 28
29 const char* kInvalidPolicies[] = { 29 const char* kInvalidPolicies[] = {
30 "Not A JSON literal", 30 "Not A JSON literal",
31 "\"Not a JSON object\"", 31 "\"Not a JSON object\"",
32 "[\"Also\", \"Not a JSON object\"]", 32 "[\"Also\", \"Not a JSON object\"]",
33 "1.0", 33 "1.0",
34 "{\"Whitelist\": \"Not a JSON array\"}", 34 "{\"Whitelist\": \"Not a JSON array\"}",
35 "{\"feature1\": [\"*\"], \"feature2\": \"Not an array\"}"}; 35 "{\"feature1\": [\"*\"], \"feature2\": \"Not an array\"}"};
36 36
37 // This is an example of a feature which should be enabled by default in all
38 // frames.
39 const FeaturePolicy::Feature kDefaultOnFeature{
40 "default-on", FeaturePolicy::FeatureDefault::EnableForAll};
41
42 // This is an example of a feature which should be enabled in top-level frames,
43 // and same-origin child-frames, but must be delegated to all cross-origin
44 // frames explicitly.
45 const FeaturePolicy::Feature kDefaultSelfFeature{
46 "default-self", FeaturePolicy::FeatureDefault::EnableForSelf};
47
48 // This is an example of a feature which should be disabled by default, both in
49 // top-level and nested frames.
50 const FeaturePolicy::Feature kDefaultOffFeature{
51 "default-off", FeaturePolicy::FeatureDefault::DisableForAll};
52
53 } // namespace 37 } // namespace
54 38
55 class FeaturePolicyTest : public ::testing::Test { 39 class FeaturePolicyTest : public ::testing::Test {
56 protected: 40 protected:
57 FeaturePolicyTest() 41 FeaturePolicyTest() {}
58 : m_frameworkWasEnabled(RuntimeEnabledFeatures::featurePolicyEnabled()),
59 m_featureList(
60 {&kDefaultOnFeature, &kDefaultSelfFeature, &kDefaultOffFeature}) {
61 RuntimeEnabledFeatures::setFeaturePolicyEnabled(true);
62 }
63 42
64 ~FeaturePolicyTest() { 43 ~FeaturePolicyTest() {
65 RuntimeEnabledFeatures::setFeaturePolicyEnabled(m_frameworkWasEnabled);
66 }
67
68 std::unique_ptr<FeaturePolicy> createFromParentPolicy(
69 const FeaturePolicy* parent,
70 RefPtr<SecurityOrigin> origin) {
71 return FeaturePolicy::createFromParentPolicy(parent, origin, m_featureList);
72 } 44 }
73 45
74 RefPtr<SecurityOrigin> m_originA = SecurityOrigin::createFromString(ORIGIN_A); 46 RefPtr<SecurityOrigin> m_originA = SecurityOrigin::createFromString(ORIGIN_A);
75 RefPtr<SecurityOrigin> m_originB = SecurityOrigin::createFromString(ORIGIN_B); 47 RefPtr<SecurityOrigin> m_originB = SecurityOrigin::createFromString(ORIGIN_B);
76 RefPtr<SecurityOrigin> m_originC = SecurityOrigin::createFromString(ORIGIN_C); 48 RefPtr<SecurityOrigin> m_originC = SecurityOrigin::createFromString(ORIGIN_C);
77
78 private:
79 const bool m_frameworkWasEnabled;
80
81 // Contains the list of controlled features, so that we are guaranteed to
82 // have at least one of each kind of default behaviour represented.
83 FeaturePolicy::FeatureList m_featureList;
84 }; 49 };
85 50
86 TEST_F(FeaturePolicyTest, ParseValidPolicy) { 51 TEST_F(FeaturePolicyTest, ParseValidPolicy) {
87 Vector<String> messages; 52 Vector<String> messages;
88 for (const char* policyString : kValidPolicies) { 53 for (const char* policyString : kValidPolicies) {
89 messages.clear(); 54 messages.clear();
90 FeaturePolicy::parseFeaturePolicy(policyString, m_originA.get(), &messages); 55 parseFeaturePolicy(policyString, m_originA.get(), &messages);
91 EXPECT_EQ(0UL, messages.size()); 56 EXPECT_EQ(0UL, messages.size());
92 } 57 }
93 } 58 }
94 59
95 TEST_F(FeaturePolicyTest, ParseInvalidPolicy) { 60 TEST_F(FeaturePolicyTest, ParseInvalidPolicy) {
96 Vector<String> messages; 61 Vector<String> messages;
97 for (const char* policyString : kInvalidPolicies) { 62 for (const char* policyString : kInvalidPolicies) {
98 messages.clear(); 63 messages.clear();
99 FeaturePolicy::parseFeaturePolicy(policyString, m_originA.get(), &messages); 64 parseFeaturePolicy(policyString, m_originA.get(), &messages);
100 EXPECT_NE(0UL, messages.size()); 65 EXPECT_NE(0UL, messages.size());
101 } 66 }
102 } 67 }
103 68
104 TEST_F(FeaturePolicyTest, PolicyParsedCorrectly) { 69 TEST_F(FeaturePolicyTest, PolicyParsedCorrectly) {
105 Vector<String> messages; 70 Vector<String> messages;
106 71
107 // Empty policy. 72 // Empty policy.
108 WebParsedFeaturePolicy parsedPolicy = 73 WebParsedFeaturePolicyHeader parsedPolicy =
109 FeaturePolicy::parseFeaturePolicy("{}", m_originA.get(), &messages); 74 parseFeaturePolicy("{}", m_originA.get(), &messages);
110 EXPECT_EQ(0UL, parsedPolicy.size()); 75 EXPECT_EQ(0UL, parsedPolicy.size());
111 76
112 // Simple policy with "self". 77 // Simple policy with "self".
113 parsedPolicy = FeaturePolicy::parseFeaturePolicy( 78 parsedPolicy = parseFeaturePolicy("{\"default-self\": [\"self\"]}",
114 "{\"default-self\": [\"self\"]}", m_originA.get(), &messages); 79 m_originA.get(), &messages);
115 EXPECT_EQ(1UL, parsedPolicy.size()); 80 EXPECT_EQ(1UL, parsedPolicy.size());
116 EXPECT_EQ("default-self", parsedPolicy[0].featureName); 81 EXPECT_EQ("default-self", parsedPolicy[0].featureName);
117 EXPECT_FALSE(parsedPolicy[0].matchesAllOrigins); 82 EXPECT_FALSE(parsedPolicy[0].whitelist.matchesAllOrigins);
118 EXPECT_EQ(1UL, parsedPolicy[0].origins.size()); 83 EXPECT_EQ(1UL, parsedPolicy[0].whitelist.origins.size());
119 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin( 84 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin(
120 parsedPolicy[0].origins[0].get())); 85 parsedPolicy[0].whitelist.origins[0].get()));
121 86
122 // Simple policy with *. 87 // Simple policy with *.
123 parsedPolicy = FeaturePolicy::parseFeaturePolicy( 88 parsedPolicy = parseFeaturePolicy("{\"default-self\": [\"*\"]}",
124 "{\"default-self\": [\"*\"]}", m_originA.get(), &messages); 89 m_originA.get(), &messages);
125 EXPECT_EQ(1UL, parsedPolicy.size()); 90 EXPECT_EQ(1UL, parsedPolicy.size());
126 EXPECT_EQ("default-self", parsedPolicy[0].featureName); 91 EXPECT_EQ("default-self", parsedPolicy[0].featureName);
127 EXPECT_TRUE(parsedPolicy[0].matchesAllOrigins); 92 EXPECT_TRUE(parsedPolicy[0].whitelist.matchesAllOrigins);
128 EXPECT_EQ(0UL, parsedPolicy[0].origins.size()); 93 EXPECT_EQ(0UL, parsedPolicy[0].whitelist.origins.size());
129 94
130 // Complicated policy. 95 // Complicated policy.
131 parsedPolicy = FeaturePolicy::parseFeaturePolicy( 96 parsedPolicy = parseFeaturePolicy(
132 "{\"default-self\": [\"*\"], " 97 "{\"default-self\": [\"*\"], "
133 "\"default-on\": [\"https://example.net\", \"https://example.org\"], " 98 "\"default-on\": [\"https://example.net\", \"https://example.org\"], "
134 "\"default-off\": [\"self\"]}", 99 "\"default-off\": [\"self\"]}",
135 m_originA.get(), &messages); 100 m_originA.get(), &messages);
136 EXPECT_EQ(3UL, parsedPolicy.size()); 101 EXPECT_EQ(3UL, parsedPolicy.size());
137 EXPECT_EQ("default-self", parsedPolicy[0].featureName); 102 EXPECT_EQ("default-self", parsedPolicy[0].featureName);
138 EXPECT_TRUE(parsedPolicy[0].matchesAllOrigins); 103 EXPECT_TRUE(parsedPolicy[0].whitelist.matchesAllOrigins);
139 EXPECT_EQ(0UL, parsedPolicy[0].origins.size()); 104 EXPECT_EQ(0UL, parsedPolicy[0].whitelist.origins.size());
140 EXPECT_EQ("default-on", parsedPolicy[1].featureName); 105 EXPECT_EQ("default-on", parsedPolicy[1].featureName);
141 EXPECT_FALSE(parsedPolicy[1].matchesAllOrigins); 106 EXPECT_FALSE(parsedPolicy[1].whitelist.matchesAllOrigins);
142 EXPECT_EQ(2UL, parsedPolicy[1].origins.size()); 107 EXPECT_EQ(2UL, parsedPolicy[1].whitelist.origins.size());
143 EXPECT_TRUE(m_originB->isSameSchemeHostPortAndSuborigin( 108 EXPECT_TRUE(m_originB->isSameSchemeHostPortAndSuborigin(
144 parsedPolicy[1].origins[0].get())); 109 parsedPolicy[1].whitelist.origins[0].get()));
145 EXPECT_TRUE(m_originC->isSameSchemeHostPortAndSuborigin( 110 EXPECT_TRUE(m_originC->isSameSchemeHostPortAndSuborigin(
146 parsedPolicy[1].origins[1].get())); 111 parsedPolicy[1].whitelist.origins[1].get()));
147 EXPECT_EQ("default-off", parsedPolicy[2].featureName); 112 EXPECT_EQ("default-off", parsedPolicy[2].featureName);
148 EXPECT_FALSE(parsedPolicy[2].matchesAllOrigins); 113 EXPECT_FALSE(parsedPolicy[2].whitelist.matchesAllOrigins);
149 EXPECT_EQ(1UL, parsedPolicy[2].origins.size()); 114 EXPECT_EQ(1UL, parsedPolicy[2].whitelist.origins.size());
150 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin( 115 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin(
151 parsedPolicy[2].origins[0].get())); 116 parsedPolicy[2].whitelist.origins[0].get()));
152 }
153
154 TEST_F(FeaturePolicyTest, TestInitialPolicy) {
155 // +-------------+
156 // |(1)Origin A |
157 // |No Policy |
158 // +-------------+
159 // Default-on and top-level-only features should be enabled in top-level
160 // frame. Default-off features should be disabled.
161 std::unique_ptr<FeaturePolicy> policy1 =
162 createFromParentPolicy(nullptr, m_originA);
163 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultOnFeature));
164 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultSelfFeature));
165 EXPECT_FALSE(policy1->isFeatureEnabled(kDefaultOffFeature));
166 }
167
168 TEST_F(FeaturePolicyTest, TestInitialSameOriginChildPolicy) {
169 // +-----------------+
170 // |(1)Origin A |
171 // |No Policy |
172 // | +-------------+ |
173 // | |(2)Origin A | |
174 // | |No Policy | |
175 // | +-------------+ |
176 // +-----------------+
177 // Default-on and Default-self features should be enabled in a same-origin
178 // child frame. Default-off features should be disabled.
179 std::unique_ptr<FeaturePolicy> policy1 =
180 createFromParentPolicy(nullptr, m_originA);
181 std::unique_ptr<FeaturePolicy> policy2 =
182 createFromParentPolicy(policy1.get(), m_originA);
183 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultOnFeature));
184 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
185 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultOffFeature));
186 }
187
188 TEST_F(FeaturePolicyTest, TestInitialCrossOriginChildPolicy) {
189 // +-----------------+
190 // |(1)Origin A |
191 // |No Policy |
192 // | +-------------+ |
193 // | |(2)Origin B | |
194 // | |No Policy | |
195 // | +-------------+ |
196 // +-----------------+
197 // Default-on features should be enabled in child frame. Default-self and
198 // Default-off features should be disabled.
199 std::unique_ptr<FeaturePolicy> policy1 =
200 createFromParentPolicy(nullptr, m_originA);
201 std::unique_ptr<FeaturePolicy> policy2 =
202 createFromParentPolicy(policy1.get(), m_originB);
203 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultOnFeature));
204 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultSelfFeature));
205 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultOffFeature));
206 }
207
208 TEST_F(FeaturePolicyTest, TestCrossOriginChildCannotEnableFeature) {
209 // +---------------------------------------+
210 // |(1) Origin A |
211 // |No Policy |
212 // | +-----------------------------------+ |
213 // | |(2) Origin B | |
214 // | |Policy: {"default-self": ["self"]} | |
215 // | +-----------------------------------+ |
216 // +---------------------------------------+
217 // Default-self feature should be disabled in cross origin frame, even if no
218 // policy was specified in the parent frame.
219 Vector<String> messages;
220 std::unique_ptr<FeaturePolicy> policy1 =
221 createFromParentPolicy(nullptr, m_originA);
222 std::unique_ptr<FeaturePolicy> policy2 =
223 createFromParentPolicy(policy1.get(), m_originB);
224 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
225 "{\"default-self\": [\"self\"]}", m_originB.get(), &messages));
226 EXPECT_EQ(0UL, messages.size());
227 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultSelfFeature));
228 }
229
230 TEST_F(FeaturePolicyTest, TestFrameSelfInheritance) {
231 // +------------------------------------------+
232 // |(1) Origin A |
233 // |Policy: {"default-self": ["self"]} |
234 // | +-----------------+ +-----------------+ |
235 // | |(2) Origin A | |(4) Origin B | |
236 // | |No Policy | |No Policy | |
237 // | | +-------------+ | | +-------------+ | |
238 // | | |(3)Origin A | | | |(5)Origin B | | |
239 // | | |No Policy | | | |No Policy | | |
240 // | | +-------------+ | | +-------------+ | |
241 // | +-----------------+ +-----------------+ |
242 // +------------------------------------------+
243 // Feature should be enabled at the top-level, and through the chain of
244 // same-origin frames 2 and 3. It should be disabled in frames 4 and 5, as
245 // they are at a different origin.
246 Vector<String> messages;
247 std::unique_ptr<FeaturePolicy> policy1 =
248 createFromParentPolicy(nullptr, m_originA);
249 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
250 "{\"default-self\": [\"self\"]}", m_originA.get(), &messages));
251 EXPECT_EQ(0UL, messages.size());
252 std::unique_ptr<FeaturePolicy> policy2 =
253 createFromParentPolicy(policy1.get(), m_originA);
254 std::unique_ptr<FeaturePolicy> policy3 =
255 createFromParentPolicy(policy2.get(), m_originA);
256 std::unique_ptr<FeaturePolicy> policy4 =
257 createFromParentPolicy(policy1.get(), m_originB);
258 std::unique_ptr<FeaturePolicy> policy5 =
259 createFromParentPolicy(policy4.get(), m_originB);
260 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
261 EXPECT_TRUE(policy3->isFeatureEnabled(kDefaultSelfFeature));
262 EXPECT_FALSE(policy4->isFeatureEnabled(kDefaultSelfFeature));
263 EXPECT_FALSE(policy5->isFeatureEnabled(kDefaultSelfFeature));
264 }
265
266 TEST_F(FeaturePolicyTest, TestReflexiveFrameSelfInheritance) {
267 // +-----------------------------------+
268 // |(1) Origin A |
269 // |Policy: {"default-self": ["self"]} |
270 // | +-----------------+ |
271 // | |(2) Origin B | |
272 // | |No Policy | |
273 // | | +-------------+ | |
274 // | | |(3)Origin A | | |
275 // | | |No Policy | | |
276 // | | +-------------+ | |
277 // | +-----------------+ |
278 // +-----------------------------------+
279 // Feature which is enabled at top-level should be disabled in frame 3, as
280 // it is embedded by frame 2, for which the feature is not enabled.
281 Vector<String> messages;
282 std::unique_ptr<FeaturePolicy> policy1 =
283 createFromParentPolicy(nullptr, m_originA);
284 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
285 "{\"default-self\": [\"self\"]}", m_originA.get(), &messages));
286 EXPECT_EQ(0UL, messages.size());
287 std::unique_ptr<FeaturePolicy> policy2 =
288 createFromParentPolicy(policy1.get(), m_originB);
289 std::unique_ptr<FeaturePolicy> policy3 =
290 createFromParentPolicy(policy2.get(), m_originA);
291 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultSelfFeature));
292 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultSelfFeature));
293 }
294
295 TEST_F(FeaturePolicyTest, TestSelectiveFrameInheritance) {
296 // +------------------------------------------+
297 // |(1) Origin A |
298 // |Policy: {"default-self": ["Origin B"]} |
299 // | +-----------------+ +-----------------+ |
300 // | |(2) Origin B | |(3) Origin C | |
301 // | |No Policy | |No Policy | |
302 // | | | | +-------------+ | |
303 // | | | | |(4)Origin B | | |
304 // | | | | |No Policy | | |
305 // | | | | +-------------+ | |
306 // | +-----------------+ +-----------------+ |
307 // +------------------------------------------+
308 // Feature should be enabled in second level Origin B frame, but disabled in
309 // Frame 4, because it is embedded by frame 3, where the feature is not
310 // enabled.
311 Vector<String> messages;
312 std::unique_ptr<FeaturePolicy> policy1 =
313 createFromParentPolicy(nullptr, m_originA);
314 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
315 "{\"default-self\": [\"" ORIGIN_B "\"]}", m_originA.get(), &messages));
316 EXPECT_EQ(0UL, messages.size());
317 std::unique_ptr<FeaturePolicy> policy2 =
318 createFromParentPolicy(policy1.get(), m_originB);
319 std::unique_ptr<FeaturePolicy> policy3 =
320 createFromParentPolicy(policy1.get(), m_originC);
321 std::unique_ptr<FeaturePolicy> policy4 =
322 createFromParentPolicy(policy3.get(), m_originB);
323 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
324 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultSelfFeature));
325 EXPECT_FALSE(policy4->isFeatureEnabled(kDefaultSelfFeature));
326 }
327
328 TEST_F(FeaturePolicyTest, TestPolicyCanBlockSelf) {
329 // +----------------------------+
330 // |(1)Origin A |
331 // |Policy: {"default-on": []} |
332 // +----------------------------+
333 // Default-on feature should be disabled in top-level frame.
334 Vector<String> messages;
335 std::unique_ptr<FeaturePolicy> policy1 =
336 createFromParentPolicy(nullptr, m_originA);
337 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
338 "{\"default-on\": []}", m_originA.get(), &messages));
339 EXPECT_EQ(0UL, messages.size());
340 EXPECT_FALSE(policy1->isFeatureEnabled(kDefaultOnFeature));
341 }
342
343 TEST_F(FeaturePolicyTest, TestParentPolicyBlocksSameOriginChildPolicy) {
344 // +----------------------------+
345 // |(1)Origin A |
346 // |Policy: {"default-on": []} |
347 // | +-------------+ |
348 // | |(2)Origin A | |
349 // | |No Policy | |
350 // | +-------------+ |
351 // +----------------------------+
352 // Feature should be disabled in child frame.
353 Vector<String> messages;
354 std::unique_ptr<FeaturePolicy> policy1 =
355 createFromParentPolicy(nullptr, m_originA);
356 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
357 "{\"default-on\": []}", m_originA.get(), &messages));
358 EXPECT_EQ(0UL, messages.size());
359 std::unique_ptr<FeaturePolicy> policy2 =
360 createFromParentPolicy(policy1.get(), m_originA);
361 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultOnFeature));
362 }
363
364 TEST_F(FeaturePolicyTest, TestChildPolicyCanBlockSelf) {
365 // +--------------------------------+
366 // |(1)Origin A |
367 // |No Policy |
368 // | +----------------------------+ |
369 // | |(2)Origin B | |
370 // | |Policy: {"default-on": []} | |
371 // | +----------------------------+ |
372 // +--------------------------------+
373 // Default-on feature should be disabled by cross-origin child frame.
374 Vector<String> messages;
375 std::unique_ptr<FeaturePolicy> policy1 =
376 createFromParentPolicy(nullptr, m_originA);
377 std::unique_ptr<FeaturePolicy> policy2 =
378 createFromParentPolicy(policy1.get(), m_originB);
379 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
380 "{\"default-on\": []}", m_originB.get(), &messages));
381 EXPECT_EQ(0UL, messages.size());
382 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultOnFeature));
383 }
384
385 TEST_F(FeaturePolicyTest, TestChildPolicyCanBlockChildren) {
386 // +--------------------------------------+
387 // |(1)Origin A |
388 // |No Policy |
389 // | +----------------------------------+ |
390 // | |(2)Origin B | |
391 // | |Policy: {"default-on": ["self"]} | |
392 // | | +-------------+ | |
393 // | | |(3)Origin C | | |
394 // | | |No Policy | | |
395 // | | +-------------+ | |
396 // | +----------------------------------+ |
397 // +--------------------------------------+
398 // Default-on feature should be enabled in frames 1 and 2; disabled in frame
399 // 3 by child frame policy.
400 Vector<String> messages;
401 std::unique_ptr<FeaturePolicy> policy1 =
402 createFromParentPolicy(nullptr, m_originA);
403 std::unique_ptr<FeaturePolicy> policy2 =
404 createFromParentPolicy(policy1.get(), m_originB);
405 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
406 "{\"default-on\": [\"self\"]}", m_originB.get(), &messages));
407 EXPECT_EQ(0UL, messages.size());
408 std::unique_ptr<FeaturePolicy> policy3 =
409 createFromParentPolicy(policy2.get(), m_originC);
410 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultOnFeature));
411 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultOnFeature));
412 }
413
414 TEST_F(FeaturePolicyTest, TestParentPolicyBlocksCrossOriginChildPolicy) {
415 // +----------------------------+
416 // |(1)Origin A |
417 // |Policy: {"default-on": []} |
418 // | +-------------+ |
419 // | |(2)Origin B | |
420 // | |No Policy | |
421 // | +-------------+ |
422 // +----------------------------+
423 // Default-on feature should be disabled in cross-origin child frame.
424 Vector<String> messages;
425 std::unique_ptr<FeaturePolicy> policy1 =
426 createFromParentPolicy(nullptr, m_originA);
427 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
428 "{\"default-on\": []}", m_originA.get(), &messages));
429 EXPECT_EQ(0UL, messages.size());
430 std::unique_ptr<FeaturePolicy> policy2 =
431 createFromParentPolicy(policy1.get(), m_originB);
432 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultOnFeature));
433 }
434
435 TEST_F(FeaturePolicyTest, TestEnableForAllOrigins) {
436 // +--------------------------------+
437 // |(1) Origin A |
438 // |Policy: {"default-self": ["*"]} |
439 // | +-----------------+ |
440 // | |(2) Origin B | |
441 // | |No Policy | |
442 // | | +-------------+ | |
443 // | | |(3)Origin A | | |
444 // | | |No Policy | | |
445 // | | +-------------+ | |
446 // | +-----------------+ |
447 // +--------------------------------+
448 // Feature should be enabled in top and second level; disabled in frame 3.
449 Vector<String> messages;
450 std::unique_ptr<FeaturePolicy> policy1 =
451 createFromParentPolicy(nullptr, m_originA);
452 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
453 "{\"default-self\": [\"*\"]}", m_originA.get(), &messages));
454 EXPECT_EQ(0UL, messages.size());
455 std::unique_ptr<FeaturePolicy> policy2 =
456 createFromParentPolicy(policy1.get(), m_originB);
457 std::unique_ptr<FeaturePolicy> policy3 =
458 createFromParentPolicy(policy2.get(), m_originA);
459 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultSelfFeature));
460 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
461 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultSelfFeature));
462 }
463
464 TEST_F(FeaturePolicyTest, TestDefaultOnEnablesForAllAncestors) {
465 // +---------------------------------------+
466 // |(1) Origin A |
467 // |Policy: {"default-on": ["Origin B"]} |
468 // | +-----------------------------------+ |
469 // | |(2) Origin B | |
470 // | |No Policy | |
471 // | | +-------------+ +-------------+ | |
472 // | | |(3)Origin B | |(4)Origin C | | |
473 // | | |No Policy | |No Policy | | |
474 // | | +-------------+ +-------------+ | |
475 // | +-----------------------------------+ |
476 // +---------------------------------------+
477 // Feature should be disabled in frame 1; enabled in frames 2, 3 and 4.
478 Vector<String> messages;
479 std::unique_ptr<FeaturePolicy> policy1 =
480 createFromParentPolicy(nullptr, m_originA);
481 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
482 "{\"default-on\": [\"" ORIGIN_B "\"]}", m_originA.get(), &messages));
483 EXPECT_EQ(0UL, messages.size());
484 std::unique_ptr<FeaturePolicy> policy2 =
485 createFromParentPolicy(policy1.get(), m_originB);
486 std::unique_ptr<FeaturePolicy> policy3 =
487 createFromParentPolicy(policy2.get(), m_originB);
488 std::unique_ptr<FeaturePolicy> policy4 =
489 createFromParentPolicy(policy2.get(), m_originC);
490 EXPECT_FALSE(policy1->isFeatureEnabled(kDefaultOnFeature));
491 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultOnFeature));
492 EXPECT_TRUE(policy3->isFeatureEnabled(kDefaultOnFeature));
493 EXPECT_TRUE(policy4->isFeatureEnabled(kDefaultOnFeature));
494 }
495
496 TEST_F(FeaturePolicyTest, TestDefaultSelfRespectsSameOriginEmbedding) {
497 // +---------------------------------------+
498 // |(1) Origin A |
499 // |Policy: {"default-self": ["Origin B"]} |
500 // | +-----------------------------------+ |
501 // | |(2) Origin B | |
502 // | |No Policy | |
503 // | | +-------------+ +-------------+ | |
504 // | | |(3)Origin B | |(4)Origin C | | |
505 // | | |No Policy | |No Policy | | |
506 // | | +-------------+ +-------------+ | |
507 // | +-----------------------------------+ |
508 // +---------------------------------------+
509 // Feature should be disabled in frames 1 and 4; enabled in frames 2 and 3.
510 Vector<String> messages;
511 std::unique_ptr<FeaturePolicy> policy1 =
512 createFromParentPolicy(nullptr, m_originA);
513 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
514 "{\"default-self\": [\"" ORIGIN_B "\"]}", m_originA.get(), &messages));
515 EXPECT_EQ(0UL, messages.size());
516 std::unique_ptr<FeaturePolicy> policy2 =
517 createFromParentPolicy(policy1.get(), m_originB);
518 std::unique_ptr<FeaturePolicy> policy3 =
519 createFromParentPolicy(policy2.get(), m_originB);
520 std::unique_ptr<FeaturePolicy> policy4 =
521 createFromParentPolicy(policy2.get(), m_originC);
522 EXPECT_FALSE(policy1->isFeatureEnabled(kDefaultSelfFeature));
523 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
524 EXPECT_TRUE(policy3->isFeatureEnabled(kDefaultSelfFeature));
525 EXPECT_FALSE(policy4->isFeatureEnabled(kDefaultSelfFeature));
526 }
527
528 TEST_F(FeaturePolicyTest, TestDefaultOffMustBeDelegatedToAllCrossOriginFrames) {
529 // +------------------------------------------------------------+
530 // |(1) Origin A |
531 // |Policy: {"default-off": ["Origin B"]} |
532 // | +--------------------------------------------------------+ |
533 // | |(2) Origin B | |
534 // | |Policy: {"default-off": ["self"]} | |
535 // | | +-------------+ +----------------------------------+ | |
536 // | | |(3)Origin B | |(4)Origin C | | |
537 // | | |No Policy | |Policy: {"default-off": ["self"]} | | |
538 // | | +-------------+ +----------------------------------+ | |
539 // | +--------------------------------------------------------+ |
540 // +------------------------------------------------------------+
541 // Feature should be disabled in frames 1, 3 and 4; enabled in frame 2 only.
542 Vector<String> messages;
543 std::unique_ptr<FeaturePolicy> policy1 =
544 createFromParentPolicy(nullptr, m_originA);
545 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
546 "{\"default-off\": [\"" ORIGIN_B "\"]}", m_originA.get(), &messages));
547 EXPECT_EQ(0UL, messages.size());
548 std::unique_ptr<FeaturePolicy> policy2 =
549 createFromParentPolicy(policy1.get(), m_originB);
550 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
551 "{\"default-off\": [\"self\"]}", m_originB.get(), &messages));
552 std::unique_ptr<FeaturePolicy> policy3 =
553 createFromParentPolicy(policy2.get(), m_originB);
554 std::unique_ptr<FeaturePolicy> policy4 =
555 createFromParentPolicy(policy2.get(), m_originC);
556 policy4->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
557 "{\"default-off\": [\"self\"]}", m_originC.get(), &messages));
558 EXPECT_FALSE(policy1->isFeatureEnabled(kDefaultOffFeature));
559 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultOffFeature));
560 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultOffFeature));
561 EXPECT_FALSE(policy4->isFeatureEnabled(kDefaultOffFeature));
562 }
563
564 TEST_F(FeaturePolicyTest, TestReenableForAllOrigins) {
565 // +------------------------------------+
566 // |(1) Origin A |
567 // |Policy: {"default-self": ["*"]} |
568 // | +--------------------------------+ |
569 // | |(2) Origin B | |
570 // | |Policy: {"default-self": ["*"]} | |
571 // | | +-------------+ | |
572 // | | |(3)Origin A | | |
573 // | | |No Policy | | |
574 // | | +-------------+ | |
575 // | +--------------------------------+ |
576 // +------------------------------------+
577 // Feature should be enabled in all frames.
578 Vector<String> messages;
579 std::unique_ptr<FeaturePolicy> policy1 =
580 createFromParentPolicy(nullptr, m_originA);
581 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
582 "{\"default-self\": [\"*\"]}", m_originA.get(), &messages));
583 EXPECT_EQ(0UL, messages.size());
584 std::unique_ptr<FeaturePolicy> policy2 =
585 createFromParentPolicy(policy1.get(), m_originB);
586 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
587 "{\"default-self\": [\"*\"]}", m_originB.get(), &messages));
588 EXPECT_EQ(0UL, messages.size());
589 std::unique_ptr<FeaturePolicy> policy3 =
590 createFromParentPolicy(policy2.get(), m_originA);
591 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultSelfFeature));
592 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
593 EXPECT_TRUE(policy3->isFeatureEnabled(kDefaultSelfFeature));
594 }
595
596 TEST_F(FeaturePolicyTest, TestBlockedFrameCannotReenable) {
597 // +--------------------------------------+
598 // |(1)Origin A |
599 // |Policy: {"default-self": ["self"]} |
600 // | +----------------------------------+ |
601 // | |(2)Origin B | |
602 // | |Policy: {"default-self": ["*"]} | |
603 // | | +-------------+ +-------------+ | |
604 // | | |(3)Origin A | |(4)Origin C | | |
605 // | | |No Policy | |No Policy | | |
606 // | | +-------------+ +-------------+ | |
607 // | +----------------------------------+ |
608 // +--------------------------------------+
609 // Feature should be enabled at the top level; disabled in all other frames.
610 Vector<String> messages;
611 std::unique_ptr<FeaturePolicy> policy1 =
612 createFromParentPolicy(nullptr, m_originA);
613 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
614 "{\"default-self\": [\"self\"]}", m_originA.get(), &messages));
615 EXPECT_EQ(0UL, messages.size());
616 std::unique_ptr<FeaturePolicy> policy2 =
617 createFromParentPolicy(policy1.get(), m_originB);
618 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
619 "{\"default-self\": [\"*\"]}", m_originB.get(), &messages));
620 EXPECT_EQ(0UL, messages.size());
621 std::unique_ptr<FeaturePolicy> policy3 =
622 createFromParentPolicy(policy2.get(), m_originA);
623 std::unique_ptr<FeaturePolicy> policy4 =
624 createFromParentPolicy(policy2.get(), m_originC);
625 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultSelfFeature));
626 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultSelfFeature));
627 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultSelfFeature));
628 EXPECT_FALSE(policy4->isFeatureEnabled(kDefaultSelfFeature));
629 }
630
631 TEST_F(FeaturePolicyTest, TestEnabledFrameCanDelegate) {
632 // +---------------------------------------------------+
633 // |(1) Origin A |
634 // |Policy: {"default-self": ["self", "Origin B"]} |
635 // | +-----------------------------------------------+ |
636 // | |(2) Origin B | |
637 // | |Policy: {"default-self": ["self", "Origin C"]} | |
638 // | | +-------------+ | |
639 // | | |(3)Origin C | | |
640 // | | |No Policy | | |
641 // | | +-------------+ | |
642 // | +-----------------------------------------------+ |
643 // +---------------------------------------------------+
644 // Feature should be enabled in all frames.
645 Vector<String> messages;
646 std::unique_ptr<FeaturePolicy> policy1 =
647 createFromParentPolicy(nullptr, m_originA);
648 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
649 "{\"default-self\": [\"self\", \"" ORIGIN_B "\"]}", m_originA.get(),
650 &messages));
651 EXPECT_EQ(0UL, messages.size());
652 std::unique_ptr<FeaturePolicy> policy2 =
653 createFromParentPolicy(policy1.get(), m_originB);
654 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
655 "{\"default-self\": [\"self\", \"" ORIGIN_C "\"]}", m_originB.get(),
656 &messages));
657 EXPECT_EQ(0UL, messages.size());
658 std::unique_ptr<FeaturePolicy> policy3 =
659 createFromParentPolicy(policy2.get(), m_originC);
660 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultSelfFeature));
661 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
662 EXPECT_TRUE(policy3->isFeatureEnabled(kDefaultSelfFeature));
663 }
664
665 TEST_F(FeaturePolicyTest, TestEnabledFrameCanDelegateByDefault) {
666 // +-----------------------------------------------+
667 // |(1) Origin A |
668 // |Policy: {"default-on": ["self", "Origin B"]} |
669 // | +--------------------+ +--------------------+ |
670 // | |(2) Origin B | | (4) Origin C | |
671 // | |No Policy | | No Policy | |
672 // | | +-------------+ | | | |
673 // | | |(3)Origin C | | | | |
674 // | | |No Policy | | | | |
675 // | | +-------------+ | | | |
676 // | +--------------------+ +--------------------+ |
677 // +-----------------------------------------------+
678 // Feature should be enabled in frames 1, 2, and 3, and disabled in frame 4.
679 Vector<String> messages;
680 std::unique_ptr<FeaturePolicy> policy1 =
681 createFromParentPolicy(nullptr, m_originA);
682 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
683 "{\"default-on\": [\"self\", \"" ORIGIN_B "\"]}", m_originA.get(),
684 &messages));
685 EXPECT_EQ(0UL, messages.size());
686 std::unique_ptr<FeaturePolicy> policy2 =
687 createFromParentPolicy(policy1.get(), m_originB);
688 std::unique_ptr<FeaturePolicy> policy3 =
689 createFromParentPolicy(policy2.get(), m_originC);
690 std::unique_ptr<FeaturePolicy> policy4 =
691 createFromParentPolicy(policy1.get(), m_originC);
692 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultOnFeature));
693 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultOnFeature));
694 EXPECT_TRUE(policy3->isFeatureEnabled(kDefaultOnFeature));
695 EXPECT_FALSE(policy4->isFeatureEnabled(kDefaultOnFeature));
696 }
697
698 TEST_F(FeaturePolicyTest, TestNonNestedFeaturesDontDelegateByDefault) {
699 // +-----------------------------------------------+
700 // |(1) Origin A |
701 // |Policy: {"default-self": ["self", "Origin B"]} |
702 // | +--------------------+ +--------------------+ |
703 // | |(2) Origin B | | (4) Origin C | |
704 // | |No Policy | | No Policy | |
705 // | | +-------------+ | | | |
706 // | | |(3)Origin C | | | | |
707 // | | |No Policy | | | | |
708 // | | +-------------+ | | | |
709 // | +--------------------+ +--------------------+ |
710 // +-----------------------------------------------+
711 // Feature should be enabled in frames 1 and 2, and disabled in frames 3 and
712 // 4.
713 Vector<String> messages;
714 std::unique_ptr<FeaturePolicy> policy1 =
715 createFromParentPolicy(nullptr, m_originA);
716 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
717 "{\"default-self\": [\"self\", \"" ORIGIN_B "\"]}", m_originA.get(),
718 &messages));
719 EXPECT_EQ(0UL, messages.size());
720 std::unique_ptr<FeaturePolicy> policy2 =
721 createFromParentPolicy(policy1.get(), m_originB);
722 std::unique_ptr<FeaturePolicy> policy3 =
723 createFromParentPolicy(policy2.get(), m_originC);
724 std::unique_ptr<FeaturePolicy> policy4 =
725 createFromParentPolicy(policy1.get(), m_originC);
726 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultSelfFeature));
727 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
728 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultSelfFeature));
729 EXPECT_FALSE(policy4->isFeatureEnabled(kDefaultSelfFeature));
730 }
731
732 TEST_F(FeaturePolicyTest, TestFeaturesAreIndependent) {
733 // +-----------------------------------------------+
734 // |(1) Origin A |
735 // |Policy: {"default-self": ["self", "Origin B"], |
736 // | "default-on": ["self"]} |
737 // | +-------------------------------------------+ |
738 // | |(2) Origin B | |
739 // | |Policy: {"default-self": ["*"], | |
740 // | | "default-on": ["*"]} | |
741 // | | +-------------+ | |
742 // | | |(3)Origin C | | |
743 // | | |No Policy | | |
744 // | | +-------------+ | |
745 // | +-------------------------------------------+ |
746 // +-----------------------------------------------+
747 // Default-self feature should be enabled in all frames; Default-on feature
748 // should be enabled in frame 1, and disabled in frames 2 and 3.
749 Vector<String> messages;
750 std::unique_ptr<FeaturePolicy> policy1 =
751 createFromParentPolicy(nullptr, m_originA);
752 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
753 "{\"default-self\": [\"self\", \"" ORIGIN_B
754 "\"], \"default-on\": [\"self\"]}",
755 m_originA.get(), &messages));
756 EXPECT_EQ(0UL, messages.size());
757 std::unique_ptr<FeaturePolicy> policy2 =
758 createFromParentPolicy(policy1.get(), m_originB);
759 policy2->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
760 "{\"default-self\": [\"*\"], \"default-on\": [\"*\"]}", m_originB.get(),
761 &messages));
762 EXPECT_EQ(0UL, messages.size());
763 std::unique_ptr<FeaturePolicy> policy3 =
764 createFromParentPolicy(policy2.get(), m_originC);
765 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultSelfFeature));
766 EXPECT_TRUE(policy1->isFeatureEnabled(kDefaultOnFeature));
767 EXPECT_TRUE(policy2->isFeatureEnabled(kDefaultSelfFeature));
768 EXPECT_FALSE(policy2->isFeatureEnabled(kDefaultOnFeature));
769 EXPECT_TRUE(policy3->isFeatureEnabled(kDefaultSelfFeature));
770 EXPECT_FALSE(policy3->isFeatureEnabled(kDefaultOnFeature));
771 }
772
773 TEST_F(FeaturePolicyTest, TestFeatureEnabledForOrigin) {
774 // +-----------------------------------------------+
775 // |(1) Origin A |
776 // |Policy: {"default-off": ["self", "Origin B"]} |
777 // +-----------------------------------------------+
778 // Features should be enabled by the policy in frame 1 for origins A and B,
779 // and disabled for origin C.
780 Vector<String> messages;
781 std::unique_ptr<FeaturePolicy> policy1 =
782 createFromParentPolicy(nullptr, m_originA);
783 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy(
784 "{\"default-off\": [\"self\", \"" ORIGIN_B "\"]}", m_originA.get(),
785 &messages));
786 EXPECT_EQ(0UL, messages.size());
787 EXPECT_TRUE(
788 policy1->isFeatureEnabledForOrigin(kDefaultOffFeature, *m_originA));
789 EXPECT_TRUE(
790 policy1->isFeatureEnabledForOrigin(kDefaultOffFeature, *m_originB));
791 EXPECT_FALSE(
792 policy1->isFeatureEnabledForOrigin(kDefaultOffFeature, *m_originC));
793 } 117 }
794 118
795 } // namespace blink 119 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698