-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(NOTIFY-852): Enforce user storage schema usage (#4543)
## Explanation This PR enforces the`UserStorageSchema` usage in every function that gets or sets user storage. This will help keep user storage entries consistent, and improve DX when using user storage. ## References [NOTIFY-852](https://consensyssoftware.atlassian.net/browse/NOTIFY-852) ## Changelog ### `@metamask/profile-sync-controller` - **CHANGED**: User storage schema - **ADDED**: User storage path compile-time and runtime validation ### `@metamask/notification-services-controller` - **CHANGED**: Update argument to follow on `@metamask/profile-sync-controller` changes when calling `UserStorageController:performGetStorage` and `UserStorageController:performSetStorage` ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate [NOTIFY-852]: https://consensyssoftware.atlassian.net/browse/NOTIFY-852?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
- Loading branch information
1 parent
cc09e4e
commit 51f7584
Showing
11 changed files
with
191 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/profile-sync-controller/src/controllers/user-storage/schema.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { getFeatureAndKeyFromPath, USER_STORAGE_SCHEMA } from './schema'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type ErroneousUserStoragePath = any; | ||
|
||
describe('user-storage/schema.ts', () => { | ||
describe('getFeatureAndKeyFromPath', () => { | ||
it('should throw error if the feature.key format is incorrect', () => { | ||
const path = 'feature/key'; | ||
expect(() => | ||
getFeatureAndKeyFromPath(path as ErroneousUserStoragePath), | ||
).toThrow( | ||
"user-storage - path is not in the correct format. Correct format: 'feature.key'", | ||
); | ||
}); | ||
|
||
it('should throw error if feature is invalid', () => { | ||
const path = 'invalid.feature'; | ||
expect(() => | ||
getFeatureAndKeyFromPath(path as ErroneousUserStoragePath), | ||
).toThrow('user-storage - invalid feature provided: invalid'); | ||
}); | ||
|
||
it('should throw error if key is invalid', () => { | ||
const feature = 'notifications'; | ||
const path = `${feature}.invalid`; | ||
const validKeys = USER_STORAGE_SCHEMA[feature].join(', '); | ||
|
||
expect(() => | ||
getFeatureAndKeyFromPath(path as ErroneousUserStoragePath), | ||
).toThrow( | ||
`user-storage - invalid key provided for this feature: invalid. Valid keys: ${validKeys}`, | ||
); | ||
}); | ||
|
||
it('should return feature and key from path', () => { | ||
const path = 'notifications.notificationSettings'; | ||
const result = getFeatureAndKeyFromPath(path); | ||
expect(result).toStrictEqual({ | ||
feature: 'notifications', | ||
key: 'notificationSettings', | ||
}); | ||
}); | ||
}); | ||
}); |
76 changes: 57 additions & 19 deletions
76
packages/profile-sync-controller/src/controllers/user-storage/schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,76 @@ | ||
import { createSHA256Hash } from './encryption'; | ||
|
||
type UserStorageEntry = { path: string; entryName: string }; | ||
|
||
/** | ||
* The User Storage Endpoint requires a path and an entry name. | ||
* Developers can provide additional paths by extending this variable below | ||
* The User Storage Endpoint requires a feature name and a namespace key. | ||
* Developers can provide additional features and keys by extending these types below. | ||
*/ | ||
export const USER_STORAGE_ENTRIES = { | ||
notificationSettings: { | ||
path: 'notifications', | ||
entryName: 'notificationSettings', | ||
}, | ||
} satisfies Record<string, UserStorageEntry>; | ||
|
||
export type UserStorageEntryKeys = keyof typeof USER_STORAGE_ENTRIES; | ||
export const USER_STORAGE_SCHEMA = { | ||
notifications: ['notificationSettings'], | ||
} as const; | ||
|
||
type UserStorageSchema = typeof USER_STORAGE_SCHEMA; | ||
type UserStorageFeatures = keyof UserStorageSchema; | ||
type UserStorageFeatureKeys<Feature extends UserStorageFeatures> = | ||
UserStorageSchema[Feature][number]; | ||
|
||
type UserStorageFeatureAndKey = { | ||
feature: UserStorageFeatures; | ||
key: UserStorageFeatureKeys<UserStorageFeatures>; | ||
}; | ||
|
||
export type UserStoragePath = { | ||
[K in keyof UserStorageSchema]: `${K}.${UserStorageSchema[K][number]}`; | ||
}[keyof UserStorageSchema]; | ||
|
||
export const getFeatureAndKeyFromPath = ( | ||
path: UserStoragePath, | ||
): UserStorageFeatureAndKey => { | ||
const pathRegex = /^\w+\.\w+$/u; | ||
|
||
if (!pathRegex.test(path)) { | ||
throw new Error( | ||
`user-storage - path is not in the correct format. Correct format: 'feature.key'`, | ||
); | ||
} | ||
|
||
const [feature, key] = path.split('.') as [ | ||
UserStorageFeatures, | ||
UserStorageFeatureKeys<UserStorageFeatures>, | ||
]; | ||
|
||
if (!(feature in USER_STORAGE_SCHEMA)) { | ||
throw new Error(`user-storage - invalid feature provided: ${feature}`); | ||
} | ||
|
||
const validFeature = USER_STORAGE_SCHEMA[feature] as readonly string[]; | ||
|
||
if (!validFeature.includes(key)) { | ||
const validKeys = USER_STORAGE_SCHEMA[feature].join(', '); | ||
|
||
throw new Error( | ||
`user-storage - invalid key provided for this feature: ${key}. Valid keys: ${validKeys}`, | ||
); | ||
} | ||
|
||
return { feature, key }; | ||
}; | ||
|
||
/** | ||
* Constructs a unique entry path for a user. | ||
* This can be done due to the uniqueness of the storage key (no users will share the same storage key). | ||
* The users entry is a unique hash that cannot be reversed. | ||
* | ||
* @param entryKey - storage schema entry key | ||
* @param path - string in the form of `${feature}.${key}` that matches schema | ||
* @param storageKey - users storage key | ||
* @returns path to store entry | ||
*/ | ||
export function createEntryPath( | ||
entryKey: UserStorageEntryKeys, | ||
path: UserStoragePath, | ||
storageKey: string, | ||
): string { | ||
const entry = USER_STORAGE_ENTRIES[entryKey]; | ||
if (!entry) { | ||
throw new Error(`user-storage - invalid entry provided: ${entryKey}`); | ||
} | ||
const { feature, key } = getFeatureAndKeyFromPath(path); | ||
const hashedKey = createSHA256Hash(key + storageKey); | ||
|
||
const hashedKey = createSHA256Hash(entry.entryName + storageKey); | ||
return `/${entry.path}/${hashedKey}`; | ||
return `/${feature}/${hashedKey}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.