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

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

Issue 2372563002: Adding Embedding-CSP HTTP header (Closed)
Patch Set: Adding a test in FrameFetchContextModifyRequestTest 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 {
Mike West 2016/10/06 13:30:21 I would have expected you to need to set the runti
amalika 2016/10/06 18:54:57 From my understanding this might be because site_p
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 const AtomicString& setFrameOwnerBasedOnFrameType(
252 WebURLRequest::FrameType frameType,
253 HTMLIFrameElement* iframe,
254 const AtomicString& potentialValue) {
255 if (frameType == WebURLRequest::FrameTypeNested) {
Mike West 2016/10/06 13:30:21 Nit: We generally try to do the quick exit first.
256 iframe->setAttribute(HTMLNames::cspAttr, potentialValue);
257 document->frame()->setOwner(iframe);
258 return potentialValue;
259 }
260
261 document->frame()->setOwner(0);
Mike West 2016/10/06 13:30:21 Nit: s/0/nullptr/
262 return nullAtom;
263 }
264
232 RefPtr<SecurityOrigin> exampleOrigin; 265 RefPtr<SecurityOrigin> exampleOrigin;
233 RefPtr<SecurityOrigin> secureOrigin; 266 RefPtr<SecurityOrigin> secureOrigin;
234 }; 267 };
235 268
236 TEST_F(FrameFetchContextUpgradeTest, UpgradeInsecureResourceRequests) { 269 TEST_F(FrameFetchContextModifyRequestTest, UpgradeInsecureResourceRequests) {
237 struct TestCase { 270 struct TestCase {
238 const char* original; 271 const char* original;
239 const char* upgraded; 272 const char* upgraded;
240 } tests[] = { 273 } tests[] = {
241 {"http://example.test/image.png", "https://example.test/image.png"}, 274 {"http://example.test/image.png", "https://example.test/image.png"},
242 {"http://example.test:80/image.png", 275 {"http://example.test:80/image.png",
243 "https://example.test:443/image.png"}, 276 "https://example.test:443/image.png"},
244 {"http://example.test:1212/image.png", 277 {"http://example.test:1212/image.png",
245 "https://example.test:1212/image.png"}, 278 "https://example.test:1212/image.png"},
246 279
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 // InsecureNavigationsSet: 317 // InsecureNavigationsSet:
285 document->addInsecureNavigationUpgrade( 318 document->addInsecureNavigationUpgrade(
286 exampleOrigin->host().impl()->hash()); 319 exampleOrigin->host().impl()->hash());
287 expectUpgrade(test.original, WebURLRequest::RequestContextScript, 320 expectUpgrade(test.original, WebURLRequest::RequestContextScript,
288 WebURLRequest::FrameTypeTopLevel, test.upgraded); 321 WebURLRequest::FrameTypeTopLevel, test.upgraded);
289 expectUpgrade(test.original, WebURLRequest::RequestContextScript, 322 expectUpgrade(test.original, WebURLRequest::RequestContextScript,
290 WebURLRequest::FrameTypeAuxiliary, test.upgraded); 323 WebURLRequest::FrameTypeAuxiliary, test.upgraded);
291 } 324 }
292 } 325 }
293 326
294 TEST_F(FrameFetchContextUpgradeTest, DoNotUpgradeInsecureResourceRequests) { 327 TEST_F(FrameFetchContextModifyRequestTest,
328 DoNotUpgradeInsecureResourceRequests) {
295 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get()); 329 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get());
296 document->setSecurityOrigin(secureOrigin); 330 document->setSecurityOrigin(secureOrigin);
297 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone); 331 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone);
298 332
299 expectUpgrade("http://example.test/image.png", 333 expectUpgrade("http://example.test/image.png",
300 "http://example.test/image.png"); 334 "http://example.test/image.png");
301 expectUpgrade("http://example.test:80/image.png", 335 expectUpgrade("http://example.test:80/image.png",
302 "http://example.test:80/image.png"); 336 "http://example.test:80/image.png");
303 expectUpgrade("http://example.test:1212/image.png", 337 expectUpgrade("http://example.test:1212/image.png",
304 "http://example.test:1212/image.png"); 338 "http://example.test:1212/image.png");
305 339
306 expectUpgrade("https://example.test/image.png", 340 expectUpgrade("https://example.test/image.png",
307 "https://example.test/image.png"); 341 "https://example.test/image.png");
308 expectUpgrade("https://example.test:80/image.png", 342 expectUpgrade("https://example.test:80/image.png",
309 "https://example.test:80/image.png"); 343 "https://example.test:80/image.png");
310 expectUpgrade("https://example.test:1212/image.png", 344 expectUpgrade("https://example.test:1212/image.png",
311 "https://example.test:1212/image.png"); 345 "https://example.test:1212/image.png");
312 346
313 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"); 347 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png");
314 expectUpgrade("ftp://example.test:21/image.png", 348 expectUpgrade("ftp://example.test:21/image.png",
315 "ftp://example.test:21/image.png"); 349 "ftp://example.test:21/image.png");
316 expectUpgrade("ftp://example.test:1212/image.png", 350 expectUpgrade("ftp://example.test:1212/image.png",
317 "ftp://example.test:1212/image.png"); 351 "ftp://example.test:1212/image.png");
318 } 352 }
319 353
320 TEST_F(FrameFetchContextUpgradeTest, SendHTTPSHeader) { 354 TEST_F(FrameFetchContextModifyRequestTest, SendUpgradeInsecureRequestHeader) {
321 struct TestCase { 355 struct TestCase {
322 const char* toRequest; 356 const char* toRequest;
323 WebURLRequest::FrameType frameType; 357 WebURLRequest::FrameType frameType;
324 bool shouldPrefer; 358 bool shouldPrefer;
325 } tests[] = { 359 } tests[] = {
326 {"http://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, 360 {"http://example.test/page.html", WebURLRequest::FrameTypeAuxiliary,
327 true}, 361 true},
328 {"http://example.test/page.html", WebURLRequest::FrameTypeNested, true}, 362 {"http://example.test/page.html", WebURLRequest::FrameTypeNested, true},
329 {"http://example.test/page.html", WebURLRequest::FrameTypeNone, false}, 363 {"http://example.test/page.html", WebURLRequest::FrameTypeNone, false},
330 {"http://example.test/page.html", WebURLRequest::FrameTypeTopLevel, true}, 364 {"http://example.test/page.html", WebURLRequest::FrameTypeTopLevel, true},
331 {"https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, 365 {"https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary,
332 true}, 366 true},
333 {"https://example.test/page.html", WebURLRequest::FrameTypeNested, true}, 367 {"https://example.test/page.html", WebURLRequest::FrameTypeNested, true},
334 {"https://example.test/page.html", WebURLRequest::FrameTypeNone, false}, 368 {"https://example.test/page.html", WebURLRequest::FrameTypeNone, false},
335 {"https://example.test/page.html", WebURLRequest::FrameTypeTopLevel, 369 {"https://example.test/page.html", WebURLRequest::FrameTypeTopLevel,
336 true}}; 370 true}};
337 371
338 // This should work correctly both when the FrameFetchContext has a Document, 372 // 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 373 // 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. 374 // the tests both before and after providing a document to the context.
341 for (const auto& test : tests) { 375 for (const auto& test : tests) {
342 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone); 376 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone);
343 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 377 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
378 test.shouldPrefer);
344 379
345 document->setInsecureRequestPolicy(kUpgradeInsecureRequests); 380 document->setInsecureRequestPolicy(kUpgradeInsecureRequests);
346 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 381 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
382 test.shouldPrefer);
347 } 383 }
348 384
349 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get()); 385 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get());
350 386
351 for (const auto& test : tests) { 387 for (const auto& test : tests) {
352 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone); 388 document->setInsecureRequestPolicy(kLeaveInsecureRequestsAlone);
353 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 389 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
390 test.shouldPrefer);
354 391
355 document->setInsecureRequestPolicy(kUpgradeInsecureRequests); 392 document->setInsecureRequestPolicy(kUpgradeInsecureRequests);
356 expectHTTPSHeader(test.toRequest, test.frameType, test.shouldPrefer); 393 expectUpgradeInsecureRequestHeader(test.toRequest, test.frameType,
394 test.shouldPrefer);
357 } 395 }
358 } 396 }
359 397
398 TEST_F(FrameFetchContextModifyRequestTest, SendExpectedEmbeddingCSPHeader) {
399 struct TestCase {
400 const char* toRequest;
401 WebURLRequest::FrameType frameType;
402 } tests[] = {
403 {"https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary},
404 {"https://example.test/page.html", WebURLRequest::FrameTypeNested},
405 {"https://example.test/page.html", WebURLRequest::FrameTypeNone},
406 {"https://example.test/page.html", WebURLRequest::FrameTypeTopLevel}};
407
408 HTMLIFrameElement* iframe = HTMLIFrameElement::create(*document);
409 const AtomicString& requiredCSP = AtomicString("default-src 'none'");
410 const AtomicString& anotherRequiredCSP = AtomicString("default-src 'self'");
411
412 // This should work correctly both when the FrameFetchContext has a Document,
413 // and when it doesn't (e.g. during main frame navigations), so run through
414 // the tests both before and after providing a document to the context.
Mike West 2016/10/06 13:30:21 I don't think this comment is accurate for this he
415 for (const auto& test : tests) {
416 AtomicString expectedRequiredCSP =
417 setFrameOwnerBasedOnFrameType(test.frameType, iframe, requiredCSP);
418 expectSetEmbeddingCSPRequestHeader(test.toRequest, test.frameType,
Mike West 2016/10/06 13:30:21 Using the return value to set the expectation is s
419 expectedRequiredCSP);
420
421 expectedRequiredCSP = setFrameOwnerBasedOnFrameType(test.frameType, iframe,
422 anotherRequiredCSP);
423 expectSetEmbeddingCSPRequestHeader(test.toRequest, test.frameType,
424 expectedRequiredCSP);
425 }
426
427 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get());
428
429 for (const auto& test : tests) {
430 AtomicString expectedRequiredCSP =
431 setFrameOwnerBasedOnFrameType(test.frameType, iframe, requiredCSP);
432 expectSetEmbeddingCSPRequestHeader(test.toRequest, test.frameType,
433 expectedRequiredCSP);
434
435 expectedRequiredCSP = setFrameOwnerBasedOnFrameType(test.frameType, iframe,
436 anotherRequiredCSP);
437 expectSetEmbeddingCSPRequestHeader(test.toRequest, test.frameType,
438 expectedRequiredCSP);
439 }
440 }
441
360 class FrameFetchContextHintsTest : public FrameFetchContextTest { 442 class FrameFetchContextHintsTest : public FrameFetchContextTest {
361 public: 443 public:
362 FrameFetchContextHintsTest() {} 444 FrameFetchContextHintsTest() {}
363 445
364 protected: 446 protected:
365 void expectHeader(const char* input, 447 void expectHeader(const char* input,
366 const char* headerName, 448 const char* headerName,
367 bool isPresent, 449 bool isPresent,
368 const char* headerValue, 450 const char* headerValue,
369 float width = 0) { 451 float width = 0) {
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource); 820 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource);
739 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest()); 821 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest());
740 822
741 ResourceRequest subRequest(test.url); 823 ResourceRequest subRequest(test.url);
742 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource); 824 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource);
743 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest()); 825 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest());
744 } 826 }
745 } 827 }
746 828
747 } // namespace blink 829 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698