dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / content_security_policy / blocking_algorithms_1.lucb
#==============================================================================================
#
# blocking_algorithms_1 - ContentSecurityPolicy's checks of requests, responses and
# navigations: does a request violate a policy, should a request or a response be blocked,
# the integrity policy check, and the navigation request and response checks
#
# DESCRIPTION:
# Port of Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp (lines 1-396).
# Region r28 ported the inline and <base> checks of the file for phase 1
# (blocking_algorithms_2); region p2e ported the rest.
#
# Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
# Copyright (c) 2025, Kenneth Myhra <kennethmyhra@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## `request->policy_container().get<GC::Ref<HTML::PolicyContainer>>()`: the request's policy container, which
## must be a policy container by now (Variant::get VERIFYs).
func content_security_policy_request_policy_container(request: FetchRequest*) -> HtmlPolicyContainer*:
match fetch_request_policy_container(request):
.html_policy_container(policy_container): return policy_container
_: trap("VERIFY: request->policy_container().has<GC::Ref<HTML::PolicyContainer>>()")
# mark: Requests ================================================================================
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:31 Web::ContentSecurityPolicy::does_resource_hint_request_violate_policy.
# https://w3c.github.io/webappsec-csp/#does-resource-hint-violate-policy
func content_security_policy_does_resource_hint_request_violate_policy(heap: gc.Heap*, request: FetchRequest*, policy: ContentSecurityPolicyPolicy*) -> ContentSecurityPolicyDirective*?:
# 1. Let defaultDirective be policy’s first directive whose name is "default-src".
var default_directive: ContentSecurityPolicyDirective*? = none
for directive in ak.vector_span(&policy.m_directives):
if ak.string_eq_fly_string(&directive.m_name, &content_security_policy_directives_names_default_src):
default_directive = directive
break
# 2. If defaultDirective does not exist, return "Does Not Violate".
if default_directive == none:
return none
# 3. For each directive of policy:
for directive in ak.vector_span(&policy.m_directives):
# 1. Let result be the result of executing directive’s pre-request check on request and policy.
let result = content_security_policy_directive_pre_request_check(directive, heap, request, policy)
# 2. If result is "Allowed", then return "Does Not Violate".
if result == ContentSecurityPolicyResult.allowed:
return none
# 4. Return defaultDirective.
return default_directive
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:58 Web::ContentSecurityPolicy::does_request_violate_policy.
# https://w3c.github.io/webappsec-csp/#does-request-violate-policy
func content_security_policy_does_request_violate_policy(heap: gc.Heap*, request: FetchRequest*, policy: ContentSecurityPolicyPolicy*) -> ContentSecurityPolicyDirective*?:
# 1. If request’s initiator is "prefetch", then return the result of executing § 6.7.2.2 Does resource hint
# request violate policy? on request and policy.
if fetch_request_initiator(request) == FetchRequestInitiator.prefetch:
return content_security_policy_does_resource_hint_request_violate_policy(heap, request, policy)
# 2. Let violates be "Does Not Violate".
var violates: ContentSecurityPolicyDirective*? = none
# 3. For each directive of policy:
for directive in ak.vector_span(&policy.m_directives):
# 1. Let result be the result of executing directive’s pre-request check on request and policy.
let result = content_security_policy_directive_pre_request_check(directive, heap, request, policy)
# 2. If result is "Blocked", then let violates be directive.
if result == ContentSecurityPolicyResult.blocked:
violates = directive
# 4. Return violates.
return violates
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:84 Web::ContentSecurityPolicy::report_content_security_policy_violations_for_request.
# https://w3c.github.io/webappsec-csp/#report-for-request
pub func content_security_policy_report_content_security_policy_violations_for_request(realm: Realm*, request: FetchRequest*):
# 1. Let CSP list be request’s policy container's CSP list.
let csp_list = content_security_policy_request_policy_container(request).csp_list
# 2. For each policy of CSP list:
for policy in ak.vector_span(&csp_list.m_policies):
# 1. If policy’s disposition is "enforce", then skip to the next policy.
if content_security_policy_policy_disposition(policy) == ContentSecurityPolicyPolicyDisposition.enforce:
continue
# 2. Let violates be the result of executing § 6.7.2.1 Does request violate policy? on request and policy.
let violates = content_security_policy_does_request_violate_policy(realm_heap(realm), request, policy)
# 3. If violates is not "Does Not Violate", then execute § 5.5 Report a violation on the result of executing
# § 2.4.2 Create a violation object for request, and policy. on request, and policy.
if violates != none:
let violation = content_security_policy_violation_create_a_violation_object_for_request_and_policy(realm, request, policy)
content_security_policy_violation_report_a_violation(violation, realm)
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:108 Web::ContentSecurityPolicy::should_request_be_blocked_by_content_security_policy.
# https://w3c.github.io/webappsec-csp/#should-block-request
pub func content_security_policy_should_request_be_blocked_by_content_security_policy(realm: Realm*, request: FetchRequest*) -> ContentSecurityPolicyResult:
# 1. Let CSP list be request’s policy container's CSP list.
let csp_list = content_security_policy_request_policy_container(request).csp_list
# 2. Let result be "Allowed".
var result = ContentSecurityPolicyResult.allowed
# 3. For each policy of CSP list:
for policy in ak.vector_span(&csp_list.m_policies):
# 1. If policy’s disposition is "report", then skip to the next policy.
if content_security_policy_policy_disposition(policy) == ContentSecurityPolicyPolicyDisposition.report:
continue
# 2. Let violates be the result of executing § 6.7.2.1 Does request violate policy? on request and policy.
let violates = content_security_policy_does_request_violate_policy(realm_heap(realm), request, policy)
# 3. If violates is not "Does Not Violate", then:
if violates != none:
# 1. Execute § 5.5 Report a violation on the result of executing § 2.4.2 Create a violation object for
# request, and policy. on request, and policy.
let violation = content_security_policy_violation_create_a_violation_object_for_request_and_policy(realm, request, policy)
content_security_policy_violation_report_a_violation(violation, realm)
# 2. Set result to "Blocked".
result = ContentSecurityPolicyResult.blocked
# 4. Return result.
return result
## `policy.blocked_destinations.contains_slow(destination)` of should_request_be_blocked_by_integrity_policy.
func content_security_policy_destinations_contain(destinations: const ak.Vector[FetchRequestDestination]*, destination: FetchRequestDestination) -> bool:
for blocked_destination in ak.vector_span_const(destinations):
if blocked_destination == destination:
return true
return false
## `policy.sources.contains_slow("inline"sv)` of should_request_be_blocked_by_integrity_policy.
func content_security_policy_sources_contain_inline(sources: const ak.Vector[ak.String]*) -> bool:
for source in ak.vector_span_const(sources):
if ak.string_eq_string_view(&source, ak.sv("inline")):
return true
return false
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:142 Web::ContentSecurityPolicy::should_request_be_blocked_by_integrity_policy.
# https://w3c.github.io/webappsec-subresource-integrity/#should-request-be-blocked-by-integrity-policy
pub func content_security_policy_should_request_be_blocked_by_integrity_policy(request: FetchRequest*) -> ContentSecurityPolicyResult:
# 1. Let policyContainer be request’s policy container.
let policy_container = content_security_policy_request_policy_container(request)
# 2. Let parsedMetadata be the result of calling parse metadata with request’s integrity metadata.
let integrity_metadata = fetch_request_integrity_metadata(request)
let parsed_metadata = sri_parse_metadata(ak.string_bytes_as_string_view(&integrity_metadata)) else trap("MUST: SRI::parse_metadata")
# 3. If parsedMetadata is not the empty set and request’s mode is either "cors" or "same-origin", return "Allowed".
let mode = fetch_request_mode(request)
if not ak.vector_is_empty(&parsed_metadata) and (mode == FetchRequestMode.cors or mode == FetchRequestMode.same_origin):
return ContentSecurityPolicyResult.allowed
# 4. If request’s url is local, return "Allowed".
if fetch_is_local_url(fetch_request_url(request)):
return ContentSecurityPolicyResult.allowed
# 5. Let policy be policyContainer’s integrity policy.
let policy = &policy_container.integrity_policy
# 6. Let reportPolicy be policyContainer’s report only integrity policy.
let report_policy = &policy_container.report_only_integrity_policy
# 7. If both policy and reportPolicy are empty integrity policys, return "Allowed".
if html_integrity_policy_is_empty(policy) and html_integrity_policy_is_empty(report_policy):
return ContentSecurityPolicyResult.allowed
# 8. Let global be request’s client’s global object.
let client = fetch_request_client(request) else trap("VERIFY: the request has a client")
let global = html_environment_settings_object_global_object(client)
# 9. If global is not a Window nor a WorkerGlobalScope, return "Allowed".
if not is_html_window((gc.Cell*)global) and not is_html_worker_global_scope((gc.Cell*)global):
return ContentSecurityPolicyResult.allowed
# 10. Let block be a boolean, initially false.
var block = false
# FIXME: 11. Let reportBlock be a boolean, initially false.
var report_block = false
# 12. If policy’s sources contains "inline" and policy’s blocked destinations contains request’s destination, set block to true.
if content_security_policy_sources_contain_inline(&policy.sources):
if let destination = fetch_request_destination(request):
if content_security_policy_destinations_contain(&policy.blocked_destinations, destination):
block = true
# 13. If reportPolicy’s sources contains "inline" and reportPolicy’s blocked destinations contains request’s destination, set reportBlock to true.
if content_security_policy_sources_contain_inline(&report_policy.sources):
if let destination = fetch_request_destination(request):
if content_security_policy_destinations_contain(&report_policy.blocked_destinations, destination):
report_block = true
# FIXME: 14. If block is true or reportBlock is true, then report violation with request, block, reportBlock, policy and reportPolicy.
_ = report_block
# 15. If block is true, then return "Blocked"; otherwise "Allowed".
return ContentSecurityPolicyResult.blocked if block else ContentSecurityPolicyResult.allowed
# mark: Responses ===============================================================================
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:202 Web::ContentSecurityPolicy::should_response_to_request_be_blocked_by_content_security_policy.
# https://w3c.github.io/webappsec-csp/#should-block-response
pub func content_security_policy_should_response_to_request_be_blocked_by_content_security_policy(realm: Realm*, response: FetchResponse*, request: FetchRequest*) -> ContentSecurityPolicyResult:
# 1. Let CSP list be request’s policy container's CSP list.
let csp_list = content_security_policy_request_policy_container(request).csp_list
# 2. Let result be "Allowed".
var result = ContentSecurityPolicyResult.allowed
# 3. For each policy of CSP list:
# Spec Note: This portion of the check verifies that the page can load the response. That is, that a Service
# Worker hasn't substituted a file which would violate the page’s CSP.
for policy in ak.vector_span(&csp_list.m_policies):
# 1. For each directive of policy:
for directive in ak.vector_span(&policy.m_directives):
# 1. If the result of executing directive’s post-request check is "Blocked", then:
if content_security_policy_directive_post_request_check(directive, realm_heap(realm), request, response, policy) == ContentSecurityPolicyResult.blocked:
# 1. Execute § 5.5 Report a violation on the result of executing § 2.4.2 Create a violation object for
# request, and policy. on request, and policy.
let violation = content_security_policy_violation_create_a_violation_object_for_request_and_policy(realm, request, policy)
content_security_policy_violation_report_a_violation(violation, realm)
# 2. If policy’s disposition is "enforce", then set result to "Blocked".
if content_security_policy_policy_disposition(policy) == ContentSecurityPolicyPolicyDisposition.enforce:
result = ContentSecurityPolicyResult.blocked
# 4. Return result.
return result
# mark: Navigations =============================================================================
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:236 Web::ContentSecurityPolicy::should_navigation_request_of_type_be_blocked_by_content_security_policy.
# https://w3c.github.io/webappsec-csp/#should-block-navigation-request
pub func content_security_policy_should_navigation_request_of_type_be_blocked_by_content_security_policy(navigation_request: FetchRequest*, navigation_type: ContentSecurityPolicyNavigationType) -> ContentSecurityPolicyResult:
# 1. Let result be "Allowed".
var result = ContentSecurityPolicyResult.allowed
# 2. For each policy of navigation request’s policy container’s CSP list:
let policy_container = content_security_policy_request_policy_container(navigation_request)
for policy in ak.vector_span(&policy_container.csp_list.m_policies):
# 1. For each directive of policy:
for directive in ak.vector_span(&policy.m_directives):
# 1. If directive’s pre-navigation check returns "Allowed" when executed upon navigation request, type, and policy skip to the next directive.
let directive_result = content_security_policy_directive_pre_navigation_check(directive, navigation_request, navigation_type, policy)
if directive_result == ContentSecurityPolicyResult.allowed:
continue
# 2. Otherwise, let violation be the result of executing § 2.4.1 Create a violation object for global, policy, and directive on navigation request’s
# client’s global object, policy, and directive’s name.
let client = fetch_request_client(navigation_request) else trap("VERIFY: the navigation request has a client")
let realm = html_environment_settings_object_realm(client)
let violation = content_security_policy_violation_create_a_violation_object_for_global_policy_and_directive(realm, html_environment_settings_object_global_object(client), policy, directive.m_name)
# 3. Set violation’s resource to navigation request’s URL.
content_security_policy_violation_set_resource(violation, ContentSecurityPolicyViolationResourceType.url(fetch_request_url(navigation_request)))
# 4. Execute § 5.5 Report a violation on violation.
content_security_policy_violation_report_a_violation(violation, realm)
# 5. If policy’s disposition is "enforce", then set result to "Blocked".
if content_security_policy_policy_disposition(policy) == ContentSecurityPolicyPolicyDisposition.enforce:
result = ContentSecurityPolicyResult.blocked
# 3. If result is "Allowed", and if navigation request’s current URL’s scheme is javascript:
let current_url = fetch_request_current_url(navigation_request)
let current_url_scheme = web_url.url_scheme(¤t_url)
if result == ContentSecurityPolicyResult.allowed and ak.string_eq_string_view(¤t_url_scheme, ak.sv("javascript")):
# 1. For each policy of navigation request’s policy container’s CSP list:
let csp_list = content_security_policy_request_policy_container(navigation_request).csp_list
for policy in ak.vector_span(&csp_list.m_policies):
# 1. For each directive of policy:
for directive in ak.vector_span(&policy.m_directives):
# 1. Let directive-name be the result of executing § 6.8.2 Get the effective directive for inline
# checks on type.
# FIXME: File spec issue that the type should probably always be "navigation", as NavigationType would
# cause this algorithm to return null, making directive-name null, then piping directive-name
# into a Violation object where the directive name is defined to be a non-empty string.
# Other parts of the spec seem to refer to the "navigation" inline type as being for
# javascript: URLs. Additionally, this doesn't have an impact on the security decision here,
# just which directive is reported to have been violated.
let directive_name = content_security_policy_directives_get_the_effective_directive_for_inline_checks(ContentSecurityPolicyInlineType.navigation)
# 2. If directive’s inline check returns "Allowed" when executed upon null, "navigation" and
# navigation request’s current URL, skip to the next directive.
# FIXME: File spec issue that they forgot to pass in "policy" here.
# FIXME: File spec issue that current URL is a URL object and not a string, therefore they must use a
# spec operation to serialize the URL.
let client = fetch_request_client(navigation_request) else trap("VERIFY: the navigation request has a client")
let realm = html_environment_settings_object_realm(client)
let serialized_url = web_url.url_to_string(¤t_url)
if content_security_policy_directive_inline_check(directive, realm_heap(realm), none, ContentSecurityPolicyInlineType.navigation, policy, serialized_url) == ContentSecurityPolicyResult.allowed:
continue
# 3. Otherwise, let violation be the result of executing § 2.4.1 Create a violation object for global,
# policy, and directive on navigation request’s client’s global object, policy, and directive-name.
let violation = content_security_policy_violation_create_a_violation_object_for_global_policy_and_directive(realm, html_environment_settings_object_global_object(client), policy, ak.fly_string_to_string_const(&directive_name))
# 4. Set violation’s resource to navigation request’s URL.
content_security_policy_violation_set_resource(violation, ContentSecurityPolicyViolationResourceType.url(fetch_request_url(navigation_request)))
# 5. Execute § 5.5 Report a violation on violation.
content_security_policy_violation_report_a_violation(violation, realm)
# 6. If policy’s disposition is "enforce", then set result to "Blocked".
if content_security_policy_policy_disposition(policy) == ContentSecurityPolicyPolicyDisposition.enforce:
result = ContentSecurityPolicyResult.blocked
# 4. Return result.
return result
## Ported from Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp:319 Web::ContentSecurityPolicy::should_navigation_response_to_navigation_request_of_type_in_target_be_blocked_by_content_security_policy.
# https://w3c.github.io/webappsec-csp/#should-block-navigation-response
pub func content_security_policy_should_navigation_response_to_navigation_request_of_type_in_target_be_blocked_by_content_security_policy(navigation_request: FetchRequest*?, navigation_response: FetchResponse*, response_csp_list: ContentSecurityPolicyPolicyList*, navigation_type: ContentSecurityPolicyNavigationType, target: HtmlNavigable*) -> ContentSecurityPolicyResult:
# 1. Let result be "Allowed".
var result = ContentSecurityPolicyResult.allowed
# FIXME: File spec issue stating that the request can be null (e.g. from a srcdoc resource).
if navigation_request == none:
ak.dbgln("FIXME: Handle null navigation_request in navigation response Content Security Policy check.")
return result
let request = navigation_request else trap("unreachable: checked above")
# 2. For each policy of response CSP list:
for policy in ak.vector_span(&response_csp_list.m_policies):
# Spec Note: Some directives (like frame-ancestors) allow a response’s Content Security Policy to act on the navigation.
# 1. For each directive of policy:
for directive in ak.vector_span(&policy.m_directives):
# 1. If directive’s navigation response check returns "Allowed" when executed upon navigation request, type, navigation response, target, "response", and policy skip to the next directive.
let directive_result = content_security_policy_directive_navigation_response_check(directive, request, navigation_type, navigation_response, target, ContentSecurityPolicyDirectiveCheckType.response, policy)
if directive_result == ContentSecurityPolicyResult.allowed:
continue
# 2. Otherwise, let violation be the result of executing § 2.4.1 Create a violation object for global, policy, and directive on null, policy, and directive’s name.
# Spec Note: We use null for the global object, as no global exists: we haven’t processed the navigation to create a Document yet.
# FIXME: What should the realm be here?
let client = fetch_request_client(request) else trap("VERIFY: the navigation request has a client")
let realm = html_environment_settings_object_realm(client)
let violation = content_security_policy_violation_create_a_violation_object_for_global_policy_and_directive(realm, none, policy, directive.m_name)
# 3. Set violation’s resource to navigation response’s URL.
if let navigation_response_url = fetch_response_url(navigation_response):
content_security_policy_violation_set_resource(violation, ContentSecurityPolicyViolationResourceType.url(navigation_response_url))
else:
content_security_policy_violation_set_resource(violation, ContentSecurityPolicyViolationResourceType.empty)
# 4. Execute § 5.5 Report a violation on violation.
content_security_policy_violation_report_a_violation(violation, realm)
# 5. If policy’s disposition is "enforce", then set result to "Blocked".
if content_security_policy_policy_disposition(policy) == ContentSecurityPolicyPolicyDisposition.enforce:
result = ContentSecurityPolicyResult.blocked
# 3. For each policy of navigation request’s policy container’s CSP list:
let request_policy_container = content_security_policy_request_policy_container(request)
for policy in ak.vector_span(&request_policy_container.csp_list.m_policies):
# Spec Note: NOTE: Some directives in the navigation request’s context (like frame-ancestors) need the response before acting on the navigation.
# 1. For each directive of policy:
for directive in ak.vector_span(&policy.m_directives):
# 1. If directive’s navigation response check returns "Allowed" when executed upon navigation request, type, navigation response, target, "source", and policy skip to the next directive.
let directive_result = content_security_policy_directive_navigation_response_check(directive, request, navigation_type, navigation_response, target, ContentSecurityPolicyDirectiveCheckType.source, policy)
if directive_result == ContentSecurityPolicyResult.allowed:
continue
# 2. Otherwise, let violation be the result of executing § 2.4.1 Create a violation object for global, policy, and directive on navigation request’s client’s global object, policy, and directive’s name.
let client = fetch_request_client(request) else trap("VERIFY: the navigation request has a client")
let realm = html_environment_settings_object_realm(client)
let violation = content_security_policy_violation_create_a_violation_object_for_global_policy_and_directive(realm, html_environment_settings_object_global_object(client), policy, directive.m_name)
# 3. Set violation’s resource to navigation request’s URL.
content_security_policy_violation_set_resource(violation, ContentSecurityPolicyViolationResourceType.url(fetch_request_url(request)))
# 4. Execute § 5.5 Report a violation on violation.
content_security_policy_violation_report_a_violation(violation, realm)
# 5. If policy’s disposition is "enforce", then set result to "Blocked".
if content_security_policy_policy_disposition(policy) == ContentSecurityPolicyPolicyDisposition.enforce:
result = ContentSecurityPolicyResult.blocked
# 4. Return result.
return result