LibWeb/CSP: Implement the frame-ancestors directive

This commit is contained in:
Luke Wilde 2024-12-04 12:36:18 +00:00 committed by Alexander Kalenik
commit febe4fdb46
Notes: github-actions[bot] 2025-08-06 22:46:53 +00:00
5 changed files with 110 additions and 0 deletions

View file

@ -51,6 +51,7 @@ set(SOURCES
ContentSecurityPolicy/Directives/DirectiveOperations.cpp
ContentSecurityPolicy/Directives/FontSourceDirective.cpp
ContentSecurityPolicy/Directives/FormActionDirective.cpp
ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
ContentSecurityPolicy/Directives/FrameSourceDirective.cpp
ContentSecurityPolicy/Directives/ImageSourceDirective.cpp
ContentSecurityPolicy/Directives/KeywordSources.cpp

View file

@ -12,6 +12,7 @@
#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h>
#include <LibWeb/ContentSecurityPolicy/Directives/FontSourceDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/FormActionDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/FrameSourceDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/ImageSourceDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/ManifestSourceDirective.h>
@ -45,6 +46,9 @@ GC::Ref<Directive> create_directive(GC::Heap& heap, String name, Vector<String>
if (name == Names::FormAction)
return heap.allocate<FormActionDirective>(move(name), move(value));
if (name == Names::FrameAncestors)
return heap.allocate<FrameAncestorsDirective>(move(name), move(value));
if (name == Names::FrameSrc)
return heap.allocate<FrameSourceDirective>(move(name), move(value));

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibURL/Parser.h>
#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>
#include <LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.h>
#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/Navigable.h>
namespace Web::ContentSecurityPolicy::Directives {
GC_DEFINE_ALLOCATOR(FrameAncestorsDirective);
FrameAncestorsDirective::FrameAncestorsDirective(String name, Vector<String> value)
: Directive(move(name), move(value))
{
}
// https://w3c.github.io/webappsec-csp/#frame-ancestors-navigation-response
Directive::Result FrameAncestorsDirective::navigation_response_check(GC::Ref<Fetch::Infrastructure::Request const>, NavigationType, GC::Ref<Fetch::Infrastructure::Response const> navigation_response, GC::Ref<HTML::Navigable const> target, CheckType check_type, GC::Ref<Policy const> policy) const
{
// 1. If navigation responses URL is local, return "Allowed".
VERIFY(navigation_response->url().has_value());
if (Fetch::Infrastructure::is_local_url(navigation_response->url().value()))
return Result::Allowed;
// 2. Assert: request, navigation response, and navigation type, are unused from this point forward in this
// algorithm, as frame-ancestors is concerned only with navigation responses frame-ancestors directive.
// 3. If check type is "source", return "Allowed".
// Spec Note: The 'frame-ancestors' directive is relevant only to the target navigable and it has no impact on the
// requests context.
if (check_type == CheckType::Source)
return Result::Allowed;
// 4. If target is not a child navigable, return "Allowed".
if (!target->parent())
return Result::Allowed;
// 5. Let current be target.
auto current = target;
// 6. While current is a child navigable:
while (current->parent()) {
// 1. Let document be currents container document.
auto document = current->container_document();
VERIFY(document);
// 2. Let origin be the result of executing the URL parser on the ASCII serialization of documents origin.
auto origin = DOMURL::parse(document->origin().serialize());
// FIXME: What do we do if origin is invalid here?
VERIFY(origin.has_value());
// 3. If § 6.7.2.7 Does url match source list in origin with redirect count? returns Does Not Match when
// executed upon origin, this directives value, policys self-origin, and 0, return "Blocked".
if (does_url_match_source_list_in_origin_with_redirect_count(origin.value(), value(), policy->self_origin(), 0) == MatchResult::DoesNotMatch)
return Result::Blocked;
// 4. Set current to documents node navigable.
VERIFY(document->navigable());
current = *document->navigable();
}
// 7. Return "Allowed".
return Result::Allowed;
}
}

View file

@ -0,0 +1,27 @@
/*
* 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-frame-ancestors
class FrameAncestorsDirective final : public Directive {
GC_CELL(FrameAncestorsDirective, Directive)
GC_DECLARE_ALLOCATOR(FrameAncestorsDirective);
public:
virtual ~FrameAncestorsDirective() = default;
virtual Result navigation_response_check(GC::Ref<Fetch::Infrastructure::Request const>, NavigationType, GC::Ref<Fetch::Infrastructure::Response const>, GC::Ref<HTML::Navigable const>, CheckType, GC::Ref<Policy const>) const override;
private:
FrameAncestorsDirective(String name, Vector<String> value);
};
}

View file

@ -142,6 +142,7 @@ class DefaultSourceDirective;
class Directive;
class FontSourceDirective;
class FormActionDirective;
class FrameAncestorsDirective;
class FrameSourceDirective;
class ImageSourceDirective;
class ManifestSourceDirective;