From 8bd794268f4e19f1570650594587ac047720dbbb Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 25 Oct 2026 08:28:12 +0800 Subject: [PATCH] fix(csp): queue Fetch violations for the initiating Document Use the existing native miscellaneous-platform task source for Fetch admission violations in Window, child, and popup realms. Initiate reports and preserve call-site deduplication before event delivery; queue enforcement violations before report-only violations. Cover Promise rejection order, native event identity, and discarded browsing contexts. --- .../context_host/misc_platform_api_tasks.rs | 12 +- .../context_host/security_policy.rs | 54 +++----- .../runtime/page_vm/tests/fetch_xhr/fetch.rs | 49 +++---- .../misc/extracted/service_worker_popup.rs | 26 ++-- .../tests/browser_api/security_policy.rs | 126 +++++++++++++++++- .../webidl_interfaces_and_url_params.rs | 17 +-- 6 files changed, 197 insertions(+), 87 deletions(-) diff --git a/moli-renderer-v8/src/native_bridge/context_host/misc_platform_api_tasks.rs b/moli-renderer-v8/src/native_bridge/context_host/misc_platform_api_tasks.rs index 5f63a65e448..e3493e50ab6 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/misc_platform_api_tasks.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/misc_platform_api_tasks.rs @@ -37,10 +37,10 @@ pub(crate) enum MiscPlatformApiTask { } impl PageScriptHost { - /// Inline CSP checks report on the policy's exact Window/Document. The - /// element is retained until the task runs, when its connectivity is - /// checked again; disconnecting or adopting it must retarget the event. - pub(crate) fn queue_inline_csp_violation_task( + /// Report on the policy's exact Window/Document, then queue its event. + /// An optional element is retained until dispatch, when disconnecting or + /// adopting it must retarget the event to that Document. + pub(crate) fn queue_content_security_policy_violation_task( &mut self, scope: &mut v8::PinScope<'_, '_>, owner: OwnerDispatchScope, @@ -255,7 +255,7 @@ impl PageScriptHost { MiscPlatformApiTask::EncryptedMediaRequest(task) => task.invoke(scope), MiscPlatformApiTask::ContentSecurityPolicyViolation { element, violation } => { // CSP checks the shadow-including root at dispatch time, not - // at the earlier inline check. A stale owner has + // at the earlier policy check. A stale owner has // already been rejected by the Page task arbiter. let event_target = element .filter(|element| { @@ -281,7 +281,7 @@ impl PageScriptHost { host_ptr, target, event, - "inline securitypolicyviolation", + "securitypolicyviolation", ); true }) diff --git a/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs b/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs index 2d0ef19056a..22b0b131299 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/security_policy.rs @@ -353,14 +353,8 @@ impl PageScriptHost { }) } OwnerDispatchScope::Child(handle) => { - let mut policy_container = + let policy_container = self.child_browsing_context_policy_container_snapshot(handle)?; - policy_container.response_content_security_policies = - self.child_effective_enforced_content_security_policies(handle); - policy_container.response_content_security_report_only_policies = - self.child_effective_response_content_security_report_only_policies(handle); - policy_container.content_security_reporting_endpoints = - self.child_effective_content_security_reporting_endpoints(handle); Some(OwnerDocumentPolicySnapshot { document_handle: self.child_browsing_context_document_handle(handle), document_url: self.child_browsing_context_current_url(handle)?, @@ -1142,7 +1136,7 @@ impl PageScriptHost { violation.line_number = line; violation.column_number = column; } - self.queue_inline_csp_violation_task(scope, owner, element, violation); + self.queue_content_security_policy_violation_task(scope, owner, element, violation); } enforced_violations.is_empty() } @@ -1250,7 +1244,15 @@ impl PageScriptHost { } let location = crate::content_security_policy::ContentSecurityPolicySourceLocation::capture(scope); - self.dispatch_document_connect_csp_check(scope, owner, check, Some(&location)) + let (mut reports, mut enforced) = check.into_violations(); + for violation in enforced.iter_mut().chain(&mut reports) { + location.apply_to(violation); + self.queue_content_security_policy_violation_task(scope, owner, None, violation); + } + match enforced.into_iter().next() { + Some(violation) => DocumentCspOutcome::Blocked(violation), + None => DocumentCspOutcome::Allowed, + } } pub(crate) fn check_document_connect_csp_for_owner_with_redirect_status<'s>( @@ -1269,7 +1271,7 @@ impl PageScriptHost { ) else { return DocumentCspOutcome::Allowed; }; - self.dispatch_document_connect_csp_check(scope, owner, check, None) + self.dispatch_document_connect_csp_check(scope, owner, check) } fn dispatch_document_connect_csp_check<'s>( @@ -1277,34 +1279,16 @@ impl PageScriptHost { scope: &mut v8::PinScope<'s, '_>, owner: OwnerDispatchScope, check: DocumentContentSecurityPolicyCheck, - source_location: Option< - &crate::content_security_policy::ContentSecurityPolicySourceLocation, - >, ) -> DocumentCspOutcome { - let (mut report_only_violation, mut enforced_violation) = check.into_violations(); - if let Some(source_location) = source_location { - for violation in report_only_violation - .iter_mut() - .chain(&mut enforced_violation) - { - source_location.apply_to(violation); - } + let (reports, enforced) = check.into_violations(); + + for violation in reports.iter().chain(&enforced) { + self.queue_content_security_policy_violation_task(scope, owner, None, violation); } - let host_ptr: *mut PageScriptHost = self; - for violation in report_only_violation { - self.dispatch_content_security_policy_violation_event_for_owner_best_effort( - scope, host_ptr, owner, &violation, - ); + match enforced.into_iter().next() { + Some(violation) => DocumentCspOutcome::Blocked(violation), + None => DocumentCspOutcome::Allowed, } - let Some(first_violation) = enforced_violation.first().cloned() else { - return DocumentCspOutcome::Allowed; - }; - for violation in enforced_violation { - self.dispatch_content_security_policy_violation_event_for_owner_best_effort( - scope, host_ptr, owner, &violation, - ); - } - DocumentCspOutcome::Blocked(first_violation) } fn document_connect_csp_check_for_owner_with_redirect_status( diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr/fetch.rs b/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr/fetch.rs index 11b16b11c67..4a1cf80eb34 100644 --- a/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr/fetch.rs +++ b/moli-renderer-v8/src/runtime/page_vm/tests/fetch_xhr/fetch.rs @@ -1119,31 +1119,33 @@ async fn opener_calling_popup_fetch_uses_popup_response_csp() { if (event.data !== "popup-ready") { return; } - popup.addEventListener("securitypolicyviolation", event => { - globalThis.__popupFetchCspEvents.push({ - blockedURI: event.blockedURI, - effectiveDirective: event.effectiveDirective, - disposition: event.disposition, - instance: event instanceof popup.SecurityPolicyViolationEvent, + const reported = new Promise(resolve => { + popup.addEventListener("securitypolicyviolation", event => { + globalThis.__popupFetchCspEvents.push({ + blockedURI: event.blockedURI, + effectiveDirective: event.effectiveDirective, + disposition: event.disposition, + instance: event instanceof popup.SecurityPolicyViolationEvent, + }); + resolve(); }); }); - popup.fetch("data:text/plain,blocked").then( - () => { - globalThis.__popupFetchCspObserved = JSON.stringify({ - fulfilled: true, - events: globalThis.__popupFetchCspEvents, - }); - }, - error => { - globalThis.__popupFetchCspObserved = JSON.stringify({ - name: error && error.name, - isTypeError: error instanceof popup.TypeError, - hasCspMessage: String(error && error.message) - .includes("Content Security Policy"), - events: globalThis.__popupFetchCspEvents, - }); - } - ).finally(() => { + const rejected = popup.fetch("data:text/plain,blocked").then( + () => ({ fulfilled: true }), + error => ({ + name: error && error.name, + isTypeError: error instanceof popup.TypeError, + hasCspMessage: String(error && error.message) + .includes("Content Security Policy"), + eventsAtRejection: globalThis.__popupFetchCspEvents.length, + }) + ); + Promise.all([rejected, reported]).then(([result]) => { + globalThis.__popupFetchCspObserved = JSON.stringify({ + ...result, + events: globalThis.__popupFetchCspEvents, + }); + }).finally(() => { globalThis.__popupFetchCspDone = true; }); }); @@ -1174,6 +1176,7 @@ async fn opener_calling_popup_fetch_uses_popup_response_csp() { "name": "TypeError", "isTypeError": true, "hasCspMessage": true, + "eventsAtRejection": 0, "events": [{ "blockedURI": "data", "effectiveDirective": "connect-src", diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_popup.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_popup.rs index 764994c836d..bf2396d8417 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_popup.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_popup.rs @@ -43,17 +43,25 @@ async fn navigator_service_worker_intercepts_popup_csp_report_destination() { r#"