dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / policy_containers.lucb
#==============================================================================================
#
# policy_containers - HTML::PolicyContainer: the policies that apply to a Document, and the
# embedder policy values
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/PolicyContainers.cpp (lines 1-113) with the inline
# IntegrityPolicy::is_empty of PolicyContainers.h, and of EmbedderPolicy.cpp (lines 14-36;
# the IPC encoders are not ported).
#
# Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
# Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
# Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
# Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: PolicyContainers.h ======================================================================
## Ported from PolicyContainers.h:26 Web::HTML::IntegrityPolicy::is_empty.
pub func html_integrity_policy_is_empty(this: const HtmlIntegrityPolicy*) -> bool:
return ak.vector_is_empty(&this.sources) and ak.vector_is_empty(&this.blocked_destinations) and ak.vector_is_empty(&this.endpoints)
# mark: PolicyContainers.cpp ====================================================================
## Ported from PolicyContainers.cpp:22 Web::HTML::PolicyContainer::PolicyContainer, with the
## default member initializers.
pub func html_policy_container_construct(this: HtmlPolicyContainer*, heap: gc.Heap*):
this.referrer_policy = referrer_policy_default_referrer_policy
this.csp_list = heap_allocate_content_security_policy_policy_list(heap)
## Ported from PolicyContainers.cpp:28 Web::HTML::url_requires_storing_the_policy_container_in_history.
# https://html.spec.whatwg.org/multipage/browsers.html#requires-storing-the-policy-container-in-history
pub func html_url_requires_storing_the_policy_container_in_history(url: web_url.Url) -> bool:
# 1. If url's scheme is "blob", then return false.
let scheme = web_url.url_scheme(&url)
if ak.string_eq_string_view(&scheme, ak.sv("blob")):
return false
# 2. If url is local, then return true.
# 3. Return false.
return fetch_is_local_url(url)
## Ported from PolicyContainers.cpp:40 Web::HTML::create_a_policy_container_from_a_fetch_response.
# https://html.spec.whatwg.org/multipage/browsers.html#creating-a-policy-container-from-a-fetch-response
pub func html_create_a_policy_container_from_a_fetch_response(heap: gc.Heap*, response: FetchResponse*, environment: HtmlEnvironment*?) -> HtmlPolicyContainer*:
# FIXME: 1. If response's URL's scheme is "blob", then return a clone of response's URL's blob URL entry's
# environment's policy container.
# 2. Let result be a new policy container.
let result = heap_allocate_html_policy_container(heap)
# 3. Set result's CSP list to the result of parsing a response's Content Security Policies given response.
result.csp_list = content_security_policy_policy_parse_a_responses_content_security_policies(heap, response)
# FIXME: 4. If environment is non-null, then set result's embedder policy to the result of obtaining an embedder
# policy given response and environment. Otherwise, set it to "unsafe-none".
# 5. Set result's referrer policy to the result of parsing the `Referrer-Policy` header given response.
# [REFERRERPOLICY]
let parsed_referrer_policy = referrer_policy_parse_a_referrer_policy_from_a_referrer_policy_header(response)
if parsed_referrer_policy != ReferrerPolicyReferrerPolicy.empty_string:
result.referrer_policy = parsed_referrer_policy
# FIXME: 6. Parse Integrity-Policy headers with response and result.
# 7. Return result.
return result
## Ported from PolicyContainers.cpp:66 Web::HTML::create_a_policy_container_from_serialized_policy_container.
pub func html_create_a_policy_container_from_serialized_policy_container(heap: gc.Heap*, serialized_policy_container: HtmlSerializedPolicyContainer) -> HtmlPolicyContainer*:
let result = heap_allocate_html_policy_container(heap)
result.csp_list = content_security_policy_policy_list_create_serialized(heap, &serialized_policy_container.csp_list)
result.embedder_policy = serialized_policy_container.embedder_policy
result.referrer_policy = serialized_policy_container.referrer_policy
return result
## Ported from PolicyContainers.cpp:76 Web::HTML::PolicyContainer::clone.
# https://html.spec.whatwg.org/multipage/browsers.html#clone-a-policy-container
pub func html_policy_container_clone(this: HtmlPolicyContainer*, heap: gc.Heap*) -> HtmlPolicyContainer*:
# 1. Let clone be a new policy container.
let clone = heap_allocate_html_policy_container(heap)
# 2. For each policy in policyContainer's CSP list, append a copy of policy into clone's CSP list.
clone.csp_list = content_security_policy_policy_list_clone(this.csp_list, heap)
# 3. Set clone's embedder policy to a copy of policyContainer's embedder policy.
# NOTE: This is a C++ copy.
clone.embedder_policy = this.embedder_policy
# 4. Set clone's referrer policy to policyContainer's referrer policy.
clone.referrer_policy = this.referrer_policy
# 5. Set clone's integrity policy to a copy of policyContainer's integrity policy.
clone.integrity_policy = HtmlIntegrityPolicy(sources = ak.vector_clone(&this.integrity_policy.sources), blocked_destinations = ak.vector_clone(&this.integrity_policy.blocked_destinations), endpoints = ak.vector_clone(&this.integrity_policy.endpoints))
# 6. Return clone.
return clone
## Ported from PolicyContainers.cpp:98 Web::HTML::PolicyContainer::serialize.
pub func html_policy_container_serialize(this: HtmlPolicyContainer*) -> HtmlSerializedPolicyContainer:
return HtmlSerializedPolicyContainer(csp_list = content_security_policy_policy_list_serialize(this.csp_list), embedder_policy = this.embedder_policy, referrer_policy = this.referrer_policy)
## Ported from PolicyContainers.cpp:107 Web::HTML::PolicyContainer::visit_edges (override).
pub func html_policy_container_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlPolicyContainer*)base
gc.cell_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.csp_list)
# mark: EmbedderPolicy.cpp ======================================================================
## Ported from EmbedderPolicy.cpp:14 Web::HTML::embedder_policy_value_to_string.
pub func html_embedder_policy_value_to_string(embedder_policy_value: HtmlEmbedderPolicyValue) -> ak.StringView:
match embedder_policy_value:
.unsafe_none:
return ak.sv("unsafe-none")
.require_corp:
return ak.sv("require-corp")
.credentialless:
return ak.sv("credentialless")
_:
trap("VERIFY_NOT_REACHED")
## Ported from EmbedderPolicy.cpp:27 Web::HTML::embedder_policy_value_from_string.
pub func html_embedder_policy_value_from_string(string: ak.StringView) -> HtmlEmbedderPolicyValue?:
if ak.string_view_equals_ignoring_ascii_case(&string, ak.sv("unsafe-none")):
return HtmlEmbedderPolicyValue.unsafe_none
if ak.string_view_equals_ignoring_ascii_case(&string, ak.sv("require-corp")):
return HtmlEmbedderPolicyValue.require_corp
if ak.string_view_equals_ignoring_ascii_case(&string, ak.sv("credentialless")):
return HtmlEmbedderPolicyValue.credentialless
return none