Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: add sanity checks to handle `cannot read properties of undefined (reading 'hash')` error

Abdullah Atta87bec4e2ab0d
diff --git a/packages/core/src/api/index.js b/packages/core/src/api/index.js
index 02df14c31..b3111b14c 100644
--- a/packages/core/src/api/index.js
+++ b/packages/core/src/api/index.js
@@ -113,7 +113,7 @@ class Database {
this
);
EV.subscribe(EVENTS.attachmentDeleted, async (attachment) => {
- await this.fs.cancel(attachment.metadata.hash);
+ await this.fs.cancel(attachment.metadata?.hash);
});
EV.subscribe(EVENTS.userLoggedOut, async () => {
await this.monographs.deinit();
diff --git a/packages/core/src/api/lookup.js b/packages/core/src/api/lookup.js
index 72a5fb651..16ff4b0b4 100644
--- a/packages/core/src/api/lookup.js
+++ b/packages/core/src/api/lookup.js
@@ -67,10 +67,10 @@ export default class Lookup {
}
attachments(array, query) {
- return search(
- array,
- query,
- (n) => `${n.metadata.filename} ${n.metadata.type} ${n.metadata.hash}`
+ return search(array, query, (n) =>
+ n.metadata
+ ? `${n.metadata.filename} ${n.metadata.type} ${n.metadata.hash}`
+ : ""
);
}
diff --git a/packages/core/src/api/sync/merger.js b/packages/core/src/api/sync/merger.js
index 5c3c7e98c..ea7cafde4 100644
--- a/packages/core/src/api/sync/merger.js
+++ b/packages/core/src/api/sync/merger.js
@@ -169,9 +169,13 @@ class Merger {
if (!remoteItem.metadata) break;
const localItem = this._db.attachments.attachment(
- remoteItem.metadata.hash
+ remoteItem.metadata?.hash
);
- if (localItem && localItem.dateUploaded !== remoteItem.dateUploaded) {
+ if (
+ localItem &&
+ localItem.metadata &&
+ localItem.dateUploaded !== remoteItem.dateUploaded
+ ) {
const noteIds = localItem.noteIds.slice();
const isRemoved = await this._db.attachments.remove(
localItem.metadata.hash,
diff --git a/packages/core/src/collections/attachments.js b/packages/core/src/collections/attachments.js
index 0778e5790..a85891247 100644
--- a/packages/core/src/collections/attachments.js
+++ b/packages/core/src/collections/attachments.js
@@ -43,7 +43,7 @@ export default class Attachments extends Collection {
if (!attachment) return;
const src = await this.read(filename, getOutputType(attachment));
- if (!src) return;
+ if (!src || !attachment.metadata) return;
EV.publish(EVENTS.mediaAttachmentDownloaded, {
groupId,
@@ -108,7 +108,7 @@ export default class Attachments extends Collection {
if (!attachmentArg.hash) throw new Error("Please provide attachment hash.");
const oldAttachment =
- this.all.find((a) => a.metadata.hash === attachmentArg.hash) || {};
+ this.all.find((a) => a.metadata?.hash === attachmentArg.hash) || {};
let id = oldAttachment.id || getId();
const noteIds = oldAttachment.noteIds || [];
@@ -205,7 +205,7 @@ export default class Attachments extends Collection {
async remove(hashOrId, localOnly) {
const attachment = this.attachment(hashOrId);
- if (!attachment) return false;
+ if (!attachment || !attachment.metadata) return false;
if (!localOnly && !(await this._canDetach(attachment)))
throw new Error("This attachment is inside a locked note.");
@@ -267,7 +267,7 @@ export default class Attachments extends Collection {
}
exists(hash) {
- const attachment = this.all.find((a) => a.metadata.hash === hash);
+ const attachment = this.all.find((a) => a.metadata?.hash === hash);
return !!attachment;
}
@@ -277,7 +277,7 @@ export default class Attachments extends Collection {
* @returns {Promise<string>} dataurl formatted string
*/
async read(hash, outputType) {
- const attachment = this.all.find((a) => a.metadata.hash === hash);
+ const attachment = this.all.find((a) => a.metadata?.hash === hash);
if (!attachment) return;
const key = await this.decryptKey(attachment.key);
@@ -302,7 +302,7 @@ export default class Attachments extends Collection {
attachment(hashOrId) {
return this.all.find(
- (a) => a.id === hashOrId || a.metadata.hash === hashOrId
+ (a) => a.id === hashOrId || a.metadata?.hash === hashOrId
);
}
@@ -349,7 +349,7 @@ export default class Attachments extends Collection {
const attachments = this.media.filter(
(attachment) =>
hasItem(attachment.noteIds, noteId) &&
- (!hashesToLoad || hasItem(hashesToLoad, attachment.metadata.hash))
+ (!hashesToLoad || hasItem(hashesToLoad, attachment.metadata?.hash))
);
await this._db.fs.queueDownloads(
@@ -366,7 +366,11 @@ export default class Attachments extends Collection {
async cleanup() {
const now = dayjs().unix();
for (const attachment of this.deleted) {
- if (dayjs(attachment.dateDeleted).add(7, "days").unix() < now) continue;
+ if (
+ !attachment.metadata ||
+ dayjs(attachment.dateDeleted).add(7, "days").unix() < now
+ )
+ continue;
const isDeleted = await this._db.fs.deleteFile(attachment.metadata.hash);
if (!isDeleted) continue;
@@ -377,7 +381,9 @@ export default class Attachments extends Collection {
get pending() {
return this.all.filter(
- (attachment) => attachment.dateUploaded <= 0 || !attachment.dateUploaded
+ (attachment) =>
+ attachment.metadata &&
+ (attachment.dateUploaded <= 0 || !attachment.dateUploaded)
);
}
diff --git a/packages/core/src/collections/content.js b/packages/core/src/collections/content.js
index 564c17540..da0b7d789 100644
--- a/packages/core/src/collections/content.js
+++ b/packages/core/src/collections/content.js
@@ -122,7 +122,9 @@ export default class Content extends Collection {
const content = getContentFromData(contentItem.type, contentItem.data);
if (!content) console.log(contentItem);
contentItem.data = await content.insertMedia(async (hashes) => {
- const attachments = hashes.map((h) => this._db.attachments.attachment(h));
+ const attachments = hashes
+ .map((h) => this._db.attachments.attachment(h))
+ .filter((a) => !!a && !!a.metadata);
await this._db.fs.queueDownloads(
attachments.map((a) => ({
filename: a.metadata.hash,
@@ -134,6 +136,8 @@ export default class Content extends Collection {
);
const sources = {};
for (const attachment of attachments) {
+ if (!attachment.metadata) continue;
+
const src = await this._db.attachments.read(
attachment.metadata.hash,
getOutputType(attachment)
@@ -188,20 +192,23 @@ export default class Content extends Collection {
const toDelete = noteAttachments.filter((attachment) => {
return attachments.every(
- (a) => a.hash && a.hash !== attachment.metadata.hash
+ (a) => a && a.hash && a.hash !== attachment.metadata?.hash
);
});
const toAdd = attachments.filter((attachment) => {
return (
+ attachment &&
attachment.hash &&
- noteAttachments.every((a) => attachment.hash !== a.metadata.hash)
+ noteAttachments.every((a) => attachment.hash !== a.metadata?.hash)
);
});
for (let attachment of toDelete) {
+ if (!attachment.metadata) continue;
+
await this._db.attachments.delete(
- attachment.metadata.hash,
+ attachment.metadata?.hash,
contentItem.noteId
);
}
diff --git a/packages/core/src/collections/notes.js b/packages/core/src/collections/notes.js
index 3e1644e40..ad3ffd641 100644
--- a/packages/core/src/collections/notes.js
+++ b/packages/core/src/collections/notes.js
@@ -251,6 +251,7 @@ export default class Notes extends Collection {
const attachments = this._db.attachments.ofNote(itemData.id, "all");
for (let attachment of attachments) {
+ if (!attachment || !attachment.metadata) continue;
await this._db.attachments.delete(
attachment.metadata.hash,
itemData.id