change salts on password change + more robust handling of decryption failures

This commit is contained in:
zadam 2020-09-25 20:55:45 +02:00
parent 9de51c8b9e
commit de30095737
6 changed files with 26 additions and 10 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "trilium", "name": "trilium",
"version": "0.44.3-beta", "version": "0.44.4",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -805,7 +805,7 @@ class Note extends Entity {
* @returns {boolean} - true if note has children * @returns {boolean} - true if note has children
*/ */
hasChildren() { hasChildren() {
return (this.getChildNotes()).length > 0; return this.getChildNotes().length > 0;
} }
/** /**

View File

@ -14,10 +14,14 @@ function changePassword(currentPassword, newPassword) {
}; };
} }
const newPasswordVerificationKey = utils.toBase64(myScryptService.getVerificationHash(newPassword));
const decryptedDataKey = passwordEncryptionService.getDataKey(currentPassword);
sql.transactional(() => { sql.transactional(() => {
const decryptedDataKey = passwordEncryptionService.getDataKey(currentPassword);
optionService.setOption('passwordVerificationSalt', utils.randomSecureToken(32));
optionService.setOption('passwordDerivedKeySalt', utils.randomSecureToken(32));
const newPasswordVerificationKey = utils.toBase64(myScryptService.getVerificationHash(newPassword));
passwordEncryptionService.setDataKey(newPassword, decryptedDataKey); passwordEncryptionService.setDataKey(newPassword, decryptedDataKey);
optionService.setOption('passwordVerificationHash', newPasswordVerificationKey); optionService.setOption('passwordVerificationHash', newPasswordVerificationKey);

View File

@ -327,7 +327,7 @@ class Note {
decrypt() { decrypt() {
if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) { if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {
this.title = protectedSessionService.decryptString(note.title); this.title = protectedSessionService.decryptString(this.title);
this.isDecrypted = true; this.isDecrypted = true;
} }

View File

@ -4,6 +4,7 @@ const sql = require('../sql.js');
const eventService = require('../events.js'); const eventService = require('../events.js');
const noteCache = require('./note_cache'); const noteCache = require('./note_cache');
const sqlInit = require('../sql_init'); const sqlInit = require('../sql_init');
const log = require('../log');
const Note = require('./entities/note'); const Note = require('./entities/note');
const Branch = require('./entities/branch'); const Branch = require('./entities/branch');
const Attribute = require('./entities/attribute'); const Attribute = require('./entities/attribute');
@ -147,7 +148,12 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED
}); });
eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
noteCache.decryptProtectedNotes(); try {
noteCache.decryptProtectedNotes();
}
catch (e) {
log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`);
}
}); });
module.exports = { module.exports = {

View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
const utils = require('./utils'); const utils = require('./utils');
const log = require('./log');
const dataEncryptionService = require('./data_encryption'); const dataEncryptionService = require('./data_encryption');
const cls = require('./cls'); const cls = require('./cls');
@ -35,11 +36,16 @@ function isProtectedSessionAvailable() {
} }
function decryptNotes(notes) { function decryptNotes(notes) {
for (const note of notes) { try {
if (note.isProtected) { for (const note of notes) {
note.title = decryptString(note.title); if (note.isProtected) {
note.title = decryptString(note.title);
}
} }
} }
catch (e) {
log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`);
}
} }
function encrypt(plainText) { function encrypt(plainText) {