Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refactor

Manav Rathib50091265073
diff --git a/web/apps/photos/src/services/collectionService.ts b/web/apps/photos/src/services/collectionService.ts
index 82f7fc7521..a2ef44a624 100644
--- a/web/apps/photos/src/services/collectionService.ts
+++ b/web/apps/photos/src/services/collectionService.ts
@@ -60,7 +60,9 @@ const REQUEST_BATCH_SIZE = 1000;
export const createAlbum = (albumName: string) => {
if (isDevBuild && process.env.NEXT_PUBLIC_ENTE_WIP_NEWIMPL) {
- return createCollection2(albumName, "album");
+ // TODO: WIP
+ console.log(createCollection2);
+ // return createCollection2(albumName, "album");
}
return createCollection(albumName, "album");
};
diff --git a/web/packages/media/collection.ts b/web/packages/media/collection.ts
index 1ec4b451d8..bf0f84fdab 100644
--- a/web/packages/media/collection.ts
+++ b/web/packages/media/collection.ts
@@ -4,7 +4,7 @@ import {
type MagicMetadataCore,
} from "ente-media/file";
import { ItemVisibility } from "ente-media/file-metadata";
-import { nullToUndefined } from "ente-utils/transform";
+import { nullishToEmpty, nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
import {
decryptMagicMetadata,
@@ -12,8 +12,6 @@ import {
type RemoteMagicMetadata,
} from "./magic-metadata";
-// TODO: Audit this file
-
/**
* The type of a collection.
*
@@ -155,47 +153,22 @@ type RemoteCollectionUser = z.infer<typeof RemoteCollectionUser>;
* Zod schema for {@link Collection}.
*
* See: [Note: Schema suffix for exported Zod schemas].
- *
- * TODO(RE): The following reasoning is not fully correct, since we anyways need
- * to do a conversion when decrypting the collection's fields. The intent is to
- * update this once we've figured out something that also works with the current
- * persistence mechanism (trying to avoid a migration).
- *
- * [Note: Schema validation for bulk persisted remote objects]
- *
- * Objects like files and collections that we get from remote are treated
- * specially when it comes to schema validation.
- *
- * 4. Casting instead of validating when reading local values.
- *
- * Let us take a concrete example of the {@link Collection} TypeScript type,
- * whose zod schema is defined by {@link RemoteCollection}.
- *
- * While we always use the {@link RemoteCollection} schema validator when
- * parsing remote network responses, we don't do the same when reading the
- * persisted values. This is to retain the performance characteristics of the
- * existing code. This might seem miniscule for the {@link Collection} example,
- * but users can easily have hundreds of thousands of {@link EnteFile}s
- * persisted locally, and while the overhead of validation when reading from DB
- * might not matter, but it needs to be profiled first before adding it to the
- * existing code paths.
- *
- * So when reading arrays of these objects from local DB, we do a cast instead
- * of do a runtime zod validation.
- *
- * This means that the "types might lie". On the other hand, we need to special
- * case only very specific objects this way:
- *
- * 1. {@link EnteFile}
- * 2. {@link Collection}
- * 3. {@link Trash}
- *
*/
export const RemoteCollectionSchema = z.object({
id: z.number(),
owner: RemoteCollectionUser,
encryptedKey: z.string(),
+ /**
+ * Remote will set this to a blank string for albums which have been shared
+ * with the user (the decryption pipeline for those doesn't use the nonce).
+ */
keyDecryptionNonce: z.string(),
+ /**
+ * Expected to be present (along with {@link nameDecryptionNonce}), but it
+ * is still optional since it might not be present if {@link name} is present.
+ */
+ encryptedName: z.string().nullish().transform(nullToUndefined),
+ nameDecryptionNonce: z.string().nullish().transform(nullToUndefined),
/**
* Not used anymore, but still might be present for very old collections.
*
@@ -206,17 +179,11 @@ export const RemoteCollectionSchema = z.object({
*/
name: z.string().nullish().transform(nullToUndefined),
/**
- * Expected to be present (along with {@link nameDecryptionNonce}), but it
- * is still optional since it might not be present if {@link name} is present.
+ * Expected to be one of {@link CollectionType}
*/
- encryptedName: z.string().nullish().transform(nullToUndefined),
- nameDecryptionNonce: z.string().nullish().transform(nullToUndefined),
- /* Expected to be one of {@link CollectionType} */
type: z.string(),
- // TODO(RE): Use nullishToEmpty?
- sharees: z.array(RemoteCollectionUser).nullish().transform(nullToUndefined), // ?
- // TODO(RE): Use nullishToEmpty?
- publicURLs: z.array(z.looseObject({})).nullish().transform(nullToUndefined), // ?
+ sharees: z.array(RemoteCollectionUser).nullish().transform(nullishToEmpty),
+ publicURLs: z.array(z.looseObject({})).nullish().transform(nullishToEmpty),
updationTime: z.number(),
/**
* Tombstone marker.
@@ -326,7 +293,7 @@ export interface Collection2 {
*
* Expected to be one of {@link CollectionType}.
*/
- type: string; // CollectionType;
+ type: string;
/**
* The other Ente users with whom the collection has been shared with.
*
@@ -335,7 +302,7 @@ export interface Collection2 {
* - {@link email} will be set.
* - {@link role} is expected to be one of "VIEWER" or "COLLABORATOR".
*/
- sharees?: CollectionUser[];
+ sharees: CollectionUser[];
/**
* Public links that can be used to access and update the collection.
*/
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index 5a27c94458..11633e2010 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -1,5 +1,11 @@
-import type { User } from "ente-accounts/services/user";
-import { boxSeal, decryptBox, encryptBox, generateKey } from "ente-base/crypto";
+import { ensureLocalUser, type User } from "ente-accounts/services/user";
+import {
+ boxSeal,
+ boxSealOpen,
+ decryptBox,
+ encryptBox,
+ generateKey,
+} from "ente-base/crypto";
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import { apiURL } from "ente-base/origins";
import { ensureMasterKeyFromSession } from "ente-base/session";
@@ -22,7 +28,7 @@ import {
} from "ente-media/magic-metadata";
import { batch } from "ente-utils/array";
import { z } from "zod/v4";
-import { getPublicKey } from "./user";
+import { ensureUserKeyPair, getPublicKey } from "./user";
/**
* An reasonable but otherwise arbitrary number of items (e.g. files) to include
@@ -123,7 +129,7 @@ export const createCollection2 = async (
)
: undefined;
- const remoteCollection = await postCollections({
+ const collection = await postCollections({
encryptedKey,
keyDecryptionNonce,
encryptedName,
@@ -132,30 +138,32 @@ export const createCollection2 = async (
...(magicMetadata ? { magicMetadata } : {}),
});
- return decryptRemoteCollectionUsingMasterKey(remoteCollection);
+ return decryptRemoteCollection(
+ collection,
+ await decryptCollectionKey(collection),
+ );
};
/**
- * Decrypt a collection obtained from remote ({@link RemoteCollection}) into the
- * collection object that we use and persist on the client ({@link Collection}).
- *
- * @param collection The collection object we received from remote. This
- * can be thought of as an envelope, the actual consumables within it are
- * encrypted using the user's master key or public key.
- *
- * @param user The ID of the currently logged in user, and their keys. This is
- * needed to decrypt the encrypted remote collection.
+ * Return the decrypted collection key (as a base64 string) for the given
+ * {@link RemoteCollection}.
*/
-export const decryptRemoteCollectionUsingMasterKey = async (
+export const decryptCollectionKey = async (
collection: RemoteCollection,
-): Promise<Collection2> => {
- const { encryptedKey, keyDecryptionNonce } = collection;
- const collectionKey = await decryptBox(
- { encryptedData: encryptedKey, nonce: keyDecryptionNonce },
- await ensureMasterKeyFromSession(),
- );
-
- return decryptRemoteCollection(collection, collectionKey);
+): Promise<string> => {
+ const { owner, encryptedKey, keyDecryptionNonce } = collection;
+ if (owner.id == ensureLocalUser().id) {
+ // The collection key of collections owned by the user is encrypted with
+ // the user's master key.
+ return decryptBox(
+ { encryptedData: encryptedKey, nonce: keyDecryptionNonce },
+ await ensureMasterKeyFromSession(),
+ );
+ } else {
+ // The collection key of collections shared with the user is encrypted
+ // with the user's public key.
+ return boxSealOpen(encryptedKey, await ensureUserKeyPair());
+ }
};
/**
diff --git a/web/packages/utils/transform.ts b/web/packages/utils/transform.ts
index 11c16b5472..905977c6d7 100644
--- a/web/packages/utils/transform.ts
+++ b/web/packages/utils/transform.ts
@@ -4,13 +4,6 @@
export const nullToUndefined = <T>(v: T | null | undefined): T | undefined =>
v === null ? undefined : v;
-/**
- * Convert any falsey value (including blank strings) to `undefined`;
- * passthrough everything else unchanged.
- */
-export const falseyToUndefined = <T>(v: T | null | undefined): T | undefined =>
- v || undefined;
-
/**
* Convert `null` and `undefined` to `0`, passthrough everything else unchanged.
*/