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