Skip to content

Truly independent web browser

LibWeb: Upgrade WebSocket connections to Known HSTS Hosts to wss:

Andreas Klingec6dcd843e09
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 693cb18510f..08154c5b045 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -607,6 +607,27 @@ static bool is_request_from_an_svg_image(Infrastructure::Request const& request)
return window && window->associated_document().is_decoded_svg();
}
+// https://fetch.spec.whatwg.org/#concept-main-fetch
+// NB: This is step 10 of main fetch. The WebSocket opening handshake does not go through main fetch yet, so it runs this
+// step on its own.
+void upgrade_request_to_https_for_known_hsts_host_if_appropriate(Page& page, Infrastructure::Request& request)
+{
+ // 10. Set request’s current URL’s scheme to "https" if all of the following conditions are true:
+ if (
+ // - request’s current URL’s scheme is "http"
+ request.current_url().scheme() == "http"sv
+ // - request’s current URL’s host is a domain
+ && request.current_url().host().has_value() && request.current_url().host()->is_domain()
+ // - Matching request’s current URL’s host per Known HSTS Host Domain Name Matching results in either a
+ // superdomain match with an asserted includeSubDomains directive or a congruent match (with or without an
+ // asserted includeSubDomains directive) [HSTS]
+ && ResourceLoader::is_known_hsts_host(page, request.current_url().host()->get<String>())
+ // FIXME: or DNS resolution for the request finds a matching HTTPS RR per section 9.5 of [SVCB].
+ ) {
+ request.current_url().set_scheme("https"sv);
+ }
+}
+
// https://fetch.spec.whatwg.org/#concept-main-fetch
GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Recursive recursive)
{
@@ -672,19 +693,7 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParam
}
// 10. Set request’s current URL’s scheme to "https" if all of the following conditions are true:
- if (
- // - request’s current URL’s scheme is "http"
- request->current_url().scheme() == "http"sv
- // - request’s current URL’s host is a domain
- && request->current_url().host().has_value() && request->current_url().host()->is_domain()
- // - Matching request’s current URL’s host per Known HSTS Host Domain Name Matching results in either a
- // superdomain match with an asserted includeSubDomains directive or a congruent match (with or without an
- // asserted includeSubDomains directive) [HSTS]
- && ResourceLoader::is_known_hsts_host(Bindings::principal_host_defined_page(realm), request->current_url().host()->get<String>())
- // FIXME: or DNS resolution for the request finds a matching HTTPS RR per section 9.5 of [SVCB].
- ) {
- request->current_url().set_scheme("https"sv);
- }
+ upgrade_request_to_https_for_known_hsts_host_if_appropriate(Bindings::principal_host_defined_page(realm), *request);
auto get_response = GC::create_function<GC::Ref<PendingResponse>()>(GC::Heap::the(), GC::captures(realm, NonnullRefPtr { fetch_params }, fetch_params.controller(), request), [](auto& realm, NonnullRefPtr<Infrastructure::FetchParams const>& fetch_params, GC::Ref<Infrastructure::FetchController>& controller, NonnullRefPtr<Infrastructure::Request>& request) -> GC::Ref<PendingResponse> {
dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' get_response() function");
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.h b/Libraries/LibWeb/Fetch/Fetching/Fetching.h
index 3e430c05fa6..a26d661e489 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.h
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.h
@@ -52,6 +52,7 @@ ENUMERATE_BOOL_PARAMS
WEB_API GC::Ref<Infrastructure::FetchController> fetch(JS::Realm&, Infrastructure::Request&, Infrastructure::FetchAlgorithms const&, UseParallelQueue use_parallel_queue = UseParallelQueue::No, CreateResponseBodyTransferLease = CreateResponseBodyTransferLease::No);
GC::Ptr<PendingResponse> main_fetch(JS::Realm&, Infrastructure::FetchParams const&, Recursive recursive = Recursive::No);
+void upgrade_request_to_https_for_known_hsts_host_if_appropriate(Page&, Infrastructure::Request&);
// Runs the in-parallel steps that main fetch deferred under the id.
void run_deferred_main_fetch_steps(Engine::FetchId, NonnullRefPtr<Engine::FetchEngineRecord>);
void populate_request_from_client(Infrastructure::Request&, Optional<HTML::FetchClient> const& client);
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cpp
index 71f70cad31a..a36fec55577 100644
--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp
+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp
@@ -13,6 +13,7 @@
#include <LibJS/Runtime/VM.h>
#include <LibRequests/RequestClient.h>
#include <LibURL/Origin.h>
+#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
@@ -23,6 +24,7 @@
#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Fetching/ClientContextSnapshots.h>
+#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/NetworkPartitionKey.h>
#include <LibWeb/Fetch/Infrastructure/PortBlocking.h>
@@ -195,7 +197,8 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
request->set_redirect_mode(Fetch::Infrastructure::Request::RedirectMode::Error);
// FIXME: Fetch request as per the remaining steps of https://websockets.spec.whatwg.org/#concept-websocket-establish
- // AD-HOC: Until the opening handshake goes through fetch, run the blocking checks of main fetch here.
+ // AD-HOC: Until the opening handshake goes through fetch, run the blocking checks and the HSTS upgrade of main
+ // fetch here.
request->set_policy_container(client.policy_container()->clone());
request->set_client_snapshot(Fetch::Fetching::snapshot_client_context(client));
auto report_violation = ContentSecurityPolicy::violation_reporter_for_request(client.realm(), request);
@@ -207,6 +210,8 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
return Error::from_string_literal("WebSocket connection was blocked");
}
+ Fetch::Fetching::upgrade_request_to_https_for_known_hsts_host_if_appropriate(Bindings::principal_host_defined_page(client.realm()), *request);
+
if (m_ready_state != Requests::WebSocket::ReadyState::Connecting)
return Error::from_string_literal("The WebSocket was closed before it connected");
diff --git a/Tests/LibWeb/Text/expected/HSTS/hsts-websocket-upgrade.txt b/Tests/LibWeb/Text/expected/HSTS/hsts-websocket-upgrade.txt
index 06baf589ffd..e3bd2d67c67 100644
--- a/Tests/LibWeb/Text/expected/HSTS/hsts-websocket-upgrade.txt
+++ b/Tests/LibWeb/Text/expected/HSTS/hsts-websocket-upgrade.txt
@@ -1,4 +1,4 @@
Window without HSTS policy: failed
-Window with HSTS policy: failed
+Window with HSTS policy: opened
Worker without HSTS policy: failed
-Worker with HSTS policy: failed
+Worker with HSTS policy: opened