From b9db4a0bea92c4e4ffe37cd31cb1ff6f452053ef Mon Sep 17 00:00:00 2001 From: ivanThePleasant Date: Wed, 26 Oct 2022 14:27:10 +0300 Subject: [PATCH] Fix test, add missing await clause, add web crypto api for FE user hash --- .../core/admin/admin/src/pages/App/index.js | 2 +- .../admin/src/utils/unique-admin-hash.js | 22 +++++++++---- .../src/hooks/useTracking/tests/index.test.js | 31 +++++++++++++------ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/packages/core/admin/admin/src/pages/App/index.js b/packages/core/admin/admin/src/pages/App/index.js index be9074622f..804ed0c6dd 100644 --- a/packages/core/admin/admin/src/pages/App/index.js +++ b/packages/core/admin/admin/src/pages/App/index.js @@ -93,7 +93,7 @@ function App() { setTelemetryProperties(properties); try { - fetch('https://analytics.strapi.io/api/v2/track', { + await fetch('https://analytics.strapi.io/api/v2/track', { method: 'POST', body: JSON.stringify({ // This event is anonymous diff --git a/packages/core/admin/admin/src/utils/unique-admin-hash.js b/packages/core/admin/admin/src/utils/unique-admin-hash.js index b3a0267ade..eca4a5656f 100644 --- a/packages/core/admin/admin/src/utils/unique-admin-hash.js +++ b/packages/core/admin/admin/src/utils/unique-admin-hash.js @@ -1,12 +1,22 @@ -const hash = require('hash.js'); +function bufferToHex(buffer) { + return [...new Uint8Array(buffer)].map((b) => b.toString(16).padStart(2, '0')).join(''); +} -const hashAdminUserEmail = (payload) => { +async function digestMessage(message) { + const msgUint8 = new TextEncoder().encode(message); + const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); + + return bufferToHex(hashBuffer); +} + +const hashAdminUserEmail = async (payload) => { try { - const adminUserEmailHash = hash.sha256().update(payload.email).digest('hex'); - - return adminUserEmailHash; + return await digestMessage(payload.email); } catch (error) { - return ''; + // not a secure context + const hash = import('hash.js'); + + return hash.sha256().update(payload.email).digest('hex'); } }; diff --git a/packages/core/helper-plugin/lib/src/hooks/useTracking/tests/index.test.js b/packages/core/helper-plugin/lib/src/hooks/useTracking/tests/index.test.js index 26e73eb764..fec589c0bc 100644 --- a/packages/core/helper-plugin/lib/src/hooks/useTracking/tests/index.test.js +++ b/packages/core/helper-plugin/lib/src/hooks/useTracking/tests/index.test.js @@ -25,6 +25,7 @@ function setup(props) { telemetryProperties: { nestedProperty: true, }, + deviceId: 'someTestDeviceId', ...props, }} > @@ -45,21 +46,33 @@ describe('useTracking', () => { test('Call trackUsage() with all attributes', async () => { useAppInfos.mockReturnValue({ currentEnvironment: 'testing', + adminUserId: 'someTestUserId', }); const { result } = await setup(); result.current.trackUsage('event', { trackingProperty: true }); - expect(axios.post).toBeCalledWith(expect.any(String), { - event: 'event', - uuid: 1, - properties: expect.objectContaining({ - environment: 'testing', - nestedProperty: true, - trackingProperty: true, - }), - }); + expect(axios.post).toBeCalledWith( + expect.any(String), + { + adminUserId: 'someTestUserId', + deviceId: 'someTestDeviceId', + event: 'event', + eventProperties: { + trackingProperty: true, + }, + groupProperties: { + nestedProperty: true, + projectId: 1, + projectType: 'Community', + }, + userProperties: {}, + }, + { + headers: { 'Content-Type': 'application/json' }, + } + ); }); test('Do not track if it has been disabled', async () => {