LibWeb/CSP: Implement the style-src-elem directive

This commit is contained in:
Luke Wilde 2024-12-03 17:16:27 +00:00 committed by Shannon Booth
commit 574b736156
Notes: github-actions[bot] 2025-07-17 23:59:17 +00:00
5 changed files with 126 additions and 0 deletions

View file

@ -61,6 +61,7 @@ set(SOURCES
ContentSecurityPolicy/Directives/SerializedDirective.cpp
ContentSecurityPolicy/Directives/SourceExpression.cpp
ContentSecurityPolicy/Directives/StyleSourceDirective.cpp
ContentSecurityPolicy/Directives/StyleSourceElementDirective.cpp
ContentSecurityPolicy/Policy.cpp
ContentSecurityPolicy/PolicyList.cpp
ContentSecurityPolicy/SecurityPolicyViolationEvent.cpp

View file

@ -19,6 +19,7 @@
#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.h>
namespace Web::ContentSecurityPolicy::Directives {
@ -57,6 +58,9 @@ GC::Ref<Directive> create_directive(GC::Heap& heap, String name, Vector<String>
if (name == Names::StyleSrc)
return heap.allocate<StyleSourceDirective>(move(name), move(value));
if (name == Names::StyleSrcElem)
return heap.allocate<StyleSourceElementDirective>(move(name), move(value));
return heap.allocate<Directive>(move(name), move(value));
}

View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
#include <LibWeb/ContentSecurityPolicy/Directives/StyleSourceElementDirective.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
namespace Web::ContentSecurityPolicy::Directives {
GC_DEFINE_ALLOCATOR(StyleSourceElementDirective);
StyleSourceElementDirective::StyleSourceElementDirective(String name, Vector<String> value)
: Directive(move(name), move(value))
{
}
// https://w3c.github.io/webappsec-csp/#style-src-elem-pre-request
Directive::Result StyleSourceElementDirective::pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Policy const> policy) const
{
// 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
auto name = get_the_effective_directive_for_request(request);
// 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
// return "Allowed".
if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
return Result::Allowed;
// 3. If the result of executing § 6.7.2.3 Does nonce match source list? on requests cryptographic nonce metadata
// and this directives value is "Matches", return "Allowed".
if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), value()) == MatchResult::Matches)
return Result::Allowed;
// 4. If the result of executing § 6.7.2.5 Does request match source list? on request, this directives value, and
// policy, is "Does Not Match", return "Blocked".
if (does_request_match_source_list(request, value(), policy) == MatchResult::DoesNotMatch)
return Result::Blocked;
// 5. Return "Allowed".
return Result::Allowed;
}
// https://w3c.github.io/webappsec-csp/#style-src-elem-post-request
Directive::Result StyleSourceElementDirective::post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const> request, GC::Ref<Fetch::Infrastructure::Response const> response, GC::Ref<Policy const> policy) const
{
// 1. Let name be the result of executing § 6.8.1 Get the effective directive for request on request.
auto name = get_the_effective_directive_for_request(request);
// 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
// return "Allowed".
if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
return Result::Allowed;
// 3. If the result of executing § 6.7.2.3 Does nonce match source list? on requests cryptographic nonce metadata
// and this directives value is "Matches", return "Allowed".
if (does_nonce_match_source_list(request->cryptographic_nonce_metadata(), value()) == MatchResult::Matches)
return Result::Allowed;
// 4. If the result of executing § 6.7.2.6 Does response to request match source list? on response, request, this
// directives value, and policy, is "Does Not Match", return "Blocked".
if (does_response_match_source_list(response, request, value(), policy) == MatchResult::DoesNotMatch)
return Result::Blocked;
// 5. Return "Allowed".
return Result::Allowed;
}
// https://w3c.github.io/webappsec-csp/#style-src-elem-inline
Directive::Result StyleSourceElementDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, String const& source) const
{
// 1. Let name be the result of executing § 6.8.2 Get the effective directive for inline checks on type.
auto name = get_the_effective_directive_for_inline_checks(type);
// 2. If the result of executing § 6.8.4 Should fetch directive execute on name, style-src-elem and policy is "No",
// return "Allowed".
if (should_fetch_directive_execute(name, Names::StyleSrcElem, policy) == ShouldExecute::No)
return Result::Allowed;
// 3. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
// directives value, type, and source, is "Does Not Match", return "Blocked".
if (does_element_match_source_list_for_type_and_source(element, value(), type, source) == MatchResult::DoesNotMatch)
return Result::Blocked;
// 4. Return "Allowed".
return Result::Allowed;
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
namespace Web::ContentSecurityPolicy::Directives {
// https://w3c.github.io/webappsec-csp/#directive-style-src-elem
class StyleSourceElementDirective final : public Directive {
GC_CELL(StyleSourceElementDirective, Directive)
GC_DECLARE_ALLOCATOR(StyleSourceElementDirective);
public:
virtual ~StyleSourceElementDirective() = default;
virtual Result pre_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Policy const>) const override;
virtual Result post_request_check(GC::Heap&, GC::Ref<Fetch::Infrastructure::Request const>, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<Policy const>) const override;
virtual Result inline_check(GC::Heap&, GC::Ptr<DOM::Element const>, InlineType, GC::Ref<Policy const>, String const&) const override;
private:
StyleSourceElementDirective(String name, Vector<String> value);
};
}

View file

@ -144,6 +144,7 @@ class ScriptSourceAttributeDirective;
class ScriptSourceDirective;
class ScriptSourceElementDirective;
class StyleSourceDirective;
class StyleSourceElementDirective;
struct SerializedDirective;
}