Skip to content

Commit

Permalink
fix: remove old phishfort list from clients (#27743) (#27746)
Browse files Browse the repository at this point in the history
Cherry-pick #27743  for v12.4.1

Co-authored-by: Mark Stacey <[email protected]>
  • Loading branch information
AugmentedMode and Gudahtt authored Oct 10, 2024
1 parent 13c758a commit 3d096c2
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
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;
}

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'),
];

export default migrations;

0 comments on commit 3d096c2

Please sign in to comment.