| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/parser/HTMLPreloadScanner.h" | 5 #include "core/html/parser/HTMLPreloadScanner.h" |
| 6 | 6 |
| 7 #include "core/MediaTypeNames.h" | 7 #include "core/MediaTypeNames.h" |
| 8 #include "core/css/MediaValuesCached.h" | 8 #include "core/css/MediaValuesCached.h" |
| 9 #include "core/fetch/ClientHintsPreferences.h" | 9 #include "core/fetch/ClientHintsPreferences.h" |
| 10 #include "core/frame/Settings.h" | 10 #include "core/frame/Settings.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 struct ReferrerPolicyTestCase { | 37 struct ReferrerPolicyTestCase { |
| 38 const char* baseURL; | 38 const char* baseURL; |
| 39 const char* inputHTML; | 39 const char* inputHTML; |
| 40 const char* preloadedURL; // Or nullptr if no preload is expected. | 40 const char* preloadedURL; // Or nullptr if no preload is expected. |
| 41 const char* outputBaseURL; | 41 const char* outputBaseURL; |
| 42 Resource::Type type; | 42 Resource::Type type; |
| 43 int resourceWidth; | 43 int resourceWidth; |
| 44 ReferrerPolicy referrerPolicy; | 44 ReferrerPolicy referrerPolicy; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 struct NonceTestCase { |
| 48 const char* baseURL; |
| 49 const char* inputHTML; |
| 50 const char* nonce; |
| 51 }; |
| 52 |
| 47 class MockHTMLResourcePreloader : public ResourcePreloader { | 53 class MockHTMLResourcePreloader : public ResourcePreloader { |
| 48 public: | 54 public: |
| 49 void preloadRequestVerification(Resource::Type type, const char* url, const
char* baseURL, int width, const ClientHintsPreferences& preferences) | 55 void preloadRequestVerification(Resource::Type type, const char* url, const
char* baseURL, int width, const ClientHintsPreferences& preferences) |
| 50 { | 56 { |
| 51 if (!url) { | 57 if (!url) { |
| 52 EXPECT_FALSE(m_preloadRequest); | 58 EXPECT_FALSE(m_preloadRequest); |
| 53 return; | 59 return; |
| 54 } | 60 } |
| 55 EXPECT_NE(nullptr, m_preloadRequest.get()); | 61 EXPECT_NE(nullptr, m_preloadRequest.get()); |
| 56 if (m_preloadRequest) { | 62 if (m_preloadRequest) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 73 | 79 |
| 74 void preconnectRequestVerification(const String& host, CrossOriginAttributeV
alue crossOrigin) | 80 void preconnectRequestVerification(const String& host, CrossOriginAttributeV
alue crossOrigin) |
| 75 { | 81 { |
| 76 if (!host.isNull()) { | 82 if (!host.isNull()) { |
| 77 EXPECT_TRUE(m_preloadRequest->isPreconnect()); | 83 EXPECT_TRUE(m_preloadRequest->isPreconnect()); |
| 78 EXPECT_STREQ(m_preloadRequest->resourceURL().ascii().data(), host.as
cii().data()); | 84 EXPECT_STREQ(m_preloadRequest->resourceURL().ascii().data(), host.as
cii().data()); |
| 79 EXPECT_EQ(m_preloadRequest->crossOrigin(), crossOrigin); | 85 EXPECT_EQ(m_preloadRequest->crossOrigin(), crossOrigin); |
| 80 } | 86 } |
| 81 } | 87 } |
| 82 | 88 |
| 89 void nonceRequestVerification(const char* nonce) |
| 90 { |
| 91 ASSERT_TRUE(m_preloadRequest.get()); |
| 92 if (strlen(nonce)) |
| 93 EXPECT_EQ(nonce, m_preloadRequest->nonce()); |
| 94 else |
| 95 EXPECT_TRUE(m_preloadRequest->nonce().isEmpty()); |
| 96 } |
| 97 |
| 83 protected: | 98 protected: |
| 84 void preload(std::unique_ptr<PreloadRequest> preloadRequest, const NetworkHi
ntsInterface&) override | 99 void preload(std::unique_ptr<PreloadRequest> preloadRequest, const NetworkHi
ntsInterface&) override |
| 85 { | 100 { |
| 86 m_preloadRequest = std::move(preloadRequest); | 101 m_preloadRequest = std::move(preloadRequest); |
| 87 } | 102 } |
| 88 | 103 |
| 89 private: | 104 private: |
| 90 std::unique_ptr<PreloadRequest> m_preloadRequest; | 105 std::unique_ptr<PreloadRequest> m_preloadRequest; |
| 91 }; | 106 }; |
| 92 | 107 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 void test(ReferrerPolicyTestCase testCase) | 179 void test(ReferrerPolicyTestCase testCase) |
| 165 { | 180 { |
| 166 MockHTMLResourcePreloader preloader; | 181 MockHTMLResourcePreloader preloader; |
| 167 KURL baseURL(ParsedURLString, testCase.baseURL); | 182 KURL baseURL(ParsedURLString, testCase.baseURL); |
| 168 m_scanner->appendToEnd(String(testCase.inputHTML)); | 183 m_scanner->appendToEnd(String(testCase.inputHTML)); |
| 169 m_scanner->scanAndPreload(&preloader, baseURL, nullptr); | 184 m_scanner->scanAndPreload(&preloader, baseURL, nullptr); |
| 170 | 185 |
| 171 preloader.preloadRequestVerification(testCase.type, testCase.preloadedUR
L, testCase.outputBaseURL, testCase.resourceWidth, testCase.referrerPolicy); | 186 preloader.preloadRequestVerification(testCase.type, testCase.preloadedUR
L, testCase.outputBaseURL, testCase.resourceWidth, testCase.referrerPolicy); |
| 172 } | 187 } |
| 173 | 188 |
| 189 void test(NonceTestCase testCase) |
| 190 { |
| 191 MockHTMLResourcePreloader preloader; |
| 192 KURL baseURL(ParsedURLString, testCase.baseURL); |
| 193 m_scanner->appendToEnd(String(testCase.inputHTML)); |
| 194 m_scanner->scanAndPreload(&preloader, baseURL, nullptr); |
| 195 |
| 196 preloader.nonceRequestVerification(testCase.nonce); |
| 197 } |
| 198 |
| 174 private: | 199 private: |
| 175 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | 200 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
| 176 std::unique_ptr<HTMLPreloadScanner> m_scanner; | 201 std::unique_ptr<HTMLPreloadScanner> m_scanner; |
| 177 }; | 202 }; |
| 178 | 203 |
| 179 TEST_F(HTMLPreloadScannerTest, testImages) | 204 TEST_F(HTMLPreloadScannerTest, testImages) |
| 180 { | 205 { |
| 181 TestCase testCases[] = { | 206 TestCase testCases[] = { |
| 182 {"http://example.test", "<img src='bla.gif'>", "bla.gif", "http://exampl
e.test/", Resource::Image, 0}, | 207 {"http://example.test", "<img src='bla.gif'>", "bla.gif", "http://exampl
e.test/", Resource::Image, 0}, |
| 183 {"http://example.test", "<img srcset='bla.gif 320w, blabla.gif 640w'>",
"blabla.gif", "http://example.test/", Resource::Image, 0}, | 208 {"http://example.test", "<img srcset='bla.gif 320w, blabla.gif 640w'>",
"blabla.gif", "http://example.test/", Resource::Image, 0}, |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 { "http://example.test", "<meta name='referrer' content='no-referrer'><i
mg referrerpolicy='origin' src='bla.gif'/>", "bla.gif", "http://example.test/",
Resource::Image, 0, ReferrerPolicyOrigin }, | 392 { "http://example.test", "<meta name='referrer' content='no-referrer'><i
mg referrerpolicy='origin' src='bla.gif'/>", "bla.gif", "http://example.test/",
Resource::Image, 0, ReferrerPolicyOrigin }, |
| 368 // The scanner's state is not reset between test cases, so all subsequen
t test cases have a document referrer policy of no-referrer. | 393 // The scanner's state is not reset between test cases, so all subsequen
t test cases have a document referrer policy of no-referrer. |
| 369 { "http://example.test", "<img referrerpolicy='not-a-valid-policy' src='
bla.gif'/>", "bla.gif", "http://example.test/", Resource::Image, 0, ReferrerPoli
cyNever }, | 394 { "http://example.test", "<img referrerpolicy='not-a-valid-policy' src='
bla.gif'/>", "bla.gif", "http://example.test/", Resource::Image, 0, ReferrerPoli
cyNever }, |
| 370 { "http://example.test", "<img src='bla.gif'/>", "bla.gif", "http://exam
ple.test/", Resource::Image, 0, ReferrerPolicyNever } | 395 { "http://example.test", "<img src='bla.gif'/>", "bla.gif", "http://exam
ple.test/", Resource::Image, 0, ReferrerPolicyNever } |
| 371 }; | 396 }; |
| 372 | 397 |
| 373 for (const auto& testCase : testCases) | 398 for (const auto& testCase : testCases) |
| 374 test(testCase); | 399 test(testCase); |
| 375 } | 400 } |
| 376 | 401 |
| 402 TEST_F(HTMLPreloadScannerTest, testNonce) |
| 403 { |
| 404 NonceTestCase testCases[] = { |
| 405 { "http://example.test", "<script src='/script'></script>", "" }, |
| 406 { "http://example.test", "<script src='/script' nonce=''></script>", ""
}, |
| 407 { "http://example.test", "<script src='/script' nonce='abc'></script>",
"abc" }, |
| 408 { "http://example.test", "<link rel='import' href='/import'>", "" }, |
| 409 { "http://example.test", "<link rel='import' href='/import' nonce=''>",
"" }, |
| 410 { "http://example.test", "<link rel='import' href='/import' nonce='abc'>
", "abc" }, |
| 411 { "http://example.test", "<link rel='stylesheet' href='/style'>", "" }, |
| 412 { "http://example.test", "<link rel='stylesheet' href='/style' nonce=''>
", "" }, |
| 413 { "http://example.test", "<link rel='stylesheet' href='/style' nonce='ab
c'>", "abc" }, |
| 414 |
| 415 // <img> doesn't support nonces: |
| 416 { "http://example.test", "<img src='/image'>", "" }, |
| 417 { "http://example.test", "<img src='/image' nonce=''>", "" }, |
| 418 { "http://example.test", "<img src='/image' nonce='abc'>", "" }, |
| 419 }; |
| 420 |
| 421 for (const auto& testCase : testCases) { |
| 422 SCOPED_TRACE(testCase.inputHTML); |
| 423 test(testCase); |
| 424 } |
| 425 } |
| 426 |
| 377 // Tests that a document-level referrer policy (e.g. one set by HTTP | 427 // Tests that a document-level referrer policy (e.g. one set by HTTP |
| 378 // header) is applied for preload requests. | 428 // header) is applied for preload requests. |
| 379 TEST_F(HTMLPreloadScannerTest, testReferrerPolicyOnDocument) | 429 TEST_F(HTMLPreloadScannerTest, testReferrerPolicyOnDocument) |
| 380 { | 430 { |
| 381 runSetUp(ViewportEnabled, PreloadEnabled, ReferrerPolicyOrigin); | 431 runSetUp(ViewportEnabled, PreloadEnabled, ReferrerPolicyOrigin); |
| 382 ReferrerPolicyTestCase testCases[] = { | 432 ReferrerPolicyTestCase testCases[] = { |
| 383 { "http://example.test", "<img src='blah.gif'/>", "blah.gif", "http://ex
ample.test/", Resource::Image, 0, ReferrerPolicyOrigin }, | 433 { "http://example.test", "<img src='blah.gif'/>", "blah.gif", "http://ex
ample.test/", Resource::Image, 0, ReferrerPolicyOrigin }, |
| 384 { "http://example.test", "<style>@import url('blah.css');</style>", "bla
h.css", "http://example.test/", Resource::CSSStyleSheet, 0, ReferrerPolicyOrigin
}, | 434 { "http://example.test", "<style>@import url('blah.css');</style>", "bla
h.css", "http://example.test/", Resource::CSSStyleSheet, 0, ReferrerPolicyOrigin
}, |
| 385 // Tests that a meta-delivered referrer policy with an | 435 // Tests that a meta-delivered referrer policy with an |
| 386 // unrecognized policy value does not override the document's | 436 // unrecognized policy value does not override the document's |
| (...skipping 21 matching lines...) Expand all Loading... |
| 408 {"http://example.test", "<link rel=preload href=bla as=track>", "bla", "
http://example.test/", Resource::TextTrack, 0}, | 458 {"http://example.test", "<link rel=preload href=bla as=track>", "bla", "
http://example.test/", Resource::TextTrack, 0}, |
| 409 {"http://example.test", "<link rel=preload href=bla as=image media=\"(ma
x-width: 800px)\">", "bla", "http://example.test/", Resource::Image, 0}, | 459 {"http://example.test", "<link rel=preload href=bla as=image media=\"(ma
x-width: 800px)\">", "bla", "http://example.test/", Resource::Image, 0}, |
| 410 {"http://example.test", "<link rel=preload href=bla as=image media=\"(ma
x-width: 400px)\">", nullptr, "http://example.test/", Resource::Image, 0}, | 460 {"http://example.test", "<link rel=preload href=bla as=image media=\"(ma
x-width: 400px)\">", nullptr, "http://example.test/", Resource::Image, 0}, |
| 411 }; | 461 }; |
| 412 | 462 |
| 413 for (const auto& testCase : testCases) | 463 for (const auto& testCase : testCases) |
| 414 test(testCase); | 464 test(testCase); |
| 415 } | 465 } |
| 416 | 466 |
| 417 } // namespace blink | 467 } // namespace blink |
| OLD | NEW |