2 * Copyright (C) 2012-2018 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. 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.
27 #include "NetworkConnectionToWebProcess.h"
29 #include "BlobDataFileReferenceWithSandboxExtension.h"
30 #include "CacheStorageEngineConnectionMessages.h"
31 #include "DataReference.h"
32 #include "NetworkBlobRegistry.h"
33 #include "NetworkCache.h"
34 #include "NetworkConnectionToWebProcessMessages.h"
35 #include "NetworkLoad.h"
36 #include "NetworkMDNSRegisterMessages.h"
37 #include "NetworkProcess.h"
38 #include "NetworkProcessConnectionMessages.h"
39 #include "NetworkRTCMonitorMessages.h"
40 #include "NetworkRTCProviderMessages.h"
41 #include "NetworkRTCSocketMessages.h"
42 #include "NetworkResourceLoadParameters.h"
43 #include "NetworkResourceLoader.h"
44 #include "NetworkResourceLoaderMessages.h"
45 #include "NetworkSocketStream.h"
46 #include "NetworkSocketStreamMessages.h"
48 #include "PreconnectTask.h"
49 #include "SessionTracker.h"
50 #include "WebCoreArgumentCoders.h"
51 #include "WebErrors.h"
52 #include "WebsiteDataStore.h"
53 #include "WebsiteDataStoreParameters.h"
54 #include <WebCore/NetworkStorageSession.h>
55 #include <WebCore/PlatformCookieJar.h>
56 #include <WebCore/ResourceLoaderOptions.h>
57 #include <WebCore/ResourceRequest.h>
58 #include <WebCore/SameSiteInfo.h>
59 #include <WebCore/SecurityPolicy.h>
60 #include <pal/SessionID.h>
62 using namespace WebCore;
66 Ref<NetworkConnectionToWebProcess> NetworkConnectionToWebProcess::create(IPC::Connection::Identifier connectionIdentifier)
68 return adoptRef(*new NetworkConnectionToWebProcess(connectionIdentifier));
71 NetworkConnectionToWebProcess::NetworkConnectionToWebProcess(IPC::Connection::Identifier connectionIdentifier)
72 : m_connection(IPC::Connection::createServerConnection(connectionIdentifier, *this))
74 , m_mdnsRegister(*this)
80 NetworkConnectionToWebProcess::~NetworkConnectionToWebProcess()
82 m_connection->invalidate();
85 m_rtcProvider->close();
89 void NetworkConnectionToWebProcess::didCleanupResourceLoader(NetworkResourceLoader& loader)
91 ASSERT(m_networkResourceLoaders.get(loader.identifier()) == &loader);
93 m_networkResourceLoaders.remove(loader.identifier());
96 void NetworkConnectionToWebProcess::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
98 if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
99 didReceiveNetworkConnectionToWebProcessMessage(connection, decoder);
103 if (decoder.messageReceiverName() == Messages::NetworkResourceLoader::messageReceiverName()) {
104 auto loaderIterator = m_networkResourceLoaders.find(decoder.destinationID());
105 if (loaderIterator != m_networkResourceLoaders.end())
106 loaderIterator->value->didReceiveNetworkResourceLoaderMessage(connection, decoder);
110 if (decoder.messageReceiverName() == Messages::NetworkSocketStream::messageReceiverName()) {
111 auto socketIterator = m_networkSocketStreams.find(decoder.destinationID());
112 if (socketIterator != m_networkSocketStreams.end()) {
113 socketIterator->value->didReceiveMessage(connection, decoder);
114 if (decoder.messageName() == Messages::NetworkSocketStream::Close::name())
115 m_networkSocketStreams.remove(socketIterator);
121 if (decoder.messageReceiverName() == Messages::NetworkRTCSocket::messageReceiverName()) {
122 rtcProvider().didReceiveNetworkRTCSocketMessage(connection, decoder);
125 if (decoder.messageReceiverName() == Messages::NetworkRTCMonitor::messageReceiverName()) {
126 rtcProvider().didReceiveNetworkRTCMonitorMessage(connection, decoder);
129 if (decoder.messageReceiverName() == Messages::NetworkRTCProvider::messageReceiverName()) {
130 rtcProvider().didReceiveMessage(connection, decoder);
135 if (decoder.messageReceiverName() == Messages::NetworkMDNSRegister::messageReceiverName()) {
136 mdnsRegister().didReceiveMessage(connection, decoder);
141 if (decoder.messageReceiverName() == Messages::CacheStorageEngineConnection::messageReceiverName()) {
142 cacheStorageConnection().didReceiveMessage(connection, decoder);
146 ASSERT_NOT_REACHED();
150 NetworkRTCProvider& NetworkConnectionToWebProcess::rtcProvider()
153 m_rtcProvider = NetworkRTCProvider::create(*this);
154 return *m_rtcProvider;
158 CacheStorageEngineConnection& NetworkConnectionToWebProcess::cacheStorageConnection()
160 if (!m_cacheStorageConnection)
161 m_cacheStorageConnection = CacheStorageEngineConnection::create(*this);
162 return *m_cacheStorageConnection;
165 void NetworkConnectionToWebProcess::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& reply)
167 if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
168 didReceiveSyncNetworkConnectionToWebProcessMessage(connection, decoder, reply);
171 ASSERT_NOT_REACHED();
174 void NetworkConnectionToWebProcess::didClose(IPC::Connection&)
176 // Protect ourself as we might be otherwise be deleted during this function.
177 Ref<NetworkConnectionToWebProcess> protector(*this);
179 for (auto& loader : copyToVector(m_networkResourceLoaders.values()))
181 ASSERT(m_networkResourceLoaders.isEmpty());
183 NetworkBlobRegistry::singleton().connectionToWebProcessDidClose(this);
184 NetworkProcess::singleton().removeNetworkConnectionToWebProcess(this);
188 m_rtcProvider->close();
189 m_rtcProvider = nullptr;
194 void NetworkConnectionToWebProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
198 void NetworkConnectionToWebProcess::createSocketStream(URL&& url, PAL::SessionID sessionID, String cachePartition, uint64_t identifier)
200 ASSERT(!m_networkSocketStreams.contains(identifier));
201 WebCore::SourceApplicationAuditToken token = { };
203 token = { NetworkProcess::singleton().sourceApplicationAuditData() };
205 m_networkSocketStreams.set(identifier, NetworkSocketStream::create(WTFMove(url), sessionID, cachePartition, identifier, m_connection, WTFMove(token)));
208 void NetworkConnectionToWebProcess::destroySocketStream(uint64_t identifier)
210 ASSERT(m_networkSocketStreams.get(identifier));
211 m_networkSocketStreams.remove(identifier);
214 void NetworkConnectionToWebProcess::cleanupForSuspension(Function<void()>&& completionHandler)
218 m_rtcProvider->closeListeningSockets(WTFMove(completionHandler));
225 void NetworkConnectionToWebProcess::endSuspension()
229 m_rtcProvider->authorizeListeningSockets();
233 void NetworkConnectionToWebProcess::scheduleResourceLoad(NetworkResourceLoadParameters&& loadParameters)
235 auto identifier = loadParameters.identifier;
236 ASSERT(!m_networkResourceLoaders.contains(identifier));
238 auto loader = NetworkResourceLoader::create(WTFMove(loadParameters), *this);
239 m_networkResourceLoaders.add(identifier, loader.ptr());
243 void NetworkConnectionToWebProcess::performSynchronousLoad(NetworkResourceLoadParameters&& loadParameters, Ref<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&& reply)
245 auto identifier = loadParameters.identifier;
246 ASSERT(!m_networkResourceLoaders.contains(identifier));
248 auto loader = NetworkResourceLoader::create(WTFMove(loadParameters), *this, WTFMove(reply));
249 m_networkResourceLoaders.add(identifier, loader.ptr());
253 void NetworkConnectionToWebProcess::loadPing(NetworkResourceLoadParameters&& loadParameters)
255 auto completionHandler = [this, protectedThis = makeRef(*this), identifier = loadParameters.identifier] (const ResourceError& error, const ResourceResponse& response) {
256 didFinishPingLoad(identifier, error, response);
259 // PingLoad manages its own lifetime, deleting itself when its purpose has been fulfilled.
260 new PingLoad(WTFMove(loadParameters), WTFMove(completionHandler));
263 void NetworkConnectionToWebProcess::didFinishPingLoad(uint64_t pingLoadIdentifier, const ResourceError& error, const ResourceResponse& response)
265 if (!m_connection->isValid())
268 m_connection->send(Messages::NetworkProcessConnection::DidFinishPingLoad(pingLoadIdentifier, error, response), 0);
271 void NetworkConnectionToWebProcess::setOnLineState(bool isOnLine)
273 m_connection->send(Messages::NetworkProcessConnection::SetOnLineState(isOnLine), 0);
276 void NetworkConnectionToWebProcess::removeLoadIdentifier(ResourceLoadIdentifier identifier)
278 RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
280 // It's possible we have no loader for this identifier if the NetworkProcess crashed and this was a respawned NetworkProcess.
284 // Abort the load now, as the WebProcess won't be able to respond to messages any more which might lead
285 // to leaked loader resources (connections, threads, etc).
287 ASSERT(!m_networkResourceLoaders.contains(identifier));
290 void NetworkConnectionToWebProcess::setDefersLoading(ResourceLoadIdentifier identifier, bool defers)
292 RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
296 loader->setDefersLoading(defers);
299 void NetworkConnectionToWebProcess::prefetchDNS(const String& hostname)
301 NetworkProcess::singleton().prefetchDNS(hostname);
304 void NetworkConnectionToWebProcess::preconnectTo(uint64_t preconnectionIdentifier, NetworkResourceLoadParameters&& parameters)
306 #if ENABLE(SERVER_PRECONNECT)
307 new PreconnectTask(WTFMove(parameters), [this, protectedThis = makeRef(*this), identifier = preconnectionIdentifier] (const ResourceError& error) {
308 didFinishPreconnection(identifier, error);
311 UNUSED_PARAM(parameters);
312 didFinishPreconnection(preconnectionIdentifier, internalError(parameters.request.url()));
316 void NetworkConnectionToWebProcess::didFinishPreconnection(uint64_t preconnectionIdentifier, const ResourceError& error)
318 if (!m_connection->isValid())
321 m_connection->send(Messages::NetworkProcessConnection::DidFinishPreconnection(preconnectionIdentifier, error), 0);
324 static NetworkStorageSession& storageSession(PAL::SessionID sessionID)
326 ASSERT(sessionID.isValid());
327 if (sessionID != PAL::SessionID::defaultSessionID()) {
328 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
329 return *storageSession;
331 // Some requests with private browsing mode requested may still be coming shortly after NetworkProcess was told to destroy its session.
332 // FIXME: Find a way to track private browsing sessions more rigorously.
333 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.");
335 return NetworkStorageSession::defaultStorageSession();
338 void NetworkConnectionToWebProcess::startDownload(PAL::SessionID sessionID, DownloadID downloadID, const ResourceRequest& request, const String& suggestedName)
340 NetworkProcess::singleton().downloadManager().startDownload(this, sessionID, downloadID, request, suggestedName);
343 void NetworkConnectionToWebProcess::convertMainResourceLoadToDownload(PAL::SessionID sessionID, uint64_t mainResourceLoadIdentifier, DownloadID downloadID, const ResourceRequest& request, const ResourceResponse& response)
345 auto& networkProcess = NetworkProcess::singleton();
346 // In case a response is served from service worker, we do not have yet the ability to convert the load.
347 if (!mainResourceLoadIdentifier || response.source() == ResourceResponse::Source::ServiceWorker) {
348 networkProcess.downloadManager().startDownload(this, sessionID, downloadID, request);
352 NetworkResourceLoader* loader = m_networkResourceLoaders.get(mainResourceLoadIdentifier);
354 // If we're trying to download a blob here loader can be null.
358 loader->convertToDownload(downloadID, request, response);
361 void NetworkConnectionToWebProcess::cookiesForDOM(PAL::SessionID sessionID, const URL& firstParty, const SameSiteInfo& sameSiteInfo, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, IncludeSecureCookies includeSecureCookies, String& cookieString, bool& secureCookiesAccessed)
363 auto& networkStorageSession = storageSession(sessionID);
364 std::tie(cookieString, secureCookiesAccessed) = WebCore::cookiesForDOM(networkStorageSession, firstParty, sameSiteInfo, url, frameID, pageID, includeSecureCookies);
365 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
366 if (NetworkProcess::singleton().shouldLogCookieInformation())
367 NetworkResourceLoader::logCookieInformation("NetworkConnectionToWebProcess::cookiesForDOM", reinterpret_cast<const void*>(this), networkStorageSession, firstParty, sameSiteInfo, url, emptyString(), frameID, pageID, std::nullopt);
371 void NetworkConnectionToWebProcess::setCookiesFromDOM(PAL::SessionID sessionID, const URL& firstParty, const SameSiteInfo& sameSiteInfo, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, const String& cookieString)
373 auto& networkStorageSession = storageSession(sessionID);
374 WebCore::setCookiesFromDOM(networkStorageSession, firstParty, sameSiteInfo, url, frameID, pageID, cookieString);
375 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
376 if (NetworkProcess::singleton().shouldLogCookieInformation())
377 NetworkResourceLoader::logCookieInformation("NetworkConnectionToWebProcess::setCookiesFromDOM", reinterpret_cast<const void*>(this), networkStorageSession, firstParty, sameSiteInfo, url, emptyString(), frameID, pageID, std::nullopt);
381 void NetworkConnectionToWebProcess::cookiesEnabled(PAL::SessionID sessionID, bool& result)
383 result = WebCore::cookiesEnabled(storageSession(sessionID));
386 void NetworkConnectionToWebProcess::cookieRequestHeaderFieldValue(PAL::SessionID sessionID, const URL& firstParty, const SameSiteInfo& sameSiteInfo, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, IncludeSecureCookies includeSecureCookies, String& cookieString, bool& secureCookiesAccessed)
388 std::tie(cookieString, secureCookiesAccessed) = WebCore::cookieRequestHeaderFieldValue(storageSession(sessionID), firstParty, sameSiteInfo, url, frameID, pageID, includeSecureCookies);
391 void NetworkConnectionToWebProcess::getRawCookies(PAL::SessionID sessionID, const URL& firstParty, const SameSiteInfo& sameSiteInfo, const URL& url, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, Vector<Cookie>& result)
393 WebCore::getRawCookies(storageSession(sessionID), firstParty, sameSiteInfo, url, frameID, pageID, result);
396 void NetworkConnectionToWebProcess::deleteCookie(PAL::SessionID sessionID, const URL& url, const String& cookieName)
398 WebCore::deleteCookie(storageSession(sessionID), url, cookieName);
401 void NetworkConnectionToWebProcess::registerFileBlobURL(const URL& url, const String& path, SandboxExtension::Handle&& extensionHandle, const String& contentType)
403 RefPtr<SandboxExtension> extension = SandboxExtension::create(WTFMove(extensionHandle));
405 NetworkBlobRegistry::singleton().registerFileBlobURL(this, url, path, WTFMove(extension), contentType);
408 void NetworkConnectionToWebProcess::registerBlobURL(const URL& url, Vector<BlobPart>&& blobParts, const String& contentType)
410 NetworkBlobRegistry::singleton().registerBlobURL(this, url, WTFMove(blobParts), contentType);
413 void NetworkConnectionToWebProcess::registerBlobURLFromURL(const URL& url, const URL& srcURL, bool shouldBypassConnectionCheck)
415 NetworkBlobRegistry::singleton().registerBlobURL(this, url, srcURL, shouldBypassConnectionCheck);
418 void NetworkConnectionToWebProcess::preregisterSandboxExtensionsForOptionallyFileBackedBlob(const Vector<String>& filePaths, SandboxExtension::HandleArray&& handles)
420 #if ENABLE(SANDBOX_EXTENSIONS)
421 ASSERT(filePaths.size() == handles.size());
423 for (size_t i = 0; i < filePaths.size(); ++i)
424 m_blobDataFileReferences.add(filePaths[i], BlobDataFileReferenceWithSandboxExtension::create(filePaths[i], SandboxExtension::create(WTFMove(handles[i]))));
426 for (size_t i = 0; i < filePaths.size(); ++i)
427 m_blobDataFileReferences.add(filePaths[i], BlobDataFileReferenceWithSandboxExtension::create(filePaths[i], nullptr));
431 RefPtr<WebCore::BlobDataFileReference> NetworkConnectionToWebProcess::getBlobDataFileReferenceForPath(const String& path)
433 ASSERT(m_blobDataFileReferences.contains(path));
434 return m_blobDataFileReferences.get(path);
437 void NetworkConnectionToWebProcess::registerBlobURLOptionallyFileBacked(const URL& url, const URL& srcURL, const String& fileBackedPath, const String& contentType)
439 NetworkBlobRegistry::singleton().registerBlobURLOptionallyFileBacked(this, url, srcURL, fileBackedPath, contentType);
442 void NetworkConnectionToWebProcess::registerBlobURLForSlice(const URL& url, const URL& srcURL, int64_t start, int64_t end)
444 NetworkBlobRegistry::singleton().registerBlobURLForSlice(this, url, srcURL, start, end);
447 void NetworkConnectionToWebProcess::unregisterBlobURL(const URL& url)
449 NetworkBlobRegistry::singleton().unregisterBlobURL(this, url);
452 void NetworkConnectionToWebProcess::blobSize(const URL& url, uint64_t& resultSize)
454 resultSize = NetworkBlobRegistry::singleton().blobSize(this, url);
457 void NetworkConnectionToWebProcess::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, uint64_t requestIdentifier)
459 Vector<RefPtr<BlobDataFileReference>> fileReferences;
460 for (auto& url : blobURLs)
461 fileReferences.appendVector(NetworkBlobRegistry::singleton().filesInBlob(*this, { ParsedURLString, url }));
463 for (auto& file : fileReferences)
464 file->prepareForFileAccess();
466 NetworkBlobRegistry::singleton().writeBlobsToTemporaryFiles(blobURLs, [this, protectedThis = makeRef(*this), requestIdentifier, fileReferences = WTFMove(fileReferences)](auto& fileNames) mutable {
467 for (auto& file : fileReferences)
468 file->revokeFileAccess();
470 NetworkProcess::singleton().grantSandboxExtensionsToStorageProcessForBlobs(fileNames, [this, protectedThis = WTFMove(protectedThis), requestIdentifier, fileNames]() {
471 if (!m_connection->isValid())
474 m_connection->send(Messages::NetworkProcessConnection::DidWriteBlobsToTemporaryFiles(requestIdentifier, fileNames), 0);
479 void NetworkConnectionToWebProcess::storeDerivedDataToCache(const WebKit::NetworkCache::DataKey& dataKey, const IPC::DataReference& data)
481 if (auto* cache = NetworkProcess::singleton().cache())
482 cache->storeData(dataKey, data.data(), data.size());
485 void NetworkConnectionToWebProcess::setCaptureExtraNetworkLoadMetricsEnabled(bool enabled)
487 m_captureExtraNetworkLoadMetricsEnabled = enabled;
488 if (!m_captureExtraNetworkLoadMetricsEnabled)
489 m_networkLoadInformationByID.clear();
492 void NetworkConnectionToWebProcess::ensureLegacyPrivateBrowsingSession()
494 NetworkProcess::singleton().addWebsiteDataStore(WebsiteDataStoreParameters::legacyPrivateSessionParameters());
497 void NetworkConnectionToWebProcess::removeStorageAccessForFrame(PAL::SessionID sessionID, uint64_t frameID, uint64_t pageID)
499 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
500 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
501 storageSession->removeStorageAccessForFrame(frameID, pageID);
503 UNUSED_PARAM(sessionID);
504 UNUSED_PARAM(frameID);
505 UNUSED_PARAM(pageID);
509 void NetworkConnectionToWebProcess::removeStorageAccessForAllFramesOnPage(PAL::SessionID sessionID, uint64_t pageID)
511 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
512 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
513 storageSession->removeStorageAccessForAllFramesOnPage(pageID);
515 UNUSED_PARAM(sessionID);
516 UNUSED_PARAM(pageID);
520 void NetworkConnectionToWebProcess::addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
522 SecurityPolicy::addOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
525 void NetworkConnectionToWebProcess::removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
527 SecurityPolicy::removeOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
530 void NetworkConnectionToWebProcess::resetOriginAccessWhitelists()
532 SecurityPolicy::resetOriginAccessWhitelists();
535 } // namespace WebKit