From c2fd687a45d88426cb76565d317e23212a8dd4ba Mon Sep 17 00:00:00 2001 From: Joan Vicens Date: Thu, 4 Apr 2024 12:47:22 +0200 Subject: [PATCH] [PB-1977] fix: SIGTRAP when calling safe storage methods (#481) fix: add 1s delay before trying to encrypt the credentials --- src/apps/main/auth/handlers.ts | 21 +++++++++++++++------ src/apps/main/auth/service.ts | 8 +++++++- src/apps/shared/Delay.ts | 7 +++++++ yarn.lock | 5 +++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 src/apps/shared/Delay.ts diff --git a/src/apps/main/auth/handlers.ts b/src/apps/main/auth/handlers.ts index 9b5adfd5b..5303e14e8 100644 --- a/src/apps/main/auth/handlers.ts +++ b/src/apps/main/auth/handlers.ts @@ -58,13 +58,22 @@ export function onUserUnauthorized() { ipcMain.on('user-is-unauthorized', onUserUnauthorized); ipcMain.on('user-logged-in', async (_, data: AccessResponse) => { - setCredentials(data.user, data.user.mnemonic, data.token, data.newToken); - if (!canHisConfigBeRestored(data.user.uuid)) { - await setupRootFolder(); + try { + await setCredentials( + data.user, + data.user.mnemonic, + data.token, + data.newToken + ); + if (!canHisConfigBeRestored(data.user.uuid)) { + await setupRootFolder(); + } + + setIsLoggedIn(true); + eventBus.emit('USER_LOGGED_IN'); + } catch (err) { + Logger.error(err); } - - setIsLoggedIn(true); - eventBus.emit('USER_LOGGED_IN'); }); ipcMain.on('user-logged-out', () => { diff --git a/src/apps/main/auth/service.ts b/src/apps/main/auth/service.ts index 57c110a03..a9940714a 100644 --- a/src/apps/main/auth/service.ts +++ b/src/apps/main/auth/service.ts @@ -4,6 +4,7 @@ import Logger from 'electron-log'; import packageConfig from '../../../../package.json'; import ConfigStore, { defaults, fieldsToSave } from '../config'; import { User } from '../types'; +import { Delay } from '../../shared/Delay'; const TOKEN_ENCODING = 'latin1'; @@ -39,7 +40,7 @@ function ecnryptToken(token: string): string { return buffer.toString(TOKEN_ENCODING); } -export function setCredentials( +export async function setCredentials( userData: User, mnemonic: string, bearerToken: string, @@ -48,6 +49,11 @@ export function setCredentials( ConfigStore.set('mnemonic', mnemonic); ConfigStore.set('userData', userData); + await Delay.ms(1_000); + // In the version of electron we are using calling + // isEncryptionAvailable or decryptString "too son" will crash the app + // we will be able to remove once we can update the electron version + const isSafeStorageAvailable = safeStorage.isEncryptionAvailable(); const token = isSafeStorageAvailable diff --git a/src/apps/shared/Delay.ts b/src/apps/shared/Delay.ts new file mode 100644 index 000000000..20567a3a7 --- /dev/null +++ b/src/apps/shared/Delay.ts @@ -0,0 +1,7 @@ +export class Delay { + static ms(milliseconds: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, milliseconds); + }); + } +} diff --git a/yarn.lock b/yarn.lock index 9fce0289a..607d42922 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12673,6 +12673,11 @@ unbox-primitive@^1.0.2: has-symbols "^1.0.3" which-boxed-primitive "^1.0.2" +undici-types@~5.26.4: + version "5.26.5" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" + integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== + undici@^5.5.1: version "5.22.1" resolved "https://registry.yarnpkg.com/undici/-/undici-5.22.1.tgz#877d512effef2ac8be65e695f3586922e1a57d7b"