Skip to content

Truly independent web browser

WebContent: Upgrade a worker's request when its HSTS lookup fails

Luke Wildebbeeccd50753
diff --git a/Services/WebContent/Worker/PageHost.cpp b/Services/WebContent/Worker/PageHost.cpp
index a708b3c9e77..bd6a4f6352d 100644
--- a/Services/WebContent/Worker/PageHost.cpp
+++ b/Services/WebContent/Worker/PageHost.cpp
@@ -100,8 +100,16 @@ Optional<Web::Permissions::PermissionSnapshot> PageHost::page_did_request_permis
return response->take_snapshot();
}
-// NB: The sync requests here fail once the worker is terminated or the UI process is gone. The script that made one no
-// longer runs by then, so what it gets back here does not matter, but it has to get something.
+bool PageHost::page_did_is_known_hsts_host(String const& domain)
+{
+ auto response = m_client.send_sync_but_allow_failure<Messages::WebWorkerClient::DidIsKnownHstsHost>(domain);
+ if (!response)
+ return true;
+ return response->result();
+}
+
+// NB: The sync requests below fail once the worker is terminated or loses its connection, and the empty answers they
+// fall back to grant nothing.
HTTP::Cookie::VersionedCookie PageHost::page_did_request_cookie(Optional<Web::HTML::EnvironmentId> const&, URL::URL const& url, HTTP::Cookie::Source source)
{
auto response = m_client.send_sync_but_allow_failure<Messages::WebWorkerClient::DidRequestCookie>(url, source);
@@ -110,12 +118,6 @@ HTTP::Cookie::VersionedCookie PageHost::page_did_request_cookie(Optional<Web::HT
return response->take_cookie();
}
-bool PageHost::page_did_is_known_hsts_host(String const& domain)
-{
- auto response = m_client.send_sync_but_allow_failure<Messages::WebWorkerClient::DidIsKnownHstsHost>(domain);
- return response && response->result();
-}
-
void PageHost::page_did_report_worker_exception(Utf16String const& message, Utf16String const& filename, u32 lineno, u32 colno)
{
if (auto* link = m_client.dedicated_worker_thread_link())
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index ef8ed55c206..f2b8e2f86ec 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -114,6 +114,7 @@ if (ENABLE_GUI_TARGETS)
add_subdirectory(LibWebCommon)
add_subdirectory(LibWebView)
add_subdirectory(UI)
+ add_subdirectory(WebContent)
endif()
if (ENABLE_CLANG_PLUGINS AND CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
diff --git a/Tests/WebContent/CMakeLists.txt b/Tests/WebContent/CMakeLists.txt
new file mode 100644
index 00000000000..6e771db289e
--- /dev/null
+++ b/Tests/WebContent/CMakeLists.txt
@@ -0,0 +1,8 @@
+ladybird_test_suite(WebContent)
+
+set(TEST_SOURCES
+ TestWorkerPageHost.cpp
+)
+
+target_sources(TestWebContent PRIVATE ${TEST_SOURCES})
+target_link_libraries(TestWebContent PRIVATE webcontentservice LibCore LibIPC LibJS LibThreading LibWeb LibWebCommon)
diff --git a/Tests/WebContent/TestWorkerPageHost.cpp b/Tests/WebContent/TestWorkerPageHost.cpp
new file mode 100644
index 00000000000..b5ba25093fa
--- /dev/null
+++ b/Tests/WebContent/TestWorkerPageHost.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/EventLoop.h>
+#include <LibIPC/Message.h>
+#include <LibIPC/Transport.h>
+#include <LibIPC/TransportHandle.h>
+#include <LibJS/Runtime/VM.h>
+#include <LibTest/TestCase.h>
+#include <LibThreading/Thread.h>
+#include <LibWeb/Bindings/MainThreadVM.h>
+#include <LibWeb/Platform/EventLoopPlugin.h>
+#include <WebContent/WebWorkerServerEndpoint.h>
+#include <WebContent/Worker/ConnectionFromClient.h>
+#include <WebContent/Worker/DedicatedWorkerThreadLink.h>
+#include <WebContent/Worker/PageHost.h>
+
+TEST_SETUP
+{
+ Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPlugin);
+}
+
+// Runs the callback on a thread that hosts a worker's agent. The other end of the worker's connection stands in for
+// the Hub.
+template<typename Callback>
+static void run_on_worker_thread(Callback const& callback)
+{
+ auto thread = Threading::Thread::construct("WorkerAgent"sv, [&callback] -> intptr_t {
+ IPC::ScopedSharedIOThread shared_io_thread;
+ Core::EventLoop event_loop;
+ Web::Bindings::initialize_agent(Web::HTML::AgentType::SharedWorker);
+
+ auto pair = MUST(IPC::Transport::create_paired());
+ auto client = WebWorker::ConnectionFromClient::construct(move(pair.local), nullptr);
+ callback(*client, move(pair.remote_handle));
+
+ event_loop.exec();
+ client->shut_down_agent();
+ return 0;
+ });
+ thread->set_stack_size(JS::VM::MINIMUM_THREAD_STACK_SIZE);
+ thread->start();
+ MUST(thread->join());
+}
+
+TEST_CASE(every_host_is_a_known_hsts_host_once_the_hub_is_gone)
+{
+ run_on_worker_thread([](WebWorker::ConnectionFromClient& client, IPC::TransportHandle hub) {
+ {
+ auto hung_up_hub = move(hub);
+ }
+ EXPECT(client.page_host().page_did_is_known_hsts_host("example.test"_string));
+ });
+}
+
+TEST_CASE(every_host_is_a_known_hsts_host_once_the_hub_closes_the_worker)
+{
+ run_on_worker_thread([](WebWorker::ConnectionFromClient& client, IPC::TransportHandle hub) {
+ auto hub_transport = MUST(hub.create_transport());
+ auto close_worker = MUST(Messages::WebWorkerServer::CloseWorker::static_encode());
+ MUST(close_worker.transfer_message(*hub_transport));
+ EXPECT(client.page_host().page_did_is_known_hsts_host("example.test"_string));
+ });
+}