2 * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "PlatformCookieJar.h"
29 #if USE(CFURLCONNECTION)
32 #include "CookiesStrategy.h"
33 #include "NetworkStorageSession.h"
34 #include "NotImplemented.h"
36 #include <CFNetwork/CFHTTPCookiesPriv.h>
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <WebKitSystemInterface/WebKitSystemInterface.h>
39 #include <pal/spi/cf/CFNetworkSPI.h>
41 #include <wtf/SoftLinking.h>
42 #include <wtf/cf/TypeCastsCF.h>
43 #include <wtf/text/WTFString.h>
46 CFHTTPCookieStorageAcceptPolicyExclusivelyFromMainDocumentDomain = 3
51 #define DECLARE_CF_TYPE_TRAIT(ClassName) \
53 struct CFTypeTrait<ClassName##Ref> { \
54 static inline CFTypeID typeID() { return ClassName##GetTypeID(); } \
58 #pragma clang diagnostic push
59 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
61 DECLARE_CF_TYPE_TRAIT(CFHTTPCookie);
63 #pragma clang diagnostic pop
66 #undef DECLARE_CF_TYPE_TRAIT
71 static const CFStringRef s_setCookieKeyCF = CFSTR("Set-Cookie");
72 static const CFStringRef s_cookieCF = CFSTR("Cookie");
73 static const CFStringRef s_createdCF = CFSTR("Created");
75 static inline RetainPtr<CFStringRef> cookieDomain(CFHTTPCookieRef cookie)
77 return adoptCF(CFHTTPCookieCopyDomain(cookie));
80 static double canonicalCookieTime(double time)
85 return (time + kCFAbsoluteTimeIntervalSince1970) * 1000;
88 static double cookieCreatedTime(CFHTTPCookieRef cookie)
90 RetainPtr<CFDictionaryRef> props = adoptCF(CFHTTPCookieCopyProperties(cookie));
91 auto value = CFDictionaryGetValue(props.get(), s_createdCF);
93 auto asNumber = dynamic_cf_cast<CFNumberRef>(value);
96 if (CFNumberGetValue(asNumber, kCFNumberFloat64Type, &asDouble))
97 return canonicalCookieTime(asDouble);
101 auto asString = dynamic_cf_cast<CFStringRef>(value);
103 return canonicalCookieTime(CFStringGetDoubleValue(asString));
108 static inline CFAbsoluteTime cookieExpirationTime(CFHTTPCookieRef cookie)
110 return canonicalCookieTime(CFHTTPCookieGetExpirationTime(cookie));
113 static inline RetainPtr<CFStringRef> cookieName(CFHTTPCookieRef cookie)
115 return adoptCF(CFHTTPCookieCopyName(cookie));
118 static inline RetainPtr<CFStringRef> cookiePath(CFHTTPCookieRef cookie)
120 return adoptCF(CFHTTPCookieCopyPath(cookie));
123 static inline RetainPtr<CFStringRef> cookieValue(CFHTTPCookieRef cookie)
125 return adoptCF(CFHTTPCookieCopyValue(cookie));
128 static RetainPtr<CFArrayRef> filterCookies(CFArrayRef unfilteredCookies)
130 ASSERT(unfilteredCookies);
131 CFIndex count = CFArrayGetCount(unfilteredCookies);
132 RetainPtr<CFMutableArrayRef> filteredCookies = adoptCF(CFArrayCreateMutable(0, count, &kCFTypeArrayCallBacks));
133 for (CFIndex i = 0; i < count; ++i) {
134 CFHTTPCookieRef cookie = (CFHTTPCookieRef)CFArrayGetValueAtIndex(unfilteredCookies, i);
136 // <rdar://problem/5632883> CFHTTPCookieStorage would store an empty cookie,
137 // which would be sent as "Cookie: =". We have a workaround in setCookies() to prevent
138 // that, but we also need to avoid sending cookies that were previously stored, and
139 // there's no harm to doing this check because such a cookie is never valid.
140 if (!CFStringGetLength(cookieName(cookie).get()))
143 if (CFHTTPCookieIsHTTPOnly(cookie))
146 CFArrayAppendValue(filteredCookies.get(), cookie);
148 return filteredCookies;
151 static RetainPtr<CFArrayRef> copyCookiesForURLWithFirstPartyURL(const NetworkStorageSession& session, const URL& firstParty, const URL& url, IncludeSecureCookies includeSecureCookies)
153 bool secure = includeSecureCookies == IncludeSecureCookies::Yes;
155 ASSERT(!secure || (secure && url.protocolIs("https")));
157 UNUSED_PARAM(firstParty);
158 return adoptCF(CFHTTPCookieStorageCopyCookiesForURL(session.cookieStorage().get(), url.createCFURL().get(), secure));
161 static CFArrayRef createCookies(CFDictionaryRef headerFields, CFURLRef url)
163 CFArrayRef parsedCookies = CFHTTPCookieCreateWithResponseHeaderFields(kCFAllocatorDefault, headerFields, url);
165 parsedCookies = CFArrayCreate(kCFAllocatorDefault, 0, 0, &kCFTypeArrayCallBacks);
167 return parsedCookies;
170 void setCookiesFromDOM(const NetworkStorageSession& session, const URL& firstParty, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, const String& value)
172 UNUSED_PARAM(frameID);
173 UNUSED_PARAM(pageID);
174 // <rdar://problem/5632883> CFHTTPCookieStorage stores an empty cookie, which would be sent as "Cookie: =".
178 RetainPtr<CFURLRef> urlCF = url.createCFURL();
179 RetainPtr<CFURLRef> firstPartyForCookiesCF = firstParty.createCFURL();
181 // <http://bugs.webkit.org/show_bug.cgi?id=6531>, <rdar://4409034>
182 // cookiesWithResponseHeaderFields doesn't parse cookies without a value
183 String cookieString = value.contains('=') ? value : value + "=";
185 RetainPtr<CFStringRef> cookieStringCF = cookieString.createCFString();
186 auto cookieStringCFPtr = cookieStringCF.get();
187 RetainPtr<CFDictionaryRef> headerFieldsCF = adoptCF(CFDictionaryCreate(kCFAllocatorDefault,
188 (const void**)&s_setCookieKeyCF, (const void**)&cookieStringCFPtr, 1,
189 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
191 RetainPtr<CFArrayRef> unfilteredCookies = adoptCF(createCookies(headerFieldsCF.get(), urlCF.get()));
192 CFHTTPCookieStorageSetCookies(session.cookieStorage().get(), filterCookies(unfilteredCookies.get()).get(), urlCF.get(), firstPartyForCookiesCF.get());
195 static bool containsSecureCookies(CFArrayRef cookies)
197 CFIndex cookieCount = CFArrayGetCount(cookies);
198 while (cookieCount--) {
199 if (CFHTTPCookieIsSecure(checked_cf_cast<CFHTTPCookieRef>(CFArrayGetValueAtIndex(cookies, cookieCount))))
206 std::pair<String, bool> cookiesForDOM(const NetworkStorageSession& session, const URL& firstParty, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, IncludeSecureCookies includeSecureCookies)
208 UNUSED_PARAM(frameID);
209 UNUSED_PARAM(pageID);
210 RetainPtr<CFArrayRef> cookiesCF = copyCookiesForURLWithFirstPartyURL(session, firstParty, url, includeSecureCookies);
212 auto filteredCookies = filterCookies(cookiesCF.get());
214 bool didAccessSecureCookies = containsSecureCookies(filteredCookies.get());
216 RetainPtr<CFDictionaryRef> headerCF = adoptCF(CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, filteredCookies.get()));
217 String cookieString = checked_cf_cast<CFStringRef>(CFDictionaryGetValue(headerCF.get(), s_cookieCF));
218 return { cookieString, didAccessSecureCookies };
221 std::pair<String, bool> cookieRequestHeaderFieldValue(const NetworkStorageSession& session, const URL& firstParty, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, IncludeSecureCookies includeSecureCookies)
223 UNUSED_PARAM(frameID);
224 UNUSED_PARAM(pageID);
225 RetainPtr<CFArrayRef> cookiesCF = copyCookiesForURLWithFirstPartyURL(session, firstParty, url, includeSecureCookies);
227 bool didAccessSecureCookies = containsSecureCookies(cookiesCF.get());
229 RetainPtr<CFDictionaryRef> headerCF = adoptCF(CFHTTPCookieCopyRequestHeaderFields(kCFAllocatorDefault, cookiesCF.get()));
230 String cookieString = checked_cf_cast<CFStringRef>(CFDictionaryGetValue(headerCF.get(), s_cookieCF));
231 return { cookieString, didAccessSecureCookies };
234 bool cookiesEnabled(const NetworkStorageSession& session)
236 CFHTTPCookieStorageAcceptPolicy policy = CFHTTPCookieStorageGetCookieAcceptPolicy(session.cookieStorage().get());
237 return policy == CFHTTPCookieStorageAcceptPolicyOnlyFromMainDocumentDomain || policy == CFHTTPCookieStorageAcceptPolicyExclusivelyFromMainDocumentDomain || policy == CFHTTPCookieStorageAcceptPolicyAlways;
240 bool getRawCookies(const NetworkStorageSession& session, const URL& firstParty, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, Vector<Cookie>& rawCookies)
242 UNUSED_PARAM(frameID);
243 UNUSED_PARAM(pageID);
246 auto includeSecureCookies = url.protocolIs("https") ? IncludeSecureCookies::Yes : IncludeSecureCookies::No;
248 RetainPtr<CFArrayRef> cookiesCF = copyCookiesForURLWithFirstPartyURL(session, firstParty, url, includeSecureCookies);
250 CFIndex count = CFArrayGetCount(cookiesCF.get());
251 rawCookies.reserveCapacity(count);
253 for (CFIndex i = 0; i < count; i++) {
254 CFHTTPCookieRef cookie = checked_cf_cast<CFHTTPCookieRef>(CFArrayGetValueAtIndex(cookiesCF.get(), i));
255 String name = cookieName(cookie).get();
256 String value = cookieValue(cookie).get();
257 String domain = cookieDomain(cookie).get();
258 String path = cookiePath(cookie).get();
260 double created = cookieCreatedTime(cookie);
261 double expires = cookieExpirationTime(cookie);
263 bool httpOnly = CFHTTPCookieIsHTTPOnly(cookie);
264 bool secure = CFHTTPCookieIsSecure(cookie);
265 bool session = false; // FIXME: Need API for if a cookie is a session cookie.
269 Vector<uint16_t> ports;
271 rawCookies.uncheckedAppend(Cookie(name, value, domain, path, created, expires, httpOnly, secure, session, comment, commentURL, ports));
277 void deleteCookie(const NetworkStorageSession& session, const URL& url, const String& name)
279 RetainPtr<CFHTTPCookieStorageRef> cookieStorage = session.cookieStorage();
281 RetainPtr<CFURLRef> urlCF = url.createCFURL();
283 bool sendSecureCookies = url.protocolIs("https");
284 RetainPtr<CFArrayRef> cookiesCF = adoptCF(CFHTTPCookieStorageCopyCookiesForURL(cookieStorage.get(), urlCF.get(), sendSecureCookies));
286 CFIndex count = CFArrayGetCount(cookiesCF.get());
287 for (CFIndex i = 0; i < count; i++) {
288 CFHTTPCookieRef cookie = checked_cf_cast<CFHTTPCookieRef>(CFArrayGetValueAtIndex(cookiesCF.get(), i));
289 if (String(cookieName(cookie).get()) == name) {
290 CFHTTPCookieStorageDeleteCookie(cookieStorage.get(), cookie);
296 void getHostnamesWithCookies(const NetworkStorageSession& session, HashSet<String>& hostnames)
298 RetainPtr<CFArrayRef> cookiesCF = adoptCF(CFHTTPCookieStorageCopyCookies(session.cookieStorage().get()));
302 CFIndex count = CFArrayGetCount(cookiesCF.get());
303 for (CFIndex i = 0; i < count; ++i) {
304 CFHTTPCookieRef cookie = checked_cf_cast<CFHTTPCookieRef>(CFArrayGetValueAtIndex(cookiesCF.get(), i));
305 RetainPtr<CFStringRef> domain = cookieDomain(cookie);
306 hostnames.add(domain.get());
310 void deleteAllCookies(const NetworkStorageSession& session)
312 CFHTTPCookieStorageDeleteAllCookies(session.cookieStorage().get());
315 void deleteCookiesForHostnames(const NetworkStorageSession& session, const Vector<String>& hostnames)
319 void deleteAllCookiesModifiedSince(const NetworkStorageSession&, std::chrono::system_clock::time_point)
323 } // namespace WebCore
325 #endif // USE(CFURLCONNECTION)