1 | /*
|
---|
2 | * Copyright (C) 2012-2018 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
---|
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
---|
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
---|
23 | * THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "CookieJar.h"
|
---|
28 |
|
---|
29 | #include "CookieRequestHeaderFieldProxy.h"
|
---|
30 | #include "Document.h"
|
---|
31 | #include "DocumentLoader.h"
|
---|
32 | #include "Frame.h"
|
---|
33 | #include "FrameDestructionObserverInlines.h"
|
---|
34 | #include "FrameLoader.h"
|
---|
35 | #include "HTTPCookieAcceptPolicy.h"
|
---|
36 | #include "NetworkStorageSession.h"
|
---|
37 | #include "NetworkingContext.h"
|
---|
38 | #include "Page.h"
|
---|
39 | #include "PlatformStrategies.h"
|
---|
40 | #include "StorageSessionProvider.h"
|
---|
41 | #include <wtf/SystemTracing.h>
|
---|
42 |
|
---|
43 | namespace WebCore {
|
---|
44 |
|
---|
45 | static ShouldRelaxThirdPartyCookieBlocking shouldRelaxThirdPartyCookieBlocking(const Document& document)
|
---|
46 | {
|
---|
47 | if (auto* page = document.page())
|
---|
48 | return page->shouldRelaxThirdPartyCookieBlocking();
|
---|
49 | return ShouldRelaxThirdPartyCookieBlocking::No;
|
---|
50 | }
|
---|
51 |
|
---|
52 | Ref<CookieJar> CookieJar::create(Ref<StorageSessionProvider>&& storageSessionProvider)
|
---|
53 | {
|
---|
54 | return adoptRef(*new CookieJar(WTFMove(storageSessionProvider)));
|
---|
55 | }
|
---|
56 |
|
---|
57 | IncludeSecureCookies CookieJar::shouldIncludeSecureCookies(const Document& document, const URL& url)
|
---|
58 | {
|
---|
59 | return (url.protocolIs("https"_s) && !document.foundMixedContent().contains(SecurityContext::MixedContentType::Active)) ? IncludeSecureCookies::Yes : IncludeSecureCookies::No;
|
---|
60 | }
|
---|
61 |
|
---|
62 | SameSiteInfo CookieJar::sameSiteInfo(const Document& document, IsForDOMCookieAccess isAccessForDOM)
|
---|
63 | {
|
---|
64 | if (auto* loader = document.loader())
|
---|
65 | return SameSiteInfo::create(loader->request(), isAccessForDOM);
|
---|
66 | return { };
|
---|
67 | }
|
---|
68 |
|
---|
69 | CookieJar::CookieJar(Ref<StorageSessionProvider>&& storageSessionProvider)
|
---|
70 | : m_storageSessionProvider(WTFMove(storageSessionProvider))
|
---|
71 | {
|
---|
72 | }
|
---|
73 |
|
---|
74 | CookieJar::~CookieJar() = default;
|
---|
75 |
|
---|
76 | String CookieJar::cookies(Document& document, const URL& url) const
|
---|
77 | {
|
---|
78 | TraceScope scope(FetchCookiesStart, FetchCookiesEnd);
|
---|
79 |
|
---|
80 | auto includeSecureCookies = shouldIncludeSecureCookies(document, url);
|
---|
81 |
|
---|
82 | std::optional<FrameIdentifier> frameID;
|
---|
83 | std::optional<PageIdentifier> pageID;
|
---|
84 | if (auto* frame = document.frame()) {
|
---|
85 | frameID = frame->loader().frameID();
|
---|
86 | pageID = frame->loader().pageID();
|
---|
87 | }
|
---|
88 |
|
---|
89 | std::pair<String, bool> result;
|
---|
90 | if (auto* session = m_storageSessionProvider->storageSession())
|
---|
91 | result = session->cookiesForDOM(document.firstPartyForCookies(), sameSiteInfo(document, IsForDOMCookieAccess::Yes), url, frameID, pageID, includeSecureCookies, ShouldAskITP::Yes, shouldRelaxThirdPartyCookieBlocking(document));
|
---|
92 | else
|
---|
93 | ASSERT_NOT_REACHED();
|
---|
94 |
|
---|
95 | if (result.second)
|
---|
96 | document.setSecureCookiesAccessed();
|
---|
97 |
|
---|
98 | return result.first;
|
---|
99 | }
|
---|
100 |
|
---|
101 | CookieRequestHeaderFieldProxy CookieJar::cookieRequestHeaderFieldProxy(const Document& document, const URL& url)
|
---|
102 | {
|
---|
103 | TraceScope scope(FetchCookiesStart, FetchCookiesEnd);
|
---|
104 |
|
---|
105 | std::optional<FrameIdentifier> frameID;
|
---|
106 | std::optional<PageIdentifier> pageID;
|
---|
107 | if (auto* frame = document.frame()) {
|
---|
108 | frameID = frame->loader().frameID();
|
---|
109 | pageID = frame->loader().pageID();
|
---|
110 | }
|
---|
111 |
|
---|
112 | return { document.firstPartyForCookies(), sameSiteInfo(document), url, frameID, pageID, shouldIncludeSecureCookies(document, url) };
|
---|
113 | }
|
---|
114 |
|
---|
115 | void CookieJar::setCookies(Document& document, const URL& url, const String& cookieString)
|
---|
116 | {
|
---|
117 | std::optional<FrameIdentifier> frameID;
|
---|
118 | std::optional<PageIdentifier> pageID;
|
---|
119 | if (auto* frame = document.frame()) {
|
---|
120 | frameID = frame->loader().frameID();
|
---|
121 | pageID = frame->loader().pageID();
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (auto* session = m_storageSessionProvider->storageSession())
|
---|
125 | session->setCookiesFromDOM(document.firstPartyForCookies(), sameSiteInfo(document, IsForDOMCookieAccess::Yes), url, frameID, pageID, ShouldAskITP::Yes, cookieString, shouldRelaxThirdPartyCookieBlocking(document));
|
---|
126 | else
|
---|
127 | ASSERT_NOT_REACHED();
|
---|
128 | }
|
---|
129 |
|
---|
130 | bool CookieJar::cookiesEnabled(const Document&) const
|
---|
131 | {
|
---|
132 | if (auto* session = m_storageSessionProvider->storageSession())
|
---|
133 | return session->cookieAcceptPolicy() != HTTPCookieAcceptPolicy::Never;
|
---|
134 |
|
---|
135 | ASSERT_NOT_REACHED();
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 |
|
---|
139 | std::pair<String, SecureCookiesAccessed> CookieJar::cookieRequestHeaderFieldValue(const URL& firstParty, const SameSiteInfo& sameSiteInfo, const URL& url, std::optional<FrameIdentifier> frameID, std::optional<PageIdentifier> pageID, IncludeSecureCookies includeSecureCookies) const
|
---|
140 | {
|
---|
141 | if (auto* session = m_storageSessionProvider->storageSession()) {
|
---|
142 | std::pair<String, bool> result = session->cookieRequestHeaderFieldValue(firstParty, sameSiteInfo, url, frameID, pageID, includeSecureCookies, ShouldAskITP::Yes, ShouldRelaxThirdPartyCookieBlocking::No);
|
---|
143 | return { result.first, result.second ? SecureCookiesAccessed::Yes : SecureCookiesAccessed::No };
|
---|
144 | }
|
---|
145 |
|
---|
146 | ASSERT_NOT_REACHED();
|
---|
147 | return { };
|
---|
148 | }
|
---|
149 |
|
---|
150 | String CookieJar::cookieRequestHeaderFieldValue(Document& document, const URL& url) const
|
---|
151 | {
|
---|
152 | std::optional<FrameIdentifier> frameID;
|
---|
153 | std::optional<PageIdentifier> pageID;
|
---|
154 | if (auto* frame = document.frame()) {
|
---|
155 | frameID = frame->loader().frameID();
|
---|
156 | pageID = frame->loader().pageID();
|
---|
157 | }
|
---|
158 |
|
---|
159 | auto result = cookieRequestHeaderFieldValue(document.firstPartyForCookies(), sameSiteInfo(document), url, frameID, pageID, shouldIncludeSecureCookies(document, url));
|
---|
160 | if (result.second == SecureCookiesAccessed::Yes)
|
---|
161 | document.setSecureCookiesAccessed();
|
---|
162 | return result.first;
|
---|
163 | }
|
---|
164 |
|
---|
165 | bool CookieJar::getRawCookies(const Document& document, const URL& url, Vector<Cookie>& cookies) const
|
---|
166 | {
|
---|
167 | std::optional<FrameIdentifier> frameID;
|
---|
168 | std::optional<PageIdentifier> pageID;
|
---|
169 | if (auto* frame = document.frame()) {
|
---|
170 | frameID = frame->loader().frameID();
|
---|
171 | pageID = frame->loader().pageID();
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (auto* session = m_storageSessionProvider->storageSession())
|
---|
175 | return session->getRawCookies(document.firstPartyForCookies(), sameSiteInfo(document), url, frameID, pageID, ShouldAskITP::Yes, shouldRelaxThirdPartyCookieBlocking(document), cookies);
|
---|
176 |
|
---|
177 | ASSERT_NOT_REACHED();
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | void CookieJar::setRawCookie(const Document&, const Cookie& cookie)
|
---|
182 | {
|
---|
183 | if (auto* session = m_storageSessionProvider->storageSession())
|
---|
184 | session->setCookie(cookie);
|
---|
185 | else
|
---|
186 | ASSERT_NOT_REACHED();
|
---|
187 | }
|
---|
188 |
|
---|
189 | void CookieJar::deleteCookie(const Document&, const URL& url, const String& cookieName, CompletionHandler<void()>&& completionHandler)
|
---|
190 | {
|
---|
191 | if (auto* session = m_storageSessionProvider->storageSession())
|
---|
192 | session->deleteCookie(url, cookieName, WTFMove(completionHandler));
|
---|
193 | else {
|
---|
194 | ASSERT_NOT_REACHED();
|
---|
195 | completionHandler();
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | }
|
---|