Move CSP code into one entrypoint (#37604)

This refactoring moves various CSP-related methods away from GlobalScope
and Document into a dedicated entrypoint. It also reduces the amount of
imports of the CSP crate, so that types are consolidated into this one
entrypoint. That way, we control how CSP code interacts with the script
crate.

For reviewing purposes, I split up the refactoring into separate
distinct commits that all move 1 method(group) into the new file.

Testing: no change in behavior, only a build improvement + code cleanup

---------

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
Signed-off-by: Tim van der Lippe <TimvdLippe@users.noreply.github.com>
This commit is contained in:
Tim van der Lippe 2025-06-24 10:50:30 +02:00 committed by GitHub
parent 2265570c88
commit fc20d8b2e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 424 additions and 289 deletions

View file

@ -19,7 +19,6 @@ use std::time::{Duration, Instant};
use std::{os, ptr, thread};
use background_hang_monitor_api::ScriptHangAnnotation;
use content_security_policy::CheckResult;
use js::conversions::jsstr_to_string;
use js::glue::{
CollectServoSizes, CreateJobQueue, DeleteJobQueue, DispatchableRun, JobQueueTraps,
@ -72,6 +71,7 @@ use crate::dom::bindings::reflector::{DomGlobal, DomObject};
use crate::dom::bindings::root::trace_roots;
use crate::dom::bindings::utils::DOM_CALLBACKS;
use crate::dom::bindings::{principals, settings_stack};
use crate::dom::csp::CspReporting;
use crate::dom::event::{Event, EventBubbles, EventCancelable, EventStatus};
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
@ -380,25 +380,20 @@ unsafe extern "C" fn content_security_policy_allows(
wrap_panic(&mut || {
// SpiderMonkey provides null pointer when executing webassembly.
let in_realm_proof = AlreadyInRealm::assert_for_cx(cx);
let global = GlobalScope::from_context(*cx, InRealm::Already(&in_realm_proof));
let Some(csp_list) = global.get_csp_list() else {
allowed = true;
return;
};
let global = &GlobalScope::from_context(*cx, InRealm::Already(&in_realm_proof));
let (is_evaluation_allowed, violations) = match runtime_code {
allowed = match runtime_code {
RuntimeCode::JS => {
let source = match sample {
sample if !sample.is_null() => &jsstr_to_string(*cx, *sample),
_ => "",
};
csp_list.is_js_evaluation_allowed(source)
global
.get_csp_list()
.is_js_evaluation_allowed(global, source)
},
RuntimeCode::WASM => csp_list.is_wasm_evaluation_allowed(),
RuntimeCode::WASM => global.get_csp_list().is_wasm_evaluation_allowed(global),
};
global.report_csp_violations(violations, None);
allowed = is_evaluation_allowed == CheckResult::Allowed;
});
allowed
}