diff --git a/services/notes.js b/services/notes.js index a568f06b5..7cbf41d8b 100644 --- a/services/notes.js +++ b/services/notes.js @@ -81,6 +81,12 @@ async function protectNoteRecursively(noteId, dataKey, protect) { } } +function decryptNote(note, dataKey) { + note.note_title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title); + note.note_text = data_encryption.decryptString(dataKey, data_encryption.noteTextIv(note.note_id), note.note_text); + note.is_protected = false; +} + async function protectNote(note, dataKey, protect) { let changed = false; @@ -92,9 +98,7 @@ async function protectNote(note, dataKey, protect) { changed = true; } else if (!protect && note.is_protected) { - note.note_title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title); - note.note_text = data_encryption.decryptString(dataKey, data_encryption.noteTextIv(note.note_id), note.note_text); - note.is_protected = false; + decryptNote(note, dataKey); changed = true; } @@ -150,14 +154,19 @@ async function updateNote(noteId, newNote, ctx) { if (!existingNoteHistoryId && (now - newNote.detail.date_created) >= historySnapshotTimeInterval) { const oldNote = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); + if (oldNote.is_protected) { + decryptNote(oldNote, ctx.getDataKey()); + } + const newNoteHistoryId = utils.newNoteHistoryId(); await sql.insert('notes_history', { note_history_id: newNoteHistoryId, note_id: noteId, + // title and text should be decrypted now note_title: oldNote.note_title, note_text: oldNote.note_text, - is_protected: oldNote.is_protected, + is_protected: 0, // will be fixed in the protectNoteHistory() call date_modified_from: oldNote.date_modified, date_modified_to: now });