Truly independent web browser
LibWeb: Introduce fetch client context snapshots
Luke Wildec5388a436932
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex e511aeeac16..cba41956b74 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -355,6 +355,7 @@ set(SOURCESFetch/Engine/WholeBodyTransport.cppFetch/Fetching/BodyStreamPullSource.cppFetch/Fetching/Checks.cpp+ Fetch/Fetching/ClientContextSnapshots.cppFetch/Fetching/FetchedDataReceiver.cppFetch/Fetching/NetworkBodyPump.cppFetch/Fetching/Fetching.cppdiff --git a/Libraries/LibWeb/Fetch/Fetching/ClientContextSnapshots.cpp b/Libraries/LibWeb/Fetch/Fetching/ClientContextSnapshots.cppnew file mode 100644index 00000000000..15926f7081f--- /dev/null+++ b/Libraries/LibWeb/Fetch/Fetching/ClientContextSnapshots.cpp@@ -0,0 +1,128 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibWeb/DOM/Document.h>+#include <LibWeb/Fetch/Fetching/ClientContextSnapshots.h>+#include <LibWeb/HTML/BrowsingContext.h>+#include <LibWeb/HTML/LocalNavigable.h>+#include <LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h>+#include <LibWeb/HTML/Scripting/Environments.h>+#include <LibWeb/HTML/Window.h>+#include <LibWeb/HTML/WorkerGlobalScope.h>+#include <LibWeb/MixedContent/AbstractOperations.h>++namespace Web::Fetch::Fetching {++// https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer+// NB: The steps of the "client" case that read environment, which is non-null.+static Optional<URL::URL> referrer_source_of_client(HTML::EnvironmentSettingsObject& environment)+{+ // NB: A snapshot of an environment another process hosts answers from the global object it was taken from.+ if (auto const* snapshot = as_if<HTML::EnvironmentSettingsSnapshot>(environment)) {+ if (auto const* window = snapshot->serialized_global().get_pointer<HTML::SerializedWindow>()) {+ if (snapshot->origin().is_opaque())+ return {};+ return window->associated_document.url;+ }+ return environment.creation_url;+ }++ // 2. If environment’s global object is a Window object, then+ if (auto const* window = HTML::window_from_global_object(environment.global_object())) {+ // 1. Let document be the associated Document of environment’s global object.+ auto const& document = window->associated_document();++ // 2. If document’s origin is an opaque origin, return no referrer.+ if (document.origin().is_opaque())+ return {};++ // FIXME: 3. While document is an iframe srcdoc document, let document be document’s browsing context’s+ // browsing context container’s node document.++ // 4. Let referrerSource be document’s URL.+ return document.url();+ }++ // 3. Otherwise, let referrerSource be environment’s creation URL.+ return environment.creation_url;+}++NonnullRefPtr<Infrastructure::ClientContextSnapshot const> snapshot_client_context(HTML::EnvironmentSettingsObject& client)+{+ using Context = Infrastructure::ClientContext;++ auto& global = client.global_object();+ auto const* window = HTML::window_from_global_object(global);+ auto global_kind = Context::GlobalKind::Other;+ if (window)+ global_kind = Context::GlobalKind::Window;+ else if (Bindings::worker_global_scope_from_global_object(global))+ global_kind = Context::GlobalKind::Worker;+ auto navigable = window ? window->navigable() : nullptr;+ auto document = client.responsible_document();++ return make_ref_counted<Infrastructure::ClientContextSnapshot>(Context {+ .origin = client.origin(),+ .creation_url = client.creation_url,+ .top_level_creation_url = client.top_level_creation_url,+ .top_level_origin = client.top_level_origin,+ .global_kind = global_kind,+ .is_window_of_a_top_level_navigable = navigable && navigable->is_top_level_traversable() ? Context::IsWindowOfATopLevelNavigable::Yes : Context::IsWindowOfATopLevelNavigable::No,+ .has_cross_site_ancestor = client.has_cross_site_ancestor() ? Infrastructure::HasCrossSiteAncestor::Yes : Infrastructure::HasCrossSiteAncestor::No,+ .cross_origin_isolated_capability = client.cross_origin_isolated_capability(),+ .is_secure_context = HTML::is_secure_context(client) ? Context::IsSecureContext::Yes : Context::IsSecureContext::No,+ .prohibits_mixed_security_contexts = MixedContent::does_settings_prohibit_mixed_security_contexts(client),+ .is_loading_bypassing_cache = window && window->associated_document().is_loading_bypassing_cache() ? Context::IsLoadingBypassingCache::Yes : Context::IsLoadingBypassingCache::No,+ .time_origin = client.time_origin(),+ .navigable_id = navigable ? navigable->id() : Optional<HTML::CrossProcessId> {},+ .referrer_source = referrer_source_of_client(client),+ .content_blocker_source_url = document ? document->fallback_base_url() : client.api_base_url(),+ .keepalive_quota_accountant = client.keepalive_quota_accountant(),+ });+}++static Variant<Infrastructure::ReservedClientContext::Environment, Infrastructure::ReservedClientContext::SettingsObject> reserved_client_kind(HTML::Environment const& reserved_client)+{+ using Context = Infrastructure::ReservedClientContext;++ if (auto const* settings_object = as_if<HTML::EnvironmentSettingsObject>(reserved_client)) {+ return Context::SettingsObject {+ .origin = settings_object->origin(),+ .has_cross_site_ancestor = settings_object->has_cross_site_ancestor() ? Infrastructure::HasCrossSiteAncestor::Yes : Infrastructure::HasCrossSiteAncestor::No,+ };+ }++ Context::Environment environment;+ auto const& target_browsing_context = reserved_client.target_browsing_context;+ if (!target_browsing_context)+ return environment;+ environment.target_browsing_context = target_browsing_context->is_top_level() ? Context::TargetBrowsingContext::TopLevel : Context::TargetBrowsingContext::Nested;++ auto const* document = target_browsing_context->active_document();+ auto navigable = document ? document->navigable() : nullptr;+ if (auto parent = navigable ? navigable->parent() : nullptr) {+ environment.parent = Context::Parent {+ .active_document_origin = parent->active_document_origin(),+ .active_document_has_cross_site_ancestor = parent->active_document_has_cross_site_ancestor() ? Infrastructure::HasCrossSiteAncestor::Yes : Infrastructure::HasCrossSiteAncestor::No,+ };+ }+ return environment;+}++NonnullRefPtr<Infrastructure::ReservedClientContextSnapshot const> snapshot_reserved_client_context(HTML::Environment const& reserved_client)+{+ using Context = Infrastructure::ReservedClientContext;++ return make_ref_counted<Infrastructure::ReservedClientContextSnapshot>(Context {+ .creation_url = reserved_client.creation_url,+ .top_level_creation_url = reserved_client.top_level_creation_url,+ .top_level_origin = reserved_client.top_level_origin,+ .is_secure_context = HTML::is_secure_context(reserved_client) ? Context::IsSecureContext::Yes : Context::IsSecureContext::No,+ .kind = reserved_client_kind(reserved_client),+ });+}++}diff --git a/Libraries/LibWeb/Fetch/Fetching/ClientContextSnapshots.h b/Libraries/LibWeb/Fetch/Fetching/ClientContextSnapshots.hnew file mode 100644index 00000000000..ba79dd5f9be--- /dev/null+++ b/Libraries/LibWeb/Fetch/Fetching/ClientContextSnapshots.h@@ -0,0 +1,19 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/NonnullRefPtr.h>+#include <LibWeb/Export.h>+#include <LibWeb/Fetch/Infrastructure/ClientContextSnapshot.h>+#include <LibWeb/Forward.h>++namespace Web::Fetch::Fetching {++[[nodiscard]] WEB_API NonnullRefPtr<Infrastructure::ClientContextSnapshot const> snapshot_client_context(HTML::EnvironmentSettingsObject&);+[[nodiscard]] WEB_API NonnullRefPtr<Infrastructure::ReservedClientContextSnapshot const> snapshot_reserved_client_context(HTML::Environment const&);++}diff --git a/Libraries/LibWeb/Fetch/Infrastructure/ClientContextSnapshot.h b/Libraries/LibWeb/Fetch/Infrastructure/ClientContextSnapshot.hnew file mode 100644index 00000000000..15646c8af89--- /dev/null+++ b/Libraries/LibWeb/Fetch/Infrastructure/ClientContextSnapshot.h@@ -0,0 +1,130 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/AtomicRefCounted.h>+#include <AK/NonnullRefPtr.h>+#include <AK/Optional.h>+#include <LibURL/Origin.h>+#include <LibURL/URL.h>+#include <LibWeb/Export.h>+#include <LibWeb/Fetch/Infrastructure/KeepaliveQuotaAccountant.h>+#include <LibWeb/MixedContent/ProhibitsMixedSecurityContexts.h>+#include <LibWebCommon/HTML/CrossProcessId.h>+#include <LibWebCommon/HTML/Scripting/SerializedEnvironmentSettingsObject.h>++namespace Web::Fetch::Infrastructure {++enum class HasCrossSiteAncestor : u8 {+ No,+ Yes,+};++// What fetching reads from a request's client.+struct ClientContext {+ enum class GlobalKind : u8 {+ Window,+ Worker,+ Other,+ };++ enum class IsSecureContext : u8 {+ No,+ Yes,+ };++ // Whether the global object is a Window whose navigable is non-null and has no parent.+ enum class IsWindowOfATopLevelNavigable : u8 {+ No,+ Yes,+ };++ // Whether the global object is a Window whose associated Document is loading bypassing the HTTP cache.+ enum class IsLoadingBypassingCache : u8 {+ No,+ Yes,+ };++ URL::Origin origin;+ URL::URL creation_url;+ Optional<URL::URL> top_level_creation_url;+ Optional<URL::Origin> top_level_origin;+ GlobalKind global_kind { GlobalKind::Other };+ IsWindowOfATopLevelNavigable is_window_of_a_top_level_navigable { IsWindowOfATopLevelNavigable::No };+ HasCrossSiteAncestor has_cross_site_ancestor { HasCrossSiteAncestor::No };+ HTML::CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability { HTML::CanUseCrossOriginIsolatedAPIs::No };+ IsSecureContext is_secure_context { IsSecureContext::No };+ MixedContent::ProhibitsMixedSecurityContexts prohibits_mixed_security_contexts { MixedContent::ProhibitsMixedSecurityContexts::DoesNotRestrictMixedSecurityContexts };+ IsLoadingBypassingCache is_loading_bypassing_cache { IsLoadingBypassingCache::No };+ double time_origin { 0 };++ // The navigable of the Window that is the global object, if any.+ Optional<HTML::CrossProcessId> navigable_id;++ // The URL a "client" referrer is determined from, or none for no referrer.+ Optional<URL::URL> referrer_source;++ // The URL the content blocker treats as the source of a request that is not for a document.+ URL::URL content_blocker_source_url;++ NonnullRefPtr<KeepaliveQuotaAccountant> keepalive_quota_accountant;+};++// A request's client context as it was when the fetch started.+struct WEB_API ClientContextSnapshot final : public AtomicRefCounted<ClientContextSnapshot>+ , public ClientContext {+ explicit ClientContextSnapshot(ClientContext context)+ : ClientContext(move(context))+ {+ }+};++// What fetching reads from a request's reserved client.+struct ReservedClientContext {+ using IsSecureContext = ClientContext::IsSecureContext;++ enum class TargetBrowsingContext : u8 {+ None,+ TopLevel,+ Nested,+ };++ // The active document of the parent of the target browsing context's navigable.+ struct Parent {+ Optional<URL::Origin> active_document_origin;+ HasCrossSiteAncestor active_document_has_cross_site_ancestor { HasCrossSiteAncestor::No };+ };++ // A reserved client that is an environment but not a settings object.+ struct Environment {+ TargetBrowsingContext target_browsing_context { TargetBrowsingContext::None };+ Optional<Parent> parent;+ };++ // A reserved client that is a settings object.+ struct SettingsObject {+ URL::Origin origin;+ HasCrossSiteAncestor has_cross_site_ancestor { HasCrossSiteAncestor::No };+ };++ URL::URL creation_url;+ Optional<URL::URL> top_level_creation_url;+ Optional<URL::Origin> top_level_origin;+ IsSecureContext is_secure_context { IsSecureContext::No };+ Variant<Environment, SettingsObject> kind;+};++// A request's reserved client context as it was when the reserved client was set.+struct WEB_API ReservedClientContextSnapshot final : public AtomicRefCounted<ReservedClientContextSnapshot>+ , public ReservedClientContext {+ explicit ReservedClientContextSnapshot(ReservedClientContext context)+ : ReservedClientContext(move(context))+ {+ }+};++}diff --git a/Libraries/LibWeb/MixedContent/AbstractOperations.h b/Libraries/LibWeb/MixedContent/AbstractOperations.hindex 9bc027200c4..400723d9e90 100644--- a/Libraries/LibWeb/MixedContent/AbstractOperations.h+++ b/Libraries/LibWeb/MixedContent/AbstractOperations.h@@ -10,16 +10,12 @@#include <LibWeb/Export.h>#include <LibWeb/Fetch/Infrastructure/RequestOrResponseBlocking.h>#include <LibWeb/HTML/Scripting/Environments.h>+#include <LibWeb/MixedContent/ProhibitsMixedSecurityContexts.h>namespace Web::MixedContent {WEB_API void upgrade_a_mixed_content_request_to_a_potentially_trustworthy_url_if_appropriate(Fetch::Infrastructure::Request&);-enum class ProhibitsMixedSecurityContexts {- ProhibitsMixedSecurityContexts,- DoesNotRestrictMixedSecurityContexts,-};-ProhibitsMixedSecurityContexts does_settings_prohibit_mixed_security_contexts(GC::Ptr<HTML::EnvironmentSettingsObject>);WEB_API Fetch::Infrastructure::RequestOrResponseBlocking should_fetching_request_be_blocked_as_mixed_content(Fetch::Infrastructure::Request&);diff --git a/Libraries/LibWeb/MixedContent/ProhibitsMixedSecurityContexts.h b/Libraries/LibWeb/MixedContent/ProhibitsMixedSecurityContexts.hnew file mode 100644index 00000000000..0308d56ec57--- /dev/null+++ b/Libraries/LibWeb/MixedContent/ProhibitsMixedSecurityContexts.h@@ -0,0 +1,19 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Types.h>++namespace Web::MixedContent {++// https://w3c.github.io/webappsec-mixed-content/#categorize-settings-object+enum class ProhibitsMixedSecurityContexts : u8 {+ ProhibitsMixedSecurityContexts,+ DoesNotRestrictMixedSecurityContexts,+};++}