Truly independent web browser
RequestServer: Share one certificate store between macOS connections
Andreas Kling088e16453693
diff --git a/Services/RequestServer/Request.cpp b/Services/RequestServer/Request.cppindex 2c92b827094..d43f2f14267 100644--- a/Services/RequestServer/Request.cpp+++ b/Services/RequestServer/Request.cpp@@ -49,6 +49,40 @@ static ByteString url_without_credentials(URL::URL url)return url.to_byte_string();}+#ifdef AK_OS_MACOS+// curl parses the system's root certificates for every connection on macOS, because it keeps its store cache off when+// verifying through the system trust APIs, and the parsed store costs about 1 MiB for as long as the connection lives.+// So all connections share one parsed store, replaced as often as curl would replace a store it caches.+static void share_certificate_store(SSL_CTX* ssl_context)+{+ // NB: A custom certificate is only trusted by the handles that set it, so those handles can't share a store.+ if (!default_certificate_path().is_empty())+ return;++ static constexpr auto s_shared_store_lifetime = AK::Duration::from_seconds(24 * 60 * 60);+ static X509_STORE* s_shared_store = nullptr;+ static MonotonicTime s_shared_store_expiry = MonotonicTime::now_coarse();++ auto now = MonotonicTime::now_coarse();+ if (!s_shared_store || now >= s_shared_store_expiry) {+ // NB: curl ignores errors when it loads the store. An empty store stays with this connection only, and the+ // next connection tries again.+ auto* store = SSL_CTX_get_cert_store(ssl_context);+ if (!store || sk_X509_OBJECT_num(X509_STORE_get0_objects(store)) == 0 || !X509_STORE_up_ref(store))+ return;++ X509_STORE_free(s_shared_store);+ s_shared_store = store;+ s_shared_store_expiry = now + s_shared_store_lifetime;+ return;+ }++ // NB: The SSL_CTX takes over the reference, and frees the store curl just parsed for it.+ if (X509_STORE_up_ref(s_shared_store))+ SSL_CTX_set_cert_store(ssl_context, s_shared_store);+}+#endif+static CURLcode configure_ssl_context(CURL*, [[maybe_unused]] void* ssl_context, [[maybe_unused]] void* aia_collector){// Everything below casts ssl_context to an SSL_CTX, which only holds when curl is built against OpenSSL.@@ -66,7 +100,10 @@ static CURLcode configure_ssl_context(CURL*, [[maybe_unused]] void* ssl_context,SSL_CTX_set_options(static_cast<SSL_CTX*>(ssl_context), SSL_OP_IGNORE_UNEXPECTED_EOF);#endif-#ifndef AK_OS_MACOS+#ifdef AK_OS_MACOS+ if (curl_uses_openssl)+ share_certificate_store(static_cast<SSL_CTX*>(ssl_context));+#else// curl keeps one SSL context callback per handle, and AIA has to run against the same SSL_CTX configured above. So// both are applied here. The collector arrives as CURLOPT_SSL_CTX_DATA and is null on handles lacking AIA state.if (curl_uses_openssl && aia_collector)