|
2 | 2 | * Copyright (c) 2018 Gnock |
3 | 3 | * Copyright (c) 2018-2019 The Masari Project |
4 | 4 | * Copyright (c) 2018-2020 The Karbo developers |
5 | | - * Copyright (c) 2018-2023 Conceal Community, Conceal.Network & Conceal Devs |
| 5 | + * Copyright (c) 2018-2025 Conceal Community, Conceal.Network & Conceal Devs |
6 | 6 | * |
7 | 7 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
8 | 8 | * |
|
17 | 17 |
|
18 | 18 | import {WalletRepository} from "./model/WalletRepository"; |
19 | 19 |
|
| 20 | +// List of allowed parent origins |
| 21 | +const ALLOWED_ORIGINS = [ |
| 22 | + 'http://localhost:3000', |
| 23 | + 'https://wallet.conceal.network', |
| 24 | + 'https://wws.conceal.network' |
| 25 | +]; |
| 26 | + |
20 | 27 | function sendMessageToParent(type : string, data : any){ |
21 | | - window.parent.postMessage({ |
22 | | - type:type, |
23 | | - payload:data |
24 | | - }, '*'); |
| 28 | + // Get the parent origin from referrer or use the production URL as fallback |
| 29 | + const parentOrigin = document.referrer ? |
| 30 | + new URL(document.referrer).origin : |
| 31 | + ALLOWED_ORIGINS[1]; // wallet.conceal.network |
| 32 | + |
| 33 | + // Only send message if the origin is in our allowed list |
| 34 | + if (ALLOWED_ORIGINS.includes(parentOrigin)) { |
| 35 | + window.parent.postMessage({ |
| 36 | + type: type, |
| 37 | + payload: data |
| 38 | + }, parentOrigin); |
| 39 | + } else { |
| 40 | + console.warn('Attempted to send message to non-allowed origin:', parentOrigin); |
| 41 | + } |
25 | 42 | } |
26 | 43 |
|
27 | 44 | window.addEventListener('message', function(e : MessageEvent){ |
28 | | - //console.log(e); |
| 45 | + // Verify the origin of the message for security |
| 46 | + if (!ALLOWED_ORIGINS.includes(e.origin)) { |
| 47 | + console.warn('Received message from non-allowed origin:', e.origin); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Process the message only if it comes from an allowed origin |
29 | 52 | if(e.data == 'hasWallet'){ |
30 | 53 | sendMessageToParent('hasWallet', WalletRepository.hasOneStored()); |
31 | 54 | } |
|
0 commit comments