Compare commits

...
Author SHA1 Message Date
A. F. Dudley abb601a17b Remove private key export handler and gate three message handlers
The REQUEST_ACCOUNT_PK handler (src/hooks/useExportPrivateKeyEmbed.ts)
sent the raw private key to any window that posted the message. It had
no origin check and no user approval. App.tsx mounted it on every route.
This commit deletes the hook and its two message constants.

Three other handlers had no origin check: REQUEST_WALLET_ACCOUNTS and
REQUEST_TX (WalletEmbed.tsx) and CHECK_BALANCE (App.tsx). This commit
adds the REACT_APP_ALLOWED_URLS check that the six other handlers use.

Local test on a dev build with REACT_APP_ALLOWED_URLS set to one origin:
- REQUEST_ACCOUNT_PK from an allowed and a disallowed origin: no reply.
- REQUEST_WALLET_ACCOUNTS from a disallowed origin: no reply.
- REQUEST_WALLET_ACCOUNTS from the allowed origin: reply with the address.
eslint and tsc --noEmit pass.
2026-11-15 20:30:19 +00:00
4 changed files with 40 additions and 47 deletions

No files matched your search

+14 -2
View File
@@ -43,10 +43,11 @@ import useAccountsData from "./hooks/useAccountsData";
import { useWebViewHandler } from "./hooks/useWebViewHandler";
import SignRequestEmbed from "./screens/SignRequestEmbed";
import useAddAccountEmbed from "./hooks/useAddAccountEmbed";
import useExportPKEmbed from "./hooks/useExportPrivateKeyEmbed";
import { SignTxEmbed } from "./screens/SignTxEmbed";
import useCreateNetwork from "./hooks/useCreateNetwork";
const REACT_APP_ALLOWED_URLS = import.meta.env.REACT_APP_ALLOWED_URLS;
const Stack = createStackNavigator<StackParamsList>();
const App = (): React.JSX.Element => {
@@ -235,6 +236,18 @@ const App = (): React.JSX.Element => {
const handleCheckBalance = async (event: MessageEvent) => {
if (event.data.type !== CHECK_BALANCE) return;
if (!REACT_APP_ALLOWED_URLS) {
console.log('Allowed URLs are not set');
return;
}
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map((url: string) => url.trim());
if (!allowedUrls.includes(event.origin)) {
console.log('Unauthorized app.');
return;
}
const { chainId, amount } = event.data;
const network = networksData.find(net => net.chainId === chainId);
@@ -287,7 +300,6 @@ const App = (): React.JSX.Element => {
useWebViewHandler();
useAddAccountEmbed();
useExportPKEmbed();
useCreateNetwork();
return (
-43
View File
@@ -1,43 +0,0 @@
import { useEffect } from 'react';
import { useAccounts } from '../context/AccountsContext';
import { getPathKey, sendMessage } from '../utils/misc';
import { ACCOUNT_PK_RESPONSE, REQUEST_ACCOUNT_PK } from '../utils/constants';
const useExportPKEmbed = () => {
const { accounts } = useAccounts();
useEffect(() => {
const handleMessage = async (event: MessageEvent) => {
const { type, chainId, address } = event.data;
if (type !== REQUEST_ACCOUNT_PK) return;
try {
const selectedAccount = accounts.find(account => account.address === address);
if (!selectedAccount) {
throw new Error("Account not found")
}
const pathKey = await getPathKey(chainId, selectedAccount.index);
const privateKey = pathKey.privKey;
sendMessage(
event.source as Window,
ACCOUNT_PK_RESPONSE,
{ privateKey },
event.origin,
);
} catch (error) {
console.error('Error fetching private key:', error);
}
};
window.addEventListener('message', handleMessage);
return () => {
window.removeEventListener('message', handleMessage);
};
}, [accounts]);
};
export default useExportPKEmbed;
+26
View File
@@ -28,6 +28,8 @@ import useGetOrCreateAccounts from '../hooks/useGetOrCreateAccounts';
import useAccountsData from '../hooks/useAccountsData';
import { REQUEST_TX, REQUEST_WALLET_ACCOUNTS, TRANSACTION_RESPONSE, WALLET_ACCOUNTS_DATA } from '../utils/constants';
const REACT_APP_ALLOWED_URLS = import.meta.env.REACT_APP_ALLOWED_URLS;
type TransactionDetails = {
chainId: string;
fromAddress: string;
@@ -54,6 +56,18 @@ export const WalletEmbed = () => {
const handleGetAccounts = async (event: MessageEvent) => {
if (event.data.type !== REQUEST_WALLET_ACCOUNTS) return;
if (!REACT_APP_ALLOWED_URLS) {
console.log('Allowed URLs are not set');
return;
}
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map((url: string) => url.trim());
if (!allowedUrls.includes(event.origin)) {
console.log('Unauthorized app.');
return;
}
const accountsData = await getAccountsData(event.data.chainId);
if (accountsData.length === 0) {
@@ -84,6 +98,18 @@ export const WalletEmbed = () => {
try {
if (event.data.type !== REQUEST_TX) return;
if (!REACT_APP_ALLOWED_URLS) {
console.log('Allowed URLs are not set');
return;
}
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map((url: string) => url.trim());
if (!allowedUrls.includes(event.origin)) {
console.log('Unauthorized app.');
return;
}
txEventRef.current = event;
const { chainId, fromAddress, toAddress, amount } = event.data;
-2
View File
@@ -70,7 +70,6 @@ export const REQUEST_SIGN_MESSAGE = 'REQUEST_SIGN_MESSAGE';
export const REQUEST_WALLET_ACCOUNTS = 'REQUEST_WALLET_ACCOUNTS';
export const REQUEST_CREATE_OR_GET_ACCOUNTS = 'REQUEST_CREATE_OR_GET_ACCOUNTS';
export const REQUEST_TX = 'REQUEST_TX';
export const REQUEST_ACCOUNT_PK = 'REQUEST_ACCOUNT_PK';
export const REQUEST_ADD_ACCOUNT = 'REQUEST_ADD_ACCOUNT';
export const AUTO_SIGN_IN = 'AUTO_SIGN_IN';
export const CHECK_BALANCE = 'CHECK_BALANCE';
@@ -82,7 +81,6 @@ export const SIGN_TX_RESPONSE = 'SIGN_TX_RESPONSE';
export const SIGN_MESSAGE_RESPONSE = 'SIGN_MESSAGE_RESPONSE';
export const TRANSACTION_RESPONSE = 'TRANSACTION_RESPONSE';
export const SIGN_IN_RESPONSE = 'SIGN_IN_RESPONSE';
export const ACCOUNT_PK_RESPONSE = 'ACCOUNT_PK_RESPONSE';
export const ADD_ACCOUNT_RESPONSE = 'ADD_ACCOUNT_RESPONSE';
export const WALLET_ACCOUNTS_DATA = 'WALLET_ACCOUNTS_DATA';
export const IS_SUFFICIENT = 'IS_SUFFICIENT';