Skip to content

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

fix: sanitize tag title before insertion

thecodrr19f0e55362dc
diff --git a/packages/core/collections/tags.js b/packages/core/collections/tags.js
index 35bc90e71..920f4d41b 100644
--- a/packages/core/collections/tags.js
+++ b/packages/core/collections/tags.js
@@ -11,11 +11,15 @@ export default class Tags extends Collection {
}
async add(tagId, ...noteIds) {
- if (!tagId) return console.error("tagId cannot be undefined.");
- if (typeof tagId === "object")
- throw new Error("tagId cannot be an object: " + JSON.stringify(tagId));
+ tagId = this.sanitize(tagId);
+ if (!tagId) throw new Error("Tag title cannot be empty.");
- let tag = this.tag(tagId) || {
+ let tag = this.tag(tagId);
+
+ if (tag && !noteIds.length)
+ throw new Error("A tag with this id already exists.");
+
+ tag = tag || {
title: tagId,
};
@@ -94,4 +98,11 @@ export default class Tags extends Collection {
await this._collection.deleteItem(tag.id);
}
}
+
+ sanitize(tag) {
+ if (!tag) return;
+ let sanitized = tag.toLocaleLowerCase();
+ sanitized = sanitized.replace(/[^a-z0-9_\-]+/g, "");
+ return sanitized.trim();
+ }
}
diff --git a/packages/core/package-lock.json b/packages/core/package-lock.json
index 558f89d32..145e4ba0a 100644
--- a/packages/core/package-lock.json
+++ b/packages/core/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "notes-core",
- "version": "6.8.4",
+ "version": "6.9.3",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "notes-core",
- "version": "6.8.4",
+ "version": "6.9.3",
"dependencies": {
"@stablelib/blake2s": "^1.0.1",
"async-mutex": "^0.3.2",