Skip to content

Commit

Permalink
core: add checks when accessing a note using this._db.notes.note
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Jan 17, 2023
1 parent ffacb88 commit a62ca2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
16 changes: 11 additions & 5 deletions packages/core/api/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ export default class Vault {
* @param {string} password The password to unlock note with
*/
async remove(noteId, password) {
const note = this._db.notes.note(noteId).data;
await this._unlockNote(note, password, true);
const note = this._db.notes.note(noteId);
if (!note) return;
await this._unlockNote(note.data, password, true);
}

/**
Expand All @@ -186,8 +187,10 @@ export default class Vault {
* @param {string} password The password to open note with
*/
async open(noteId, password) {
const note = this._db.notes.note(noteId).data;
const unlockedNote = await this._unlockNote(note, password, false);
const note = this._db.notes.note(noteId);
if (!note) return;

const unlockedNote = await this._unlockNote(note.data, password, false);
this._password = password;
return unlockedNote;
}
Expand Down Expand Up @@ -271,7 +274,10 @@ export default class Vault {
async _lockNote(note, password) {
let { id, content: { type, data } = {}, sessionId, title } = note;

note = this._db.notes.note(id).data;
note = this._db.notes.note(id);
if (!note) return;

note = note.data;
const contentId = note.contentId;
if (!contentId) throw new Error("Cannot lock note because it is empty.");

Expand Down
8 changes: 4 additions & 4 deletions packages/core/collections/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default class Attachments extends Collection {

async detach(attachment) {
await this._db.notes.init();
for (let noteId of attachment.noteIds) {
for (const noteId of attachment.noteIds) {
const note = this._db.notes.note(noteId);
if (!note) continue;
const contentId = note.data.contentId;
Expand All @@ -195,9 +195,9 @@ export default class Attachments extends Collection {

async _canDetach(attachment) {
await this._db.notes.init();
for (let noteId of attachment.noteIds) {
const note = this._db.notes.note(noteId).data;
if (note.locked) return false;
for (const noteId of attachment.noteIds) {
const note = this._db.notes.note(noteId);
if (note && note.data.locked) return false;
}

return true;
Expand Down
14 changes: 8 additions & 6 deletions packages/core/collections/note-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export default class NoteHistory extends Collection {
if (!noteId || !dateEdited || !content) return;
let sessionId = `${noteId}_${dateEdited}`;
let oldSession = await this._collection.getItem(sessionId);
let locked = this._db.notes.note(noteId)?.data?.locked;

let session = {
type: "session",
Expand All @@ -96,7 +95,8 @@ export default class NoteHistory extends Collection {
localOnly: true
};

if (locked) {
const note = this._db.notes.note(noteId);
if (note && note.data.locked) {
session.locked = true;
}

Expand Down Expand Up @@ -178,12 +178,14 @@ export default class NoteHistory extends Collection {
/**
* @type {Session}
*/
let session = await this._collection.getItem(sessionId);
let content = await this.sessionContent.get(session.sessionContentId);
let note = this._db.notes.note(session.noteId).data;
const session = await this._collection.getItem(sessionId);
const content = await this.sessionContent.get(session.sessionContentId);
const note = this._db.notes.note(session.noteId);
if (!note) return;

if (session.locked) {
await this._db.content.add({
id: note.contentId,
id: note.data.contentId,
data: content.data,
type: content.type
});
Expand Down
7 changes: 5 additions & 2 deletions packages/core/collections/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ export default class Relations extends Collection {
case "reminder":
item = this._db.reminders.reminder(reference.id);
break;
case "note":
item = this._db.notes.note(reference.id)?.data;
case "note": {
const note = this._db.notes.note(reference.id);
if (!note) continue;
item = note.data;
break;
}
}
if (item) items.push(item);
}
Expand Down

0 comments on commit a62ca2a

Please sign in to comment.