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