mirror of
https://github.com/lexmount/moli.git
synced 2026-11-23 00:02:13 +00:00
fix(csp): carry initiator policies through local document navigation
This commit is contained in:
@@ -111,9 +111,10 @@ pub use moli_renderer_v8::{
|
||||
RendererJavaScriptDialogSource, RendererLayoutMetrics, RendererLifecycleEpoch,
|
||||
RendererLifecycleEventStamp, RendererLifecycleStartReason, RendererLifecycleTerminationStamp,
|
||||
RendererMainDocumentCommit, RendererMainDocumentResource, RendererNavigationInitiator,
|
||||
RendererPageCommandPostResponseContinuation, RendererPageCreationArtifacts,
|
||||
RendererPageCreationDiagnostics, RendererPageDiagnosticsSnapshot, RendererPageDumpFormat,
|
||||
RendererPageDumpOptions, RendererPageDumpStripOptions, RendererPageReplacementError,
|
||||
RendererNavigationPolicy, RendererPageCommandPostResponseContinuation,
|
||||
RendererPageCreationArtifacts, RendererPageCreationDiagnostics,
|
||||
RendererPageDiagnosticsSnapshot, RendererPageDumpFormat, RendererPageDumpOptions,
|
||||
RendererPageDumpStripOptions, RendererPageReplacementError,
|
||||
RendererPageReplacementFailureDisposition, RendererPdfOptions, RendererPendingAuxiliaryPage,
|
||||
RendererPendingDownloadActivation, RendererPendingDownloadResponse,
|
||||
RendererPendingFileChooserActivation, RendererPendingJavaScriptDialog,
|
||||
|
||||
@@ -3910,6 +3910,7 @@ mod tests {
|
||||
security_origin: "https://nav.example".to_owned(),
|
||||
secure_context_type: "Secure".to_owned(),
|
||||
document_referrer: None,
|
||||
navigation_policy: None,
|
||||
timestamp: 0.0,
|
||||
session_history_position: None,
|
||||
browsing_context_group: None,
|
||||
|
||||
@@ -384,6 +384,7 @@ async fn stale_initial_document_page_build_does_not_overwrite_committed_page() {
|
||||
security_origin: "null".to_owned(),
|
||||
secure_context_type: "InsecureScheme".to_owned(),
|
||||
document_referrer: None,
|
||||
navigation_policy: None,
|
||||
timestamp: 0.0,
|
||||
session_history_position: None,
|
||||
browsing_context_group: None,
|
||||
|
||||
@@ -173,6 +173,10 @@ impl RendererMainDocumentCommitSeed {
|
||||
.navigation_initiator
|
||||
.as_ref()
|
||||
.map(|initiator| initiator.document_referrer(final_url)),
|
||||
navigation_policy: self
|
||||
.navigation_initiator
|
||||
.as_ref()
|
||||
.and_then(|initiator| initiator.document_policy(final_url)),
|
||||
timestamp: self.timestamp,
|
||||
session_history_position: None,
|
||||
browsing_context_group: self
|
||||
|
||||
+96
@@ -61,3 +61,99 @@ async fn popup_location_navigation_captures_delivered_meta_policies() {
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn local_navigation_inherits_the_initiator_policy_without_mutating_network_responses() {
|
||||
const TARGET: &str = r#"<!doctype html><body><script>
|
||||
addEventListener('load', () => {
|
||||
let outcome = 'allowed';
|
||||
try { document.createElement('div').innerHTML = '<p>untrusted</p>'; }
|
||||
catch (error) { outcome = error.name + ':' + (error instanceof TypeError); }
|
||||
(opener || parent).postMessage({policyOutcome: outcome}, '*');
|
||||
});
|
||||
</script>"#;
|
||||
const INITIATOR: &str = r#"<!doctype html><body><script>
|
||||
addEventListener('message', event => {
|
||||
const policy = document.createElement('meta');
|
||||
policy.httpEquiv = 'Content-Security-Policy';
|
||||
policy.content = "require-trusted-types-for 'script'";
|
||||
if (!event.data.late) document.head.appendChild(policy);
|
||||
parent.__policyTarget.location.href = event.data.url;
|
||||
if (event.data.late) document.head.appendChild(policy);
|
||||
});
|
||||
</script>"#;
|
||||
run_history_test(async {
|
||||
for mode in ["popup-blob", "child-blob", "child-data", "popup-network"] {
|
||||
for late in [false, true] {
|
||||
let routes = axum::Router::new()
|
||||
.route(
|
||||
"/policy-initiator.html",
|
||||
axum::routing::get(|| async { axum::response::Html(INITIATOR) }),
|
||||
)
|
||||
.route(
|
||||
"/policy-target.html",
|
||||
axum::routing::get(|| async { axum::response::Html(TARGET) }),
|
||||
);
|
||||
let mut page = SameDocumentPage::with_routes(routes).await;
|
||||
let script = r#"(async () => {
|
||||
const mode = MODE, late = LATE, markup = TARGET_DOCUMENT;
|
||||
const child = mode.startsWith('child-');
|
||||
const createFrame = async path => {
|
||||
const frame = document.createElement('iframe');
|
||||
frame.src = path;
|
||||
const loaded = new Promise(resolve => frame.onload = resolve);
|
||||
document.body.appendChild(frame);
|
||||
await loaded;
|
||||
return frame;
|
||||
};
|
||||
let targetFrame, initiatorFrame, target;
|
||||
if (child) {
|
||||
targetFrame = await createFrame('history.html?target');
|
||||
target = globalThis.__policyTarget = targetFrame.contentWindow;
|
||||
initiatorFrame = await createFrame('policy-initiator.html');
|
||||
} else {
|
||||
target = open('history.html?initial-popup');
|
||||
await new Promise(resolve => target.addEventListener('load', resolve, {once:true}));
|
||||
}
|
||||
const completed = new Promise(resolve => {
|
||||
addEventListener('message', function listener(event) {
|
||||
if (event.source === target && event.data?.policyOutcome) {
|
||||
removeEventListener('message', listener);
|
||||
resolve(event.data.policyOutcome);
|
||||
}
|
||||
});
|
||||
});
|
||||
const url = mode === 'child-data'
|
||||
? 'data:text/html;charset=utf-8,' + encodeURIComponent(markup)
|
||||
: mode === 'popup-network' ? new URL('policy-target.html', location.href).href
|
||||
: URL.createObjectURL(new Blob([markup], {type:'text/html'}));
|
||||
if (child) {
|
||||
initiatorFrame.contentWindow.postMessage({url, late}, '*');
|
||||
} else {
|
||||
const policy = document.createElement('meta');
|
||||
policy.httpEquiv = 'Content-Security-Policy';
|
||||
policy.content = "require-trusted-types-for 'script'";
|
||||
if (!late) document.head.appendChild(policy);
|
||||
target.location.href = url;
|
||||
if (late) document.head.appendChild(policy);
|
||||
}
|
||||
const result = await completed;
|
||||
if (child) { targetFrame.remove(); initiatorFrame.remove(); }
|
||||
else target.close();
|
||||
if (url.startsWith('blob:')) URL.revokeObjectURL(url);
|
||||
return result;
|
||||
})()"#
|
||||
.replace("MODE", &serde_json::to_string(mode).unwrap())
|
||||
.replace("LATE", if late { "true" } else { "false" })
|
||||
.replace("TARGET_DOCUMENT", &serde_json::to_string(TARGET).unwrap());
|
||||
let result = page.evaluate(&script).await;
|
||||
assert_eq!(
|
||||
result,
|
||||
if late || mode == "popup-network" { "allowed" } else { "TypeError:true" },
|
||||
"mode={mode}, late={late}"
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -1029,6 +1029,9 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>(
|
||||
resolved.as_str(),
|
||||
entry_seed,
|
||||
initiator_url,
|
||||
navigation_source
|
||||
.as_ref()
|
||||
.and_then(|(initiator, _)| initiator.document_policy(&resolved)),
|
||||
)
|
||||
&& let Some(navigation_load) = host.current_child_navigation_load(handle)
|
||||
{
|
||||
@@ -1124,10 +1127,6 @@ fn capture_location_navigation_source<'s>(
|
||||
let source_context = scope.get_incumbent_context()?;
|
||||
let source_host = crate::util::context_host_ptr_from_context_slot(source_context)?;
|
||||
let target_host = crate::util::context_host_ptr_from_window_object(scope, target)?;
|
||||
// The ordinary same-realm path already carries its Document environment.
|
||||
if source_host == target_host {
|
||||
return None;
|
||||
}
|
||||
let source = source_context.global(scope);
|
||||
// Only the browsing context's fixed restrictions survive navigation.
|
||||
// Sandbox CSP from its old response belongs to the retiring Document.
|
||||
@@ -1150,7 +1149,13 @@ fn capture_location_navigation_source<'s>(
|
||||
initiator_url,
|
||||
loader.fetch_context().request_origin(),
|
||||
policy.referrer_policy.clone(),
|
||||
policy.clone(),
|
||||
);
|
||||
// Same-owner navigation already captures its origin environment separately.
|
||||
// Its policy still needs to cross the browser navigation boundary.
|
||||
if source_host == target_host {
|
||||
return Some((initiator, None));
|
||||
}
|
||||
policy.document_referrer = initiator.outgoing_referrer();
|
||||
let base_url = super::window_runtime::entered_window_api_base_url(scope, host);
|
||||
let child = match dispatch {
|
||||
|
||||
@@ -152,11 +152,15 @@ pub(super) fn handle_navigation_navigate_cross_document<'s>(
|
||||
mutation,
|
||||
);
|
||||
let host = unsafe { &mut *host_ptr };
|
||||
let policy = host
|
||||
.document_policy_container_for_inheritance(host.entered_owner_dispatch_scope(scope))
|
||||
.map(|policy| crate::runtime::RendererNavigationPolicy::capture(&policy));
|
||||
if host.queue_deferred_child_browsing_context_navigation_from_entry_seed(
|
||||
child_handle,
|
||||
next_url.as_str(),
|
||||
entry_seed,
|
||||
None,
|
||||
policy,
|
||||
) && let Some(navigation_load) = host.current_child_navigation_load(child_handle)
|
||||
{
|
||||
host.check_child_navigation_beforeunload(scope, child_handle, navigation_load);
|
||||
|
||||
@@ -583,6 +583,7 @@ fn commit(scope: &mut v8::PinScope<'_, '_>, admission: &PendingHistoryTraversalA
|
||||
participant.url.as_str(),
|
||||
participant.seed.clone(),
|
||||
None,
|
||||
None,
|
||||
) {
|
||||
for queued in &cross_document {
|
||||
host.cancel_joint_child_history_navigation(queued.handle, step);
|
||||
|
||||
@@ -310,6 +310,7 @@ pub(crate) fn window_open_callback<'s>(
|
||||
} else {
|
||||
creator_policy_container.referrer_policy.clone()
|
||||
},
|
||||
creator_policy_container.clone(),
|
||||
);
|
||||
creator_policy_container.document_referrer = if parsed_features.suppresses_referrer() {
|
||||
String::new()
|
||||
|
||||
@@ -399,6 +399,22 @@ impl DocumentRequestPolicySnapshot {
|
||||
}
|
||||
|
||||
impl DocumentPolicyContainer {
|
||||
pub(crate) fn for_navigation_response(
|
||||
headers: &[(String, Vec<u8>)],
|
||||
final_url: &Url,
|
||||
history: Option<&Self>,
|
||||
initiator: Option<&crate::runtime::RendererNavigationPolicy>,
|
||||
) -> Self {
|
||||
if let Some(history) = history {
|
||||
return history.clone();
|
||||
}
|
||||
let mut policy = Self::from_navigation_response_headers(headers, final_url);
|
||||
if let Some(initiator) = initiator {
|
||||
initiator.apply_to(&mut policy, final_url);
|
||||
}
|
||||
policy
|
||||
}
|
||||
|
||||
pub(crate) fn from_navigation_response_headers(
|
||||
headers: &[(String, Vec<u8>)],
|
||||
final_url: &Url,
|
||||
@@ -1072,6 +1088,21 @@ impl DocumentRuntime {
|
||||
self.policy_container = policy;
|
||||
}
|
||||
|
||||
/// Install inherited delivery state exactly once for a newly allocated
|
||||
/// Document, independently of whether its Window is new or reused.
|
||||
pub(crate) fn initialize_new_document_policy(
|
||||
&mut self,
|
||||
policy: DocumentPolicyContainer,
|
||||
bypass: bool,
|
||||
) {
|
||||
self.set_bypass_content_security_policy(bypass);
|
||||
self.initialize_inherited_meta_content_security_policies(
|
||||
self.document_handle(),
|
||||
&policy.inherited_meta_content_security_policies,
|
||||
);
|
||||
self.set_initial_document_policy_container(policy);
|
||||
}
|
||||
|
||||
pub(crate) fn set_main_navigation_policy_container(
|
||||
&mut self,
|
||||
mut policy: DocumentPolicyContainer,
|
||||
|
||||
@@ -294,6 +294,7 @@ pub use runtime::{
|
||||
RendererNativeCommandResponseGuard, RendererNativeCommandTerminal, RendererNativeOperation,
|
||||
RendererNativeOperationStep, RendererNativeProtocolError, RendererNativeProtocolNotification,
|
||||
RendererNativeProtocolResponse, RendererNativeProtocolStateUpdate, RendererNavigationInitiator, RendererNavigationHistory, RendererNavigationHistoryRequest,
|
||||
RendererNavigationPolicy,
|
||||
RendererOutputCursor, RendererOutputFence, RendererOutputFenceLeaseId, RendererOutputItem,
|
||||
RendererOutputPublication, RendererOutputPublicationOrdering, RendererOutputRecord,
|
||||
RendererOutputResidenceIdentity, RendererOutputStreamCloseReason, RendererOutputStreamControl,
|
||||
|
||||
@@ -24,6 +24,7 @@ impl JsContextHost {
|
||||
initiator,
|
||||
initiator_url,
|
||||
target_snapshot,
|
||||
source_policy,
|
||||
} = navigation;
|
||||
if !self.child_browsing_contexts.contains_key(&handle) {
|
||||
return None;
|
||||
@@ -88,6 +89,10 @@ impl JsContextHost {
|
||||
match self.materialize_local_child_snapshot_for_bootstrap(handle, &bootstrap) {
|
||||
LocalChildDocumentPreparation::Ready(snapshot) => {
|
||||
let mut snapshot = *snapshot;
|
||||
if let Some(policy) = &source_policy {
|
||||
policy.apply_to(&mut snapshot.policy_container, &snapshot.url);
|
||||
}
|
||||
snapshot = self.apply_page_csp_bypass_to_child_snapshot(snapshot);
|
||||
if !target_snapshot.apply_response_policy(
|
||||
&mut snapshot.policy_container,
|
||||
&snapshot.url,
|
||||
@@ -144,7 +149,8 @@ impl JsContextHost {
|
||||
initiator_url,
|
||||
initiator,
|
||||
target_snapshot,
|
||||
),
|
||||
)
|
||||
.with_source_policy(source_policy),
|
||||
)
|
||||
.is_some()
|
||||
{
|
||||
|
||||
@@ -124,6 +124,7 @@ impl JsContextHost {
|
||||
initiator,
|
||||
initiator_url,
|
||||
target_snapshot,
|
||||
source_policy,
|
||||
} = navigation;
|
||||
let target_url = Self::child_browsing_context_bootstrap_url(&bootstrap)?;
|
||||
let initiating_loader = self.document_resource_loader_for_owner(navigation_load.owner())?;
|
||||
@@ -234,6 +235,7 @@ impl JsContextHost {
|
||||
request_method: request.method.clone(),
|
||||
request_headers: request.request_headers.to_byte_strings(),
|
||||
target: target_snapshot,
|
||||
source_policy,
|
||||
};
|
||||
if let Some(client_id) = service_worker_client_id
|
||||
&& let Some(response) = browser_context_runtime
|
||||
@@ -929,6 +931,7 @@ struct ChildDocumentRequestSnapshot {
|
||||
request_method: String,
|
||||
request_headers: Vec<(String, String)>,
|
||||
target: ChildNavigationTargetSnapshot,
|
||||
source_policy: Option<crate::runtime::RendererNavigationPolicy>,
|
||||
}
|
||||
|
||||
fn child_document_load_outcome_from_response(
|
||||
@@ -973,8 +976,12 @@ fn child_document_load_outcome_from_response(
|
||||
.negotiated_http_version
|
||||
.filter(|_| head.cache_state != moli_fetch::ResponseCacheState::Local),
|
||||
};
|
||||
let mut policy_container =
|
||||
DocumentPolicyContainer::from_navigation_response_headers(&head.headers, &head.final_url);
|
||||
let mut policy_container = DocumentPolicyContainer::for_navigation_response(
|
||||
&head.headers,
|
||||
&head.final_url,
|
||||
None,
|
||||
request.source_policy.as_ref(),
|
||||
);
|
||||
if !request
|
||||
.target
|
||||
.apply_response_policy(&mut policy_container, &head.final_url, &head.headers)
|
||||
@@ -1128,6 +1135,7 @@ mod tests {
|
||||
request_method: "GET".to_owned(),
|
||||
request_headers: Vec::new(),
|
||||
target: Default::default(),
|
||||
source_policy: None,
|
||||
},
|
||||
head.clone(),
|
||||
moli_fetch::ResponseBody::materialized_bytes(b"body".to_vec()),
|
||||
@@ -1166,6 +1174,7 @@ mod tests {
|
||||
request_method: "GET".to_owned(),
|
||||
request_headers: Vec::new(),
|
||||
target: Default::default(),
|
||||
source_policy: None,
|
||||
},
|
||||
moli_fetch::ResponseHead {
|
||||
status_text: None,
|
||||
|
||||
@@ -45,11 +45,15 @@ impl JsContextHost {
|
||||
)
|
||||
});
|
||||
if request.method == "GET" {
|
||||
let policy = initiator
|
||||
.and_then(|source| self.document_policy_container_for_inheritance(source))
|
||||
.map(|policy| crate::runtime::RendererNavigationPolicy::capture(&policy));
|
||||
return self.queue_deferred_child_browsing_context_navigation_from_entry_seed(
|
||||
handle,
|
||||
request.url.as_str(),
|
||||
entry_seed,
|
||||
None,
|
||||
policy,
|
||||
);
|
||||
}
|
||||
self.reject_replaced_service_worker_child_client_navigation(
|
||||
@@ -70,7 +74,7 @@ impl JsContextHost {
|
||||
self.snapshot_child_navigation_target(handle, &url),
|
||||
),
|
||||
false,
|
||||
None,
|
||||
initiator,
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
@@ -196,6 +200,7 @@ impl JsContextHost {
|
||||
resolved_url: &str,
|
||||
entry_seed: NavigationHistoryEntrySeed,
|
||||
initiator_url: Option<Url>,
|
||||
source_policy: Option<crate::runtime::RendererNavigationPolicy>,
|
||||
) -> bool {
|
||||
if !self.child_browsing_contexts.contains_key(&handle) {
|
||||
return false;
|
||||
@@ -223,7 +228,8 @@ impl JsContextHost {
|
||||
initiator_url,
|
||||
ChildDocumentNavigationInitiator::BrowsingContext,
|
||||
self.snapshot_child_navigation_target(handle, &url),
|
||||
),
|
||||
)
|
||||
.with_source_policy(source_policy),
|
||||
false,
|
||||
None,
|
||||
)
|
||||
|
||||
@@ -418,8 +418,6 @@ impl JsContextHost {
|
||||
) -> Option<ChildDocumentCommitResult> {
|
||||
let entry_snapshot = self.child_browsing_contexts.get(&handle).cloned()?;
|
||||
let pending = entry_snapshot.pending_navigation()?;
|
||||
let pending_bootstrap = pending.bootstrap;
|
||||
let initiator_url = pending.initiator_url;
|
||||
if self.current_child_navigation_load(handle) != Some(navigation_load) {
|
||||
tracing::debug!(
|
||||
?handle,
|
||||
@@ -428,7 +426,7 @@ impl JsContextHost {
|
||||
);
|
||||
return None;
|
||||
}
|
||||
if let ChildBrowsingContextBootstrap::Url(url) = &pending_bootstrap
|
||||
if let ChildBrowsingContextBootstrap::Url(url) = &pending.bootstrap
|
||||
&& url.scheme() == "javascript"
|
||||
{
|
||||
self.queue_child_browsing_context_javascript_url_execution(
|
||||
@@ -441,11 +439,13 @@ impl JsContextHost {
|
||||
self.sync_existing_child_browsing_context_window_state(scope, handle);
|
||||
return None;
|
||||
}
|
||||
let initiator = if self.child_browsing_context_has_pending_cross_document_traversal(handle)
|
||||
{
|
||||
ChildDocumentNavigationInitiator::HistoryTraversal
|
||||
} else {
|
||||
pending.initiator
|
||||
let pending = PendingChildFrameNavigation {
|
||||
initiator: if self.child_browsing_context_has_pending_cross_document_traversal(handle) {
|
||||
ChildDocumentNavigationInitiator::HistoryTraversal
|
||||
} else {
|
||||
pending.initiator
|
||||
},
|
||||
..pending
|
||||
};
|
||||
self.clear_child_browsing_context_pending_navigation(handle);
|
||||
self.clear_pending_form_submission_child_target(handle);
|
||||
@@ -453,12 +453,7 @@ impl JsContextHost {
|
||||
scope,
|
||||
handle,
|
||||
navigation_load,
|
||||
PendingChildFrameNavigation::new(
|
||||
pending_bootstrap,
|
||||
initiator_url,
|
||||
initiator,
|
||||
pending.target_snapshot,
|
||||
),
|
||||
pending,
|
||||
);
|
||||
self.sync_existing_child_browsing_context_window_state(scope, handle);
|
||||
commit_result
|
||||
|
||||
@@ -102,6 +102,7 @@ pub(super) struct PendingChildFrameNavigation {
|
||||
pub(super) initiator_url: Option<Url>,
|
||||
pub(super) initiator: ChildDocumentNavigationInitiator,
|
||||
pub(super) target_snapshot: ChildNavigationTargetSnapshot,
|
||||
pub(super) source_policy: Option<crate::runtime::RendererNavigationPolicy>,
|
||||
}
|
||||
|
||||
impl PendingChildFrameNavigation {
|
||||
@@ -116,8 +117,17 @@ impl PendingChildFrameNavigation {
|
||||
initiator_url,
|
||||
initiator,
|
||||
target_snapshot,
|
||||
source_policy: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn with_source_policy(
|
||||
mut self,
|
||||
policy: Option<crate::runtime::RendererNavigationPolicy>,
|
||||
) -> Self {
|
||||
self.source_policy = policy;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@@ -678,6 +678,12 @@ impl JsContextHost {
|
||||
if !self.child_browsing_contexts.contains_key(&handle) {
|
||||
return None;
|
||||
};
|
||||
let source_policy = pending_navigation.source_policy.clone().or_else(|| {
|
||||
initiator
|
||||
.and_then(|source| self.document_policy_container_for_inheritance(source))
|
||||
.map(|policy| crate::runtime::RendererNavigationPolicy::capture(&policy))
|
||||
});
|
||||
let pending_navigation = pending_navigation.with_source_policy(source_policy);
|
||||
self.cancel_planned_form_navigation_for_url(
|
||||
super::super::OwnerDispatchScope::Child(handle),
|
||||
Self::child_browsing_context_bootstrap_url(&pending_navigation.bootstrap).as_ref(),
|
||||
|
||||
@@ -255,6 +255,7 @@ impl JsContextHost {
|
||||
&url,
|
||||
record.seed,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -319,6 +319,12 @@ impl JsContextHost {
|
||||
browser_navigation_kind: BrowserNavigationRequestKind,
|
||||
about_document_state: Option<crate::runtime::RendererAboutDocumentState>,
|
||||
) {
|
||||
let navigation_initiator =
|
||||
if browser_navigation_kind == BrowserNavigationRequestKind::Navigate {
|
||||
self.navigation_initiator_for_owner(OwnerDispatchScope::Top)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.cancel_planned_form_navigation_for_url(OwnerDispatchScope::Top, Some(&url));
|
||||
self.clear_pending_top_level_navigation();
|
||||
let handoff = self.top_level_navigation_handoff_tx.next_handoff();
|
||||
@@ -337,7 +343,7 @@ impl JsContextHost {
|
||||
request_body,
|
||||
request_headers,
|
||||
browser_navigation_kind,
|
||||
navigation_initiator: None,
|
||||
navigation_initiator,
|
||||
initial_document_environment: None,
|
||||
entry_seed,
|
||||
about_document_state,
|
||||
|
||||
@@ -373,6 +373,20 @@ impl JsContextHost {
|
||||
Some(policy)
|
||||
}
|
||||
|
||||
pub(crate) fn navigation_initiator_for_owner(
|
||||
&self,
|
||||
owner: OwnerDispatchScope,
|
||||
) -> Option<crate::runtime::RendererNavigationInitiator> {
|
||||
let policy = self.document_policy_container_for_inheritance(owner)?;
|
||||
let loader = self.document_resource_loader_for_dispatch_scope(owner)?;
|
||||
Some(crate::runtime::RendererNavigationInitiator::new(
|
||||
self.document_referrer_source_url_for_dispatch_scope(owner)?,
|
||||
loader.fetch_context().request_origin(),
|
||||
policy.referrer_policy.clone(),
|
||||
policy,
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn capture_about_document_state(
|
||||
&self,
|
||||
owner: OwnerDispatchScope,
|
||||
|
||||
@@ -232,6 +232,7 @@ fn navigate_element_popup_target(
|
||||
.map(str::to_owned)
|
||||
.or_else(|| creator.policy_container.referrer_policy.clone())
|
||||
},
|
||||
creator.policy_container.clone(),
|
||||
);
|
||||
creator.policy_container.document_referrer = navigation_initiator
|
||||
.document_referrer(&url::Url::parse(resolved_url).expect("resolved hyperlink URL"));
|
||||
@@ -728,6 +729,9 @@ pub(in crate::native_bridge) fn navigate_element_target_browsing_context(
|
||||
return false;
|
||||
};
|
||||
let initiator_url = runtime.document_url_for_handle(document);
|
||||
let Some(policy) = runtime.document_policy_container_for_inheritance(source) else {
|
||||
return false;
|
||||
};
|
||||
let initiator = crate::runtime::RendererNavigationInitiator::new(
|
||||
runtime
|
||||
.document_referrer_source_url_for_dispatch_scope(source)
|
||||
@@ -743,12 +747,9 @@ pub(in crate::native_bridge) fn navigate_element_target_browsing_context(
|
||||
} else {
|
||||
element_popup_referrer_policy(runtime, source_handle)
|
||||
.map(str::to_owned)
|
||||
.or_else(|| {
|
||||
runtime
|
||||
.document_policy_container_for_inheritance(source)
|
||||
.and_then(|policy| policy.referrer_policy)
|
||||
})
|
||||
.or_else(|| policy.referrer_policy.clone())
|
||||
},
|
||||
policy,
|
||||
);
|
||||
let Some(source_element) =
|
||||
runtime
|
||||
|
||||
@@ -956,6 +956,10 @@ pub(in crate::native_bridge) fn apply_planned_form_navigation(
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let initiator = navigation
|
||||
.source_document
|
||||
.and_then(|document| runtime.owner_dispatch_scope_for_node(document))
|
||||
.and_then(|owner| runtime.navigation_initiator_for_owner(owner));
|
||||
runtime.record_pending_location_navigation_request(
|
||||
request.url,
|
||||
request.method,
|
||||
@@ -965,6 +969,9 @@ pub(in crate::native_bridge) fn apply_planned_form_navigation(
|
||||
moli_fetch::BrowserNavigationRequestKind::Navigate,
|
||||
None,
|
||||
);
|
||||
if let Some(initiator) = initiator {
|
||||
runtime.set_pending_location_navigation_source(initiator, None);
|
||||
}
|
||||
crate::context_bootstrap::web_mcp::bind_root_navigation(runtime, navigation.form);
|
||||
true
|
||||
}
|
||||
|
||||
@@ -389,6 +389,7 @@ pub use self::page_surface::{
|
||||
RendererLayoutMetrics, RendererMainDocumentCommit, RendererMoliDomMemoryDiagnostics,
|
||||
RendererMoliMemoryDiagnostics, RendererMoliMemoryScopeDiagnostics,
|
||||
RendererMoliRuntimeMemoryDiagnostics, RendererNavigationInitiator, RendererPageCommand,
|
||||
RendererNavigationPolicy,
|
||||
RendererPageCommandPostResponseContinuation, RendererPageCookieFacadeSnapshotReply,
|
||||
RendererPageCreationDiagnostics, RendererPageDiagnosticsSnapshot, RendererPageDumpFormat,
|
||||
RendererPageDumpOptions, RendererPageDumpStripOptions, RendererPageReply, RendererPageView,
|
||||
|
||||
@@ -88,16 +88,17 @@ impl RendererCreateStreamingRawPageRequest {
|
||||
let about_document_state = navigation_history
|
||||
.as_ref()
|
||||
.and_then(|request| request.about_document_state(&final_url));
|
||||
let document_policy_container = about_document_state
|
||||
.as_ref()
|
||||
.map(|state| state.policy_container().clone())
|
||||
.unwrap_or_else(|| {
|
||||
DocumentPolicyContainer::from_navigation_response_headers(
|
||||
&response_headers,
|
||||
&final_url,
|
||||
)
|
||||
})
|
||||
.with_content_security_policy_bypass(document_settings.bypass_content_security_policy);
|
||||
let document_policy_container = DocumentPolicyContainer::for_navigation_response(
|
||||
&response_headers,
|
||||
&final_url,
|
||||
about_document_state
|
||||
.as_ref()
|
||||
.map(|state| state.policy_container()),
|
||||
main_document_commit
|
||||
.as_ref()
|
||||
.and_then(|commit| commit.navigation_policy.as_ref()),
|
||||
)
|
||||
.with_content_security_policy_bypass(document_settings.bypass_content_security_policy);
|
||||
let document_default_language =
|
||||
crate::document_language::document_default_language_from_headers(&response_headers);
|
||||
let document_last_modified =
|
||||
|
||||
@@ -878,16 +878,17 @@ impl RendererOwnerHandle {
|
||||
let about_document_state = navigation_history
|
||||
.as_ref()
|
||||
.and_then(|request| request.about_document_state(&final_url));
|
||||
let document_policy_container = about_document_state
|
||||
.as_ref()
|
||||
.map(|state| state.policy_container().clone())
|
||||
.unwrap_or_else(|| {
|
||||
DocumentPolicyContainer::from_navigation_response_headers(
|
||||
&response_headers,
|
||||
&final_url,
|
||||
)
|
||||
})
|
||||
.with_content_security_policy_bypass(document_settings.bypass_content_security_policy);
|
||||
let document_policy_container = DocumentPolicyContainer::for_navigation_response(
|
||||
&response_headers,
|
||||
&final_url,
|
||||
about_document_state
|
||||
.as_ref()
|
||||
.map(|state| state.policy_container()),
|
||||
main_document_commit
|
||||
.as_ref()
|
||||
.and_then(|commit| commit.navigation_policy.as_ref()),
|
||||
)
|
||||
.with_content_security_policy_bypass(document_settings.bypass_content_security_policy);
|
||||
let document_default_language =
|
||||
crate::document_language::document_default_language_from_headers(&response_headers);
|
||||
let document_last_modified =
|
||||
|
||||
@@ -42,8 +42,8 @@ pub use javascript_dialog::{
|
||||
};
|
||||
pub use popup_activation::RendererPopupActivationParts;
|
||||
pub use popup_activation::{
|
||||
RendererNavigationInitiator, RendererPendingPopupActivation, RendererPopupActivationSource,
|
||||
RendererPopupDisposition,
|
||||
RendererNavigationInitiator, RendererNavigationPolicy, RendererPendingPopupActivation,
|
||||
RendererPopupActivationSource, RendererPopupDisposition,
|
||||
};
|
||||
pub use window_document_source::RendererWindowDocumentSource;
|
||||
|
||||
@@ -928,6 +928,8 @@ pub struct RendererMainDocumentCommit {
|
||||
pub secure_context_type: String,
|
||||
/// Selected request referrer, including an explicitly suppressed empty value.
|
||||
pub document_referrer: Option<String>,
|
||||
/// Frozen policy for a local-scheme destination, independent of its origin.
|
||||
pub navigation_policy: Option<RendererNavigationPolicy>,
|
||||
pub timestamp: f64,
|
||||
/// Browser-owned session-history cursor and length at this document's commit.
|
||||
pub session_history_position: Option<moli_session_history::SessionHistoryPosition>,
|
||||
|
||||
@@ -3,6 +3,66 @@ use std::sync::Arc;
|
||||
use super::{RendererDocumentLifecycleIdentity, RendererWindowDocumentSource};
|
||||
use crate::SharedWebStorageStore;
|
||||
|
||||
/// An immutable navigation policy, without source Window or origin authority.
|
||||
/// Frame sandbox, permissions and storage state belong to the destination.
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct RendererNavigationPolicy(Arc<NavigationPolicyData>);
|
||||
|
||||
#[derive(Debug, Default, Eq, PartialEq)]
|
||||
struct NavigationPolicyData {
|
||||
referrer_policy: Option<String>,
|
||||
embedder_policy: crate::cross_origin_isolation::CrossOriginEmbedderPolicy,
|
||||
document_content_security_policies: Vec<String>,
|
||||
csp: crate::content_security_policy::InheritedContentSecurityPolicy,
|
||||
}
|
||||
|
||||
impl RendererNavigationPolicy {
|
||||
pub(crate) fn capture(policy: &crate::document_runtime::DocumentPolicyContainer) -> Self {
|
||||
Self(Arc::new(NavigationPolicyData {
|
||||
referrer_policy: policy.referrer_policy.clone(),
|
||||
embedder_policy: policy.cross_origin_embedder_policy,
|
||||
document_content_security_policies: policy.document_content_security_policies.clone(),
|
||||
csp: crate::content_security_policy::InheritedContentSecurityPolicy {
|
||||
self_url: policy.content_security_policy_self_url.clone(),
|
||||
header_policies: policy.response_content_security_policies.clone(),
|
||||
meta_policies: policy.inherited_meta_content_security_policies.clone(),
|
||||
report_only_policies: policy
|
||||
.response_content_security_report_only_policies
|
||||
.clone(),
|
||||
reporting_endpoints: policy.content_security_reporting_endpoints.clone(),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
pub(crate) fn apply_to(
|
||||
&self,
|
||||
policy: &mut crate::document_runtime::DocumentPolicyContainer,
|
||||
url: &url::Url,
|
||||
) {
|
||||
if !Self::applies_to(url) {
|
||||
return;
|
||||
}
|
||||
policy.referrer_policy = self.0.referrer_policy.clone();
|
||||
policy.cross_origin_embedder_policy = self.0.embedder_policy;
|
||||
policy.document_content_security_policies =
|
||||
self.0.document_content_security_policies.clone();
|
||||
policy.response_content_security_policies = self.0.csp.header_policies.clone();
|
||||
policy.inherited_meta_content_security_policies = self.0.csp.meta_policies.clone();
|
||||
policy.response_content_security_report_only_policies =
|
||||
self.0.csp.report_only_policies.clone();
|
||||
policy.content_security_policy_self_url = self.0.csp.self_url.clone();
|
||||
policy.content_security_reporting_endpoints = self.0.csp.reporting_endpoints.clone();
|
||||
policy.sandbox =
|
||||
crate::document_runtime::DocumentSandboxPolicy::from_response_content_security_policies(
|
||||
&self.0.csp.header_policies,
|
||||
);
|
||||
}
|
||||
|
||||
fn applies_to(url: &url::Url) -> bool {
|
||||
moli_url::is_about_blank(url) || matches!(url.scheme(), "blob" | "data")
|
||||
}
|
||||
}
|
||||
|
||||
/// Request context frozen in the initiating Document, before target selection
|
||||
/// or an Inspector startup pause can change the source's URL or policy.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -15,6 +75,7 @@ struct RendererNavigationInitiatorData {
|
||||
url: url::Url,
|
||||
origin: String,
|
||||
referrer_policy: String,
|
||||
policy: RendererNavigationPolicy,
|
||||
}
|
||||
|
||||
impl RendererNavigationInitiator {
|
||||
@@ -22,7 +83,13 @@ impl RendererNavigationInitiator {
|
||||
url: url::Url,
|
||||
origin: moli_url::WebOrigin,
|
||||
referrer_policy: Option<String>,
|
||||
policy: crate::document_runtime::DocumentPolicyContainer,
|
||||
) -> Self {
|
||||
let origin = if policy.sandbox.forces_opaque_origin {
|
||||
moli_url::WebOrigin::Opaque
|
||||
} else {
|
||||
origin
|
||||
};
|
||||
Self {
|
||||
data: Arc::new(RendererNavigationInitiatorData {
|
||||
url,
|
||||
@@ -33,6 +100,7 @@ impl RendererNavigationInitiator {
|
||||
.unwrap_or_else(|| moli_fetch::DEFAULT_REFERRER_POLICY.to_owned())
|
||||
},
|
||||
origin: origin.ascii_serialization().to_owned(),
|
||||
policy: RendererNavigationPolicy::capture(&policy),
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -45,6 +113,10 @@ impl RendererNavigationInitiator {
|
||||
moli_url::WebOrigin::from_serialized(&self.data.origin)
|
||||
}
|
||||
|
||||
pub fn document_policy(&self, destination: &url::Url) -> Option<RendererNavigationPolicy> {
|
||||
RendererNavigationPolicy::applies_to(destination).then(|| self.data.policy.clone())
|
||||
}
|
||||
|
||||
pub fn request_metadata(&self) -> moli_fetch::SubresourceRequestMetadata {
|
||||
moli_fetch::SubresourceRequestMetadata {
|
||||
referrer_policy: Some(self.data.referrer_policy.clone()),
|
||||
|
||||
@@ -1199,19 +1199,21 @@ impl PageVm {
|
||||
let navigation_handoff = environment
|
||||
.page_runtime_task_source()
|
||||
.next_top_level_navigation_handoff();
|
||||
let policy = bootstrap
|
||||
.initial_document_environment
|
||||
.as_ref()
|
||||
.map(|environment| environment.policy_container.clone())
|
||||
.unwrap_or_else(|| {
|
||||
crate::document_runtime::DocumentPolicyContainer::from_navigation_response_headers(
|
||||
&request.response_headers,
|
||||
&request.final_url,
|
||||
)
|
||||
.with_content_security_policy_bypass(
|
||||
request.document_settings.bypass_content_security_policy,
|
||||
)
|
||||
});
|
||||
let policy = crate::document_runtime::DocumentPolicyContainer::for_navigation_response(
|
||||
&request.response_headers,
|
||||
&request.final_url,
|
||||
bootstrap
|
||||
.initial_document_environment
|
||||
.as_ref()
|
||||
.map(|environment| &environment.policy_container),
|
||||
request
|
||||
.main_document_commit
|
||||
.as_ref()
|
||||
.and_then(|commit| commit.navigation_policy.as_ref()),
|
||||
)
|
||||
.with_content_security_policy_bypass(
|
||||
request.document_settings.bypass_content_security_policy,
|
||||
);
|
||||
let origin = bootstrap
|
||||
.initial_document_environment
|
||||
.as_ref()
|
||||
|
||||
@@ -1146,15 +1146,17 @@ impl PageVmEnvConfig {
|
||||
.about_document_state
|
||||
.take()
|
||||
.filter(|_| moli_url::is_about_blank(final_url));
|
||||
self.document_policy_container = self
|
||||
.about_document_state
|
||||
.as_ref()
|
||||
.map(|state| state.policy_container().clone())
|
||||
.unwrap_or_else(|| {
|
||||
crate::document_runtime::DocumentPolicyContainer::from_navigation_response_headers(
|
||||
headers, final_url,
|
||||
)
|
||||
})
|
||||
self.document_policy_container =
|
||||
crate::document_runtime::DocumentPolicyContainer::for_navigation_response(
|
||||
headers,
|
||||
final_url,
|
||||
self.about_document_state
|
||||
.as_ref()
|
||||
.map(|state| state.policy_container()),
|
||||
self.main_document_commit
|
||||
.as_ref()
|
||||
.and_then(|commit| commit.navigation_policy.as_ref()),
|
||||
)
|
||||
.with_content_security_policy_bypass(
|
||||
self.document_settings.bypass_content_security_policy,
|
||||
);
|
||||
|
||||
@@ -261,6 +261,7 @@ async fn selected_navigation_referrer_precedes_parser_and_document_start_scripts
|
||||
security_origin: "https://destination.test".to_owned(),
|
||||
secure_context_type: "Secure".to_owned(),
|
||||
document_referrer: Some(referrer.to_owned()),
|
||||
navigation_policy: None,
|
||||
timestamp: 0.0,
|
||||
session_history_position: None,
|
||||
browsing_context_group: None,
|
||||
|
||||
@@ -2101,7 +2101,16 @@ impl ScriptVmPageRealmBootstrap {
|
||||
let initial_policy = initial_environment
|
||||
.as_ref()
|
||||
.map(|environment| environment.policy_container.clone())
|
||||
.unwrap_or_default();
|
||||
.unwrap_or_else(|| {
|
||||
crate::document_runtime::DocumentPolicyContainer::for_navigation_response(
|
||||
&[],
|
||||
&document_url,
|
||||
None,
|
||||
main_document_commit
|
||||
.as_ref()
|
||||
.and_then(|commit| commit.navigation_policy.as_ref()),
|
||||
)
|
||||
});
|
||||
let mut frame_owner_store = FrameOwnerStore::default();
|
||||
frame_owner_store.ensure_main_frame(
|
||||
document_handle,
|
||||
@@ -2139,17 +2148,10 @@ impl ScriptVmPageRealmBootstrap {
|
||||
main_parser_continuation_sender,
|
||||
));
|
||||
document_runtime.set_author_styles_disabled(author_styles_disabled);
|
||||
document_runtime
|
||||
.set_bypass_content_security_policy(document_settings.bypass_content_security_policy);
|
||||
if initial_environment.is_some() {
|
||||
// Every initial Document, including a synchronously staged related
|
||||
// Page, installs its inherited meta policies before entering JS.
|
||||
document_runtime.initialize_inherited_meta_content_security_policies(
|
||||
document_handle,
|
||||
&initial_policy.inherited_meta_content_security_policies,
|
||||
);
|
||||
document_runtime.set_initial_document_policy_container(initial_policy);
|
||||
}
|
||||
document_runtime.initialize_new_document_policy(
|
||||
initial_policy,
|
||||
document_settings.bypass_content_security_policy,
|
||||
);
|
||||
let (page_context_cancel_tx, page_context_cancel_rx) =
|
||||
renderer_page_context_cancel_channel();
|
||||
|
||||
|
||||
@@ -111,7 +111,8 @@ impl ScriptVm {
|
||||
page_task_tx.main_parser_continuation_sender(),
|
||||
);
|
||||
runtime.set_author_styles_disabled(author_styles_disabled);
|
||||
runtime.set_bypass_content_security_policy(
|
||||
runtime.initialize_new_document_policy(
|
||||
env.document_policy_container.clone(),
|
||||
env.document_settings.bypass_content_security_policy,
|
||||
);
|
||||
runtime.retain_event_targets_from_initial_document(&mut vm.document_runtime);
|
||||
|
||||
Reference in New Issue
Block a user