Implement Same-Site cookies
[WebKit-https.git] / Source / WebCore / PAL / pal / spi / cf / CFNetworkSPI.h
1 /*
2  * Copyright (C) 2014-2017 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 #pragma clang system_header
27
28 #pragma once
29
30 #include <CFNetwork/CFNetwork.h>
31 #include <pal/spi/cf/CFNetworkConnectionCacheSPI.h>
32
33 #if ((PLATFORM(MAC) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101302 && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110200) || (PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MIN_REQUIRED >= 40200) || (PLATFORM(TVOS) && __TV_OS_VERSION_MIN_REQUIRED >= 110200))
34 #define USE_CFNETWORK_IGNORE_HSTS 1
35 #endif
36
37 #if PLATFORM(WIN) || USE(APPLE_INTERNAL_SDK)
38
39 #include <CFNetwork/CFHTTPCookiesPriv.h>
40 #include <CFNetwork/CFProxySupportPriv.h>
41 #include <CFNetwork/CFURLCachePriv.h>
42 #include <CFNetwork/CFURLConnectionPriv.h>
43 #include <CFNetwork/CFURLCredentialStorage.h>
44 #include <CFNetwork/CFURLProtocolPriv.h>
45 #include <CFNetwork/CFURLRequestPriv.h>
46 #include <CFNetwork/CFURLResponsePriv.h>
47 #include <CFNetwork/CFURLStorageSession.h>
48
49 // FIXME: Remove the defined(__OBJC__)-guard once we fix <rdar://problem/19033610>.
50 #if defined(__OBJC__) && PLATFORM(COCOA)
51 #import <CFNetwork/CFNSURLConnection.h>
52 #endif
53
54 #else // !PLATFORM(WIN) && !USE(APPLE_INTERNAL_SDK)
55
56 typedef CF_ENUM(int64_t, _TimingDataOptions)
57 {
58     _TimingDataOptionsEnableW3CNavigationTiming = (1 << 0)
59 };
60
61 enum CFURLCacheStoragePolicy {
62     kCFURLCacheStorageAllowed = 0,
63     kCFURLCacheStorageAllowedInMemoryOnly = 1,
64     kCFURLCacheStorageNotAllowed = 2
65 };
66 typedef enum CFURLCacheStoragePolicy CFURLCacheStoragePolicy;
67
68 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
69 typedef const struct _CFURLCache* CFURLCacheRef;
70 typedef const struct _CFURLCredential* CFURLCredentialRef;
71 typedef const struct _CFURLRequest* CFURLRequestRef;
72 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
73 typedef const struct __CFData* CFDataRef;
74 typedef const struct OpaqueCFHTTPCookie* CFHTTPCookieRef;
75 typedef struct _CFURLConnection* CFURLConnectionRef;
76 typedef struct _CFURLCredentialStorage* CFURLCredentialStorageRef;
77 typedef struct _CFURLProtectionSpace* CFURLProtectionSpaceRef;
78 typedef struct _CFURLRequest* CFMutableURLRequestRef;
79 typedef struct _CFURLResponse* CFURLResponseRef;
80 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
81 typedef CFIndex CFURLRequestPriority;
82 typedef int CFHTTPCookieStorageAcceptPolicy;
83
84 #ifdef __BLOCKS__
85 typedef void (^CFCachedURLResponseCallBackBlock)(CFCachedURLResponseRef);
86 #endif
87
88 #if defined(__OBJC__)
89
90 @interface NSURLCache ()
91 -(instancetype)_initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity relativePath:(NSString *)path;
92 - (CFURLCacheRef)_CFURLCache;
93 @end
94
95 @interface NSURLRequest ()
96 + (NSArray *)allowsSpecificHTTPSCertificateForHost:(NSString *)host;
97 + (void)setAllowsSpecificHTTPSCertificate:(NSArray *)allow forHost:(NSString *)host;
98 + (void)setDefaultTimeoutInterval:(NSTimeInterval)seconds;
99 - (NSArray *)contentDispositionEncodingFallbackArray;
100 - (CFURLRequestRef)_CFURLRequest;
101 - (id)_initWithCFURLRequest:(CFURLRequestRef)request;
102 - (id)_propertyForKey:(NSString *)key;
103 - (void)_setProperty:(id)value forKey:(NSString *)key;
104 #if USE(CFNETWORK_IGNORE_HSTS)
105 - (BOOL)_schemeWasUpgradedDueToDynamicHSTS;
106 - (BOOL)_preventHSTSStorage;
107 - (BOOL)_ignoreHSTS;
108 #endif
109 @end
110
111 @interface NSMutableURLRequest ()
112 - (void)setContentDispositionEncodingFallbackArray:(NSArray *)theEncodingFallbackArray;
113 - (void)setBoundInterfaceIdentifier:(NSString *)identifier;
114 #if USE(CFNETWORK_IGNORE_HSTS)
115 - (void)_setPreventHSTSStorage:(BOOL)preventHSTSStorage;
116 - (void)_setIgnoreHSTS:(BOOL)ignoreHSTS;
117 #endif
118 @end
119
120 @interface NSURLResponse ()
121 + (NSURLResponse *)_responseWithCFURLResponse:(CFURLResponseRef)response;
122 - (CFURLResponseRef)_CFURLResponse;
123 - (NSDate *)_lastModifiedDate;
124 @end
125
126 @interface NSHTTPCookie ()
127 - (CFHTTPCookieRef)_CFHTTPCookie;
128 + (CFArrayRef __nullable)_ns2cfCookies:(NSArray * __nullable)nsCookies CF_RETURNS_RETAINED;
129 - (CFHTTPCookieRef __nullable)_GetInternalCFHTTPCookie;
130 @end
131
132 @interface NSURLSessionConfiguration ()
133 @property (assign) _TimingDataOptions _timingDataOptions;
134 @property (copy) NSData *_sourceApplicationAuditTokenData;
135 @property (nullable, copy) NSString *_sourceApplicationBundleIdentifier;
136 @property (nullable, copy) NSString *_sourceApplicationSecondaryIdentifier;
137 @property BOOL _shouldSkipPreferredClientCertificateLookup NS_AVAILABLE(10_10, 8_0);
138 #if PLATFORM(IOS)
139 @property (nullable, copy) NSString *_CTDataConnectionServiceType;
140 #endif
141 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
142 @property (nullable, copy) NSSet *_suppressedAutoAddedHTTPHeaders;
143 #endif
144 @end
145
146 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
147 @interface NSURLSessionTaskTransactionMetrics ()
148 @property (copy, readonly) NSString* _remoteAddressAndPort;
149 @property (copy, readonly) NSUUID* _connectionIdentifier;
150 @property (assign, readonly) NSInteger _requestHeaderBytesSent;
151 @property (assign, readonly) NSInteger _responseHeaderBytesReceived;
152 @property (assign, readonly) int64_t _responseBodyBytesReceived;
153 @property (assign, readonly) int64_t _responseBodyBytesDecoded;
154 @end
155 #endif
156
157 @interface NSHTTPCookie ()
158 @property (nullable, readonly, copy) NSString *_storagePartition;
159 @end
160
161 @interface NSHTTPCookieStorage ()
162 - (id)_initWithIdentifier:(NSString *)identifier private:(bool)isPrivate;
163 - (void)_getCookiesForURL:(NSURL *)url mainDocumentURL:(NSURL *)mainDocumentURL partition:(NSString *)partition completionHandler:(void (^)(NSArray *))completionHandler;
164 - (void)_getCookiesForURL:(NSURL *)url mainDocumentURL:(NSURL *)mainDocumentURL partition:(NSString *)partition policyProperties:(NSDictionary*)props completionHandler:(void (^)(NSArray *))completionHandler;
165 - (void)_setCookies:(NSArray *)cookies forURL:(NSURL *)URL mainDocumentURL:(NSURL *)mainDocumentURL policyProperties:(NSDictionary*) props;
166 @end
167
168 @interface NSURLSessionTask ()
169 - (NSDictionary *)_timingData;
170 @property (readwrite, copy) NSString *_pathToDownloadTaskFile;
171 @property (copy) NSString *_storagePartitionIdentifier;
172 @property (nullable, readwrite, retain) NSURL *_siteForCookies;
173 @property (readwrite) BOOL _isTopLevelNavigation;
174 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
175 @property (nonatomic, assign) BOOL _preconnect;
176 #endif
177 @end
178
179 #endif // defined(__OBJC__)
180
181 #endif // !PLATFORM(WIN) && !USE(APPLE_INTERNAL_SDK)
182
183 WTF_EXTERN_C_BEGIN
184
185 #if !PLATFORM(WIN)
186
187 CFURLStorageSessionRef _CFURLStorageSessionCreate(CFAllocatorRef, CFStringRef, CFDictionaryRef);
188 CFURLCacheRef _CFURLStorageSessionCopyCache(CFAllocatorRef, CFURLStorageSessionRef);
189
190 void CFURLRequestSetShouldStartSynchronously(CFURLRequestRef, Boolean);
191
192 CFURLCacheRef CFURLCacheCopySharedURLCache();
193 void CFURLCacheSetMemoryCapacity(CFURLCacheRef, CFIndex memoryCapacity);
194 CFIndex CFURLCacheMemoryCapacity(CFURLCacheRef);
195 void CFURLCacheSetDiskCapacity(CFURLCacheRef, CFIndex);
196 CFCachedURLResponseRef CFURLCacheCopyResponseForRequest(CFURLCacheRef, CFURLRequestRef);
197
198 #if PLATFORM(COCOA)
199 Boolean _CFNetworkIsKnownHSTSHostWithSession(CFURLRef, CFURLStorageSessionRef);
200 void _CFNetworkResetHSTSHostsWithSession(CFURLStorageSessionRef);
201 #endif
202
203 void CFHTTPCookieStorageDeleteAllCookies(CFHTTPCookieStorageRef);
204 void _CFHTTPCookieStorageFlushCookieStores();
205
206 #if PLATFORM(COCOA)
207 CFDataRef _CFCachedURLResponseGetMemMappedData(CFCachedURLResponseRef);
208 #endif
209
210 #if PLATFORM(COCOA) && defined(__BLOCKS__)
211 void _CFCachedURLResponseSetBecameFileBackedCallBackBlock(CFCachedURLResponseRef, CFCachedURLResponseCallBackBlock, dispatch_queue_t);
212 #endif
213
214 extern CFStringRef const kCFHTTPCookieLocalFileDomain;
215 extern const CFStringRef kCFHTTPVersion1_1;
216 extern const CFStringRef kCFURLRequestAllowAllPOSTCaching;
217 extern const CFStringRef _kCFURLCachePartitionKey;
218 extern const CFStringRef _kCFURLConnectionPropertyShouldSniff;
219 extern const CFStringRef _kCFURLStorageSessionIsPrivate;
220
221 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101302
222 extern const CFStringRef kCFURLRequestContentDecoderSkipURLCheck;
223 #endif
224
225 CFHTTPCookieStorageRef _CFHTTPCookieStorageGetDefault(CFAllocatorRef);
226 CFHTTPCookieStorageRef CFHTTPCookieStorageCreateFromFile(CFAllocatorRef, CFURLRef, CFHTTPCookieStorageRef);
227 void CFHTTPCookieStorageScheduleWithRunLoop(CFHTTPCookieStorageRef, CFRunLoopRef, CFStringRef);
228 void CFHTTPCookieStorageSetCookie(CFHTTPCookieStorageRef, CFHTTPCookieRef);
229 void CFHTTPCookieStorageSetCookieAcceptPolicy(CFHTTPCookieStorageRef, CFHTTPCookieStorageAcceptPolicy);
230 CFHTTPCookieStorageAcceptPolicy CFHTTPCookieStorageGetCookieAcceptPolicy(CFHTTPCookieStorageRef);
231
232 typedef void (*CFHTTPCookieStorageChangedProcPtr)(CFHTTPCookieStorageRef, void*);
233 void CFHTTPCookieStorageAddObserver(CFHTTPCookieStorageRef, CFRunLoopRef, CFStringRef, CFHTTPCookieStorageChangedProcPtr, void*);
234 void CFHTTPCookieStorageRemoveObserver(CFHTTPCookieStorageRef, CFRunLoopRef, CFStringRef, CFHTTPCookieStorageChangedProcPtr, void*);
235
236 void _CFNetworkSetOverrideSystemProxySettings(CFDictionaryRef);
237 CFURLCredentialStorageRef CFURLCredentialStorageCreate(CFAllocatorRef);
238 CFURLCredentialRef CFURLCredentialStorageCopyDefaultCredentialForProtectionSpace(CFURLCredentialStorageRef, CFURLProtectionSpaceRef);
239 CFURLRequestPriority CFURLRequestGetRequestPriority(CFURLRequestRef);
240 void _CFURLRequestSetProtocolProperty(CFURLRequestRef, CFStringRef, CFTypeRef);
241 void CFURLRequestSetRequestPriority(CFURLRequestRef, CFURLRequestPriority);
242 void CFURLRequestSetShouldPipelineHTTP(CFURLRequestRef, Boolean, Boolean);
243 void _CFURLRequestSetStorageSession(CFMutableURLRequestRef, CFURLStorageSessionRef);
244 CFStringRef CFURLResponseCopySuggestedFilename(CFURLResponseRef);
245 CFHTTPMessageRef CFURLResponseGetHTTPResponse(CFURLResponseRef);
246 CFStringRef CFURLResponseGetMIMEType(CFURLResponseRef);
247 CFDictionaryRef _CFURLResponseGetSSLCertificateContext(CFURLResponseRef);
248 CFURLRef CFURLResponseGetURL(CFURLResponseRef);
249 void CFURLResponseSetMIMEType(CFURLResponseRef, CFStringRef);
250 CFHTTPCookieStorageRef _CFURLStorageSessionCopyCookieStorage(CFAllocatorRef, CFURLStorageSessionRef);
251 CFArrayRef _CFHTTPCookieStorageCopyCookiesForURLWithMainDocumentURL(CFHTTPCookieStorageRef inCookieStorage, CFURLRef inURL, CFURLRef inMainDocumentURL, Boolean sendSecureCookies);
252 CFStringRef CFURLResponseGetTextEncodingName(CFURLResponseRef);
253 SInt64 CFURLResponseGetExpectedContentLength(CFURLResponseRef);
254 CFTypeID CFURLResponseGetTypeID();
255 Boolean CFURLRequestShouldHandleHTTPCookies(CFURLRequestRef);
256 CFURLRef CFURLRequestGetURL(CFURLRequestRef);
257 CFURLResponseRef CFURLResponseCreate(CFAllocatorRef, CFURLRef, CFStringRef mimeType, SInt64 expectedContentLength, CFStringRef textEncodingName, CFURLCacheStoragePolicy);
258 void CFURLResponseSetExpectedContentLength(CFURLResponseRef, SInt64 length);
259 CFURLResponseRef CFURLResponseCreateWithHTTPResponse(CFAllocatorRef, CFURLRef, CFHTTPMessageRef, CFURLCacheStoragePolicy);
260 CFArrayRef CFHTTPCookieStorageCopyCookies(CFHTTPCookieStorageRef);
261 void CFHTTPCookieStorageSetCookies(CFHTTPCookieStorageRef, CFArrayRef cookies, CFURLRef, CFURLRef mainDocumentURL);
262 void CFHTTPCookieStorageDeleteCookie(CFHTTPCookieStorageRef, CFHTTPCookieRef);
263 CFMutableURLRequestRef CFURLRequestCreateMutableCopy(CFAllocatorRef, CFURLRequestRef);
264 CFStringRef _CFURLCacheCopyCacheDirectory(CFURLCacheRef);
265 Boolean _CFHostIsDomainTopLevel(CFStringRef domain);
266 void _CFURLRequestCreateArchiveList(CFAllocatorRef, CFURLRequestRef, CFIndex* version, CFTypeRef** objects, CFIndex* objectCount, CFDictionaryRef* protocolProperties);
267 CFMutableURLRequestRef _CFURLRequestCreateFromArchiveList(CFAllocatorRef, CFIndex version, CFTypeRef* objects, CFIndex objectCount, CFDictionaryRef protocolProperties);
268
269 #endif // !PLATFORM(WIN)
270
271 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTProxy;
272 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTProxyHost;
273 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTProxyPort;
274 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTAdditionalHeaders;
275 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTResponse;
276
277 CFN_EXPORT void _CFHTTPMessageSetResponseURL(CFHTTPMessageRef, CFURLRef);
278 CFN_EXPORT void _CFHTTPMessageSetResponseProxyURL(CFHTTPMessageRef, CFURLRef);
279
280 WTF_EXTERN_C_END
281
282 #if defined(__OBJC__) && !USE(APPLE_INTERNAL_SDK)
283
284 enum : NSUInteger {
285     NSHTTPCookieAcceptPolicyExclusivelyFromMainDocumentDomain = 3,
286 };
287
288 @interface NSCachedURLResponse ()
289 -(id)_initWithCFCachedURLResponse:(CFCachedURLResponseRef)cachedResponse;
290 -(CFCachedURLResponseRef)_CFCachedURLResponse;
291 @end
292
293 #endif
294
295 WTF_EXTERN_C_BEGIN
296
297 CFDataRef _CFNetworkCopyATSContext(void);
298 Boolean _CFNetworkSetATSContext(CFDataRef);
299
300 #if PLATFORM(COCOA)
301 void _CFNetworkResetHSTSHostsSinceDate(CFURLStorageSessionRef, CFDateRef);
302 #endif
303
304 CFDataRef CFHTTPCookieStorageCreateIdentifyingData(CFAllocatorRef inAllocator, CFHTTPCookieStorageRef inStorage);
305 CFHTTPCookieStorageRef CFHTTPCookieStorageCreateFromIdentifyingData(CFAllocatorRef inAllocator, CFDataRef inData);
306 CFArrayRef _CFHTTPParsedCookiesWithResponseHeaderFields(CFAllocatorRef inAllocator, CFDictionaryRef headerFields, CFURLRef inURL);
307
308 WTF_EXTERN_C_END
309
310 #if defined(__OBJC__)
311
312 @interface NSHTTPCookie ()
313 #if PLATFORM(MAC)
314 + (NSArray *)_parsedCookiesWithResponseHeaderFields:(NSDictionary *)headerFields forURL:(NSURL *)aURL;
315 #endif
316 + (NSArray *)_cf2nsCookies:(CFArrayRef)cfCookies;
317 @end
318
319 #if !USE(APPLE_INTERNAL_SDK)
320 @interface NSHTTPCookieStorage ()
321 - (void)removeCookiesSinceDate:(NSDate *)date;
322 - (id)_initWithCFHTTPCookieStorage:(CFHTTPCookieStorageRef)cfStorage;
323 - (CFHTTPCookieStorageRef)_cookieStorage;
324 - (void)_saveCookies;
325 @end
326 #endif
327
328 // FIXME: Move +_setSharedHTTPCookieStorage: into the above section under !USE(APPLE_INTERNAL_SDK) when possible (soon).
329 @interface NSHTTPCookieStorage ()
330 + (void)_setSharedHTTPCookieStorage:(NSHTTPCookieStorage *)storage;
331 @end
332
333 @interface NSURLResponse ()
334 - (void)_setMIMEType:(NSString *)type;
335 @end
336
337 #endif // defined(__OBJC__)