From d97b09693e42c03c72aeb22411273d484a056b51 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 28 Aug 2023 11:57:21 +0200 Subject: [PATCH] LibWeb: Convert SandboxingFlagSet into a enum class Instead of having a nested enum within a struct, use the macro AK_ENUM_BITWISE_OPERATORS to add all the convienent has_flag free functions and such for ease of use. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 6 +-- Userland/Libraries/LibWeb/Forward.h | 2 +- .../Libraries/LibWeb/HTML/BrowsingContext.cpp | 16 +++---- .../Libraries/LibWeb/HTML/HTMLFormElement.cpp | 2 +- .../LibWeb/HTML/HTMLMediaElement.cpp | 2 +- Userland/Libraries/LibWeb/HTML/Navigable.cpp | 2 +- .../Libraries/LibWeb/HTML/NavigationParams.h | 2 +- .../Libraries/LibWeb/HTML/SandboxingFlagSet.h | 46 +++++++++---------- .../LibWeb/HTML/SourceSnapshotParams.h | 2 +- 9 files changed, 39 insertions(+), 41 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 3ad87ce8296..88915bb43d2 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -133,7 +133,7 @@ static JS::NonnullGCPtr obtain_a_browsing_context_to_use_ // cross-origin isolation mode to either "logical" or "concrete". The choice of which is implementation-defined. // 5. If sandboxFlags is not empty, then: - if (!sandbox_flags.is_empty()) { + if (!is_empty(sandbox_flags)) { // 1. Assert navigationCOOP's value is "unsafe-none". VERIFY(navigation_coop.value == HTML::CrossOriginOpenerPolicyValue::UnsafeNone); @@ -2535,7 +2535,7 @@ HTML::SourceSnapshotParams Document::snapshot_source_snapshot_params() const return HTML::SourceSnapshotParams { .has_transient_activation = verify_cast(HTML::relevant_global_object(*this)).has_transient_activation(), .sandboxing_flags = m_active_sandboxing_flag_set, - .allows_downloading = (m_active_sandboxing_flag_set.flags & HTML::SandboxingFlagSet::SandboxedDownloads) != HTML::SandboxingFlagSet::SandboxedDownloads, + .allows_downloading = !has_flag(m_active_sandboxing_flag_set, HTML::SandboxingFlagSet::SandboxedDownloads), .fetch_client = relevant_settings_object(), .source_policy_container = m_policy_container }; @@ -3475,7 +3475,7 @@ void Document::shared_declarative_refresh_steps(StringView input, JS::GCPtr url, SandboxingFlagSet sandbox_flags, Optional invocation_origin) { // 1. If sandboxFlags has its sandboxed origin browsing context flag set, then return a new opaque origin. - if (sandbox_flags.flags & SandboxingFlagSet::SandboxedOrigin) { + if (has_flag(sandbox_flags, SandboxingFlagSet::SandboxedOrigin)) { return HTML::Origin {}; } @@ -80,7 +80,7 @@ HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optio HTML::Origin determine_the_origin(AK::URL const& url, SandboxingFlagSet sandbox_flags, Optional source_origin, Optional container_origin) { // 1. If sandboxFlags has its sandboxed origin browsing context flag set, then return a new opaque origin. - if (sandbox_flags.flags & SandboxingFlagSet::SandboxedOrigin) { + if (has_flag(sandbox_flags, SandboxingFlagSet::SandboxedOrigin)) { return HTML::Origin {}; } @@ -135,7 +135,7 @@ JS::NonnullGCPtr BrowsingContext::create_a_new_browsing_context } // FIXME: 4. Let sandboxFlags be the result of determining the creation sandboxing flags given browsingContext and embedded. - SandboxingFlagSet sandbox_flags; + SandboxingFlagSet sandbox_flags = {}; // 5. Let origin be the result of determining the origin given browsingContext, about:blank, sandboxFlags, and browsingContext's creator origin. auto origin = determine_the_origin(*browsing_context, AK::URL("about:blank"), sandbox_flags, browsing_context->m_creator_origin); @@ -311,7 +311,7 @@ WebIDL::ExceptionOr BrowsingContext } // FIXME: 5. Let sandboxFlags be the result of determining the creation sandboxing flags given browsingContext and embedder. - SandboxingFlagSet sandbox_flags; + SandboxingFlagSet sandbox_flags = {}; // 6. Let origin be the result of determining the origin given about:blank, sandboxFlags, creatorOrigin, and null. auto origin = determine_the_origin(AK::URL("about:blank"sv), sandbox_flags, creator_origin, {}); @@ -890,7 +890,7 @@ BrowsingContext::ChosenBrowsingContext BrowsingContext::choose_a_browsing_contex } // --> If sandboxingFlagSet has the sandboxed auxiliary navigation browsing context flag set - else if (sandboxing_flag_set.flags & SandboxingFlagSet::SandboxedAuxiliaryNavigation) { + else if (has_flag(sandboxing_flag_set, SandboxingFlagSet::SandboxedAuxiliaryNavigation)) { // FIXME: The user agent may report to a developer console that a popup has been blocked. dbgln("Pop-up blocked!"); } @@ -1445,7 +1445,7 @@ bool BrowsingContext::is_allowed_to_navigate(BrowsingContext const& other) const // and A's active document's active sandboxing flag set has its sandboxed top-level navigation with user activation browsing context flag set, // then return false. if (active_window()->has_transient_activation() - && active_document()->active_sandboxing_flag_set().flags & SandboxingFlagSet::SandboxedTopLevelNavigationWithUserActivation) { + && has_flag(active_document()->active_sandboxing_flag_set(), SandboxingFlagSet::SandboxedTopLevelNavigationWithUserActivation)) { return false; } @@ -1453,7 +1453,7 @@ bool BrowsingContext::is_allowed_to_navigate(BrowsingContext const& other) const // and A's active document's active sandboxing flag set has its sandboxed top-level navigation without user activation browsing context flag set, // then return false. if (!active_window()->has_transient_activation() - && active_document()->active_sandboxing_flag_set().flags & SandboxingFlagSet::SandboxedTopLevelNavigationWithoutUserActivation) { + && has_flag(active_document()->active_sandboxing_flag_set(), SandboxingFlagSet::SandboxedTopLevelNavigationWithoutUserActivation)) { return false; } } @@ -1466,7 +1466,7 @@ bool BrowsingContext::is_allowed_to_navigate(BrowsingContext const& other) const if (other.is_top_level() && &other != this && !other.is_ancestor_of(*this) - && active_document()->active_sandboxing_flag_set().flags & SandboxingFlagSet::SandboxedNavigation + && has_flag(active_document()->active_sandboxing_flag_set(), SandboxingFlagSet::SandboxedNavigation) && this != other.the_one_permitted_sandboxed_navigator()) { return false; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp index 89bc6de3ed0..eee3fdfaaaf 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp @@ -74,7 +74,7 @@ WebIDL::ExceptionOr HTMLFormElement::submit_form(JS::NonnullGCPtrbrowsing_context(); // 4. If form document's active sandboxing flag set has its sandboxed forms browsing context flag set, then return. - if (form_document->active_sandboxing_flag_set().flags & HTML::SandboxingFlagSet::Flag::SandboxedForms) + if (has_flag(form_document->active_sandboxing_flag_set(), HTML::SandboxingFlagSet::SandboxedForms)) return {}; // 5. If the submitted from submit() method flag is not set, then: diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp index 643630e0860..1726c459eab 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp @@ -1655,7 +1655,7 @@ bool HTMLMediaElement::is_eligible_for_autoplay() const has_attribute(HTML::AttributeNames::autoplay) && // Its node document's active sandboxing flag set does not have the sandboxed automatic features browsing context flag set. - (document().active_sandboxing_flag_set().flags & SandboxingFlagSet::SandboxedAutomaticFeatures) == 0 && + !has_flag(document().active_sandboxing_flag_set(), SandboxingFlagSet::SandboxedAutomaticFeatures) && // Its node document is allowed to use the "autoplay" feature. document().is_allowed_to_use_feature(DOM::PolicyControlledFeature::Autoplay)); diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index 41deeb57928..3dfadd84a99 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -490,7 +490,7 @@ static WebIDL::ExceptionOr> create_navigation_params_ JS::GCPtr fetch_controller = nullptr; // 13. Let finalSandboxFlags be an empty sandboxing flag set. - SandboxingFlagSet final_sandbox_flags; + SandboxingFlagSet final_sandbox_flags = {}; // 16. Let locationURL be null. ErrorOr> location_url { OptionalNone {} }; diff --git a/Userland/Libraries/LibWeb/HTML/NavigationParams.h b/Userland/Libraries/LibWeb/HTML/NavigationParams.h index c239aeb16ee..a9a3384b6fd 100644 --- a/Userland/Libraries/LibWeb/HTML/NavigationParams.h +++ b/Userland/Libraries/LibWeb/HTML/NavigationParams.h @@ -36,7 +36,7 @@ struct NavigationParams { PolicyContainer policy_container; // a sandboxing flag set to impose on the new Document - SandboxingFlagSet final_sandboxing_flag_set; + SandboxingFlagSet final_sandboxing_flag_set = {}; // a cross-origin opener policy to use for the new Document CrossOriginOpenerPolicy cross_origin_opener_policy; diff --git a/Userland/Libraries/LibWeb/HTML/SandboxingFlagSet.h b/Userland/Libraries/LibWeb/HTML/SandboxingFlagSet.h index 1e16261d8a3..23a38525006 100644 --- a/Userland/Libraries/LibWeb/HTML/SandboxingFlagSet.h +++ b/Userland/Libraries/LibWeb/HTML/SandboxingFlagSet.h @@ -6,35 +6,33 @@ #pragma once +#include #include namespace Web::HTML { // https://html.spec.whatwg.org/multipage/origin.html#sandboxing-flag-set -struct SandboxingFlagSet { - enum Flag { - SandboxedNavigation = 1u << 0u, - SandboxedAuxiliaryNavigation = 1u << 1u, - SandboxedTopLevelNavigationWithoutUserActivation = 1u << 2u, - SandboxedTopLevelNavigationWithUserActivation = 1u << 3u, - SandboxedPlugins = 1u << 4u, - SandboxedOrigin = 1u << 5u, - SandboxedForms = 1u << 6u, - SandboxedPointerLock = 1u << 7u, - SandboxedScripts = 1u << 8u, - SandboxedAutomaticFeatures = 1u << 9u, - SandboxedDocumentDomain = 1u << 10u, - SandboxPropagatesToAuxiliaryBrowsingContexts = 1u << 11u, - SandboxedModals = 1u << 12u, - SandboxedOrientationLock = 1u << 13u, - SandboxedPresentation = 1u << 14u, - SandboxedDownloads = 1u << 15u, - SandboxedCustomProtocols = 1u << 16u, - }; - - bool is_empty() const { return flags == 0; } - - u32 flags { 0 }; +enum class SandboxingFlagSet { + SandboxedNavigation = 1u << 0u, + SandboxedAuxiliaryNavigation = 1u << 1u, + SandboxedTopLevelNavigationWithoutUserActivation = 1u << 2u, + SandboxedTopLevelNavigationWithUserActivation = 1u << 3u, + SandboxedPlugins = 1u << 4u, + SandboxedOrigin = 1u << 5u, + SandboxedForms = 1u << 6u, + SandboxedPointerLock = 1u << 7u, + SandboxedScripts = 1u << 8u, + SandboxedAutomaticFeatures = 1u << 9u, + SandboxedDocumentDomain = 1u << 10u, + SandboxPropagatesToAuxiliaryBrowsingContexts = 1u << 11u, + SandboxedModals = 1u << 12u, + SandboxedOrientationLock = 1u << 13u, + SandboxedPresentation = 1u << 14u, + SandboxedDownloads = 1u << 15u, + SandboxedCustomProtocols = 1u << 16u, }; +AK_ENUM_BITWISE_OPERATORS(SandboxingFlagSet); +inline bool is_empty(SandboxingFlagSet s) { return (to_underlying(s) & 0x1FFU) == 0; } + } diff --git a/Userland/Libraries/LibWeb/HTML/SourceSnapshotParams.h b/Userland/Libraries/LibWeb/HTML/SourceSnapshotParams.h index 8dc3e974dcf..52fbe887e8e 100644 --- a/Userland/Libraries/LibWeb/HTML/SourceSnapshotParams.h +++ b/Userland/Libraries/LibWeb/HTML/SourceSnapshotParams.h @@ -17,7 +17,7 @@ struct SourceSnapshotParams { bool has_transient_activation; // a sandboxing flag set - SandboxingFlagSet sandboxing_flags; + SandboxingFlagSet sandboxing_flags = {}; // a boolean bool allows_downloading;