From 25ba1bd6320a8010ad53fb3916a7a25ff0b50243 Mon Sep 17 00:00:00 2001 From: Andres Pinto Date: Thu, 19 Dec 2024 15:46:20 -0400 Subject: [PATCH] fix: backups are not being encrypted with bucket if needed --- src/app/services/backups.js | 33 +++++++++++++++++++++++++-------- src/app/services/crypt.js | 13 +++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/app/services/backups.js b/src/app/services/backups.js index c0835b19..420fd2a3 100644 --- a/src/app/services/backups.js +++ b/src/app/services/backups.js @@ -96,8 +96,8 @@ module.exports = (Model, App) => { where: { bucket: { [Op.eq]: backupsBucket }, name: { [Op.eq]: encryptedFolderName }, - deleted: { [Op.eq]: false}, - removed: { [Op.eq]: false} + deleted: { [Op.eq]: false }, + removed: { [Op.eq]: false }, }, }); @@ -163,13 +163,30 @@ module.exports = (Model, App) => { const folders = await Model.folder.findAll({ where: { bucket: backupsBucket } }); - return Promise.all( - folders.map(async (folder) => ({ - ...folder.get({ plain: true }), - hasBackups: !(await isDeviceAsFolderEmpty(folder)), - lastBackupAt: folder.updatedAt, - })), + const newFolders = await Promise.all( + folders.map(async (folder) => { + const decryptedWithBucket = App.services.Crypt.decryptName(folder.name, folder.bucket); + + const shouldUpdateName = !decryptedWithBucket && folder.bucket; + + const backupName = shouldUpdateName + ? App.services.Crypt.encryptName(App.services.Crypt.decryptNameWithNullFolderId(folder.name), folder.bucket) + : folder.name; + + if (shouldUpdateName) { + await folder.update({ name: backupName }, { silent: true }); + } + + return { + ...folder.get({ plain: true }), + name: backupName, + hasBackups: !(await isDeviceAsFolderEmpty(folder)), + lastBackupAt: folder.updatedAt, + }; + }), ); + + return newFolders; }; const create = async ({ userId, path, deviceId, encryptVersion, interval, enabled }) => { diff --git a/src/app/services/crypt.js b/src/app/services/crypt.js index a6bde2b3..82edc07d 100644 --- a/src/app/services/crypt.js +++ b/src/app/services/crypt.js @@ -20,6 +20,18 @@ module.exports = (Model, App) => { } } + function decryptNameWithNullFolderId(content) { + try { + const decryptedText = AesUtil.decrypt(content, null); + + return decryptedText; + } catch (error) { + log.error(`(decryptNameWithNullFolderId): ${error}`); + + return null; + } + } + function probabilisticDecryption(cipherText) { try { const reb64 = CryptoJS.enc.Hex.parse(cipherText); @@ -168,5 +180,6 @@ module.exports = (Model, App) => { hashSha256, encryptTextWithKey, RandomPassword, + decryptNameWithNullFolderId, }; };