diff --git a/components/constellation/constellation.rs b/components/constellation/constellation.rs index 9eef83f33a08..209094823b5a 100644 --- a/components/constellation/constellation.rs +++ b/components/constellation/constellation.rs @@ -1562,10 +1562,17 @@ where FromScriptMsg::PostMessage { target: browsing_context_id, source: source_pipeline_id, + source_origin: source_origin, target_origin: origin, data, } => { - self.handle_post_message_msg(browsing_context_id, source_pipeline_id, origin, data); + self.handle_post_message_msg( + browsing_context_id, + source_pipeline_id, + source_origin, + origin, + data, + ); }, FromScriptMsg::Focus => { self.handle_focus_msg(source_pipeline_id); @@ -3202,6 +3209,7 @@ where &mut self, browsing_context_id: BrowsingContextId, source_pipeline: PipelineId, + source_origin: Option, origin: Option, data: Vec, ) { @@ -3222,6 +3230,7 @@ where target: pipeline_id, source: source_pipeline, source_browsing_context: source_browsing_context, + source_origin: source_origin, target_origin: origin, data, }; diff --git a/components/script/dom/dissimilaroriginwindow.rs b/components/script/dom/dissimilaroriginwindow.rs index 8f7eda581006..197bf0fb4b18 100644 --- a/components/script/dom/dissimilaroriginwindow.rs +++ b/components/script/dom/dissimilaroriginwindow.rs @@ -152,8 +152,13 @@ impl DissimilarOriginWindowMethods for DissimilarOriginWindow { // TODO(#12717): Should implement the `transfer` argument. let data = StructuredCloneData::write(*cx, message)?; + let source_origin = match self.window_proxy().document() { + Some(document) => Some(document.origin().immutable().clone()), + None => None, + }; + // Step 9. - self.post_message(origin, data); + self.post_message(source_origin, origin, data); Ok(()) } @@ -186,7 +191,12 @@ impl DissimilarOriginWindowMethods for DissimilarOriginWindow { } impl DissimilarOriginWindow { - pub fn post_message(&self, origin: Option, data: StructuredCloneData) { + pub fn post_message( + &self, + source_origin: Option, + origin: Option, + data: StructuredCloneData, + ) { let incumbent = match GlobalScope::incumbent() { None => return warn!("postMessage called with no incumbent global"), Some(incumbent) => incumbent, @@ -194,6 +204,7 @@ impl DissimilarOriginWindow { let msg = ScriptMsg::PostMessage { target: self.window_proxy.browsing_context_id(), source: incumbent.pipeline_id(), + source_origin, target_origin: origin, data: data.move_to_arraybuffer(), }; diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index a79669d6c2e1..5952aa74735d 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -915,6 +915,7 @@ impl WindowMethods for Window { fn PostMessage(&self, cx: JSContext, message: HandleValue, origin: DOMString) -> ErrorResult { let source_global = GlobalScope::incumbent().expect("no incumbent global??"); let source = source_global.as_window(); + let source_origin = Some(source.Document().origin().immutable().clone()); // Step 3-5. let origin = match &origin[..] { @@ -931,7 +932,7 @@ impl WindowMethods for Window { let data = StructuredCloneData::write(*cx, message)?; // Step 9. - self.post_message(origin, &*source.window_proxy(), data); + self.post_message(source_origin, origin, &*source.window_proxy(), data); Ok(()) } @@ -2269,12 +2270,14 @@ impl Window { // https://html.spec.whatwg.org/multipage/#dom-window-postmessage step 7. pub fn post_message( &self, + source_origin: Option, target_origin: Option, source: &WindowProxy, serialize_with_transfer_result: StructuredCloneData, ) { let this = Trusted::new(self); let source = Trusted::new(source); + let origin = source_origin.map(|origin| origin.ascii_serialization()); let task = task!(post_serialised_message: move || { let this = this.root(); let source = source.root(); @@ -2305,7 +2308,7 @@ impl Window { this.upcast(), this.upcast(), message_clone.handle(), - None, + origin.as_ref().map( |origin| { &**origin } ), Some(&*source), ); }); diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index 4cda69cdcedc..1f9f056727b7 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -1838,12 +1838,14 @@ impl ScriptThread { target: target_pipeline_id, source: source_pipeline_id, source_browsing_context, + source_origin, target_origin: origin, data, } => self.handle_post_message_msg( target_pipeline_id, source_pipeline_id, source_browsing_context, + source_origin, origin, data, ), @@ -2488,6 +2490,7 @@ impl ScriptThread { pipeline_id: PipelineId, source_pipeline_id: PipelineId, source_browsing_context: TopLevelBrowsingContextId, + source_origin: Option, origin: Option, data: Vec, ) { @@ -2511,7 +2514,12 @@ impl ScriptThread { Some(source) => source, }; // FIXME(#22512): enqueues a task; unnecessary delay. - window.post_message(origin, &*source, StructuredCloneData::Vector(data)) + window.post_message( + source_origin, + origin, + &*source, + StructuredCloneData::Vector(data), + ) }, } } diff --git a/components/script_traits/lib.rs b/components/script_traits/lib.rs index 1aa37e565337..19f9e34cfd6c 100644 --- a/components/script_traits/lib.rs +++ b/components/script_traits/lib.rs @@ -322,6 +322,8 @@ pub enum ConstellationControlMsg { source: PipelineId, /// The top level browsing context associated with the source pipeline. source_browsing_context: TopLevelBrowsingContextId, + /// Source origin of the message + source_origin: Option, /// The expected origin of the target. target_origin: Option, /// The data to be posted. diff --git a/components/script_traits/script_msg.rs b/components/script_traits/script_msg.rs index c3ee8d752054..54d06dac9dc5 100644 --- a/components/script_traits/script_msg.rs +++ b/components/script_traits/script_msg.rs @@ -164,6 +164,8 @@ pub enum ScriptMsg { target: BrowsingContextId, /// The source of the posted message. source: PipelineId, + /// Source origin of the message + source_origin: Option, /// The expected origin of the target. target_origin: Option, /// The data to be posted.