Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove old phishfort list from clients #27743

Merged
merged 10 commits into from
Oct 9, 2024
142 changes: 142 additions & 0 deletions app/scripts/migrations/126.1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { migrate, version } from './126.1';

const oldVersion = 126.1;

const mockPhishingListMetaMask = {
allowlist: [],
blocklist: ['malicious1.com'],
c2DomainBlocklist: ['malicious2.com'],
fuzzylist: [],
tolerance: 0,
version: 1,
lastUpdated: Date.now(),
name: 'MetaMask',
};

const mockPhishingListPhishfort = {
allowlist: [],
blocklist: ['phishfort1.com'],
c2DomainBlocklist: ['phishfort2.com'],
fuzzylist: [],
tolerance: 0,
version: 1,
lastUpdated: Date.now(),
name: 'Phishfort',
};

describe(`migration #${version}`, () => {
it('updates the version metadata', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.meta).toStrictEqual({ version });
});

it('keeps only the MetaMask phishing list in PhishingControllerState', async () => {
const oldState = {
PhishingController: {
phishingLists: [mockPhishingListMetaMask, mockPhishingListPhishfort],
whitelist: [],
hotlistLastFetched: 0,
stalelistLastFetched: 0,
c2DomainBlocklistLastFetched: 0,
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: oldState,
});

const updatedPhishingController = transformedState.data
.PhishingController as Record<string, unknown>;

expect(updatedPhishingController.phishingLists).toStrictEqual([
mockPhishingListMetaMask,
]);
});

it('removes all phishing lists if MetaMask is not present', async () => {
const oldState = {
PhishingController: {
phishingLists: [mockPhishingListPhishfort],
whitelist: [],
hotlistLastFetched: 0,
stalelistLastFetched: 0,
c2DomainBlocklistLastFetched: 0,
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: oldState,
});

const updatedPhishingController = transformedState.data
.PhishingController as Record<string, unknown>;

expect(updatedPhishingController.phishingLists).toStrictEqual([]);
});

it('does nothing if PhishingControllerState is empty', async () => {
const oldState = {
PhishingController: {
phishingLists: [],
whitelist: [],
hotlistLastFetched: 0,
stalelistLastFetched: 0,
c2DomainBlocklistLastFetched: 0,
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: oldState,
});

const updatedPhishingController = transformedState.data
.PhishingController as Record<string, unknown>;

expect(updatedPhishingController.phishingLists).toStrictEqual([]);
});

it('does nothing if PhishingController is not in the state', async () => {
const oldState = {
NetworkController: {
providerConfig: {
chainId: '0x1',
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: oldState,
});

expect(transformedState.data).toStrictEqual(oldState);
});

it('does nothing if phishingLists is not an array (null)', async () => {
const oldState: Record<string, unknown> = {
PhishingController: {
phishingLists: null,
whitelist: [],
hotlistLastFetched: 0,
stalelistLastFetched: 0,
c2DomainBlocklistLastFetched: 0,
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: oldState,
});

expect(transformedState.data).toStrictEqual(oldState);
});
});
54 changes: 54 additions & 0 deletions app/scripts/migrations/126.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';

type VersionedData = {
meta: { version: number };
data: Record<string, unknown>;
};

export const version = 126.1;

/**
* This migration removes `providerConfig` from the network controller state.
*
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
* @param originalVersionedData.meta - State metadata.
* @param originalVersionedData.meta.version - The current state version.
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller.
* @returns Updated versioned MetaMask extension state.
*/
export async function migrate(
originalVersionedData: VersionedData,
): Promise<VersionedData> {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
transformState(versionedData.data);
return versionedData;
}

function transformState(
state: Record<string, unknown>,
): Record<string, unknown> {
if (
hasProperty(state, 'PhishingController') &&
isObject(state.PhishingController) &&
hasProperty(state.PhishingController, 'phishingLists')
) {
const phishingController = state.PhishingController;

if (!Array.isArray(phishingController.phishingLists)) {
console.error(
`Migration ${version}: Invalid PhishingController.phishingLists state`,
);
return state;
AugmentedMode marked this conversation as resolved.
Show resolved Hide resolved
}

phishingController.phishingLists = phishingController.phishingLists.filter(
(list) => list.name === 'MetaMask',
);

state.PhishingController = phishingController;
}

return state;
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const migrations = [
require('./125'),
require('./125.1'),
require('./126'),
require('./126.1'),
require('./127'),
require('./128'),
require('./129'),
Expand Down