From 174d4e67af57a46367c760dc8a9a5a88c00a8c62 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 27 Aug 2018 23:04:52 +0200 Subject: [PATCH] fix encrypting new notes --- src/entities/entity.js | 10 +++++++--- src/entities/note.js | 3 +++ src/routes/routes.js | 2 +- src/services/data_encryption.js | 8 ++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/entities/entity.js b/src/entities/entity.js index 7c7d8a961..9f1962edd 100644 --- a/src/entities/entity.js +++ b/src/entities/entity.js @@ -17,9 +17,7 @@ class Entity { } beforeSaving() { - if (!this[this.constructor.primaryKeyName]) { - this[this.constructor.primaryKeyName] = utils.newEntityId(); - } + this.generateIdIfNecessary(); const origHash = this.hash; @@ -28,6 +26,12 @@ class Entity { this.isChanged = origHash !== this.hash; } + generateIdIfNecessary() { + if (!this[this.constructor.primaryKeyName]) { + this[this.constructor.primaryKeyName] = utils.newEntityId(); + } + } + generateHash() { let contentToHash = ""; diff --git a/src/entities/note.js b/src/entities/note.js index 58b677ba2..09bfb3482 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -513,6 +513,9 @@ class Note extends Entity { this.content = JSON.stringify(this.jsonContent, null, '\t'); } + // we do this here because encryption needs the note ID for the IV + this.generateIdIfNecessary(); + if (this.isProtected) { protectedSessionService.encryptNote(this); } diff --git a/src/routes/routes.js b/src/routes/routes.js index fcbd409e1..d12ef77b9 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -84,7 +84,7 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio } } catch (e) { - log.info(`${method} ${path} threw exception: ` + e.stack); + log.error(`${method} ${path} threw exception: ` + e.stack); res.sendStatus(500); } diff --git a/src/services/data_encryption.js b/src/services/data_encryption.js index f2b2beabf..fc92dd794 100644 --- a/src/services/data_encryption.js +++ b/src/services/data_encryption.js @@ -85,10 +85,18 @@ function decryptString(dataKey, iv, cipherText) { } function noteTitleIv(iv) { + if (!iv) { + throw new Error("Empty iv!"); + } + return "0" + iv; } function noteContentIv(iv) { + if (!iv) { + throw new Error("Empty iv!"); + } + return "1" + iv; }