mirror of
https://github.com/zadam/trilium.git
synced 2025-11-04 20:36:13 +01:00
note history decryption now works, more cleanup
This commit is contained in:
@@ -55,16 +55,8 @@ const noteHistory = (function() {
|
||||
|
||||
const historyItem = historyItems.find(r => r.note_history_id === optVal);
|
||||
|
||||
let noteTitle = historyItem.note_title;
|
||||
let noteText = historyItem.note_text;
|
||||
|
||||
if (historyItem.is_protected) {
|
||||
noteTitle = encryption.decryptString(noteTitle);
|
||||
noteText = encryption.decryptString(noteText);
|
||||
}
|
||||
|
||||
titleEl.html(noteTitle);
|
||||
contentEl.html(noteText);
|
||||
titleEl.html(historyItem.note_title);
|
||||
contentEl.html(historyItem.note_text);
|
||||
});
|
||||
|
||||
$(document).on('click', "a[action='note-history']", event => {
|
||||
|
||||
@@ -6,10 +6,7 @@ const encryption = (function() {
|
||||
const encryptionPasswordEl = $("#encryption-password");
|
||||
|
||||
let encryptionDeferred = null;
|
||||
let dataKey = null;
|
||||
let lastEncryptionOperationDate = null;
|
||||
let passwordDerivedKeySalt = null;
|
||||
let encryptedDataKey = null;
|
||||
let encryptionSessionTimeout = null;
|
||||
let protectedSessionId = null;
|
||||
|
||||
@@ -18,9 +15,7 @@ const encryption = (function() {
|
||||
type: 'GET',
|
||||
error: () => showError("Error getting encryption settings.")
|
||||
}).then(settings => {
|
||||
passwordDerivedKeySalt = settings.password_derived_key_salt;
|
||||
encryptionSessionTimeout = settings.encryption_session_timeout;
|
||||
encryptedDataKey = settings.encrypted_data_key;
|
||||
});
|
||||
|
||||
function setEncryptionSessionTimeout(encSessTimeout) {
|
||||
@@ -54,7 +49,7 @@ const encryption = (function() {
|
||||
return dfd.promise();
|
||||
}
|
||||
|
||||
async function setupEncryptionSession() {
|
||||
async function setupProtectedSession() {
|
||||
const password = encryptionPasswordEl.val();
|
||||
encryptionPasswordEl.val("");
|
||||
|
||||
@@ -110,76 +105,6 @@ const encryption = (function() {
|
||||
return protectedSessionId !== null;
|
||||
}
|
||||
|
||||
function getDataAes() {
|
||||
lastEncryptionOperationDate = new Date();
|
||||
|
||||
return new aesjs.ModeOfOperation.ctr(dataKey, new aesjs.Counter(5));
|
||||
}
|
||||
|
||||
function encryptString(str) {
|
||||
return encrypt(getDataAes(), str);
|
||||
}
|
||||
|
||||
function encrypt(aes, str) {
|
||||
const payload = Array.from(aesjs.utils.utf8.toBytes(str));
|
||||
const digest = sha256Array(payload).slice(0, 4);
|
||||
|
||||
const digestWithPayload = digest.concat(payload);
|
||||
|
||||
const encryptedBytes = aes.encrypt(digestWithPayload);
|
||||
|
||||
return uint8ToBase64(encryptedBytes);
|
||||
}
|
||||
|
||||
function decryptString(encryptedBase64) {
|
||||
const decryptedBytes = decrypt(getDataAes(), encryptedBase64);
|
||||
|
||||
return aesjs.utils.utf8.fromBytes(decryptedBytes);
|
||||
}
|
||||
|
||||
function decrypt(aes, encryptedBase64) {
|
||||
const encryptedBytes = base64ToUint8Array(encryptedBase64);
|
||||
|
||||
const decryptedBytes = aes.decrypt(encryptedBytes);
|
||||
|
||||
const digest = decryptedBytes.slice(0, 4);
|
||||
const payload = decryptedBytes.slice(4);
|
||||
|
||||
const hashArray = sha256Array(payload);
|
||||
|
||||
const computedDigest = hashArray.slice(0, 4);
|
||||
|
||||
if (!arraysIdentical(digest, computedDigest)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
function sha256Array(content) {
|
||||
const hash = sha256.create();
|
||||
hash.update(content);
|
||||
return hash.array();
|
||||
}
|
||||
|
||||
function arraysIdentical(a, b) {
|
||||
let i = a.length;
|
||||
if (i !== b.length) return false;
|
||||
while (i--) {
|
||||
if (a[i] !== b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function encryptNote(note) {
|
||||
note.detail.note_title = encryptString(note.detail.note_title);
|
||||
note.detail.note_text = encryptString(note.detail.note_text);
|
||||
|
||||
note.detail.is_protected = true;
|
||||
|
||||
return note;
|
||||
}
|
||||
|
||||
async function protectNoteAndSendToServer() {
|
||||
await ensureProtectedSession(true, true);
|
||||
|
||||
@@ -191,38 +116,7 @@ const encryption = (function() {
|
||||
|
||||
await noteEditor.saveNoteToServer(note);
|
||||
|
||||
noteEditor.setNoteBackgroundIfEncrypted(note);
|
||||
}
|
||||
|
||||
async function changeEncryptionOnNoteHistory(noteId, protect) {
|
||||
const result = await $.ajax({
|
||||
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (protect ? 0 : 1),
|
||||
type: 'GET',
|
||||
error: () => showError("Error getting note history.")
|
||||
});
|
||||
|
||||
for (const row of result) {
|
||||
if (protect) {
|
||||
row.note_title = encryptString(row.note_title);
|
||||
row.note_text = encryptString(row.note_text);
|
||||
}
|
||||
else {
|
||||
row.note_title = decryptString(row.note_title);
|
||||
row.note_text = decryptString(row.note_text);
|
||||
}
|
||||
|
||||
row.is_protected = protect;
|
||||
|
||||
await $.ajax({
|
||||
url: baseApiUrl + 'notes-history',
|
||||
type: 'PUT',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify(row),
|
||||
error: () => showError("Error de/encrypting note history.")
|
||||
});
|
||||
|
||||
console.log('Note history ' + row.note_history_id + ' de/encrypted');
|
||||
}
|
||||
noteEditor.setNoteBackgroundIfProtected(note);
|
||||
}
|
||||
|
||||
async function unprotectNoteAndSendToServer() {
|
||||
@@ -236,115 +130,11 @@ const encryption = (function() {
|
||||
|
||||
await noteEditor.saveNoteToServer(note);
|
||||
|
||||
await changeEncryptionOnNoteHistory(note.detail.note_id, false);
|
||||
|
||||
noteEditor.setNoteBackgroundIfEncrypted(note);
|
||||
}
|
||||
|
||||
async function encryptSubTree(noteId) {
|
||||
await ensureProtectedSession(true, true);
|
||||
|
||||
updateSubTreeRecursively(noteId, note => {
|
||||
if (!note.detail.is_protected) {
|
||||
encryptNote(note);
|
||||
|
||||
note.detail.is_protected = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
note => {
|
||||
if (note.detail.note_id === noteEditor.getCurrentNoteId()) {
|
||||
noteEditor.loadNoteToEditor(note.detail.note_id);
|
||||
}
|
||||
else {
|
||||
noteEditor.setTreeBasedOnEncryption(note);
|
||||
}
|
||||
});
|
||||
|
||||
showMessage("Encryption finished.");
|
||||
}
|
||||
|
||||
async function decryptSubTree(noteId) {
|
||||
await ensureProtectedSession(true, true);
|
||||
|
||||
updateSubTreeRecursively(noteId, note => {
|
||||
if (note.detail.is_protected) {
|
||||
decryptNote(note);
|
||||
|
||||
note.detail.is_protected = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
note => {
|
||||
if (note.detail.note_id === noteEditor.getCurrentNoteId()) {
|
||||
noteEditor.loadNoteToEditor(note.detail.note_id);
|
||||
}
|
||||
else {
|
||||
noteEditor.setTreeBasedOnEncryption(note);
|
||||
}
|
||||
});
|
||||
|
||||
showMessage("Decryption finished.");
|
||||
}
|
||||
|
||||
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
|
||||
updateNoteSynchronously(noteId, updateCallback, successCallback);
|
||||
|
||||
const node = treeUtils.getNodeByKey(noteId);
|
||||
if (!node || !node.getChildren()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const child of node.getChildren()) {
|
||||
updateSubTreeRecursively(child.key, updateCallback, successCallback);
|
||||
}
|
||||
}
|
||||
|
||||
function updateNoteSynchronously(noteId, updateCallback, successCallback) {
|
||||
$.ajax({
|
||||
url: baseApiUrl + 'notes/' + noteId,
|
||||
type: 'GET',
|
||||
async: false,
|
||||
success: note => {
|
||||
const needSave = updateCallback(note);
|
||||
|
||||
if (!needSave) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const link of note.links) {
|
||||
delete link.type;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: baseApiUrl + 'notes/' + noteId,
|
||||
type: 'PUT',
|
||||
data: JSON.stringify(note),
|
||||
contentType: "application/json",
|
||||
async: false,
|
||||
success: () => {
|
||||
if (successCallback) {
|
||||
successCallback(note);
|
||||
}
|
||||
},
|
||||
error: () => showError("Updating " + noteId + " failed.")
|
||||
|
||||
});
|
||||
},
|
||||
error: () => showError("Reading " + noteId + " failed.")
|
||||
});
|
||||
noteEditor.setNoteBackgroundIfProtected(note);
|
||||
}
|
||||
|
||||
encryptionPasswordFormEl.submit(() => {
|
||||
setupEncryptionSession();
|
||||
setupProtectedSession();
|
||||
|
||||
return false;
|
||||
});
|
||||
@@ -360,12 +150,8 @@ const encryption = (function() {
|
||||
ensureProtectedSession,
|
||||
resetEncryptionSession,
|
||||
isEncryptionAvailable,
|
||||
encryptString,
|
||||
decryptString,
|
||||
protectNoteAndSendToServer,
|
||||
unprotectNoteAndSendToServer,
|
||||
encryptSubTree,
|
||||
decryptSubTree,
|
||||
getProtectedSessionId
|
||||
};
|
||||
})();
|
||||
@@ -4,8 +4,8 @@ const noteEditor = (function() {
|
||||
const treeEl = $("#tree");
|
||||
const noteTitleEl = $("#note-title");
|
||||
const noteDetailEl = $('#note-detail');
|
||||
const encryptButton = $("#encrypt-button");
|
||||
const decryptButton = $("#decrypt-button");
|
||||
const protectButton = $("#protect-button");
|
||||
const unprotectButton = $("#unprotect-button");
|
||||
const noteDetailWrapperEl = $("#note-detail-wrapper");
|
||||
const encryptionPasswordDialogEl = $("#encryption-password-dialog");
|
||||
const encryptionPasswordEl = $("#encryption-password");
|
||||
@@ -119,24 +119,23 @@ const noteEditor = (function() {
|
||||
|
||||
let newNoteCreated = false;
|
||||
|
||||
async function createNote(node, parentKey, target, encryption) {
|
||||
// if encryption isn't available (user didn't enter password yet), then note is created as unencrypted
|
||||
async function createNote(node, parentKey, target, isProtected) {
|
||||
// if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted
|
||||
// but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often
|
||||
if (!encryption || !encryption.isEncryptionAvailable()) {
|
||||
encryption = 0;
|
||||
if (!isProtected || !encryption.isEncryptionAvailable()) {
|
||||
isProtected = false;
|
||||
}
|
||||
|
||||
const newNoteName = "new note";
|
||||
const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryption.encryptString(newNoteName) : newNoteName;
|
||||
|
||||
const result = await $.ajax({
|
||||
url: baseApiUrl + 'notes/' + parentKey + '/children' ,
|
||||
type: 'POST',
|
||||
data: JSON.stringify({
|
||||
note_title: newNoteNameEncryptedIfNecessary,
|
||||
note_title: newNoteName,
|
||||
target: target,
|
||||
target_note_id: node.key,
|
||||
encryption: encryption
|
||||
is_protected: isProtected
|
||||
}),
|
||||
contentType: "application/json"
|
||||
});
|
||||
@@ -145,8 +144,8 @@ const noteEditor = (function() {
|
||||
title: newNoteName,
|
||||
key: result.note_id,
|
||||
note_id: result.note_id,
|
||||
encryption: encryption,
|
||||
extraClasses: encryption ? "encrypted" : ""
|
||||
is_protected: isProtected,
|
||||
extraClasses: isProtected ? "protected" : ""
|
||||
};
|
||||
|
||||
glob.allNoteIds.push(result.note_id);
|
||||
@@ -166,24 +165,24 @@ const noteEditor = (function() {
|
||||
showMessage("Created!");
|
||||
}
|
||||
|
||||
function setTreeBasedOnEncryption(note) {
|
||||
function setTreeBasedOnProtectedStatus(note) {
|
||||
const node = treeUtils.getNodeByKey(note.detail.note_id);
|
||||
node.toggleClass("encrypted", note.detail.is_protected);
|
||||
node.toggleClass("protected", note.detail.is_protected);
|
||||
}
|
||||
|
||||
function setNoteBackgroundIfEncrypted(note) {
|
||||
function setNoteBackgroundIfProtected(note) {
|
||||
if (note.detail.is_protected) {
|
||||
$(".note-editable").addClass("encrypted");
|
||||
encryptButton.hide();
|
||||
decryptButton.show();
|
||||
$(".note-editable").addClass("protected");
|
||||
protectButton.hide();
|
||||
unprotectButton.show();
|
||||
}
|
||||
else {
|
||||
$(".note-editable").removeClass("encrypted");
|
||||
encryptButton.show();
|
||||
decryptButton.hide();
|
||||
$(".note-editable").removeClass("protected");
|
||||
protectButton.show();
|
||||
unprotectButton.hide();
|
||||
}
|
||||
|
||||
setTreeBasedOnEncryption(note);
|
||||
setTreeBasedOnProtectedStatus(note);
|
||||
}
|
||||
|
||||
async function loadNoteToEditor(noteId) {
|
||||
@@ -222,7 +221,7 @@ const noteEditor = (function() {
|
||||
|
||||
noteChangeDisabled = false;
|
||||
|
||||
setNoteBackgroundIfEncrypted(currentNote);
|
||||
setNoteBackgroundIfProtected(currentNote);
|
||||
|
||||
showAppIfHidden();
|
||||
}
|
||||
@@ -260,8 +259,8 @@ const noteEditor = (function() {
|
||||
saveNoteToServer,
|
||||
createNewTopLevelNote,
|
||||
createNote,
|
||||
setNoteBackgroundIfEncrypted,
|
||||
setTreeBasedOnEncryption,
|
||||
setNoteBackgroundIfProtected,
|
||||
setTreeBasedOnProtectedStatus,
|
||||
loadNoteToEditor,
|
||||
loadNote,
|
||||
getCurrentNote,
|
||||
|
||||
@@ -26,7 +26,7 @@ const noteTree = (function() {
|
||||
note.title = note.note_title;
|
||||
|
||||
if (note.is_protected) {
|
||||
note.extraClasses = "encrypted";
|
||||
note.extraClasses = "protected";
|
||||
}
|
||||
else {
|
||||
if (note.is_clone) {
|
||||
|
||||
Reference in New Issue
Block a user