1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

limit connections (#18355)

limit background connections
This commit is contained in:
witmicko 2023-05-03 16:39:56 +01:00 committed by GitHub
parent c4e1edef19
commit 8d2803c179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,6 +32,7 @@ import {
import { checkForLastErrorAndLog } from '../../shared/modules/browser-runtime.utils';
import { isManifestV3 } from '../../shared/modules/mv3.utils';
import { maskObject } from '../../shared/modules/object.utils';
import { getEnvironmentType, deferredPromise, getPlatform } from './lib/util';
import migrations from './migrations';
import Migrator from './lib/migrator';
import ExtensionPlatform from './platforms/extension';
@ -50,7 +51,6 @@ import rawFirstTimeState from './first-time-state';
import getFirstPreferredLangCode from './lib/get-first-preferred-lang-code';
import getObjStructure from './lib/getObjStructure';
import setupEnsIpfsResolver from './lib/ens-ipfs/setup';
import { deferredPromise, getPlatform } from './lib/util';
/* eslint-enable import/first */
@ -84,6 +84,7 @@ let popupIsOpen = false;
let notificationIsOpen = false;
let uiIsTriggering = false;
const openMetamaskTabsIDs = {};
const openMetamaskConnections = new Map();
const requestAccountTabIds = {};
let controller;
@ -184,10 +185,28 @@ let connectExternal;
browser.runtime.onConnect.addListener(async (...args) => {
// Queue up connection attempts here, waiting until after initialization
await isInitialized;
const remotePort = args[0];
const { sender } = remotePort;
// This is set in `setupController`, which is called as part of initialization
connectRemote(...args);
const url = sender?.url;
const detectedProcessName = url ? getEnvironmentType(url) : '';
const connectionId = generateConnectionId(remotePort, detectedProcessName);
const openConnections = openMetamaskConnections.get(connectionId) || 0;
if (
openConnections === 0 ||
(detectedProcessName === 'background' && openConnections < 2)
// 2 background connections are allowed, one for phishing warning page and one for the ledger bridge keyring
) {
// This is set in `setupController`, which is called as part of initialization
connectRemote(...args);
openMetamaskConnections.set(connectionId, openConnections + 1);
} else {
throw new Error('CONNECTION_ALREADY_EXISTS');
}
});
browser.runtime.onConnectExternal.addListener(async (...args) => {
// Queue up connection attempts here, waiting until after initialization
await isInitialized;
@ -416,6 +435,21 @@ export async function loadStateFromPersistence() {
return versionedData.data;
}
function generateConnectionId(remotePort, detectedProcessName) {
const { sender } = remotePort;
const id = sender?.tab ? sender.tab.id : sender?.id;
if (!id || !detectedProcessName) {
console.error(
'Must provide id and detectedProcessName to generate connection id.',
id,
detectedProcessName,
); // eslint-disable-line no-console
throw new Error(
'Must provide id and detectedProcessName to generate connection id.',
);
}
return `${id}-${detectedProcessName}`;
}
/**
* Initializes the MetaMask Controller with any initial state and default language.
* Configures platform-specific error reporting strategy.
@ -570,7 +604,6 @@ export function setupController(
// communication with popup
controller.isClientOpen = true;
controller.setupTrustedCommunication(portStream, remotePort.sender);
if (isManifestV3) {
// If we get a WORKER_KEEP_ALIVE message, we respond with an ACK
remotePort.onMessage.addListener((message) => {
@ -585,9 +618,11 @@ export function setupController(
});
}
const connectionId = generateConnectionId(remotePort, processName);
if (processName === ENVIRONMENT_TYPE_POPUP) {
popupIsOpen = true;
endOfStream(portStream, () => {
openMetamaskConnections.set(connectionId, 0);
popupIsOpen = false;
const isClientOpen = isClientOpenStatus();
controller.isClientOpen = isClientOpen;
@ -597,8 +632,8 @@ export function setupController(
if (processName === ENVIRONMENT_TYPE_NOTIFICATION) {
notificationIsOpen = true;
endOfStream(portStream, () => {
openMetamaskConnections.set(connectionId, 0);
notificationIsOpen = false;
const isClientOpen = isClientOpenStatus();
controller.isClientOpen = isClientOpen;
@ -614,6 +649,7 @@ export function setupController(
openMetamaskTabsIDs[tabId] = true;
endOfStream(portStream, () => {
openMetamaskConnections.set(connectionId, 0);
delete openMetamaskTabsIDs[tabId];
const isClientOpen = isClientOpenStatus();
controller.isClientOpen = isClientOpen;