mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 12:49:19 +00:00
LibWeb/CSP: Implement the script-src-elem directive
This commit is contained in:
parent
caf45f2317
commit
f382bccc3d
Notes:
github-actions[bot]
2025-07-12 01:07:54 +00:00
Author: https://github.com/Lubrsi
Commit: f382bccc3d
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5404
Reviewed-by: https://github.com/shannonbooth ✅
5 changed files with 111 additions and 0 deletions
|
@ -56,6 +56,7 @@ set(SOURCES
|
|||
ContentSecurityPolicy/Directives/Names.cpp
|
||||
ContentSecurityPolicy/Directives/ObjectSourceDirective.cpp
|
||||
ContentSecurityPolicy/Directives/ScriptSourceDirective.cpp
|
||||
ContentSecurityPolicy/Directives/ScriptSourceElementDirective.cpp
|
||||
ContentSecurityPolicy/Directives/SerializedDirective.cpp
|
||||
ContentSecurityPolicy/Directives/SourceExpression.cpp
|
||||
ContentSecurityPolicy/Policy.cpp
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
|
||||
#include <LibWeb/ContentSecurityPolicy/Directives/ObjectSourceDirective.h>
|
||||
#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceDirective.h>
|
||||
#include <LibWeb/ContentSecurityPolicy/Directives/ScriptSourceElementDirective.h>
|
||||
|
||||
namespace Web::ContentSecurityPolicy::Directives {
|
||||
|
||||
|
@ -45,6 +46,9 @@ GC::Ref<Directive> create_directive(GC::Heap& heap, String name, Vector<String>
|
|||
if (name == Names::ScriptSrc)
|
||||
return heap.allocate<ScriptSourceDirective>(move(name), move(value));
|
||||
|
||||
if (name == Names::ScriptSrcElem)
|
||||
return heap.allocate<ScriptSourceElementDirective>(move(name), move(value));
|
||||
|
||||
return heap.allocate<Directive>(move(name), move(value));
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* 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/ScriptSourceElementDirective.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
|
||||
|
||||
namespace Web::ContentSecurityPolicy::Directives {
|
||||
|
||||
GC_DEFINE_ALLOCATOR(ScriptSourceElementDirective);
|
||||
|
||||
ScriptSourceElementDirective::ScriptSourceElementDirective(String name, Vector<String> value)
|
||||
: Directive(move(name), move(value))
|
||||
{
|
||||
}
|
||||
|
||||
// https://w3c.github.io/webappsec-csp/#script-src-elem-pre-request
|
||||
Directive::Result ScriptSourceElementDirective::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, script-src-elem and policy is "No",
|
||||
// return "Allowed".
|
||||
if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
|
||||
return Result::Allowed;
|
||||
|
||||
// 3. Return the result of executing § 6.7.1.1 Script directives pre-request check on request, this directive,
|
||||
// and policy.
|
||||
return script_directives_pre_request_check(request, *this, policy);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/webappsec-csp/#script-src-elem-post-request
|
||||
Directive::Result ScriptSourceElementDirective::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, script-src-elem and policy is "No",
|
||||
// return "Allowed".
|
||||
if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
|
||||
return Result::Allowed;
|
||||
|
||||
// 3. Return the result of executing § 6.7.1.2 Script directives post-request check on request, response, this
|
||||
// directive, and policy.
|
||||
return script_directives_post_request_check(request, response, *this, policy);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/webappsec-csp/#script-src-elem-inline
|
||||
Directive::Result ScriptSourceElementDirective::inline_check(GC::Heap&, GC::Ptr<DOM::Element const> element, InlineType type, GC::Ref<Policy const> policy, String const& source) const
|
||||
{
|
||||
// 1. Assert: element is not null or type is "navigation".
|
||||
VERIFY(element || type == InlineType::Navigation);
|
||||
|
||||
// 2. 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);
|
||||
|
||||
// 3. If the result of executing § 6.8.4 Should fetch directive execute on name, script-src-elem and policy is "No",
|
||||
// return "Allowed".
|
||||
if (should_fetch_directive_execute(name, Names::ScriptSrcElem, policy) == ShouldExecute::No)
|
||||
return Result::Allowed;
|
||||
|
||||
// 4. If the result of executing § 6.7.3.3 Does element match source list for type and source? on element, this
|
||||
// directive’s 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;
|
||||
|
||||
// 5. Return "Allowed".
|
||||
return Result::Allowed;
|
||||
}
|
||||
|
||||
}
|
|
@ -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-script-src-elem
|
||||
class ScriptSourceElementDirective final : public Directive {
|
||||
GC_CELL(ScriptSourceElementDirective, Directive)
|
||||
GC_DECLARE_ALLOCATOR(ScriptSourceElementDirective);
|
||||
|
||||
public:
|
||||
virtual ~ScriptSourceElementDirective() = 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:
|
||||
ScriptSourceElementDirective(String name, Vector<String> value);
|
||||
};
|
||||
|
||||
}
|
|
@ -141,6 +141,7 @@ class ManifestSourceDirective;
|
|||
class MediaSourceDirective;
|
||||
class ObjectSourceDirective;
|
||||
class ScriptSourceDirective;
|
||||
class ScriptSourceElementDirective;
|
||||
struct SerializedDirective;
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue