Skip to content

Commit 47d28b4

Browse files
committed
fix protecting files/images
1 parent a9762c5 commit 47d28b4

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/services/data_encryption.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,38 @@ function decrypt(key, cipherText, ivLength = 13) {
5656
return "[protected]";
5757
}
5858

59-
const cipherTextBufferWithIv = Buffer.from(cipherText, 'base64');
60-
const iv = cipherTextBufferWithIv.slice(0, ivLength);
59+
try {
60+
const cipherTextBufferWithIv = Buffer.from(cipherText.toString(), 'base64');
61+
const iv = cipherTextBufferWithIv.slice(0, ivLength);
6162

62-
const cipherTextBuffer = cipherTextBufferWithIv.slice(ivLength);
63+
const cipherTextBuffer = cipherTextBufferWithIv.slice(ivLength);
6364

64-
const decipher = crypto.createDecipheriv('aes-128-cbc', pad(key), pad(iv));
65+
const decipher = crypto.createDecipheriv('aes-128-cbc', pad(key), pad(iv));
6566

66-
const decryptedBytes = Buffer.concat([decipher.update(cipherTextBuffer), decipher.final()]);
67+
const decryptedBytes = Buffer.concat([decipher.update(cipherTextBuffer), decipher.final()]);
6768

68-
const digest = decryptedBytes.slice(0, 4);
69-
const payload = decryptedBytes.slice(4);
69+
const digest = decryptedBytes.slice(0, 4);
70+
const payload = decryptedBytes.slice(4);
7071

71-
const computedDigest = shaArray(payload).slice(0, 4);
72+
const computedDigest = shaArray(payload).slice(0, 4);
7273

73-
if (!arraysIdentical(digest, computedDigest)) {
74-
return false;
75-
}
74+
if (!arraysIdentical(digest, computedDigest)) {
75+
return false;
76+
}
7677

77-
return payload;
78+
return payload;
79+
}
80+
catch (e) {
81+
// recovery from https://github.com/zadam/trilium/issues/510
82+
if (e.message && e.message.includes("WRONG_FINAL_BLOCK_LENGTH")) {
83+
log.info("Caught WRONG_FINAL_BLOCK_LENGTH, returning cipherText instead");
84+
85+
return cipherText;
86+
}
87+
else {
88+
throw e;
89+
}
90+
}
7891
}
7992

8093
function decryptString(dataKey, cipherText) {

src/services/notes.js

+8
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ async function updateNote(noteId, noteUpdates) {
346346

347347
await saveNoteRevision(note);
348348

349+
// if protected status changed, then we need to encrypt/decrypt the content anyway
350+
if (['file', 'image'].includes(note.type) && note.isProtected !== noteUpdates.isProtected) {
351+
noteUpdates.content = await note.getContent();
352+
}
353+
349354
const noteTitleChanged = note.title !== noteUpdates.title;
350355

351356
note.title = noteUpdates.title;
@@ -357,6 +362,9 @@ async function updateNote(noteId, noteUpdates) {
357362

358363
await note.setContent(noteUpdates.content);
359364
}
365+
else if (noteUpdates.content) {
366+
await note.setContent(noteUpdates.content);
367+
}
360368

361369
if (noteTitleChanged) {
362370
await triggerNoteTitleChanged(note);

src/services/protected_session.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function decryptNote(note) {
5959
function decryptNoteContent(note) {
6060
try {
6161
if (note.content != null) {
62-
note.content = dataEncryptionService.decrypt(getDataKey(), note.content.toString());
62+
note.content = dataEncryptionService.decrypt(getDataKey(), note.content);
6363
}
6464
}
6565
catch (e) {

0 commit comments

Comments
 (0)