Compare commits
10 Commits
0d0dcf5987
...
88510a3ff5
| Author | SHA1 | Date | |
|---|---|---|---|
| 88510a3ff5 | |||
| 6c26e3ccb7 | |||
| d0b4ee979e | |||
| cb9ac29cb4 | |||
| 6a9e41a2ee | |||
| 15d2effc2d | |||
| 59e0aa7d47 | |||
| b86ac2cd20 | |||
| 68d8cfb7fe | |||
| 2c51074294 |
44
TODO.md
Normal file
44
TODO.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Status
|
||||
|
||||
pre-1.0
|
||||
|
||||
# Next Step
|
||||
|
||||
Implement the download retry policy from the README TODO: no retry on 4xx,
|
||||
exponential backoff on 5xx and network errors. Apply it to file and
|
||||
thumbnail downloads, cover it with mock-server tests, and update the
|
||||
README TODO checkbox.
|
||||
|
||||
# Completed Steps
|
||||
|
||||
- 2026-06-10: Decrypted collections shared by other users (sealed-box
|
||||
keys); listCollections drops deleted-collection tombstones.
|
||||
- 2026-06-10: Login hardening: dual-2FA empty-string fields handled, TOTP
|
||||
preferred when a passkey is also enrolled, interactive input via
|
||||
@inquirer/prompts.
|
||||
- 2026-06-10: Replaced sharp with pure JS (jpeg-js + exif-reader); added
|
||||
single-binary bun build and make install.
|
||||
- 2026-06-09: Added backup-metadata command (ML data always included,
|
||||
--exif opt-in); rewrote README to match the implementation; added
|
||||
thumbnail helper tests.
|
||||
- 2026-05-13: Full CLI surface: login, backup with dedup symlink layout,
|
||||
collections, files, get, get-thumb, thumbnail repair helpers.
|
||||
- 2026-05-13: Client OO API with literate usage tests; file download and
|
||||
decryption; all three metadata layers decrypted and persisted; renamed
|
||||
quack to quak.
|
||||
- 2026-05-11: SRP login flow (email OTP + TOTP) and ApiClient.
|
||||
|
||||
# Future Steps
|
||||
|
||||
- Retry policy: no retry on 4xx, exponential backoff on 5xx and network
|
||||
errors (the Next Step).
|
||||
- Update the README API reference section to match the current
|
||||
implementation.
|
||||
- Make `make docker` green.
|
||||
- Tag v1.0.0.
|
||||
- Future desktop client, separate repo:
|
||||
- Electron app skeleton consuming this library.
|
||||
- Local SQLite cache keyed on (collectionID, fileID, updationTime).
|
||||
- Background sync worker streaming new files into the cache.
|
||||
- Gallery UI: thumbnails, full-image view, basic search.
|
||||
- Upload, delete, and share operations in the library.
|
||||
@@ -76,8 +76,12 @@ const srpHandshake = async (
|
||||
// twoFactorSessionIDV2 is set (instead of twoFactorSessionID) when the
|
||||
// account has BOTH passkeys and TOTP. Prefer TOTP: a CLI cannot perform
|
||||
// a WebAuthn ceremony, and the user has a TOTP secret enrolled.
|
||||
//
|
||||
// The server marshals these fields without `omitempty`, so unset fields
|
||||
// arrive as "" rather than being absent. Use || (not ??) so empty
|
||||
// strings are treated as not-set.
|
||||
const totpSessionID =
|
||||
verifyResponse.twoFactorSessionID ??
|
||||
verifyResponse.twoFactorSessionID ||
|
||||
verifyResponse.twoFactorSessionIDV2;
|
||||
if (totpSessionID) {
|
||||
return { kind: "totp", sessionID: totpSessionID };
|
||||
|
||||
@@ -155,9 +155,21 @@ export class Client {
|
||||
const { collections } = await this.api.getJSON<{
|
||||
collections: RawCollection[];
|
||||
}>("/collections/v2", { sinceTime: 0 });
|
||||
return collections.map((raw) =>
|
||||
decryptCollection(raw, this.masterKey, this.userID),
|
||||
);
|
||||
// The sync API keeps returning deleted collections as tombstones
|
||||
// (isDeleted: true); their diff endpoint 404s, so drop them.
|
||||
return collections
|
||||
.filter((raw) => !raw.isDeleted)
|
||||
.map((raw) =>
|
||||
decryptCollection(
|
||||
raw,
|
||||
{
|
||||
masterKey: this.masterKey,
|
||||
publicKey: this.publicKey,
|
||||
secretKey: this.secretKey,
|
||||
},
|
||||
this.userID,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async listFiles(
|
||||
|
||||
@@ -24,6 +24,7 @@ export type {
|
||||
FileBlob,
|
||||
FileMetadata,
|
||||
FileType,
|
||||
KeyMaterial,
|
||||
Microseconds,
|
||||
RawCollection,
|
||||
RawEnteFile,
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { decryptBlob, decryptBox, fromBase64 } from "../crypto/index.js";
|
||||
import {
|
||||
decryptBlob,
|
||||
decryptBox,
|
||||
decryptSealed,
|
||||
fromBase64,
|
||||
} from "../crypto/index.js";
|
||||
import type {
|
||||
Collection,
|
||||
CollectionType,
|
||||
EnteFile,
|
||||
FileMetadata,
|
||||
FileType,
|
||||
KeyMaterial,
|
||||
RawCollection,
|
||||
RawEnteFile,
|
||||
RawMagicMetadata,
|
||||
@@ -30,14 +36,25 @@ const parseFileType = (n: number): FileType => FILE_TYPE_MAP[n] ?? "unknown";
|
||||
|
||||
export const decryptCollection = (
|
||||
raw: RawCollection,
|
||||
masterKey: Uint8Array,
|
||||
keys: KeyMaterial,
|
||||
currentUserID?: number,
|
||||
): Collection => {
|
||||
const key = decryptBox(
|
||||
fromBase64(raw.encryptedKey),
|
||||
fromBase64(raw.keyDecryptionNonce),
|
||||
masterKey,
|
||||
);
|
||||
// Owned collections carry their key as a secretbox under our master
|
||||
// key, with the nonce in keyDecryptionNonce. Collections shared with
|
||||
// us carry it as an anonymous sealed box to our public key and have
|
||||
// no keyDecryptionNonce at all (sealed boxes embed an ephemeral
|
||||
// public key instead).
|
||||
const key = raw.keyDecryptionNonce
|
||||
? decryptBox(
|
||||
fromBase64(raw.encryptedKey),
|
||||
fromBase64(raw.keyDecryptionNonce),
|
||||
keys.masterKey,
|
||||
)
|
||||
: decryptSealed(
|
||||
fromBase64(raw.encryptedKey),
|
||||
keys.publicKey,
|
||||
keys.secretKey,
|
||||
);
|
||||
|
||||
let name = "";
|
||||
if (raw.encryptedName && raw.nameDecryptionNonce) {
|
||||
|
||||
@@ -6,6 +6,7 @@ export type {
|
||||
FileBlob,
|
||||
FileMetadata,
|
||||
FileType,
|
||||
KeyMaterial,
|
||||
Microseconds,
|
||||
RawCollection,
|
||||
RawEnteFile,
|
||||
|
||||
@@ -50,13 +50,25 @@ export interface EnteFile {
|
||||
updationTime: Microseconds;
|
||||
}
|
||||
|
||||
// The key material a logged-in client holds, everything needed to decrypt
|
||||
// any collection: the master key (secretbox for owned collection keys) and
|
||||
// the X25519 keypair (sealed box for collection keys shared with us).
|
||||
export interface KeyMaterial {
|
||||
masterKey: Uint8Array;
|
||||
publicKey: Uint8Array;
|
||||
secretKey: Uint8Array;
|
||||
}
|
||||
|
||||
// Raw shapes as they arrive from the Ente API, before decryption.
|
||||
|
||||
export interface RawCollection {
|
||||
id: number;
|
||||
owner: { id: number };
|
||||
encryptedKey: string;
|
||||
keyDecryptionNonce: string;
|
||||
// Absent for collections shared with us: their encryptedKey is a
|
||||
// sealed box to our public key, which embeds an ephemeral public key
|
||||
// instead of using a nonce.
|
||||
keyDecryptionNonce?: string;
|
||||
encryptedName?: string;
|
||||
nameDecryptionNonce?: string;
|
||||
type: string;
|
||||
|
||||
@@ -200,11 +200,25 @@ const buildMockFetch = (
|
||||
srpServer.checkM1(Buffer.from(body.srpM1, "base64"));
|
||||
const M2 = srpServer.computeM2();
|
||||
|
||||
// IMPORTANT: the museum server's EmailAuthorizationResponse
|
||||
// (server/ente/user.go) declares passkeySessionID, accountsUrl,
|
||||
// twoFactorSessionID, and twoFactorSessionIDV2 WITHOUT the
|
||||
// `omitempty` JSON tag. Go therefore always serializes them,
|
||||
// sending "" (empty string, NOT null/absent) for any that do
|
||||
// not apply. These mocks must reproduce that faithfully: a
|
||||
// client that distinguishes fields with `??` instead of `||`
|
||||
// passes against an omitting mock but breaks against the real
|
||||
// server.
|
||||
|
||||
if (opts?.requireTOTP) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
id: 42,
|
||||
srpM2: M2.toString("base64"),
|
||||
passkeySessionID: "",
|
||||
accountsUrl: "",
|
||||
twoFactorSessionID: "totp-session-999",
|
||||
twoFactorSessionIDV2: "",
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
@@ -218,11 +232,14 @@ const buildMockFetch = (
|
||||
// server sets passkeySessionID + twoFactorSessionIDV2 (not
|
||||
// twoFactorSessionID -- that's deliberate, so old clients
|
||||
// that only know the V1 field keep using the passkey flow).
|
||||
// The V1 field is still present on the wire as "".
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
id: 42,
|
||||
srpM2: M2.toString("base64"),
|
||||
passkeySessionID: "passkey-session-123",
|
||||
accountsUrl: "https://accounts.ente.io",
|
||||
twoFactorSessionID: "",
|
||||
twoFactorSessionIDV2: "totp-session-v2-456",
|
||||
}),
|
||||
{
|
||||
@@ -238,6 +255,10 @@ const buildMockFetch = (
|
||||
id: 42,
|
||||
keyAttributes: fixture.keyAttributes,
|
||||
encryptedToken: fixture.encryptedToken,
|
||||
passkeySessionID: "",
|
||||
accountsUrl: "",
|
||||
twoFactorSessionID: "",
|
||||
twoFactorSessionIDV2: "",
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
|
||||
@@ -91,6 +91,7 @@ interface ServerState {
|
||||
thumbHeader: Uint8Array;
|
||||
thumbCiphertext: Uint8Array;
|
||||
collectionKey: Uint8Array;
|
||||
sharedCollectionKey: Uint8Array;
|
||||
}
|
||||
|
||||
let server: ServerState;
|
||||
@@ -211,6 +212,69 @@ const buildServer = async (): Promise<ServerState> => {
|
||||
updationTime: 1700000000000000,
|
||||
};
|
||||
|
||||
// A collection another user shared WITH us. The sharer does not have
|
||||
// our master key, only our public key, so the real server delivers the
|
||||
// collection key as an anonymous sealed box (crypto_box_seal) to our
|
||||
// public key and the response carries NO keyDecryptionNonce. Every
|
||||
// account with an incoming shared album has one of these in its
|
||||
// /collections/v2 response, so the mock must include one too.
|
||||
const sharedCollectionKey = sodium.crypto_secretbox_keygen();
|
||||
const sealedSharedKey = sodium.crypto_box_seal(
|
||||
sharedCollectionKey,
|
||||
kp.publicKey,
|
||||
);
|
||||
const sharedNameNonce = sodium.randombytes_buf(
|
||||
sodium.crypto_secretbox_NONCEBYTES,
|
||||
);
|
||||
const encSharedName = sodium.crypto_secretbox_easy(
|
||||
new TextEncoder().encode("Friend's Wedding"),
|
||||
sharedNameNonce,
|
||||
sharedCollectionKey,
|
||||
);
|
||||
const rawSharedCollection = {
|
||||
id: 2,
|
||||
owner: { id: 99 },
|
||||
encryptedKey: toBase64(sealedSharedKey),
|
||||
encryptedName: toBase64(encSharedName),
|
||||
nameDecryptionNonce: toBase64(sharedNameNonce),
|
||||
type: "album",
|
||||
updationTime: 1700000000000000,
|
||||
};
|
||||
|
||||
// A DELETED collection. /collections/v2 is a sync API: deleted
|
||||
// collections stay in the response forever as tombstones with
|
||||
// isDeleted: true (a long-lived real account accumulates hundreds).
|
||||
// Their keys still decrypt, but /collections/v2/diff returns
|
||||
// HTTP 404 for them, so they must never surface from
|
||||
// listCollections(); a client that naively iterates them dies on
|
||||
// the first deleted album.
|
||||
const deletedKey = sodium.crypto_secretbox_keygen();
|
||||
const dkNonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
|
||||
const encDeletedKey = sodium.crypto_secretbox_easy(
|
||||
deletedKey,
|
||||
dkNonce,
|
||||
masterKey,
|
||||
);
|
||||
const deletedNameNonce = sodium.randombytes_buf(
|
||||
sodium.crypto_secretbox_NONCEBYTES,
|
||||
);
|
||||
const encDeletedName = sodium.crypto_secretbox_easy(
|
||||
new TextEncoder().encode("Old Album"),
|
||||
deletedNameNonce,
|
||||
deletedKey,
|
||||
);
|
||||
const rawDeletedCollection = {
|
||||
id: 3,
|
||||
owner: { id: 42 },
|
||||
encryptedKey: toBase64(encDeletedKey),
|
||||
keyDecryptionNonce: toBase64(dkNonce),
|
||||
encryptedName: toBase64(encDeletedName),
|
||||
nameDecryptionNonce: toBase64(deletedNameNonce),
|
||||
type: "album",
|
||||
updationTime: 1700000000000000,
|
||||
isDeleted: true,
|
||||
};
|
||||
|
||||
const rawFile = {
|
||||
id: 100,
|
||||
collectionID: 1,
|
||||
@@ -230,6 +294,10 @@ const buildServer = async (): Promise<ServerState> => {
|
||||
// can return them. This is ugly plumbing; in a real program you
|
||||
// never see any of it.
|
||||
(globalThis as Record<string, unknown>).__mockRawCollection = rawCollection;
|
||||
(globalThis as Record<string, unknown>).__mockRawSharedCollection =
|
||||
rawSharedCollection;
|
||||
(globalThis as Record<string, unknown>).__mockRawDeletedCollection =
|
||||
rawDeletedCollection;
|
||||
(globalThis as Record<string, unknown>).__mockRawFile = rawFile;
|
||||
|
||||
return {
|
||||
@@ -253,6 +321,7 @@ const buildServer = async (): Promise<ServerState> => {
|
||||
thumbHeader: thumbPush.header,
|
||||
thumbCiphertext,
|
||||
collectionKey,
|
||||
sharedCollectionKey,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -303,9 +372,14 @@ const buildMockFetch = (s: ServerState) => {
|
||||
});
|
||||
}
|
||||
if (path === "/collections/v2") {
|
||||
const raw = (globalThis as Record<string, unknown>)
|
||||
.__mockRawCollection;
|
||||
return json({ collections: [raw] });
|
||||
const g = globalThis as Record<string, unknown>;
|
||||
return json({
|
||||
collections: [
|
||||
g.__mockRawCollection,
|
||||
g.__mockRawSharedCollection,
|
||||
g.__mockRawDeletedCollection,
|
||||
],
|
||||
});
|
||||
}
|
||||
if (path === "/collections/v2/diff") {
|
||||
const raw = (globalThis as Record<string, unknown>).__mockRawFile;
|
||||
@@ -412,6 +486,10 @@ describe("quak Client usage guide", () => {
|
||||
* encryption key. `listCollections()` fetches them from the server,
|
||||
* decrypts the keys and names, and returns typed objects.
|
||||
*
|
||||
* This works transparently for both kinds of collection: ones you
|
||||
* own (key encrypted with your master key) and ones shared with you
|
||||
* (key sealed to your public key, flagged with `isShared: true`).
|
||||
*
|
||||
* ```ts
|
||||
* const collections = await client.listCollections();
|
||||
* for (const c of collections) {
|
||||
@@ -419,7 +497,7 @@ describe("quak Client usage guide", () => {
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
it("3. list and decrypt collections", async () => {
|
||||
it("3. list and decrypt collections, owned and shared", async () => {
|
||||
const client = await Client.login({
|
||||
email: TEST_EMAIL,
|
||||
password: TEST_PASSWORD,
|
||||
@@ -428,12 +506,25 @@ describe("quak Client usage guide", () => {
|
||||
|
||||
const collections = await client.listCollections();
|
||||
|
||||
expect(collections.length).toBe(1);
|
||||
expect(collections[0]!.name).toBe("Vacation");
|
||||
expect(collections[0]!.type).toBe("album");
|
||||
expect(collections[0]!.id).toBe(1);
|
||||
// The mock server also returns a deleted collection (id 3).
|
||||
// Deleted collections are tombstones in the sync protocol: their
|
||||
// file diff endpoint 404s, so listCollections must drop them.
|
||||
expect(collections.length).toBe(2);
|
||||
expect(collections.find((c) => c.id === 3)).toBeUndefined();
|
||||
|
||||
const owned = collections.find((c) => c.id === 1)!;
|
||||
expect(owned.name).toBe("Vacation");
|
||||
expect(owned.type).toBe("album");
|
||||
expect(owned.isShared).toBe(false);
|
||||
// The decrypted collection key is available for advanced use.
|
||||
expect(collections[0]!.key.length).toBe(32);
|
||||
expect(owned.key.length).toBe(32);
|
||||
|
||||
const shared = collections.find((c) => c.id === 2)!;
|
||||
expect(shared.name).toBe("Friend's Wedding");
|
||||
expect(shared.isShared).toBe(true);
|
||||
// The key was unsealed with our keypair; the decrypted name above
|
||||
// already proves it round-trips, but check it exactly too.
|
||||
expect(shared.key).toEqual(server.sharedCollectionKey);
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,7 +25,10 @@ const main = async () => {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const { masterKey, token } = await unwrapAuth(challenge.response, PASSWORD);
|
||||
const { masterKey, secretKey, publicKey, token } = await unwrapAuth(
|
||||
challenge.response,
|
||||
PASSWORD,
|
||||
);
|
||||
api.setAuthToken(token);
|
||||
console.log("Logged in, user ID:", challenge.response.id);
|
||||
|
||||
@@ -37,7 +40,7 @@ const main = async () => {
|
||||
|
||||
const userID = challenge.response.id;
|
||||
const collections = rawCollections.map((raw) =>
|
||||
decryptCollection(raw, masterKey, userID),
|
||||
decryptCollection(raw, { masterKey, publicKey, secretKey }, userID),
|
||||
);
|
||||
|
||||
console.log(`${collections.length} collection(s):`);
|
||||
|
||||
@@ -7,12 +7,27 @@
|
||||
*
|
||||
* ## Collection decryption
|
||||
*
|
||||
* The server stores each collection's encryption key sealed under the
|
||||
* owner's master key (secretbox). The collection's name is then sealed
|
||||
* under that collection key (also secretbox). `decryptCollection`:
|
||||
* How a collection's key is encrypted depends on who owns it:
|
||||
*
|
||||
* 1. decryptBox(encryptedKey, keyDecryptionNonce, masterKey) -> collectionKey
|
||||
* 2. decryptBox(encryptedName, nameDecryptionNonce, collectionKey) -> name (UTF-8)
|
||||
* OWNED collections (owner == current user): the collection key is a
|
||||
* secretbox under the owner's master key, and `keyDecryptionNonce`
|
||||
* carries the nonce.
|
||||
*
|
||||
* SHARED collections (owned by someone else, shared with us): the owner
|
||||
* does not have our master key, so the server instead carries the
|
||||
* collection key as an anonymous SEALED BOX (crypto_box_seal) to our
|
||||
* X25519 public key, and `keyDecryptionNonce` is ABSENT from the wire
|
||||
* (sealed boxes embed an ephemeral public key instead of a nonce).
|
||||
*
|
||||
* `decryptCollection` therefore takes the full key material (master key
|
||||
* plus keypair) and dispatches on the presence of `keyDecryptionNonce`:
|
||||
*
|
||||
* 1a. nonce present: decryptBox(encryptedKey, keyDecryptionNonce,
|
||||
* masterKey) -> collectionKey
|
||||
* 1b. nonce absent: decryptSealed(encryptedKey, publicKey, secretKey)
|
||||
* -> collectionKey
|
||||
* 2. decryptBox(encryptedName, nameDecryptionNonce, collectionKey)
|
||||
* -> name (UTF-8)
|
||||
* 3. Maps the string `type` field to a CollectionType union member
|
||||
* 4. Returns a Collection with decrypted key, name, and type
|
||||
*
|
||||
@@ -38,12 +53,28 @@ import sodium from "libsodium-wrappers-sumo";
|
||||
import { beforeAll, describe, expect, it } from "vitest";
|
||||
import { init, toBase64 } from "../../src/crypto/index.js";
|
||||
import { decryptCollection, decryptFile } from "../../src/model/index.js";
|
||||
import type { RawCollection, RawEnteFile } from "../../src/model/index.js";
|
||||
import type {
|
||||
KeyMaterial,
|
||||
RawCollection,
|
||||
RawEnteFile,
|
||||
} from "../../src/model/index.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// The full set of key material a logged-in client holds: the master key
|
||||
// (decrypts owned collection keys) and the X25519 keypair (unseals shared
|
||||
// collection keys and the auth token).
|
||||
const buildKeys = (): KeyMaterial => {
|
||||
const kp = sodium.crypto_box_keypair();
|
||||
return {
|
||||
masterKey: sodium.crypto_secretbox_keygen(),
|
||||
publicKey: kp.publicKey,
|
||||
secretKey: kp.privateKey,
|
||||
};
|
||||
};
|
||||
|
||||
const secretboxEncrypt = (
|
||||
plaintext: Uint8Array,
|
||||
key: Uint8Array,
|
||||
@@ -80,6 +111,38 @@ const buildRawCollection = (
|
||||
return { raw, collectionKey };
|
||||
};
|
||||
|
||||
// A collection shared with us by another user. The wire format differs
|
||||
// from owned collections in two ways, both verified against the live
|
||||
// api.ente.io: `encryptedKey` is an anonymous sealed box to OUR public
|
||||
// key (80 bytes for a 32-byte key, vs 48 for a secretbox), and
|
||||
// `keyDecryptionNonce` is entirely ABSENT from the JSON.
|
||||
const buildSharedRawCollection = (
|
||||
recipientPublicKey: Uint8Array,
|
||||
opts?: { name?: string; ownerID?: number },
|
||||
): { raw: RawCollection; collectionKey: Uint8Array } => {
|
||||
const collectionKey = sodium.crypto_secretbox_keygen();
|
||||
const sealedKey = sodium.crypto_box_seal(collectionKey, recipientPublicKey);
|
||||
const name = opts?.name ?? "Shared Album";
|
||||
const { ciphertext: encName, nonce: nameNonce } = secretboxEncrypt(
|
||||
new TextEncoder().encode(name),
|
||||
collectionKey,
|
||||
);
|
||||
const raw: RawCollection = {
|
||||
id: 101,
|
||||
owner: { id: opts?.ownerID ?? 99 },
|
||||
encryptedKey: toBase64(sealedKey),
|
||||
// NOTE: no keyDecryptionNonce. Do not "fix" this fixture by adding
|
||||
// one: its absence is exactly what the real server sends, and an
|
||||
// unfaithful fixture here previously masked a crash on every
|
||||
// account with an incoming shared album.
|
||||
encryptedName: toBase64(encName),
|
||||
nameDecryptionNonce: toBase64(nameNonce),
|
||||
type: "album",
|
||||
updationTime: 1700000000000000,
|
||||
};
|
||||
return { raw, collectionKey };
|
||||
};
|
||||
|
||||
const buildRawFile = (
|
||||
collectionKey: Uint8Array,
|
||||
opts?: { title?: string; fileType?: number; creationTime?: number },
|
||||
@@ -137,13 +200,13 @@ describe("model.decryptCollection", () => {
|
||||
await sodium.ready;
|
||||
});
|
||||
|
||||
it("decrypts the collection key and name from a raw server response", () => {
|
||||
const masterKey = sodium.crypto_secretbox_keygen();
|
||||
const { raw, collectionKey } = buildRawCollection(masterKey, {
|
||||
it("decrypts an owned collection key and name from a raw server response", () => {
|
||||
const keys = buildKeys();
|
||||
const { raw, collectionKey } = buildRawCollection(keys.masterKey, {
|
||||
name: "Summer Photos",
|
||||
});
|
||||
|
||||
const col = decryptCollection(raw, masterKey, 1);
|
||||
const col = decryptCollection(raw, keys, 1);
|
||||
|
||||
expect(col.id).toBe(100);
|
||||
expect(col.key).toEqual(collectionKey);
|
||||
@@ -154,49 +217,73 @@ describe("model.decryptCollection", () => {
|
||||
expect(col.isShared).toBe(false);
|
||||
});
|
||||
|
||||
it("sets isShared when owner != current user", () => {
|
||||
const masterKey = sodium.crypto_secretbox_keygen();
|
||||
const { raw } = buildRawCollection(masterKey, { ownerID: 99 });
|
||||
it("decrypts a shared collection via sealed box when keyDecryptionNonce is absent", () => {
|
||||
// A collection shared TO us is not encrypted with our master key:
|
||||
// the sharer only knows our public key, so the collection key
|
||||
// arrives as crypto_box_seal(collectionKey, ourPublicKey) and the
|
||||
// response has NO keyDecryptionNonce. decryptCollection must
|
||||
// recover the key with the keypair, then decrypt the name with it
|
||||
// as usual. Accounts with any incoming shared album hit this path
|
||||
// on every listCollections call.
|
||||
const keys = buildKeys();
|
||||
const { raw, collectionKey } = buildSharedRawCollection(
|
||||
keys.publicKey,
|
||||
{ name: "Friend's Wedding", ownerID: 99 },
|
||||
);
|
||||
|
||||
const col = decryptCollection(raw, masterKey, 1);
|
||||
const col = decryptCollection(raw, keys, 1);
|
||||
|
||||
expect(col.key).toEqual(collectionKey);
|
||||
expect(col.name).toBe("Friend's Wedding");
|
||||
expect(col.ownerID).toBe(99);
|
||||
expect(col.isShared).toBe(true);
|
||||
});
|
||||
|
||||
it("maps known type strings to CollectionType", () => {
|
||||
const masterKey = sodium.crypto_secretbox_keygen();
|
||||
const keys = buildKeys();
|
||||
for (const type of ["album", "folder", "favorites", "uncategorized"]) {
|
||||
const { raw } = buildRawCollection(masterKey, { type });
|
||||
const col = decryptCollection(raw, masterKey, 1);
|
||||
const { raw } = buildRawCollection(keys.masterKey, { type });
|
||||
const col = decryptCollection(raw, keys, 1);
|
||||
expect(col.type).toBe(type);
|
||||
}
|
||||
});
|
||||
|
||||
it("maps unrecognised type strings to 'unknown'", () => {
|
||||
const masterKey = sodium.crypto_secretbox_keygen();
|
||||
const { raw } = buildRawCollection(masterKey, {
|
||||
const keys = buildKeys();
|
||||
const { raw } = buildRawCollection(keys.masterKey, {
|
||||
type: "someFutureType",
|
||||
});
|
||||
const col = decryptCollection(raw, masterKey, 1);
|
||||
const col = decryptCollection(raw, keys, 1);
|
||||
expect(col.type).toBe("unknown");
|
||||
});
|
||||
|
||||
it("handles a collection with no encrypted name gracefully", () => {
|
||||
// Some special collections (e.g. uncategorized) may have no name.
|
||||
const masterKey = sodium.crypto_secretbox_keygen();
|
||||
const { raw } = buildRawCollection(masterKey);
|
||||
const keys = buildKeys();
|
||||
const { raw } = buildRawCollection(keys.masterKey);
|
||||
delete raw.encryptedName;
|
||||
delete raw.nameDecryptionNonce;
|
||||
|
||||
const col = decryptCollection(raw, masterKey, 1);
|
||||
const col = decryptCollection(raw, keys, 1);
|
||||
expect(col.name).toBe("");
|
||||
});
|
||||
|
||||
it("throws when the master key is wrong", () => {
|
||||
const masterKey = sodium.crypto_secretbox_keygen();
|
||||
const wrongKey = sodium.crypto_secretbox_keygen();
|
||||
const { raw } = buildRawCollection(masterKey);
|
||||
it("throws when the master key is wrong for an owned collection", () => {
|
||||
const keys = buildKeys();
|
||||
const { raw } = buildRawCollection(keys.masterKey);
|
||||
|
||||
expect(() => decryptCollection(raw, wrongKey, 1)).toThrow();
|
||||
// buildKeys() generates fresh random key material, so this is a
|
||||
// client holding the wrong master key.
|
||||
expect(() => decryptCollection(raw, buildKeys(), 1)).toThrow();
|
||||
});
|
||||
|
||||
it("throws when the keypair is wrong for a shared collection", () => {
|
||||
const keys = buildKeys();
|
||||
const { raw } = buildSharedRawCollection(keys.publicKey);
|
||||
|
||||
// A different keypair must not be able to unseal the key.
|
||||
const wrongKeys = buildKeys();
|
||||
expect(() => decryptCollection(raw, wrongKeys, 1)).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user