| OLD | NEW |
| 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 12 matching lines...) Expand all Loading... |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/loader/FrameFetchContext.h" | 31 #include "core/loader/FrameFetchContext.h" |
| 32 | 32 |
| 33 #include <memory> |
| 33 #include "core/dom/Document.h" | 34 #include "core/dom/Document.h" |
| 34 #include "core/frame/FrameHost.h" | 35 #include "core/frame/FrameHost.h" |
| 35 #include "core/frame/FrameOwner.h" | 36 #include "core/frame/FrameOwner.h" |
| 36 #include "core/frame/FrameTypes.h" | 37 #include "core/frame/FrameTypes.h" |
| 37 #include "core/frame/FrameView.h" | 38 #include "core/frame/FrameView.h" |
| 38 #include "core/frame/Settings.h" | 39 #include "core/frame/Settings.h" |
| 39 #include "core/html/HTMLIFrameElement.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/loader/fetch/FetchInitiatorInfo.h" | 45 #include "platform/loader/fetch/FetchInitiatorInfo.h" |
| 45 #include "platform/loader/fetch/MockResource.h" | 46 #include "platform/loader/fetch/MockResource.h" |
| 46 #include "platform/loader/fetch/UniqueIdentifier.h" | 47 #include "platform/loader/fetch/UniqueIdentifier.h" |
| 47 #include "platform/network/ResourceRequest.h" | 48 #include "platform/network/ResourceRequest.h" |
| 48 #include "platform/weborigin/KURL.h" | 49 #include "platform/weborigin/KURL.h" |
| 49 #include "public/platform/WebAddressSpace.h" | 50 #include "public/platform/WebAddressSpace.h" |
| 50 #include "public/platform/WebCachePolicy.h" | 51 #include "public/platform/WebCachePolicy.h" |
| 52 #include "public/platform/WebDocumentSubresourceFilter.h" |
| 51 #include "public/platform/WebInsecureRequestPolicy.h" | 53 #include "public/platform/WebInsecureRequestPolicy.h" |
| 52 #include "testing/gmock/include/gmock/gmock.h" | 54 #include "testing/gmock/include/gmock/gmock.h" |
| 53 #include "testing/gtest/include/gtest/gtest.h" | 55 #include "testing/gtest/include/gtest/gtest.h" |
| 54 #include <memory> | |
| 55 | 56 |
| 56 namespace blink { | 57 namespace blink { |
| 57 | 58 |
| 58 class StubFrameLoaderClientWithParent final : public EmptyFrameLoaderClient { | 59 class StubFrameLoaderClientWithParent final : public EmptyFrameLoaderClient { |
| 59 public: | 60 public: |
| 60 static StubFrameLoaderClientWithParent* create(Frame* parent) { | 61 static StubFrameLoaderClientWithParent* create(Frame* parent) { |
| 61 return new StubFrameLoaderClientWithParent(parent); | 62 return new StubFrameLoaderClientWithParent(parent); |
| 62 } | 63 } |
| 63 | 64 |
| 64 DEFINE_INLINE_VIRTUAL_TRACE() { | 65 DEFINE_INLINE_VIRTUAL_TRACE() { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 class MockFrameLoaderClient : public EmptyFrameLoaderClient { | 78 class MockFrameLoaderClient : public EmptyFrameLoaderClient { |
| 78 public: | 79 public: |
| 79 MockFrameLoaderClient() : EmptyFrameLoaderClient() {} | 80 MockFrameLoaderClient() : EmptyFrameLoaderClient() {} |
| 80 MOCK_METHOD1(didDisplayContentWithCertificateErrors, void(const KURL&)); | 81 MOCK_METHOD1(didDisplayContentWithCertificateErrors, void(const KURL&)); |
| 81 MOCK_METHOD2(dispatchDidLoadResourceFromMemoryCache, | 82 MOCK_METHOD2(dispatchDidLoadResourceFromMemoryCache, |
| 82 void(const ResourceRequest&, const ResourceResponse&)); | 83 void(const ResourceRequest&, const ResourceResponse&)); |
| 83 }; | 84 }; |
| 84 | 85 |
| 86 class FixedPolicySubresourceFilter : public WebDocumentSubresourceFilter { |
| 87 public: |
| 88 FixedPolicySubresourceFilter(LoadPolicy policy, int* filteredLoadCounter) |
| 89 : m_policy(policy), m_filteredLoadCounter(filteredLoadCounter) {} |
| 90 |
| 91 LoadPolicy getLoadPolicy(const WebURL& resourceUrl, |
| 92 WebURLRequest::RequestContext) override { |
| 93 return m_policy; |
| 94 } |
| 95 |
| 96 void reportDisallowedLoad() override { ++*m_filteredLoadCounter; } |
| 97 |
| 98 private: |
| 99 const LoadPolicy m_policy; |
| 100 int* m_filteredLoadCounter; |
| 101 }; |
| 102 |
| 85 class FrameFetchContextTest : public ::testing::Test { | 103 class FrameFetchContextTest : public ::testing::Test { |
| 86 protected: | 104 protected: |
| 87 void SetUp() override { | 105 void SetUp() override { |
| 88 dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); | 106 dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); |
| 89 dummyPageHolder->page().setDeviceScaleFactor(1.0); | 107 dummyPageHolder->page().setDeviceScaleFactor(1.0); |
| 90 document = &dummyPageHolder->document(); | 108 document = &dummyPageHolder->document(); |
| 91 fetchContext = | 109 fetchContext = |
| 92 static_cast<FrameFetchContext*>(&document->fetcher()->context()); | 110 static_cast<FrameFetchContext*>(&document->fetcher()->context()); |
| 93 owner = DummyFrameOwner::create(); | 111 owner = DummyFrameOwner::create(); |
| 94 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get()); | 112 FrameFetchContext::provideDocumentToContext(*fetchContext, document.get()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 119 // usage. | 137 // usage. |
| 120 Persistent<Document> document; | 138 Persistent<Document> document; |
| 121 Persistent<FrameFetchContext> fetchContext; | 139 Persistent<FrameFetchContext> fetchContext; |
| 122 | 140 |
| 123 Persistent<StubFrameLoaderClientWithParent> childClient; | 141 Persistent<StubFrameLoaderClientWithParent> childClient; |
| 124 Persistent<LocalFrame> childFrame; | 142 Persistent<LocalFrame> childFrame; |
| 125 Persistent<Document> childDocument; | 143 Persistent<Document> childDocument; |
| 126 Persistent<DummyFrameOwner> owner; | 144 Persistent<DummyFrameOwner> owner; |
| 127 }; | 145 }; |
| 128 | 146 |
| 147 class FrameFetchContextSubresourceFilterTest : public FrameFetchContextTest { |
| 148 protected: |
| 149 void SetUp() override { |
| 150 FrameFetchContextTest::SetUp(); |
| 151 m_filteredLoadCallbackCounter = 0; |
| 152 } |
| 153 |
| 154 void TearDown() override { |
| 155 document->loader()->setSubresourceFilter(nullptr); |
| 156 FrameFetchContextTest::TearDown(); |
| 157 } |
| 158 |
| 159 int getFilteredLoadCallCount() const { return m_filteredLoadCallbackCounter; } |
| 160 |
| 161 void setFilterPolicy(WebDocumentSubresourceFilter::LoadPolicy policy) { |
| 162 document->loader()->setSubresourceFilter( |
| 163 WTF::makeUnique<FixedPolicySubresourceFilter>( |
| 164 policy, &m_filteredLoadCallbackCounter)); |
| 165 } |
| 166 |
| 167 ResourceRequestBlockedReason canRequest() { |
| 168 return canRequestInternal( |
| 169 FetchContext::SecurityViolationReportingPolicy::Report); |
| 170 } |
| 171 |
| 172 ResourceRequestBlockedReason canRequestPreload() { |
| 173 return canRequestInternal( |
| 174 FetchContext::SecurityViolationReportingPolicy::SuppressReporting); |
| 175 } |
| 176 |
| 177 private: |
| 178 ResourceRequestBlockedReason canRequestInternal( |
| 179 FetchContext::SecurityViolationReportingPolicy reportingPolicy) { |
| 180 KURL inputURL(ParsedURLString, "http://example.com/"); |
| 181 ResourceRequest resourceRequest(inputURL); |
| 182 return fetchContext->canRequest( |
| 183 Resource::Image, resourceRequest, inputURL, ResourceLoaderOptions(), |
| 184 reportingPolicy, FetchRequest::UseDefaultOriginRestrictionForType); |
| 185 } |
| 186 |
| 187 int m_filteredLoadCallbackCounter; |
| 188 }; |
| 189 |
| 129 // This test class sets up a mock frame loader client. | 190 // This test class sets up a mock frame loader client. |
| 130 class FrameFetchContextMockedFrameLoaderClientTest | 191 class FrameFetchContextMockedFrameLoaderClientTest |
| 131 : public FrameFetchContextTest { | 192 : public FrameFetchContextTest { |
| 132 protected: | 193 protected: |
| 133 void SetUp() override { | 194 void SetUp() override { |
| 134 url = KURL(KURL(), "https://example.test/foo"); | 195 url = KURL(KURL(), "https://example.test/foo"); |
| 135 mainResourceUrl = KURL(KURL(), "https://www.example.test"); | 196 mainResourceUrl = KURL(KURL(), "https://www.example.test"); |
| 136 client = new testing::NiceMock<MockFrameLoaderClient>(); | 197 client = new testing::NiceMock<MockFrameLoaderClient>(); |
| 137 dummyPageHolder = | 198 dummyPageHolder = |
| 138 DummyPageHolder::create(IntSize(500, 500), nullptr, client); | 199 DummyPageHolder::create(IntSize(500, 500), nullptr, client); |
| (...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 ResourceRequest mainRequest(test.url); | 863 ResourceRequest mainRequest(test.url); |
| 803 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource); | 864 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource); |
| 804 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest()); | 865 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest()); |
| 805 | 866 |
| 806 ResourceRequest subRequest(test.url); | 867 ResourceRequest subRequest(test.url); |
| 807 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource); | 868 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource); |
| 808 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest()); | 869 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest()); |
| 809 } | 870 } |
| 810 } | 871 } |
| 811 | 872 |
| 873 TEST_F(FrameFetchContextSubresourceFilterTest, Filter) { |
| 874 setFilterPolicy(WebDocumentSubresourceFilter::Disallow); |
| 875 |
| 876 EXPECT_EQ(ResourceRequestBlockedReason::SubresourceFilter, canRequest()); |
| 877 EXPECT_EQ(1, getFilteredLoadCallCount()); |
| 878 |
| 879 EXPECT_EQ(ResourceRequestBlockedReason::SubresourceFilter, canRequest()); |
| 880 EXPECT_EQ(2, getFilteredLoadCallCount()); |
| 881 |
| 882 EXPECT_EQ(ResourceRequestBlockedReason::SubresourceFilter, |
| 883 canRequestPreload()); |
| 884 EXPECT_EQ(2, getFilteredLoadCallCount()); |
| 885 |
| 886 EXPECT_EQ(ResourceRequestBlockedReason::SubresourceFilter, canRequest()); |
| 887 EXPECT_EQ(3, getFilteredLoadCallCount()); |
| 888 } |
| 889 |
| 890 TEST_F(FrameFetchContextSubresourceFilterTest, Allow) { |
| 891 setFilterPolicy(WebDocumentSubresourceFilter::Allow); |
| 892 |
| 893 EXPECT_EQ(ResourceRequestBlockedReason::None, canRequest()); |
| 894 EXPECT_EQ(0, getFilteredLoadCallCount()); |
| 895 |
| 896 EXPECT_EQ(ResourceRequestBlockedReason::None, canRequestPreload()); |
| 897 EXPECT_EQ(0, getFilteredLoadCallCount()); |
| 898 } |
| 899 |
| 900 TEST_F(FrameFetchContextSubresourceFilterTest, WouldDisallow) { |
| 901 setFilterPolicy(WebDocumentSubresourceFilter::WouldDisallow); |
| 902 |
| 903 EXPECT_EQ(ResourceRequestBlockedReason::None, canRequest()); |
| 904 EXPECT_EQ(0, getFilteredLoadCallCount()); |
| 905 |
| 906 EXPECT_EQ(ResourceRequestBlockedReason::None, canRequestPreload()); |
| 907 EXPECT_EQ(0, getFilteredLoadCallCount()); |
| 908 } |
| 909 |
| 812 } // namespace blink | 910 } // namespace blink |
| OLD | NEW |