dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / content_security_policy / directives / frame_ancestors_directive.lucb
#==============================================================================================
#
# frame_ancestors_directive - ContentSecurityPolicy::Directives::FrameAncestorsDirective
# (the frame-ancestors directive): its navigation response check of the target's ancestors
#
# DESCRIPTION:
# Port of Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
# (lines 1-79).
#
# Copyright (c) 2024, Luke Wilde <luke@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp:22 Web::ContentSecurityPolicy::Directives::FrameAncestorsDirective::FrameAncestorsDirective.
func content_security_policy_frame_ancestors_directive_construct(this: ContentSecurityPolicyFrameAncestorsDirective*, name: ak.String, value: ak.Vector[ak.String]):
content_security_policy_directive_construct(this.content_security_policy_directive(), name, value)
## Ported from Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp:28 Web::ContentSecurityPolicy::Directives::FrameAncestorsDirective::navigation_response_check (override).
# https://w3c.github.io/webappsec-csp/#frame-ancestors-navigation-response
pub func content_security_policy_frame_ancestors_directive_navigation_response_check_impl(base: ContentSecurityPolicyDirective*, arg0: FetchRequest*, arg1: ContentSecurityPolicyNavigationType, navigation_response: FetchResponse*, target: HtmlNavigable*, check_type: ContentSecurityPolicyDirectiveCheckType, policy: ContentSecurityPolicyPolicy*) -> ContentSecurityPolicyResult:
# 1. If navigation response’s URL is local, return "Allowed".
let navigation_response_url = fetch_response_url(navigation_response) else trap("VERIFY: navigation_response->url().has_value()")
if fetch_is_local_url(navigation_response_url):
return ContentSecurityPolicyResult.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 response’s 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
# request’s context.
if check_type == ContentSecurityPolicyDirectiveCheckType.source:
return ContentSecurityPolicyResult.allowed
# 4. If target is not a child navigable, return "Allowed".
if html_navigable_parent(target) == none:
return ContentSecurityPolicyResult.allowed
# 5. Let current be target.
var current = target
# 6. While current is a child navigable:
while html_navigable_parent(current) != none:
# 1. Let document be current’s container document.
let document = html_navigable_container_document(current) else trap("VERIFY: document")
# 2. Let origin be the result of executing the URL parser on the ASCII serialization of document’s origin.
let document_origin = dom_document_origin(document)
let serialized_origin = web_url.origin_serialize(&document_origin)
let origin = domurl_parse(ak.string_bytes_as_string_view(&serialized_origin))
# AD-HOC: If the origin is opaque, serialization produces "null" which fails URL parsing.
# All major engines block in this case, as an opaque origin can never match any source expression.
let origin_url = origin else return ContentSecurityPolicyResult.blocked
# 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 directive’s value, policy’s self-origin, and 0, return "Blocked".
if content_security_policy_directives_does_url_match_source_list_in_origin_with_redirect_count(origin_url, &base.m_value, content_security_policy_policy_self_origin(policy), 0) == ContentSecurityPolicyDirectivesMatchResult.does_not_match:
return ContentSecurityPolicyResult.blocked
# 4. Set current to document’s node navigable.
current = dom_document_navigable(document) else trap("VERIFY: document->navigable()")
# 7. Return "Allowed".
return ContentSecurityPolicyResult.allowed