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

Side by Side Diff: third_party/WebKit/Source/core/loader/FrameFetchContextTest.cpp

Issue 2372563002: Adding Embedding-CSP HTTP header (Closed)
Patch Set: Addressing comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015, Google Inc. All rights reserved. 2 * Copyright (c) 2015, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 19 matching lines...) Expand all
30 30
31 #include "core/loader/FrameFetchContext.h" 31 #include "core/loader/FrameFetchContext.h"
32 32
33 #include "core/dom/Document.h" 33 #include "core/dom/Document.h"
34 #include "core/fetch/FetchInitiatorInfo.h" 34 #include "core/fetch/FetchInitiatorInfo.h"
35 #include "core/fetch/UniqueIdentifier.h" 35 #include "core/fetch/UniqueIdentifier.h"
36 #include "core/frame/FrameHost.h" 36 #include "core/frame/FrameHost.h"
37 #include "core/frame/FrameOwner.h" 37 #include "core/frame/FrameOwner.h"
38 #include "core/frame/FrameView.h" 38 #include "core/frame/FrameView.h"
39 #include "core/frame/Settings.h" 39 #include "core/frame/Settings.h"
40 #include "core/html/HTMLIFrameElement.h"
40 #include "core/loader/DocumentLoader.h" 41 #include "core/loader/DocumentLoader.h"
41 #include "core/loader/EmptyClients.h" 42 #include "core/loader/EmptyClients.h"
42 #include "core/page/Page.h" 43 #include "core/page/Page.h"
43 #include "core/testing/DummyPageHolder.h" 44 #include "core/testing/DummyPageHolder.h"
44 #include "platform/network/ResourceRequest.h" 45 #include "platform/network/ResourceRequest.h"
45 #include "platform/weborigin/KURL.h" 46 #include "platform/weborigin/KURL.h"
46 #include "public/platform/WebAddressSpace.h" 47 #include "public/platform/WebAddressSpace.h"
47 #include "public/platform/WebCachePolicy.h" 48 #include "public/platform/WebCachePolicy.h"
48 #include "public/platform/WebInsecureRequestPolicy.h" 49 #include "public/platform/WebInsecureRequestPolicy.h"
49 #include "testing/gmock/include/gmock/gmock-generated-function-mockers.h" 50 #include "testing/gmock/include/gmock/gmock-generated-function-mockers.h"
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 fetchContext = 157 fetchContext =
157 static_cast<FrameFetchContext*>(&documentLoader->fetcher()->context()); 158 static_cast<FrameFetchContext*>(&documentLoader->fetcher()->context());
158 owner = DummyFrameOwner::create(); 159 owner = DummyFrameOwner::create();
159 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get()); 160 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get());
160 } 161 }
161 162
162 KURL url; 163 KURL url;
163 KURL mainResourceUrl; 164 KURL mainResourceUrl;
164 }; 165 };
165 166
166 class FrameFetchContextUpgradeTest : public FrameFetchContextTest { 167 class FrameFetchContextModifyRequestTest : public FrameFetchContextTest {
167 public: 168 public:
168 FrameFetchContextUpgradeTest() 169 FrameFetchContextModifyRequestTest()
169 : exampleOrigin(SecurityOrigin::create( 170 : exampleOrigin(SecurityOrigin::create(
170 KURL(ParsedURLString, "https://example.test/"))), 171 KURL(ParsedURLString, "https://example.test/"))),
171 secureOrigin(SecurityOrigin::create( 172 secureOrigin(SecurityOrigin::create(
172 KURL(ParsedURLString, "https://secureorigin.test/image.png"))) {} 173 KURL(ParsedURLString, "https://secureorigin.test/image.png"))) {}
173 174
174 protected: 175 protected:
175 void expectUpgrade(const char* input, const char* expected) { 176 void expectUpgrade(const char* input, const char* expected) {
176 expectUpgrade(input, WebURLRequest::RequestContextScript, 177 expectUpgrade(input, WebURLRequest::RequestContextScript,
177 WebURLRequest::FrameTypeNone, expected); 178 WebURLRequest::FrameTypeNone, expected);
178 } 179 }
179 180
180 void expectUpgrade(const char* input, 181 void expectUpgrade(const char* input,
181 WebURLRequest::RequestContext requestContext, 182 WebURLRequest::RequestContext requestContext,
182 WebURLRequest::FrameType frameType, 183 WebURLRequest::FrameType frameType,
183 const char* expected) { 184 const char* expected) {
184 KURL inputURL(ParsedURLString, input); 185 KURL inputURL(ParsedURLString, input);
185 KURL expectedURL(ParsedURLString, expected); 186 KURL expectedURL(ParsedURLString, expected);
186 187
187 FetchRequest fetchRequest = 188 FetchRequest fetchRequest =
188 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo()); 189 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo());
189 fetchRequest.mutableResourceRequest().setRequestContext(requestContext); 190 fetchRequest.mutableResourceRequest().setRequestContext(requestContext);
190 fetchRequest.mutableResourceRequest().setFrameType(frameType); 191 fetchRequest.mutableResourceRequest().setFrameType(frameType);
191 192
192 fetchContext->upgradeInsecureRequest(fetchRequest.mutableResourceRequest()); 193 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest());
193 194
194 EXPECT_EQ(expectedURL.getString(), 195 EXPECT_EQ(expectedURL.getString(),
195 fetchRequest.resourceRequest().url().getString()); 196 fetchRequest.resourceRequest().url().getString());
196 EXPECT_EQ(expectedURL.protocol(), 197 EXPECT_EQ(expectedURL.protocol(),
197 fetchRequest.resourceRequest().url().protocol()); 198 fetchRequest.resourceRequest().url().protocol());
198 EXPECT_EQ(expectedURL.host(), fetchRequest.resourceRequest().url().host()); 199 EXPECT_EQ(expectedURL.host(), fetchRequest.resourceRequest().url().host());
199 EXPECT_EQ(expectedURL.port(), fetchRequest.resourceRequest().url().port()); 200 EXPECT_EQ(expectedURL.port(), fetchRequest.resourceRequest().url().port());
200 EXPECT_EQ(expectedURL.hasPort(), 201 EXPECT_EQ(expectedURL.hasPort(),
201 fetchRequest.resourceRequest().url().hasPort()); 202 fetchRequest.resourceRequest().url().hasPort());
202 EXPECT_EQ(expectedURL.path(), fetchRequest.resourceRequest().url().path()); 203 EXPECT_EQ(expectedURL.path(), fetchRequest.resourceRequest().url().path());
203 } 204 }
204 205
205 void expectHTTPSHeader(const char* input, 206 void expectUpgradeInsecureRequestHeader(const char* input,
206 WebURLRequest::FrameType frameType, 207 WebURLRequest::FrameType frameType,
207 bool shouldPrefer) { 208 bool shouldPrefer) {
208 KURL inputURL(ParsedURLString, input); 209 KURL inputURL(ParsedURLString, input);
209 210
210 FetchRequest fetchRequest = 211 FetchRequest fetchRequest =
211 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo()); 212 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo());
212 fetchRequest.mutableResourceRequest().setRequestContext( 213 fetchRequest.mutableResourceRequest().setRequestContext(
213 WebURLRequest::RequestContextScript); 214 WebURLRequest::RequestContextScript);
214 fetchRequest.mutableResourceRequest().setFrameType(frameType); 215 fetchRequest.mutableResourceRequest().setFrameType(frameType);
215 216
216 fetchContext->upgradeInsecureRequest(fetchRequest.mutableResourceRequest()); 217 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest());
217 218
218 EXPECT_EQ(shouldPrefer ? String("1") : String(), 219 EXPECT_EQ(shouldPrefer ? String("1") : String(),
219 fetchRequest.resourceRequest().httpHeaderField( 220 fetchRequest.resourceRequest().httpHeaderField(
220 HTTPNames::Upgrade_Insecure_Requests)); 221 HTTPNames::Upgrade_Insecure_Requests));
221 222
222 // Calling upgradeInsecureRequest more than once shouldn't affect the 223 // Calling modifyRequestForCSP more than once shouldn't affect the
223 // header. 224 // header.
224 if (shouldPrefer) { 225 if (shouldPrefer) {
225 fetchContext->upgradeInsecureRequest( 226 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest());
226 fetchRequest.mutableResourceRequest());
227 EXPECT_EQ("1", fetchRequest.resourceRequest().httpHeaderField( 227 EXPECT_EQ("1", fetchRequest.resourceRequest().httpHeaderField(
228 HTTPNames::Upgrade_Insecure_Requests)); 228 HTTPNames::Upgrade_Insecure_Requests));
229 } 229 }
230 } 230 }
231 231
232 void expectSetEmbeddingCSPRequestHeader(
233 const char* input,
234 WebURLRequest::FrameType frameType,
235 const AtomicString& expectedEmbeddingCSP) {
236 KURL inputURL(ParsedURLString, input);
237
238 FetchRequest fetchRequest =
239 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo());
240 fetchRequest.mutableResourceRequest().setRequestContext(
241 WebURLRequest::RequestContextScript);
242 fetchRequest.mutableResourceRequest().setFrameType(frameType);
243
244 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest());
245
246 EXPECT_EQ(expectedEmbeddingCSP,
247 fetchRequest.resourceRequest().httpHeaderField(
248 HTTPNames::Embedding_CSP));
249 }
250
251 void setFrameOwnerBasedOnFrameType(WebURLRequest::FrameType frameType,
252 HTMLIFrameElement* iframe,
253 const AtomicString& potentialValue) {
254 if (frameType != WebURLRequest::FrameTypeNested) {
255 document->frame()->setOwner(nullptr);
256 return;
257 }
258
259 iframe->setAttribute(HTMLNames::cspAttr, potentialValue);
260 document->frame()->setOwner(iframe);
261 }
262
232 RefPtr<SecurityOrigin> exampleOrigin; 263 RefPtr<SecurityOrigin> exampleOrigin;
233 RefPtr<SecurityOrigin> secureOrigin; 264 RefPtr<SecurityOrigin> secureOrigin;
234 }; 265 };
235 266
236 TEST_F(FrameFetchContextUpgradeTest, UpgradeInsecureResourceRequests) { 267 TEST_F(FrameFetchContextModifyRequestTest, UpgradeInsecureResourceRequests) {
237 struct TestCase { 268 struct TestCase {
238 const char* original; 269 const char* original;
239 const char* upgraded; 270 const char* upgraded;
240 } tests[] = { 271 } tests[] = {
241 {"http://example.test/image.png", "https://example.test/image.png"}, 272 {"http://example.test/image.png", "https://example.test/image.png"},
242 {"http://example.test:80/image.png", 273 {"http://example.test:80/image.png",
243 "https://example.test:443/image.png"}, 274 "https://example.test:443/image.png"},
244 {"http://example.test:1212/image.png", 275 {"http://example.test:1212/image.png",
245 "https://example.test:1212/image.png"}, 276 "https://example.test:1212/image.png"},
246 277
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 // InsecureNavigationsSet: 315 // InsecureNavigationsSet:
285 document->addInsecureNavigationUpgrade( 316 document->addInsecureNavigationUpgrade(
286 exampleOrigin->host().impl()->hash()); 317 exampleOrigin->host().impl()->hash());
287 expectUpgrade(test.original, WebURLRequest::RequestContextScript, 318 expectUpgrade(test.original, WebURLRequest::RequestContextScript,
288 WebURLRequest::FrameTypeTopLevel, test.upgraded); 319 WebURLRequest::FrameTypeTopLevel, test.upgraded);
289 expectUpgrade(test.original, WebURLRequest::RequestContextScript, 320 expectUpgrade(test.original, WebURLRequest::RequestContextScript,
290 WebURLRequest::FrameTypeAuxiliary, test.upgraded); 321 WebURLRequest::FrameTypeAuxiliary, test.upgraded);
291 } 322 }
292 } 323 }
293 324
294 TEST_F(FrameFetchContextUpgradeTest, DoNotUpgradeInsecureResourceRequests) { 325 TEST_F(FrameFetchContextModifyRequestTest,
326 DoNotUpgradeInsecureResourceRequests) {
295 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get()); 327 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get());
296 document->setSecurityOrigin(secureOrigin); 328 document->setSecurityOrigin(secureOrigin);
297 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone); 329 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone);
298 330
299 expectUpgrade("http://example.test/image.png", 331 expectUpgrade("http://example.test/image.png",
300 "http://example.test/image.png"); 332 "http://example.test/image.png");
301 expectUpgrade("http://example.test:80/image.png", 333 expectUpgrade("http://example.test:80/image.png",
302 "http://example.test:80/image.png"); 334 "http://example.test:80/image.png");
303 expectUpgrade("http://example.test:1212/image.png", 335 expectUpgrade("http://example.test:1212/image.png",
304 "http://example.test:1212/image.png"); 336 "http://example.test:1212/image.png");
305 337
306 expectUpgrade("https://example.test/image.png", 338 expectUpgrade("https://example.test/image.png",
307 "https://example.test/image.png"); 339 "https://example.test/image.png");
308 expectUpgrade("https://example.test:80/image.png", 340 expectUpgrade("https://example.test:80/image.png",
309 "https://example.test:80/image.png"); 341 "https://example.test:80/image.png");
310 expectUpgrade("https://example.test:1212/image.png", 342 expectUpgrade("https://example.test:1212/image.png",
311 "https://example.test:1212/image.png"); 343 "https://example.test:1212/image.png");
312 344
313 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"); 345 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png");
314 expectUpgrade("ftp://example.test:21/image.png", 346 expectUpgrade("ftp://example.test:21/image.png",
315 "ftp://example.test:21/image.png"); 347 "ftp://example.test:21/image.png");
316 expectUpgrade("ftp://example.test:1212/image.png", 348 expectUpgrade("ftp://example.test:1212/image.png",
317 "ftp://example.test:1212/image.png"); 349 "ftp://example.test:1212/image.png");
318 } 350 }
319 351
320 TEST_F(FrameFetchContextUpgradeTest, SendHTTPSHeader) { 352 TEST_F(FrameFetchContextModifyRequestTest, SendUpgradeInsecureRequestHeader) {
321 struct TestCase { 353 struct TestCase {
322 const char* toRequest; 354 const char* toRequest;
323 WebURLRequest::FrameType frameType; 355 WebURLRequest::FrameType frameType;
324 bool shouldPrefer; 356 bool shouldPrefer;
325 } tests[] = { 357 } tests[] = {
326 {"http://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, 358 {"http://example.test/page.html", WebURLRequest::FrameTypeAuxiliary,
327 true}, 359 true},
328 {"http://example.test/page.html", WebURLRequest::FrameTypeNested, true}, 360 {"http://example.test/page.html", WebURLRequest::FrameTypeNested, true},
329 {"http://example.test/page.html", WebURLRequest::FrameTypeNone, false}, 361 {"http://example.test/page.html", WebURLRequest::FrameTypeNone, false},
330 {"http://example.test/page.html", WebURLRequest::FrameTypeTopLevel, true}, 362 {"http://example.test/page.html", WebURLRequest::FrameTypeTopLevel, true},
331 {"https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, 363 {"https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary,
332 true}, 364 true},
333 {"https://example.test/page.html", WebURLRequest::FrameTypeNested, true}, 365 {"https://example.test/page.html", WebURLRequest::FrameTypeNested, true},
334 {"https://example.test/page.html", WebURLRequest::FrameTypeNone, false}, 366 {"https://example.test/page.html", WebURLRequest::FrameTypeNone, false},
335 {"https://example.test/page.html", WebURLRequest::FrameTypeTopLevel, 367 {"https://example.test/page.html", WebURLRequest::FrameTypeTopLevel,
336 true}}; 368 true}};
337 369
338 // This should work correctly both when the FrameFetchContext has a Document, 370 // This should work correctly both when the FrameFetchContext has a Document,
339 // and when it doesn't (e.g. during main frame navigations), so run through 371 // and when it doesn't (e.g. during main frame navigations), so run through
340 // the tests both before and after providing a document to the context. 372 // the tests both before and after providing a document to the context.
341 for (const auto& test : tests) { 373 for (const auto& test : tests) {
342 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone); 374 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone);
343 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 375 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
376 test.shouldPrefer);
344 377
345 document->setInsecureRequestPolicy(kUpgradeInsecureRequests); 378 document->setInsecureRequestPolicy(kUpgradeInsecureRequests);
346 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 379 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
380 test.shouldPrefer);
347 } 381 }
348 382
349 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get()); 383 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get());
350 384
351 for (const auto& test : tests) { 385 for (const auto& test : tests) {
352 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone); 386 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone);
353 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 387 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
388 test.shouldPrefer);
354 389
355 document->setInsecureRequestPolicy(kUpgradeInsecureRequests); 390 document->setInsecureRequestPolicy(kUpgradeInsecureRequests);
356 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 391 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
392 test.shouldPrefer);
357 } 393 }
358 } 394 }
359 395
396 TEST_F(FrameFetchContextModifyRequestTest, SendEmbeddingCSPHeader) {
397 struct TestCase {
398 const char* toRequest;
399 WebURLRequest::FrameType frameType;
400 } tests[] = {
401 {"https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary},
402 {"https://example.test/page.html", WebURLRequest::FrameTypeNested},
403 {"https://example.test/page.html", WebURLRequest::FrameTypeNone},
404 {"https://example.test/page.html", WebURLRequest::FrameTypeTopLevel}};
405
406 HTMLIFrameElement* iframe = HTMLIFrameElement::create(*document);
407 const AtomicString& requiredCSP = AtomicString("default-src 'none'");
408 const AtomicString& anotherRequiredCSP = AtomicString("default-src 'self'");
409
410 for (const auto& test : tests) {
411 setFrameOwnerBasedOnFrameType(test.frameType, iframe, requiredCSP);
412 expectSetEmbeddingCSPRequestHeader(
413 test.toRequest, test.frameType,
414 test.frameType == WebURLRequest::FrameTypeNested ? requiredCSP
415 : nullAtom);
416
417 setFrameOwnerBasedOnFrameType(test.frameType, iframe, anotherRequiredCSP);
418 expectSetEmbeddingCSPRequestHeader(
419 test.toRequest, test.frameType,
420 test.frameType == WebURLRequest::FrameTypeNested ? anotherRequiredCSP
421 : nullAtom);
422 }
423 }
424
360 class FrameFetchContextHintsTest : public FrameFetchContextTest { 425 class FrameFetchContextHintsTest : public FrameFetchContextTest {
361 public: 426 public:
362 FrameFetchContextHintsTest() {} 427 FrameFetchContextHintsTest() {}
363 428
364 protected: 429 protected:
365 void expectHeader(const char* input, 430 void expectHeader(const char* input,
366 const char* headerName, 431 const char* headerName,
367 bool isPresent, 432 bool isPresent,
368 const char* headerValue, 433 const char* headerValue,
369 float width = 0) { 434 float width = 0) {
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource); 803 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource);
739 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest()); 804 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest());
740 805
741 ResourceRequest subRequest(test.url); 806 ResourceRequest subRequest(test.url);
742 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource); 807 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource);
743 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest()); 808 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest());
744 } 809 }
745 } 810 }
746 811
747 } // namespace blink 812 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698