神代綺凛 commited on
Commit
0153e4e
1 Parent(s): 0d58335

fix: cross-origin

Browse files
Files changed (2) hide show
  1. src/utils/const.ts +5 -9
  2. src/utils/postMessage.ts +2 -6
src/utils/const.ts CHANGED
@@ -2,13 +2,9 @@ export const IS_DEV = process.env.NODE_ENV === 'development';
2
 
3
  export const PARAM_MODE = (new URL(window.location.href).searchParams.get('mode') || '') as 'window' | 'iframe' | '';
4
 
5
- const trustOriginStr = __TRUST_ORIGIN__;
 
 
 
6
 
7
- export const TRUST_ALL_ORIGIN = trustOriginStr.trim() === '*';
8
-
9
- export const trustOrigins = new Set(
10
- trustOriginStr
11
- .split(',')
12
- .map(s => s.trim())
13
- .filter(Boolean),
14
- );
 
2
 
3
  export const PARAM_MODE = (new URL(window.location.href).searchParams.get('mode') || '') as 'window' | 'iframe' | '';
4
 
5
+ export const trustOrigins = __TRUST_ORIGIN__
6
+ .split(',')
7
+ .map(s => s.trim())
8
+ .filter(Boolean);
9
 
10
+ export const TRUST_ALL_ORIGIN = trustOrigins.includes('*');
 
 
 
 
 
 
 
src/utils/postMessage.ts CHANGED
@@ -14,11 +14,7 @@ export const postQrMessage = (data: Omit<QrMessage, 'mode'>) => {
14
  console.error('No target window');
15
  return;
16
  }
17
- const origin = targetWindow.location.origin;
18
- if (!TRUST_ALL_ORIGIN && !trustOrigins.has(origin)) {
19
- console.warn('Untrusted origin:', origin);
20
- return;
21
- }
22
  const message: QrMessage = { ...data, mode: PARAM_MODE };
23
- targetWindow.postMessage(message, origin);
 
24
  };
 
14
  console.error('No target window');
15
  return;
16
  }
 
 
 
 
 
17
  const message: QrMessage = { ...data, mode: PARAM_MODE };
18
+ if (TRUST_ALL_ORIGIN) targetWindow.postMessage(message, '*');
19
+ else trustOrigins.forEach(origin => targetWindow.postMessage(message, origin));
20
  };