From 57a722e10c39150ca140e3aff7f1bde76155860a Mon Sep 17 00:00:00 2001 From: Dovydas Stankevicius Date: Fri, 13 Sep 2024 16:55:25 +0100 Subject: [PATCH] fix: remove extra slash when constructing user storage url (#4702) ## Explanation This PR solves the bug of adding extra slash when user storage url is generated ## References ## Changelog ### `@metamask/profile-sync-controller` - **FIXED**: remove extra slash when constructing user storage url ## 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 --------- Co-authored-by: Mathieu Artu --- .../user-storage/__fixtures__/mockResponses.ts | 2 +- .../src/controllers/user-storage/schema.test.ts | 17 ++++++++++++++++- .../src/controllers/user-storage/schema.ts | 2 +- .../src/controllers/user-storage/services.ts | 4 ++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/profile-sync-controller/src/controllers/user-storage/__fixtures__/mockResponses.ts b/packages/profile-sync-controller/src/controllers/user-storage/__fixtures__/mockResponses.ts index 3d252c6086..cfb16a7aee 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/__fixtures__/mockResponses.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/__fixtures__/mockResponses.ts @@ -27,7 +27,7 @@ export const getMockUserStorageEndpoint = ( return `${USER_STORAGE_ENDPOINT}/${path}`; } - return `${USER_STORAGE_ENDPOINT}${createEntryPath( + return `${USER_STORAGE_ENDPOINT}/${createEntryPath( path as UserStoragePathWithFeatureAndKey, MOCK_STORAGE_KEY, )}`; diff --git a/packages/profile-sync-controller/src/controllers/user-storage/schema.test.ts b/packages/profile-sync-controller/src/controllers/user-storage/schema.test.ts index 048ab93d6b..f5d0f965a2 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/schema.test.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/schema.test.ts @@ -1,10 +1,25 @@ -import { getFeatureAndKeyFromPath, USER_STORAGE_SCHEMA } from './schema'; +import { + createEntryPath, + 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 correctly construct user storage url', () => { + expect( + createEntryPath( + 'notifications.notificationSettings', + 'dbdc994804e591f7bef6695e525543712358dd5c952bd257560b629887972588', + ), + ).toBe( + 'notifications/2072257b71d53b6cb8e72bab8e801e3d66faa0d5e1b822c88af466127e5e763b', + ); + }); + it('should throw error if the feature.key format is incorrect', () => { const path = 'feature/key'; expect(() => diff --git a/packages/profile-sync-controller/src/controllers/user-storage/schema.ts b/packages/profile-sync-controller/src/controllers/user-storage/schema.ts index 888939cf3d..2c2f2a9ae6 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/schema.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/schema.ts @@ -93,5 +93,5 @@ export function createEntryPath( const { feature, key } = getFeatureAndKeyFromPath(path); const hashedKey = createSHA256Hash(key + storageKey); - return `/${feature}/${hashedKey}`; + return `${feature}/${hashedKey}`; } diff --git a/packages/profile-sync-controller/src/controllers/user-storage/services.ts b/packages/profile-sync-controller/src/controllers/user-storage/services.ts index c925f792e4..752c4f899a 100644 --- a/packages/profile-sync-controller/src/controllers/user-storage/services.ts +++ b/packages/profile-sync-controller/src/controllers/user-storage/services.ts @@ -58,7 +58,7 @@ export async function getUserStorage( const { bearerToken, path, storageKey, nativeScryptCrypto } = opts; const encryptedPath = createEntryPath(path, storageKey); - const url = new URL(`${USER_STORAGE_ENDPOINT}${encryptedPath}`); + const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`); const userStorageResponse = await fetch(url.toString(), { headers: { @@ -172,7 +172,7 @@ export async function upsertUserStorage( nativeScryptCrypto, ); const encryptedPath = createEntryPath(path, storageKey); - const url = new URL(`${USER_STORAGE_ENDPOINT}${encryptedPath}`); + const url = new URL(`${USER_STORAGE_ENDPOINT}/${encryptedPath}`); const res = await fetch(url.toString(), { method: 'PUT',