@@ -88,15 +88,19 @@ export class ElementIframeComponent implements OnInit, AfterViewInit, OnDestroy
8888 this . view . iframeElement = this . iframeWindow ;
8989
9090 this . eventHandler = function ( e : any ) {
91+ if ( e . source !== this . iframeWindow || e . origin !== this . getIframeOrigin ( ) ) {
92+ return ;
93+ }
9194 const msg = e . data ;
95+ if ( ! msg || typeof msg !== 'object' ) return ;
9296
9397 if ( msg . id !== this . id ) {
9498 return ;
9599 }
96100
97101 switch ( msg . name ) {
98102 case 'PING' : {
99- this . iframeWindow . postMessage ( { name : 'PONG' , id : this . id } , '*' ) ;
103+ this . postToIframe ( { name : 'PONG' , id : this . id } ) ;
100104 break ;
101105 }
102106 case 'PONG' :
@@ -166,14 +170,30 @@ export class ElementIframeComponent implements OnInit, AfterViewInit, OnDestroy
166170 this . handleIframeEvent ( ) ;
167171 }
168172
173+ private getIframeOrigin ( ) : string | null {
174+ try {
175+ const url = new URL ( this . src , window . location . href ) ;
176+ return [ 'http:' , 'https:' ] . includes ( url . protocol ) ? url . origin : null ;
177+ } catch {
178+ return null ;
179+ }
180+ }
181+
182+ private postToIframe ( message : unknown ) : void {
183+ const origin = this . getIframeOrigin ( ) ;
184+ if ( origin && this . iframeWindow ) {
185+ this . iframeWindow . postMessage ( message , origin ) ;
186+ }
187+ }
188+
169189 ngOnDestroy ( ) {
170190 window . removeEventListener ( 'message' , this . eventHandler ) ;
171191 this . subscription . unsubscribe ( ) ;
172192 }
173193
174194 setActive ( ) {
175195 this . _logger . debug ( `[Luna] Send FOCUS to: ${ this . id } ` ) ;
176- this . iframeWindow . postMessage ( { name : 'FOCUS' } , '*' ) ;
196+ this . postToIframe ( { name : 'FOCUS' } ) ;
177197 }
178198
179199 handleIframeEvent ( ) {
@@ -187,7 +207,7 @@ export class ElementIframeComponent implements OnInit, AfterViewInit, OnDestroy
187207 // @ts -ignore
188208 this . ping = setInterval ( ( ) => {
189209 this . _logger . info ( `[Luna] Send PING to: ${ this . id } ` ) ;
190- this . iframeWindow . postMessage ( { name : 'PING' , id : this . id , disbaleFileManager } , '*' ) ;
210+ this . postToIframe ( { name : 'PING' , id : this . id , disbaleFileManager } ) ;
191211 } , 500 ) ;
192212
193213 // 30s 内未PING通, 则主动关闭
@@ -201,15 +221,15 @@ export class ElementIframeComponent implements OnInit, AfterViewInit, OnDestroy
201221
202222 this . subscription = this . _iframeSvc . message$ . subscribe ( message => {
203223 // this._logger.info('[Luna] Send msg to iframe: ', message);
204- this . iframeWindow . postMessage ( message , '*' ) ;
224+ this . postToIframe ( message ) ;
205225 } ) ;
206226
207227 window . addEventListener ( 'message' , this . eventHandler ) ;
208228 }
209229
210230 sendCommand ( data ) {
211231 this . _logger . info ( `[Luna] Send CMD to: ${ this . id } ` ) ;
212- this . iframeWindow . postMessage ( { name : 'CMD' , data : data . data } , '*' ) ;
232+ this . postToIframe ( { name : 'CMD' , data : data . data } ) ;
213233 }
214234
215235 // 没有位置用啊
@@ -252,7 +272,7 @@ export class ElementIframeComponent implements OnInit, AfterViewInit, OnDestroy
252272
253273 sendInputActive ( ) {
254274 this . _logger . info ( `[Luna] Send Input_ACTIVE to: ${ this . id } ` ) ;
255- this . iframeWindow . postMessage ( { name : 'INPUT_ACTIVE' , data : "" } , '*' ) ;
275+ this . postToIframe ( { name : 'INPUT_ACTIVE' , data : "" } ) ;
256276 }
257277
258278 sendInputActiveToOtherViews ( ) {
0 commit comments