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 "core/html/HTMLIFrameElement.h" | 5 #include "core/html/HTMLIFrameElement.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace blink { | 10 namespace blink { |
11 | 11 |
| 12 class HTMLIFrameElementTest : public ::testing::Test { |
| 13 public: |
| 14 RefPtr<SecurityOrigin> GetOriginForFeaturePolicy(HTMLIFrameElement* element) { |
| 15 return element->GetOriginForFeaturePolicy(); |
| 16 } |
| 17 }; |
| 18 |
12 // Test setting feature policy via the Element attribute (HTML codepath). | 19 // Test setting feature policy via the Element attribute (HTML codepath). |
13 TEST(HTMLIFrameElementTest, SetAllowAttribute) { | 20 TEST_F(HTMLIFrameElementTest, SetAllowAttribute) { |
14 Document* document = Document::Create(); | 21 Document* document = Document::Create(); |
15 HTMLIFrameElement* iframe = HTMLIFrameElement::Create(*document); | 22 HTMLIFrameElement* iframe = HTMLIFrameElement::Create(*document); |
16 | 23 |
17 iframe->setAttribute(HTMLNames::allowAttr, "fullscreen"); | 24 iframe->setAttribute(HTMLNames::allowAttr, "fullscreen"); |
18 EXPECT_EQ("fullscreen", iframe->allow()->value()); | 25 EXPECT_EQ("fullscreen", iframe->allow()->value()); |
19 iframe->setAttribute(HTMLNames::allowAttr, "fullscreen vibrate"); | 26 iframe->setAttribute(HTMLNames::allowAttr, "fullscreen vibrate"); |
20 EXPECT_EQ("fullscreen vibrate", iframe->allow()->value()); | 27 EXPECT_EQ("fullscreen vibrate", iframe->allow()->value()); |
21 } | 28 } |
22 | 29 |
23 // Test setting feature policy via the DOMTokenList (JS codepath). | 30 // Test setting feature policy via the DOMTokenList (JS codepath). |
24 TEST(HTMLIFrameElementTest, SetAllowAttributeJS) { | 31 TEST_F(HTMLIFrameElementTest, SetAllowAttributeJS) { |
25 Document* document = Document::Create(); | 32 Document* document = Document::Create(); |
26 HTMLIFrameElement* iframe = HTMLIFrameElement::Create(*document); | 33 HTMLIFrameElement* iframe = HTMLIFrameElement::Create(*document); |
27 | 34 |
28 iframe->allow()->setValue("fullscreen"); | 35 iframe->allow()->setValue("fullscreen"); |
29 EXPECT_EQ("fullscreen", iframe->getAttribute(HTMLNames::allowAttr)); | 36 EXPECT_EQ("fullscreen", iframe->getAttribute(HTMLNames::allowAttr)); |
30 } | 37 } |
31 | 38 |
| 39 // Test that the correct origin is used when constructing the container policy, |
| 40 // and that frames which should inherit their parent document's origin do so. |
| 41 TEST_F(HTMLIFrameElementTest, FramesUseCorrectOrigin) { |
| 42 Document* document = Document::Create(); |
| 43 KURL document_url = KURL(KURL(), "http://example.com"); |
| 44 document->SetURL(document_url); |
| 45 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 46 |
| 47 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 48 |
| 49 frame_element->setAttribute(HTMLNames::srcAttr, "about:blank"); |
| 50 RefPtr<SecurityOrigin> effective_origin = |
| 51 GetOriginForFeaturePolicy(frame_element); |
| 52 EXPECT_TRUE( |
| 53 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 54 |
| 55 frame_element->setAttribute(HTMLNames::srcAttr, |
| 56 "data:text/html;base64,PHRpdGxlPkFCQzwvdGl0bGU+"); |
| 57 effective_origin = GetOriginForFeaturePolicy(frame_element); |
| 58 EXPECT_FALSE( |
| 59 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 60 EXPECT_TRUE(effective_origin->IsUnique()); |
| 61 |
| 62 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.net/"); |
| 63 effective_origin = GetOriginForFeaturePolicy(frame_element); |
| 64 EXPECT_FALSE( |
| 65 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 66 EXPECT_FALSE(effective_origin->IsUnique()); |
| 67 } |
| 68 |
| 69 // Test that a unique origin is used when constructing the container policy in a |
| 70 // sandboxed iframe. |
| 71 TEST_F(HTMLIFrameElementTest, SandboxFramesUseCorrectOrigin) { |
| 72 Document* document = Document::Create(); |
| 73 KURL document_url = KURL(KURL(), "http://example.com"); |
| 74 document->SetURL(document_url); |
| 75 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 76 |
| 77 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 78 |
| 79 frame_element->setAttribute(HTMLNames::sandboxAttr, ""); |
| 80 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.com/"); |
| 81 RefPtr<SecurityOrigin> effective_origin = |
| 82 GetOriginForFeaturePolicy(frame_element); |
| 83 EXPECT_FALSE( |
| 84 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 85 EXPECT_TRUE(effective_origin->IsUnique()); |
| 86 |
| 87 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.net/"); |
| 88 effective_origin = GetOriginForFeaturePolicy(frame_element); |
| 89 EXPECT_FALSE( |
| 90 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 91 EXPECT_TRUE(effective_origin->IsUnique()); |
| 92 } |
| 93 |
| 94 // Test that a sandboxed iframe with the allow-same-origin sandbox flag uses the |
| 95 // parent document's origin for the container policy. |
| 96 TEST_F(HTMLIFrameElementTest, SameOriginSandboxFramesUseCorrectOrigin) { |
| 97 Document* document = Document::Create(); |
| 98 KURL document_url = KURL(KURL(), "http://example.com"); |
| 99 document->SetURL(document_url); |
| 100 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 101 |
| 102 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 103 |
| 104 frame_element->setAttribute(HTMLNames::sandboxAttr, "allow-same-origin"); |
| 105 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.com/"); |
| 106 RefPtr<SecurityOrigin> effective_origin = |
| 107 GetOriginForFeaturePolicy(frame_element); |
| 108 EXPECT_TRUE( |
| 109 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 110 EXPECT_FALSE(effective_origin->IsUnique()); |
| 111 } |
| 112 |
| 113 // Test that the parent document's origin is used when constructing the |
| 114 // container policy in a srcdoc iframe. |
| 115 TEST_F(HTMLIFrameElementTest, SrcdocFramesUseCorrectOrigin) { |
| 116 Document* document = Document::Create(); |
| 117 KURL document_url = KURL(KURL(), "http://example.com"); |
| 118 document->SetURL(document_url); |
| 119 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 120 |
| 121 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 122 |
| 123 frame_element->setAttribute(HTMLNames::srcdocAttr, "<title>title</title>"); |
| 124 RefPtr<SecurityOrigin> effective_origin = |
| 125 GetOriginForFeaturePolicy(frame_element); |
| 126 EXPECT_TRUE( |
| 127 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 128 } |
| 129 |
| 130 // Test that a unique origin is used when constructing the container policy in a |
| 131 // sandboxed iframe with a srcdoc. |
| 132 TEST_F(HTMLIFrameElementTest, SandboxedSrcdocFramesUseCorrectOrigin) { |
| 133 Document* document = Document::Create(); |
| 134 KURL document_url = KURL(KURL(), "http://example.com"); |
| 135 document->SetURL(document_url); |
| 136 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 137 |
| 138 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 139 |
| 140 frame_element->setAttribute(HTMLNames::sandboxAttr, ""); |
| 141 frame_element->setAttribute(HTMLNames::srcdocAttr, "<title>title</title>"); |
| 142 RefPtr<SecurityOrigin> effective_origin = |
| 143 GetOriginForFeaturePolicy(frame_element); |
| 144 EXPECT_FALSE( |
| 145 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 146 EXPECT_TRUE(effective_origin->IsUnique()); |
| 147 } |
| 148 |
| 149 // Test that iframes with relative src urls correctly construct their origin |
| 150 // relative to the parent document. |
| 151 TEST_F(HTMLIFrameElementTest, RelativeURLsUseCorrectOrigin) { |
| 152 Document* document = Document::Create(); |
| 153 KURL document_url = KURL(KURL(), "http://example.com"); |
| 154 document->SetURL(document_url); |
| 155 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 156 |
| 157 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 158 |
| 159 // Host-relative URLs should resolve to the same domain as the parent. |
| 160 frame_element->setAttribute(HTMLNames::srcAttr, "index2.html"); |
| 161 RefPtr<SecurityOrigin> effective_origin = |
| 162 GetOriginForFeaturePolicy(frame_element); |
| 163 EXPECT_TRUE( |
| 164 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 165 |
| 166 // Scheme-relative URLs should not resolve to the same domain as the parent. |
| 167 frame_element->setAttribute(HTMLNames::srcAttr, "//example.net/index2.html"); |
| 168 effective_origin = GetOriginForFeaturePolicy(frame_element); |
| 169 EXPECT_FALSE( |
| 170 effective_origin->IsSameSchemeHostPort(document->GetSecurityOrigin())); |
| 171 } |
| 172 |
| 173 // Test that various iframe attribute configurations result in the correct |
| 174 // container policies. |
| 175 |
| 176 // Test that the correct container policy is constructed on an iframe element. |
| 177 TEST_F(HTMLIFrameElementTest, DefaultContainerPolicy) { |
| 178 Document* document = Document::Create(); |
| 179 KURL document_url = KURL(KURL(), "http://example.com"); |
| 180 document->SetURL(document_url); |
| 181 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 182 |
| 183 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 184 |
| 185 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.net/"); |
| 186 frame_element->UpdateContainerPolicyForTests(); |
| 187 |
| 188 const WebParsedFeaturePolicy& container_policy = |
| 189 frame_element->ContainerPolicy(); |
| 190 EXPECT_EQ(0UL, container_policy.size()); |
| 191 } |
| 192 |
| 193 // Test that the allow attribute results in a container policy which is |
| 194 // restricted to the domain in the src attribute. |
| 195 TEST_F(HTMLIFrameElementTest, AllowAttributeContainerPolicy) { |
| 196 Document* document = Document::Create(); |
| 197 KURL document_url = KURL(KURL(), "http://example.com"); |
| 198 document->SetURL(document_url); |
| 199 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 200 |
| 201 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 202 |
| 203 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.net/"); |
| 204 frame_element->setAttribute(HTMLNames::allowAttr, "fullscreen"); |
| 205 frame_element->UpdateContainerPolicyForTests(); |
| 206 |
| 207 const WebParsedFeaturePolicy& container_policy1 = |
| 208 frame_element->ContainerPolicy(); |
| 209 |
| 210 EXPECT_EQ(1UL, container_policy1.size()); |
| 211 EXPECT_EQ(WebFeaturePolicyFeature::kFullscreen, container_policy1[0].feature); |
| 212 EXPECT_FALSE(container_policy1[0].matches_all_origins); |
| 213 EXPECT_EQ(1UL, container_policy1[0].origins.size()); |
| 214 EXPECT_EQ("http://example.net", container_policy1[0].origins[0].ToString()); |
| 215 |
| 216 frame_element->setAttribute(HTMLNames::allowAttr, "payment fullscreen"); |
| 217 frame_element->UpdateContainerPolicyForTests(); |
| 218 |
| 219 const WebParsedFeaturePolicy& container_policy2 = |
| 220 frame_element->ContainerPolicy(); |
| 221 EXPECT_EQ(2UL, container_policy2.size()); |
| 222 EXPECT_TRUE( |
| 223 container_policy2[0].feature == WebFeaturePolicyFeature::kFullscreen || |
| 224 container_policy2[1].feature == WebFeaturePolicyFeature::kFullscreen); |
| 225 EXPECT_TRUE( |
| 226 container_policy2[0].feature == WebFeaturePolicyFeature::kPayment || |
| 227 container_policy2[1].feature == WebFeaturePolicyFeature::kPayment); |
| 228 EXPECT_FALSE(container_policy2[0].matches_all_origins); |
| 229 EXPECT_EQ(1UL, container_policy2[0].origins.size()); |
| 230 EXPECT_EQ("http://example.net", container_policy2[0].origins[0].ToString()); |
| 231 EXPECT_FALSE(container_policy2[1].matches_all_origins); |
| 232 EXPECT_EQ(1UL, container_policy2[1].origins.size()); |
| 233 EXPECT_EQ("http://example.net", container_policy2[1].origins[0].ToString()); |
| 234 } |
| 235 |
| 236 // Test that the allow attribute on a sandboxed frame results in a container |
| 237 // policy which is restricted to a unique origin. |
| 238 TEST_F(HTMLIFrameElementTest, SandboxAttributeContainerPolicy) { |
| 239 Document* document = Document::Create(); |
| 240 KURL document_url = KURL(KURL(), "http://example.com"); |
| 241 document->SetURL(document_url); |
| 242 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 243 |
| 244 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 245 |
| 246 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.net/"); |
| 247 frame_element->setAttribute(HTMLNames::allowAttr, "fullscreen"); |
| 248 frame_element->setAttribute(HTMLNames::sandboxAttr, ""); |
| 249 frame_element->UpdateContainerPolicyForTests(); |
| 250 |
| 251 const WebParsedFeaturePolicy& container_policy = |
| 252 frame_element->ContainerPolicy(); |
| 253 |
| 254 EXPECT_EQ(1UL, container_policy.size()); |
| 255 EXPECT_EQ(WebFeaturePolicyFeature::kFullscreen, container_policy[0].feature); |
| 256 EXPECT_FALSE(container_policy[0].matches_all_origins); |
| 257 EXPECT_EQ(1UL, container_policy[0].origins.size()); |
| 258 EXPECT_TRUE(container_policy[0].origins[0].IsUnique()); |
| 259 } |
| 260 |
| 261 // Test that the allow attribute on a sandboxed frame with the allow-same-origin |
| 262 // flag results in a container policy which is restricted to the origin of the |
| 263 // containing document. |
| 264 TEST_F(HTMLIFrameElementTest, SameOriginSandboxAttributeContainerPolicy) { |
| 265 Document* document = Document::Create(); |
| 266 KURL document_url = KURL(KURL(), "http://example.com"); |
| 267 document->SetURL(document_url); |
| 268 document->UpdateSecurityOrigin(SecurityOrigin::Create(document_url)); |
| 269 |
| 270 HTMLIFrameElement* frame_element = HTMLIFrameElement::Create(*document); |
| 271 |
| 272 frame_element->setAttribute(HTMLNames::srcAttr, "http://example.net/"); |
| 273 frame_element->setAttribute(HTMLNames::allowAttr, "fullscreen"); |
| 274 frame_element->setAttribute(HTMLNames::sandboxAttr, "allow-same-origin"); |
| 275 frame_element->UpdateContainerPolicyForTests(); |
| 276 |
| 277 const WebParsedFeaturePolicy& container_policy = |
| 278 frame_element->ContainerPolicy(); |
| 279 |
| 280 EXPECT_EQ(1UL, container_policy.size()); |
| 281 EXPECT_EQ(WebFeaturePolicyFeature::kFullscreen, container_policy[0].feature); |
| 282 EXPECT_FALSE(container_policy[0].matches_all_origins); |
| 283 EXPECT_EQ(1UL, container_policy[0].origins.size()); |
| 284 EXPECT_FALSE(container_policy[0].origins[0].IsUnique()); |
| 285 EXPECT_EQ("http://example.net", container_policy[0].origins[0].ToString()); |
| 286 } |
| 287 |
32 } // namespace blink | 288 } // namespace blink |
OLD | NEW |