Regression(r220817): We should only copy the original request headers for Ping loads
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkConnectionToWebProcess.cpp
1 /*
2  * Copyright (C) 2012-2016 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 "NetworkConnectionToWebProcess.h"
28
29 #include "BlobDataFileReferenceWithSandboxExtension.h"
30 #include "DataReference.h"
31 #include "NetworkBlobRegistry.h"
32 #include "NetworkCache.h"
33 #include "NetworkConnectionToWebProcessMessages.h"
34 #include "NetworkLoad.h"
35 #include "NetworkProcess.h"
36 #include "NetworkProcessConnectionMessages.h"
37 #include "NetworkRTCMonitorMessages.h"
38 #include "NetworkRTCProviderMessages.h"
39 #include "NetworkRTCSocketMessages.h"
40 #include "NetworkResourceLoadParameters.h"
41 #include "NetworkResourceLoader.h"
42 #include "NetworkResourceLoaderMessages.h"
43 #include "NetworkSocketStream.h"
44 #include "NetworkSocketStreamMessages.h"
45 #include "RemoteNetworkingContext.h"
46 #include "SessionTracker.h"
47 #include "WebCoreArgumentCoders.h"
48 #include "WebsiteDataStoreParameters.h"
49 #include <WebCore/NetworkStorageSession.h>
50 #include <WebCore/PingHandle.h>
51 #include <WebCore/PlatformCookieJar.h>
52 #include <WebCore/ResourceLoaderOptions.h>
53 #include <WebCore/ResourceRequest.h>
54 #include <pal/SessionID.h>
55
56 #if USE(NETWORK_SESSION)
57 #include "PingLoad.h"
58 #endif
59
60 using namespace WebCore;
61
62 namespace WebKit {
63
64 Ref<NetworkConnectionToWebProcess> NetworkConnectionToWebProcess::create(IPC::Connection::Identifier connectionIdentifier)
65 {
66     return adoptRef(*new NetworkConnectionToWebProcess(connectionIdentifier));
67 }
68
69 NetworkConnectionToWebProcess::NetworkConnectionToWebProcess(IPC::Connection::Identifier connectionIdentifier)
70     : m_connection(IPC::Connection::createServerConnection(connectionIdentifier, *this))
71 {
72     m_connection->open();
73 }
74
75 NetworkConnectionToWebProcess::~NetworkConnectionToWebProcess()
76 {
77 #if USE(LIBWEBRTC)
78     if (m_rtcProvider)
79         m_rtcProvider->close();
80 #endif
81 }
82
83 void NetworkConnectionToWebProcess::didCleanupResourceLoader(NetworkResourceLoader& loader)
84 {
85     ASSERT(m_networkResourceLoaders.get(loader.identifier()) == &loader);
86
87     m_networkResourceLoaders.remove(loader.identifier());
88 }
89
90 void NetworkConnectionToWebProcess::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
91 {
92     if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
93         didReceiveNetworkConnectionToWebProcessMessage(connection, decoder);
94         return;
95     }
96
97     if (decoder.messageReceiverName() == Messages::NetworkResourceLoader::messageReceiverName()) {
98         auto loaderIterator = m_networkResourceLoaders.find(decoder.destinationID());
99         if (loaderIterator != m_networkResourceLoaders.end())
100             loaderIterator->value->didReceiveNetworkResourceLoaderMessage(connection, decoder);
101         return;
102     }
103
104     if (decoder.messageReceiverName() == Messages::NetworkSocketStream::messageReceiverName()) {
105         auto socketIterator = m_networkSocketStreams.find(decoder.destinationID());
106         if (socketIterator != m_networkSocketStreams.end()) {
107             socketIterator->value->didReceiveMessage(connection, decoder);
108             if (decoder.messageName() == Messages::NetworkSocketStream::Close::name())
109                 m_networkSocketStreams.remove(socketIterator);
110         }
111         return;
112     }
113
114 #if USE(LIBWEBRTC)
115     if (decoder.messageReceiverName() == Messages::NetworkRTCSocket::messageReceiverName()) {
116         rtcProvider().didReceiveNetworkRTCSocketMessage(connection, decoder);
117         return;
118     }
119     if (decoder.messageReceiverName() == Messages::NetworkRTCMonitor::messageReceiverName()) {
120         rtcProvider().didReceiveNetworkRTCMonitorMessage(connection, decoder);
121         return;
122     }
123     if (decoder.messageReceiverName() == Messages::NetworkRTCProvider::messageReceiverName()) {
124         rtcProvider().didReceiveMessage(connection, decoder);
125         return;
126     }
127 #endif
128
129     ASSERT_NOT_REACHED();
130 }
131
132 #if USE(LIBWEBRTC)
133 NetworkRTCProvider& NetworkConnectionToWebProcess::rtcProvider()
134 {
135     if (!m_rtcProvider)
136         m_rtcProvider = NetworkRTCProvider::create(*this);
137     return *m_rtcProvider;
138 }
139 #endif
140
141 void NetworkConnectionToWebProcess::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& reply)
142 {
143     if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
144         didReceiveSyncNetworkConnectionToWebProcessMessage(connection, decoder, reply);
145         return;
146     }
147     ASSERT_NOT_REACHED();
148 }
149
150 void NetworkConnectionToWebProcess::didClose(IPC::Connection&)
151 {
152     // Protect ourself as we might be otherwise be deleted during this function.
153     Ref<NetworkConnectionToWebProcess> protector(*this);
154
155     Vector<RefPtr<NetworkResourceLoader>> loaders;
156     copyValuesToVector(m_networkResourceLoaders, loaders);
157     for (auto& loader : loaders)
158         loader->abort();
159     ASSERT(m_networkResourceLoaders.isEmpty());
160
161     NetworkBlobRegistry::singleton().connectionToWebProcessDidClose(this);
162     NetworkProcess::singleton().removeNetworkConnectionToWebProcess(this);
163
164 #if USE(LIBWEBRTC)
165     if (m_rtcProvider) {
166         m_rtcProvider->close();
167         m_rtcProvider = nullptr;
168     }
169 #endif
170 }
171
172 void NetworkConnectionToWebProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
173 {
174 }
175
176 void NetworkConnectionToWebProcess::createSocketStream(URL&& url, PAL::SessionID sessionID, String cachePartition, uint64_t identifier)
177 {
178     ASSERT(!m_networkSocketStreams.contains(identifier));
179     WebCore::SourceApplicationAuditToken token = { };
180 #if PLATFORM(COCOA)
181     token = { NetworkProcess::singleton().sourceApplicationAuditData() };
182 #endif
183     m_networkSocketStreams.set(identifier, NetworkSocketStream::create(WTFMove(url), sessionID, cachePartition, identifier, m_connection, WTFMove(token)));
184 }
185
186 void NetworkConnectionToWebProcess::destroySocketStream(uint64_t identifier)
187 {
188     ASSERT(m_networkSocketStreams.get(identifier));
189     m_networkSocketStreams.remove(identifier);
190 }
191
192 void NetworkConnectionToWebProcess::cleanupForSuspension(Function<void()>&& completionHandler)
193 {
194 #if USE(LIBWEBRTC)
195     if (m_rtcProvider) {
196         m_rtcProvider->closeListeningSockets(WTFMove(completionHandler));
197         return;
198     }
199 #endif
200     completionHandler();
201 }
202
203 void NetworkConnectionToWebProcess::endSuspension()
204 {
205 #if USE(LIBWEBRTC)
206     if (m_rtcProvider)
207         m_rtcProvider->authorizeListeningSockets();
208 #endif
209 }
210
211 void NetworkConnectionToWebProcess::scheduleResourceLoad(const NetworkResourceLoadParameters& loadParameters)
212 {
213     auto loader = NetworkResourceLoader::create(loadParameters, *this);
214     m_networkResourceLoaders.add(loadParameters.identifier, loader.ptr());
215     loader->start();
216 }
217
218 void NetworkConnectionToWebProcess::performSynchronousLoad(const NetworkResourceLoadParameters& loadParameters, Ref<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&& reply)
219 {
220     auto loader = NetworkResourceLoader::create(loadParameters, *this, WTFMove(reply));
221     m_networkResourceLoaders.add(loadParameters.identifier, loader.ptr());
222     loader->start();
223 }
224
225 void NetworkConnectionToWebProcess::loadPing(NetworkResourceLoadParameters&& loadParameters, HTTPHeaderMap&& originalRequestHeaders)
226 {
227 #if USE(NETWORK_SESSION)
228     // PingLoad manages its own lifetime, deleting itself when its purpose has been fulfilled.
229     new PingLoad(WTFMove(loadParameters), WTFMove(originalRequestHeaders));
230 #else
231     UNUSED_PARAM(originalRequestHeaders);
232     RefPtr<NetworkingContext> context = RemoteNetworkingContext::create(loadParameters.sessionID, loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect);
233
234     // PingHandle manages its own lifetime, deleting itself when its purpose has been fulfilled.
235     new PingHandle(context.get(), loadParameters.request, loadParameters.allowStoredCredentials == AllowStoredCredentials, PingHandle::UsesAsyncCallbacks::Yes, loadParameters.shouldFollowRedirects);
236 #endif
237 }
238
239 void NetworkConnectionToWebProcess::removeLoadIdentifier(ResourceLoadIdentifier identifier)
240 {
241     RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
242
243     // It's possible we have no loader for this identifier if the NetworkProcess crashed and this was a respawned NetworkProcess.
244     if (!loader)
245         return;
246
247     // Abort the load now, as the WebProcess won't be able to respond to messages any more which might lead
248     // to leaked loader resources (connections, threads, etc).
249     loader->abort();
250     ASSERT(!m_networkResourceLoaders.contains(identifier));
251 }
252
253 void NetworkConnectionToWebProcess::setDefersLoading(ResourceLoadIdentifier identifier, bool defers)
254 {
255     RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
256     if (!loader)
257         return;
258
259     loader->setDefersLoading(defers);
260 }
261
262 void NetworkConnectionToWebProcess::prefetchDNS(const String& hostname)
263 {
264     NetworkProcess::singleton().prefetchDNS(hostname);
265 }
266
267 static NetworkStorageSession& storageSession(PAL::SessionID sessionID)
268 {
269     ASSERT(sessionID.isValid());
270     if (sessionID != PAL::SessionID::defaultSessionID()) {
271         if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
272             return *storageSession;
273
274         // Some requests with private browsing mode requested may still be coming shortly after NetworkProcess was told to destroy its session.
275         // FIXME: Find a way to track private browsing sessions more rigorously.
276         LOG_ERROR("Non-default storage session was requested, but there was no session for it. Please file a bug unless you just disabled private browsing, in which case it's an expected race.");
277     }
278     return NetworkStorageSession::defaultStorageSession();
279 }
280
281 void NetworkConnectionToWebProcess::startDownload(PAL::SessionID sessionID, DownloadID downloadID, const ResourceRequest& request, const String& suggestedName)
282 {
283     NetworkProcess::singleton().downloadManager().startDownload(this, sessionID, downloadID, request, suggestedName);
284 }
285
286 void NetworkConnectionToWebProcess::convertMainResourceLoadToDownload(PAL::SessionID sessionID, uint64_t mainResourceLoadIdentifier, DownloadID downloadID, const ResourceRequest& request, const ResourceResponse& response)
287 {
288     auto& networkProcess = NetworkProcess::singleton();
289     if (!mainResourceLoadIdentifier) {
290         networkProcess.downloadManager().startDownload(this, sessionID, downloadID, request);
291         return;
292     }
293
294     NetworkResourceLoader* loader = m_networkResourceLoaders.get(mainResourceLoadIdentifier);
295     if (!loader) {
296         // If we're trying to download a blob here loader can be null.
297         return;
298     }
299
300     loader->convertToDownload(downloadID, request, response);
301 }
302
303 void NetworkConnectionToWebProcess::cookiesForDOM(PAL::SessionID sessionID, const URL& firstParty, const URL& url, String& result)
304 {
305     result = WebCore::cookiesForDOM(storageSession(sessionID), firstParty, url);
306 }
307
308 void NetworkConnectionToWebProcess::setCookiesFromDOM(PAL::SessionID sessionID, const URL& firstParty, const URL& url, const String& cookieString)
309 {
310     WebCore::setCookiesFromDOM(storageSession(sessionID), firstParty, url, cookieString);
311 }
312
313 void NetworkConnectionToWebProcess::cookiesEnabled(PAL::SessionID sessionID, const URL& firstParty, const URL& url, bool& result)
314 {
315     result = WebCore::cookiesEnabled(storageSession(sessionID), firstParty, url);
316 }
317
318 void NetworkConnectionToWebProcess::cookieRequestHeaderFieldValue(PAL::SessionID sessionID, const URL& firstParty, const URL& url, String& result)
319 {
320     result = WebCore::cookieRequestHeaderFieldValue(storageSession(sessionID), firstParty, url);
321 }
322
323 void NetworkConnectionToWebProcess::getRawCookies(PAL::SessionID sessionID, const URL& firstParty, const URL& url, Vector<Cookie>& result)
324 {
325     WebCore::getRawCookies(storageSession(sessionID), firstParty, url, result);
326 }
327
328 void NetworkConnectionToWebProcess::deleteCookie(PAL::SessionID sessionID, const URL& url, const String& cookieName)
329 {
330     WebCore::deleteCookie(storageSession(sessionID), url, cookieName);
331 }
332
333 void NetworkConnectionToWebProcess::registerFileBlobURL(const URL& url, const String& path, const SandboxExtension::Handle& extensionHandle, const String& contentType)
334 {
335     RefPtr<SandboxExtension> extension = SandboxExtension::create(extensionHandle);
336
337     NetworkBlobRegistry::singleton().registerFileBlobURL(this, url, path, WTFMove(extension), contentType);
338 }
339
340 void NetworkConnectionToWebProcess::registerBlobURL(const URL& url, Vector<BlobPart>&& blobParts, const String& contentType)
341 {
342     NetworkBlobRegistry::singleton().registerBlobURL(this, url, WTFMove(blobParts), contentType);
343 }
344
345 void NetworkConnectionToWebProcess::registerBlobURLFromURL(const URL& url, const URL& srcURL)
346 {
347     NetworkBlobRegistry::singleton().registerBlobURL(this, url, srcURL);
348 }
349
350 void NetworkConnectionToWebProcess::preregisterSandboxExtensionsForOptionallyFileBackedBlob(const Vector<String>& filePaths, const SandboxExtension::HandleArray& handles)
351 {
352 #if ENABLE(SANDBOX_EXTENSIONS)
353     ASSERT(filePaths.size() == handles.size());
354
355     for (size_t i = 0; i < filePaths.size(); ++i)
356         m_blobDataFileReferences.add(filePaths[i], BlobDataFileReferenceWithSandboxExtension::create(filePaths[i], SandboxExtension::create(handles[i])));
357 #else
358     for (size_t i = 0; i < filePaths.size(); ++i)
359         m_blobDataFileReferences.add(filePaths[i], BlobDataFileReferenceWithSandboxExtension::create(filePaths[i], nullptr));
360 #endif
361 }
362
363 RefPtr<WebCore::BlobDataFileReference> NetworkConnectionToWebProcess::getBlobDataFileReferenceForPath(const String& path)
364 {
365     ASSERT(m_blobDataFileReferences.contains(path));
366     return m_blobDataFileReferences.get(path);
367 }
368
369 void NetworkConnectionToWebProcess::registerBlobURLOptionallyFileBacked(const URL& url, const URL& srcURL, const String& fileBackedPath, const String& contentType)
370 {
371     NetworkBlobRegistry::singleton().registerBlobURLOptionallyFileBacked(this, url, srcURL, fileBackedPath, contentType);
372 }
373
374 void NetworkConnectionToWebProcess::registerBlobURLForSlice(const URL& url, const URL& srcURL, int64_t start, int64_t end)
375 {
376     NetworkBlobRegistry::singleton().registerBlobURLForSlice(this, url, srcURL, start, end);
377 }
378
379 void NetworkConnectionToWebProcess::unregisterBlobURL(const URL& url)
380 {
381     NetworkBlobRegistry::singleton().unregisterBlobURL(this, url);
382 }
383
384 void NetworkConnectionToWebProcess::blobSize(const URL& url, uint64_t& resultSize)
385 {
386     resultSize = NetworkBlobRegistry::singleton().blobSize(this, url);
387 }
388
389 void NetworkConnectionToWebProcess::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, uint64_t requestIdentifier)
390 {
391     Vector<RefPtr<BlobDataFileReference>> fileReferences;
392     for (auto& url : blobURLs)
393         fileReferences.appendVector(NetworkBlobRegistry::singleton().filesInBlob(*this, { ParsedURLString, url }));
394
395     for (auto& file : fileReferences)
396         file->prepareForFileAccess();
397
398     NetworkBlobRegistry::singleton().writeBlobsToTemporaryFiles(blobURLs, [this, protectedThis = makeRef(*this), requestIdentifier, fileReferences = WTFMove(fileReferences)](auto& fileNames) mutable {
399         for (auto& file : fileReferences)
400             file->revokeFileAccess();
401
402         NetworkProcess::singleton().grantSandboxExtensionsToStorageProcessForBlobs(fileNames, [this, protectedThis = WTFMove(protectedThis), requestIdentifier, fileNames]() {
403             if (!m_connection->isValid())
404                 return;
405
406             m_connection->send(Messages::NetworkProcessConnection::DidWriteBlobsToTemporaryFiles(requestIdentifier, fileNames), 0);
407         });
408     });
409 }
410
411 void NetworkConnectionToWebProcess::storeDerivedDataToCache(const WebKit::NetworkCache::DataKey& dataKey, const IPC::DataReference& data)
412 {
413     if (auto* cache = NetworkProcess::singleton().cache())
414         cache->storeData(dataKey, data.data(), data.size());
415 }
416
417 void NetworkConnectionToWebProcess::setCaptureExtraNetworkLoadMetricsEnabled(bool enabled)
418 {
419     m_captureExtraNetworkLoadMetricsEnabled = enabled;
420 }
421
422 void NetworkConnectionToWebProcess::ensureLegacyPrivateBrowsingSession()
423 {
424     NetworkProcess::singleton().ensurePrivateBrowsingSession({PAL::SessionID::legacyPrivateSessionID(), { }, { }, { }});
425 }
426
427 } // namespace WebKit