A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: fix non-legacy users can't change password
Abdullah Atta9e251d6f9e83
diff --git a/packages/core/__tests__/encryption-verification.test.ts b/packages/core/__tests__/encryption-verification.test.tsindex 8cbc7a075..fa2631a2b 100644--- a/packages/core/__tests__/encryption-verification.test.ts+++ b/packages/core/__tests__/encryption-verification.test.ts@@ -229,7 +229,9 @@ describe("UserManager.verifyEncryptionKey", () => {// Server returns undefined — should throwawait expect(db.user.verifyEncryptionKey({ password, salt })- ).rejects.toThrow("Failed to fetch encryption verifier.");+ ).rejects.toThrow(+ "Encryption key cannot be verified: no encryption verifier found."+ );});mockGet.mockRestore();@@ -315,7 +317,7 @@ describe("UserManager.verifyEncryptionKey", () => {});describe("user with only one of legacy DEK or DEK", () => {- test("throws when only dataEncryptionKey exists", async () => {+ test("succeeds when only dataEncryptionKey exists", async () => {await databaseTest().then(async (db) => {const password = "password";const salt = randomBytes(16).toString("base64");@@ -336,13 +338,11 @@ describe("UserManager.verifyEncryptionKey", () => {await expect(db.user.verifyEncryptionKey({ password, salt })- ).rejects.toThrow(- "Cannot verify the provided encryption key as user has only a single encryption key."- );+ ).resolves.toBeUndefined();});});- test("throws when only legacyDataEncryptionKey exists", async () => {+ test("succeeds when only legacyDataEncryptionKey exists", async () => {await databaseTest().then(async (db) => {const password = "password";const salt = randomBytes(16).toString("base64");@@ -363,8 +363,33 @@ describe("UserManager.verifyEncryptionKey", () => {await expect(db.user.verifyEncryptionKey({ password, salt })+ ).resolves.toBeUndefined();+ });+ });++ test("invalid key throws even when only a single DEK exists", async () => {+ await databaseTest().then(async (db) => {+ const password = "correct";+ const salt = randomBytes(16).toString("base64");+ await db.user.setUser({+ id: "user-123", email: "[email protected]", isEmailConfirmed: true,+ salt, mfa: { isEnabled: false, primaryMethod: "app", remainingValidCodes: 0 },+ subscription: { appId: 0, cancelURL: null, expiry: 0, productId: null, provider: "none", start: 0, plan: "free", status: "trial", updateURL: null, googlePurchaseToken: null }+ } as any);+ await db.storage().deriveCryptoKey({ password, salt });+ const masterKey = await db.user.getMasterKey();+ const km = new KeyManager(db);++ const randomKey = await db.crypto().generateRandomKey();+ const dek = await km.wrapKey(randomKey, masterKey!);++ const user = await db.user.getUser();+ await db.user.setUser({ ...user, dataEncryptionKey: dek });++ await expect(+ db.user.verifyEncryptionKey({ password: "wrongpassword", salt })).rejects.toThrow(- "Cannot verify the provided encryption key as user has only a single encryption key."+ "Your data cannot be decrypted using the provided encryption key.");});});diff --git a/packages/core/__tests__/password-change.test.ts b/packages/core/__tests__/password-change.test.tsindex 661b18b98..618146079 100644--- a/packages/core/__tests__/password-change.test.ts+++ b/packages/core/__tests__/password-change.test.ts@@ -286,7 +286,7 @@ describe("Key migration during password change", () => {});});- test("user with only DEK (no legacy): password change fails verification", async () => {+ test("user with only DEK (no legacy): password change succeeds verification", async () => {await databaseTest().then(async (db) => {const password = "oldpassword";const salt = randomBytes(16).toString("base64");@@ -305,12 +305,14 @@ describe("Key migration during password change", () => {await db.user.setUser({ ...FULL_USER, salt, dataEncryptionKey: dek });- // verifyEncryptionKey throws when only one of legacy DEK or DEK exists- await expect(- db.user.changePassword("oldpassword", "newpassword")- ).rejects.toThrow(- "Cannot verify the provided encryption key as user has only a single encryption key."- );+ // A single DEK is now a valid verifier, so the change should succeed+ // and the DEK should be rewrapped with the new password.+ const result = await db.user.changePassword("oldpassword", "newpassword");+ expect(result).toBe(true);++ const userAfter = await db.user.getUser();+ expect(userAfter.dataEncryptionKey).toBeDefined();+ expect(userAfter.dataEncryptionKey?.cipher).not.toBe(dek.cipher);});});diff --git a/packages/core/src/api/user-manager.ts b/packages/core/src/api/user-manager.tsindex 165adcaaa..9862dcc6a 100644--- a/packages/core/src/api/user-manager.ts+++ b/packages/core/src/api/user-manager.ts@@ -637,28 +637,34 @@ class UserManager {const user = await this.getUser();if (!user) throw new Error("User not found.");- if (user.legacyDataEncryptionKey && user.dataEncryptionKey)- await this.keyManager.unwrapKey(user.legacyDataEncryptionKey, key);- else if (!user.legacyDataEncryptionKey && !user.dataEncryptionKey) {- const verifier =- user.attachmentsKey ??- user.monographPasswordsKey ??- (await this.fetchEncryptionVerifier());- if (!verifier) throw new Error("Failed to fetch encryption verifier.");+ const verifiers = [+ user.attachmentsKey,+ user.monographPasswordsKey,+ user.legacyDataEncryptionKey,+ user.dataEncryptionKey,+ user.inboxKeys?.private+ ].filter((v): v is Cipher<"base64"> => !!v);++ if (verifiers.length === 0) {+ const verifier = await this.fetchEncryptionVerifier();+ if (verifier) verifiers.push(verifier);+ else+ throw new Error(+ "Encryption key cannot be verified: no encryption verifier found."+ );+ }++ for (const verifier of verifiers) {const decryptedData = await this.db.storage().decrypt(key, verifier).then(() => true).catch(() => false);-if (!decryptedData)throw new Error("Your data cannot be decrypted using the provided encryption key.");- } else- throw new Error(- "Cannot verify the provided encryption key as user has only a single encryption key."- );+ }}async _updatePassword(