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

Force an update of the phishing warning configuration (#20381)

The "last fetched" state for the `PhishingController` has been deleted
to force an immediate full update of the phishing configuration state.
We're doing this because the state was cleared in v10.34.2 because the
format of that state had changed.

This has been implemented in migration 92. The previous migration 92
has been renamed to 93 because it won't be included until a future
release. We need the migrations to remain sequential, and this will
save us from having to resolve a complex conflict when releasing this.
This commit is contained in:
Mark Stacey 2023-08-03 13:12:21 -02:30 committed by GitHub
parent 0eb8e33e45
commit a7a086537b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 190 additions and 73 deletions

View File

@ -1,11 +1,13 @@
import { InfuraNetworkType, NetworkType } from '@metamask/controller-utils';
import { cloneDeep } from 'lodash';
import { migrate, version } from './092';
const PREVIOUS_VERSION = version - 1;
describe('migration #92', () => {
it('should update the version metadata', async () => {
const oldStorage = {
meta: {
version: 91,
version: PREVIOUS_VERSION,
},
data: {},
};
@ -16,81 +18,51 @@ describe('migration #92', () => {
});
});
it('should return state unaltered if there is no network controller state', async () => {
it('should return state unaltered if there is no phishing controller state', async () => {
const oldData = {
other: 'data',
};
const oldStorage = {
meta: {
version: 91,
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if there is no network controller providerConfig state', async () => {
it('should return state unaltered if there is no phishing controller last fetched state', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
},
},
PhishingController: {
whitelist: [],
},
};
const oldStorage = {
meta: {
version: 91,
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if there is already a ticker in the providerConfig state', async () => {
it('should remove both last fetched properties from phishing controller state', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
ticker: 'GoerliETH',
type: InfuraNetworkType.goerli,
chainId: '5',
nickname: 'Goerli Testnet',
id: 'goerli',
},
PhishingController: {
whitelist: [],
hotlistLastFetched: 0,
stalelistLastFetched: 0,
},
};
const oldStorage = {
meta: {
version: 91,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});
it('should update the provider config to have a ticker set to "ETH" if none is currently present', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
},
},
};
const oldStorage = {
meta: {
version: 91,
version: PREVIOUS_VERSION,
},
data: oldData,
};
@ -98,13 +70,8 @@ describe('migration #92', () => {
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
ticker: 'ETH',
},
PhishingController: {
whitelist: [],
},
});
});

View File

@ -4,7 +4,8 @@ import { hasProperty, isObject } from '@metamask/utils';
export const version = 92;
/**
* Add ticker to the providerConfig object if missing
* Delete `stalelistLastFetched` and `hotlistLastFetched` to force a phishing configuration refresh
* because the format has changed.
*
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
* @param originalVersionedData.meta - State metadata.
@ -24,26 +25,11 @@ export async function migrate(originalVersionedData: {
function transformState(state: Record<string, unknown>) {
if (
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController) &&
hasProperty(state.NetworkController, 'providerConfig') &&
isObject(state.NetworkController.providerConfig)
hasProperty(state, 'PhishingController') &&
isObject(state.PhishingController)
) {
const { providerConfig } = state.NetworkController;
if (providerConfig.ticker) {
return state;
}
state.NetworkController.providerConfig = {
ticker: 'ETH',
...providerConfig,
};
return {
...state,
NetworkController: state.NetworkController,
};
delete state.PhishingController.stalelistLastFetched;
delete state.PhishingController.hotlistLastFetched;
}
return state;
}

View File

@ -0,0 +1,113 @@
import { InfuraNetworkType, NetworkType } from '@metamask/controller-utils';
import { migrate, version } from './093';
const PREVIOUS_VERSION = version - 1;
describe('migration #93', () => {
it('should update the version metadata', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: {},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.meta).toStrictEqual({
version,
});
});
it('should return state unaltered if there is no network controller state', async () => {
const oldData = {
other: 'data',
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if there is no network controller providerConfig state', async () => {
const oldData = {
other: 'data',
NetworkController: {
networkConfigurations: {
id1: {
foo: 'bar',
},
},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if there is already a ticker in the providerConfig state', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
ticker: 'GoerliETH',
type: InfuraNetworkType.goerli,
chainId: '5',
nickname: 'Goerli Testnet',
id: 'goerli',
},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldData);
});
it('should update the provider config to have a ticker set to "ETH" if none is currently present', async () => {
const oldData = {
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
other: 'data',
NetworkController: {
providerConfig: {
type: NetworkType.rpc,
chainId: '0x9292',
nickname: 'Funky Town Chain',
ticker: 'ETH',
},
},
});
});
});

View File

@ -0,0 +1,49 @@
import { cloneDeep } from 'lodash';
import { hasProperty, isObject } from '@metamask/utils';
export const version = 93;
/**
* Add ticker to the providerConfig object if missing
*
* @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: {
meta: { version: number };
data: Record<string, unknown>;
}) {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
versionedData.data = transformState(versionedData.data);
return versionedData;
}
function transformState(state: Record<string, unknown>) {
if (
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController) &&
hasProperty(state.NetworkController, 'providerConfig') &&
isObject(state.NetworkController.providerConfig)
) {
const { providerConfig } = state.NetworkController;
if (providerConfig.ticker) {
return state;
}
state.NetworkController.providerConfig = {
ticker: 'ETH',
...providerConfig,
};
return {
...state,
NetworkController: state.NetworkController,
};
}
return state;
}

View File

@ -96,6 +96,7 @@ import * as m089 from './089';
import * as m090 from './090';
import * as m091 from './091';
import * as m092 from './092';
import * as m093 from './093';
const migrations = [
m002,
@ -189,5 +190,6 @@ const migrations = [
m090,
m091,
m092,
m093,
];
export default migrations;