Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix(locker-web): harden trash restore key resolution

Neeraj Gupta007f1da74065
diff --git a/web/apps/locker/src/pages/locker.tsx b/web/apps/locker/src/pages/locker.tsx
index 4db122229d..5ce8184d35 100644
--- a/web/apps/locker/src/pages/locker.tsx
+++ b/web/apps/locker/src/pages/locker.tsx
@@ -406,7 +406,11 @@ const Page: React.FC = () => {
const handleRestoreItem = useCallback(
async (item: LockerItem, collectionID: number) => {
if (!masterKey) return;
- await restoreFromTrash([item.id], collectionID, masterKey);
+ await restoreFromTrash(
+ [{ id: item.id, collectionID: item.collectionID }],
+ collectionID,
+ masterKey,
+ );
await refreshData();
setToast(t("filesRestoredSuccessfully", { count: 1 }));
},
diff --git a/web/apps/locker/src/services/remote.ts b/web/apps/locker/src/services/remote.ts
index 711fcb8696..1bcdf65f03 100644
--- a/web/apps/locker/src/services/remote.ts
+++ b/web/apps/locker/src/services/remote.ts
@@ -167,6 +167,8 @@ interface EncryptedCollectionRecord {
type: string;
/** Whether the collection has sharees. */
isShared: boolean;
+ /** Whether this collection is deleted on remote (key still needed for trash restore). */
+ isDeleted: boolean;
/** Updation time (structural metadata). */
updationTime: number;
}
@@ -395,9 +397,6 @@ const fetchEncryptedCollections = async () => {
const nextCollections = new Map<number, EncryptedCollectionRecord>();
for (const c of collections) {
- if (c.isDeleted) {
- continue;
- }
nextCollections.set(c.id, {
id: c.id,
owner: { ...toLockerCollectionParticipant(c.owner), role: "OWNER" },
@@ -410,6 +409,7 @@ const fetchEncryptedCollections = async () => {
name: c.name ?? undefined,
type: c.type,
isShared: (c.sharees?.length ?? 0) > 0,
+ isDeleted: !!c.isDeleted,
updationTime: c.updationTime,
});
}
@@ -422,10 +422,11 @@ const fetchEncryptedCollections = async () => {
// ---------------------------------------------------------------------------
const fetchAllEncryptedFiles = async () => {
+ const activeCollections = [...encryptedCollections.values()].filter(
+ (collection) => !collection.isDeleted,
+ );
const fileResults = await Promise.all(
- [...encryptedCollections.values()].map((c) =>
- fetchEncryptedFilesForCollection(c.id),
- ),
+ activeCollections.map((c) => fetchEncryptedFilesForCollection(c.id)),
);
const nextFiles = new Map<number, Map<number, EncryptedFileRecord>>();
@@ -766,9 +767,12 @@ const decryptFileToLockerItem = async (
const decryptAllData = async (
masterKey: string,
): Promise<DecryptAllDataResult> => {
+ const activeCollectionRecords = [...encryptedCollections.values()].filter(
+ (collection) => !collection.isDeleted,
+ );
const result: LockerCollection[] = [];
const failedCollectionIDs: number[] = [];
- const totalCollectionCount = encryptedCollections.size;
+ const totalCollectionCount = activeCollectionRecords.length;
// Group files by collection
const filesByCollection = new Map<number, EncryptedFileRecord[]>();
@@ -779,7 +783,7 @@ const decryptAllData = async (
}
// Decrypt each collection and its files
- for (const collectionRecord of encryptedCollections.values()) {
+ for (const collectionRecord of activeCollectionRecords) {
try {
const collectionKey = await decryptCollectionKey(
collectionRecord,
@@ -1470,25 +1474,27 @@ export const emptyTrash = async (): Promise<void> => {
/**
* Restore files from trash to a collection.
*
- * @param fileIDs The file IDs to restore.
- * @param collectionID The target collection.
+ * @param items The files to restore with their source collection IDs.
+ * @param targetCollectionID The target collection.
* @param masterKey The user's master key.
*/
export const restoreFromTrash = async (
- fileIDs: number[],
- collectionID: number,
+ items: Pick<LockerItem, "id" | "collectionID">[],
+ targetCollectionID: number,
masterKey: string,
): Promise<void> => {
- const collectionRecord = encryptedCollections.get(collectionID);
+ const collectionRecord = encryptedCollections.get(targetCollectionID);
if (!collectionRecord)
- throw new Error(`Collection ${collectionID} not in cache`);
+ throw new Error(`Collection ${targetCollectionID} not in cache`);
const collectionKey = await decryptCollectionKey(
collectionRecord,
masterKey,
);
- const buildRestorePayload = async (candidateFileIDs: number[]) => {
+ const buildRestorePayload = async (
+ candidateItems: Pick<LockerItem, "id" | "collectionID">[],
+ ) => {
const files: {
id: number;
encryptedKey: string;
@@ -1496,10 +1502,13 @@ export const restoreFromTrash = async (
}[] = [];
const skippedFileIDs: number[] = [];
- for (const fileID of candidateFileIDs) {
- const fileRecord = getEncryptedFileRecord(fileID);
+ for (const item of candidateItems) {
+ const fileRecord = getEncryptedFileRecord(
+ item.id,
+ item.collectionID,
+ );
if (!fileRecord) {
- skippedFileIDs.push(fileID);
+ skippedFileIDs.push(item.id);
continue;
}
@@ -1508,7 +1517,7 @@ export const restoreFromTrash = async (
fileRecord.collectionID,
);
if (!origCollectionRecord) {
- skippedFileIDs.push(fileID);
+ skippedFileIDs.push(item.id);
continue;
}
@@ -1527,7 +1536,7 @@ export const restoreFromTrash = async (
// Re-encrypt file key with target collection key
const encryptedFileKey = await encryptBox(fileKey, collectionKey);
files.push({
- id: fileID,
+ id: item.id,
encryptedKey: encryptedFileKey.encryptedData,
keyDecryptionNonce: encryptedFileKey.nonce,
});
@@ -1536,11 +1545,11 @@ export const restoreFromTrash = async (
return { files, skippedFileIDs };
};
- let { files, skippedFileIDs } = await buildRestorePayload(fileIDs);
+ let { files, skippedFileIDs } = await buildRestorePayload(items);
if (files.length === 0 && skippedFileIDs.length > 0) {
// Recover from stale cache by refetching trash metadata once.
await fetchLockerTrash(masterKey);
- ({ files, skippedFileIDs } = await buildRestorePayload(fileIDs));
+ ({ files, skippedFileIDs } = await buildRestorePayload(items));
}
if (skippedFileIDs.length > 0) {
@@ -1561,7 +1570,7 @@ export const restoreFromTrash = async (
...(await authenticatedRequestHeaders()),
"Content-Type": "application/json",
},
- body: JSON.stringify({ collectionID, files }),
+ body: JSON.stringify({ collectionID: targetCollectionID, files }),
});
ensureOk(res);
};