-
Notifications
You must be signed in to change notification settings - Fork 5k
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 invalid providerConfig id #26310
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { cloneDeep } from 'lodash'; | ||
import { hasProperty, isObject } from '@metamask/utils'; | ||
import log from 'loglevel'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
|
@@ -40,7 +41,7 @@ function removeObsoleteSnapControllerState( | |
if (!hasProperty(state, 'SnapController')) { | ||
return; | ||
} else if (!isObject(state.SnapController)) { | ||
global.sentry.captureException( | ||
global.sentry?.captureException?.( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updating these to follow the convention in other migrations of not assuming this global is set. It should always be set in production, but one of the migrator tests doesn't set it and can blow up here. Discovered while investigating this test failure: |
||
new Error( | ||
`Migration ${version}: Invalid SnapController state of type '${typeof state.SnapController}'`, | ||
), | ||
|
@@ -93,7 +94,7 @@ function removeObsoleteNetworkControllerState( | |
if (!hasProperty(state, 'NetworkController')) { | ||
return; | ||
} else if (!isObject(state.NetworkController)) { | ||
global.sentry.captureException( | ||
global.sentry?.captureException?.( | ||
new Error( | ||
`Migration ${version}: Invalid NetworkController state of type '${typeof state.NetworkController}'`, | ||
), | ||
|
@@ -103,6 +104,53 @@ function removeObsoleteNetworkControllerState( | |
|
||
const networkControllerState = state.NetworkController; | ||
|
||
// Check for invalid `providerConfig.id`, and remove if found | ||
if ( | ||
hasProperty(networkControllerState, 'providerConfig') && | ||
// This should be impossible because `undefined` cannot be returned from persisted state, | ||
// it's not valid JSON. But a bug in migration 14 ends up setting this to `undefined`. | ||
networkControllerState.providerConfig !== undefined | ||
) { | ||
if (!isObject(networkControllerState.providerConfig)) { | ||
global.sentry?.captureException?.( | ||
new Error( | ||
`Migration ${version}: Invalid NetworkController providerConfig state of type '${typeof state | ||
.NetworkController.providerConfig}'`, | ||
), | ||
); | ||
return; | ||
} | ||
const { providerConfig } = networkControllerState; | ||
|
||
const validNetworkConfigurationIds = []; | ||
if (hasProperty(networkControllerState, 'networkConfigurations')) { | ||
if (!isObject(networkControllerState.networkConfigurations)) { | ||
global.sentry?.captureException?.( | ||
new Error( | ||
`Migration ${version}: Invalid NetworkController networkConfigurations state of type '${typeof networkControllerState.networkConfigurations}'`, | ||
), | ||
); | ||
return; | ||
} | ||
|
||
validNetworkConfigurationIds.push( | ||
...Object.keys(networkControllerState.networkConfigurations), | ||
); | ||
} | ||
|
||
if (hasProperty(providerConfig, 'id')) { | ||
if ( | ||
typeof providerConfig.id !== 'string' || | ||
!validNetworkConfigurationIds.includes(providerConfig.id) | ||
) { | ||
log.warn( | ||
`Migration ${version}: Removing invalid provider id ${providerConfig.id}`, | ||
); | ||
delete providerConfig.id; | ||
} | ||
} | ||
} | ||
|
||
delete networkControllerState.networkDetails; | ||
delete networkControllerState.networkId; | ||
delete networkControllerState.networkStatus; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discovered this when investigating this test failure: https://github.com/MetaMask/metamask-extension/actions/runs/10216054639/job/28266703200