11import { EventType , EventData } from './types' ;
2- import { isIPAddress } from '../utilities/isIPAddress' ;
32
43const ALLOWED_ORIGINS : string [ ] = [
54 'https://nmrxiv.org' ,
@@ -16,22 +15,6 @@ const ALLOWED_ORIGINS: string[] = [
1615
1716const namespace = 'nmr-wrapper' ;
1817
19- function parseOrigin ( origin : string ) {
20- let url : string | null = '' ;
21- const urlSegments = origin . split ( '://' ) ;
22- if ( isIPAddress ( origin ) ) {
23- url = origin ;
24- } else {
25- const hostSegments = urlSegments [ 1 ] . split ( '.' ) ;
26- const startIndex = hostSegments . length > 2 ? 1 : 0 ;
27- url = `${ urlSegments [ 0 ] } ://${ hostSegments
28- . slice ( hostSegments . length > 1 ? startIndex : 0 )
29- . join ( '.' ) } `;
30- }
31-
32- return url ;
33- }
34-
3518function trigger < T extends EventType > ( type : T , data : EventData < T > ) {
3619 window . parent . postMessage ( { type : `${ namespace } :${ type } ` , data } , '*' ) ;
3720}
@@ -47,7 +30,9 @@ function on<T extends EventType>(
4730 data : { type : targetType , data } ,
4831 } = event ;
4932
50- if ( ! ALLOWED_ORIGINS . includes ( parseOrigin ( origin ) ) ) {
33+ const url = new URL ( origin ) ;
34+
35+ if ( ! ALLOWED_ORIGINS . includes ( url . origin ) ) {
5136 throw new Error ( `Invalid Origin ${ origin } ` ) ;
5237 }
5338
0 commit comments