Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/lib/components/shared/note-screen-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,15 @@ shared.isModified = function(comp: BaseNoteScreenComponent) {
shared.reloadNote = async (comp: BaseNoteScreenComponent, useDefaultEditorState = false) => {
const isProvisionalNote = comp.props.provisionalNoteIds.includes(comp.props.noteId);

const note = await Note.load(comp.props.noteId);
let note = await Note.load(comp.props.noteId);
if (note?.encryption_cipher_text) {
try {
note = await Note.decrypt(note);
} catch (error) {
reg.logger().info(`Could not decrypt note ${note.id}, note could not be refreshed:`, error.message);
note = null; // fall into the non existent note handling
}
}
let mode = comp.state.mode;

if (useDefaultEditorState) {
Expand Down
26 changes: 19 additions & 7 deletions packages/lib/services/DecryptionWorker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BaseItem, { ItemsThatNeedDecryptionResult } from '../models/BaseItem';
import BaseModel from '../BaseModel';
import BaseModel, { ModelType } from '../BaseModel';
import MasterKey from '../models/MasterKey';
import Resource from '../models/Resource';
import ResourceService from './ResourceService';
Expand Down Expand Up @@ -219,6 +219,12 @@ export default class DecryptionWorker {
await this.kvStore().deleteValue(errorKey);
};

const markSuccessfulDecryption = async (decryptedItemType: number) => {
await clearDecryptionCounter();
if (!decryptedItemCounts[decryptedItemType]) decryptedItemCounts[decryptedItemType] = 0;
decryptedItemCounts[decryptedItemType]++;
};

// Don't log in production as it results in many messages when importing many items
// this.logger().debug('DecryptionWorker: decrypting: ' + item.id + ' (' + ItemClass.tableName() + ')');
try {
Expand All @@ -231,13 +237,19 @@ export default class DecryptionWorker {
continue;
}

const decryptedItem = await ItemClass.decrypt(item);

await clearDecryptionCounter();

if (!decryptedItemCounts[decryptedItem.type_]) decryptedItemCounts[decryptedItem.type_] = 0;
if (item.type_ === ModelType.Note) {
// Validate if still eligible to decrypt using the latest encryption_applied value, as notes may be decrypted on demand while the decryption worker is running.
// If it has been decrypted already, avoid decrypting it again to avoid potentially overwriting it with an outdated version
const encryptionApplied = (await ItemClass.load(item.id, { fields: ['encryption_applied'] }))?.encryption_applied;
if (!encryptionApplied) {
this.logger().info(`DecryptionWorker: Skipping decryption for note ${item.id} as it was already decrypted on demand`);
await markSuccessfulDecryption(item.type_);
continue;
}
}

decryptedItemCounts[decryptedItem.type_]++;
const decryptedItem = await ItemClass.decrypt(item);
await markSuccessfulDecryption(decryptedItem.type_);

if (decryptedItem.type_ === Resource.modelType() && !!decryptedItem.encryption_blob_encrypted) {
// itemsThatNeedDecryption() will return the resource again if the blob has not been decrypted,
Expand Down
Loading