Skip to content

Commit

Permalink
fix: backups are not being encrypted with bucket if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
apsantiso committed Dec 19, 2024
1 parent f6dbe3f commit 25ba1bd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/app/services/backups.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
});

Expand Down Expand Up @@ -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 }) => {
Expand Down
13 changes: 13 additions & 0 deletions src/app/services/crypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -168,5 +180,6 @@ module.exports = (Model, App) => {
hashSha256,
encryptTextWithKey,
RandomPassword,
decryptNameWithNullFolderId,
};
};

0 comments on commit 25ba1bd

Please sign in to comment.