Skip to content

Build your personal knowledge base with Trilium Notes

fix(notes): reject a forced noteId that links cannot match (closes #8169)

matt wilkie78cd007af807
diff --git a/apps/server/src/etapi/validators.ts b/apps/server/src/etapi/validators.ts
index 2381a47b9d..799a871e3f 100644
--- a/apps/server/src/etapi/validators.ts
+++ b/apps/server/src/etapi/validators.ts
@@ -1,4 +1,4 @@
-import { date_utils as dateUtils, note_types as noteTypeService } from "@triliumnext/core";
+import { date_utils as dateUtils, note_types as noteTypeService, utils } from "@triliumnext/core";
import { becca } from "@triliumnext/core";
@@ -101,7 +101,7 @@ function isValidEntityId(obj: unknown) {
return;
}
- if (typeof obj !== "string" || !/^[A-Za-z0-9_]{4,128}$/.test(obj)) {
+ if (typeof obj !== "string" || !utils.isValidEntityId(obj)) {
return `'${obj}' is not a valid entityId. Only alphanumeric characters are allowed of length 4 to 32.`;
}
}
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.ts
index 6197ff0f22..75b97f870e 100644
--- a/packages/trilium-core/src/services/notes.spec.ts
+++ b/packages/trilium-core/src/services/notes.spec.ts
@@ -178,6 +178,30 @@ describe("notes service (real DB)", () => {
);
});
+ it("accepts a well-formed forced noteId and rejects one that links cannot match", () => {
+ for (const noteId of [ "forcedSpecId01", "_specForced01" ]) {
+ const { note } = createNote("root", { title: "spec-forced-id", noteId });
+
+ expect(note.noteId).toBe(noteId);
+ expect(becca.notes[noteId]).toBe(note);
+ }
+
+ const generated = createNote("root", { noteId: null as unknown as string }).note;
+ expect(generated.noteId).toMatch(/^[A-Za-z0-9]{12}$/);
+
+ const dashedId = "0d8949e4-6fe3-4f4b-82c1-679baf64fccb";
+ const countRows = (noteId: string) =>
+ getSql().getValue("SELECT COUNT(*) FROM notes WHERE noteId = ?", [ noteId ]);
+ for (const noteId of [ dashedId, "abc", "bad/id01", "bad id01" ]) {
+ expect(() => createNote("root", { noteId }), noteId).toThrow(/is not valid/);
+ expect(becca.notes[noteId]).toBeUndefined();
+ expect(countRows(noteId)).toBe(0);
+ }
+
+ const numericId = 12345 as unknown as string;
+ expect(() => createNote("root", { noteId: numericId })).toThrow(/is not valid/);
+ });
+
it("inherits the template's mime and adds a template relation when creating from a template", () => {
const template = createNote("root", { title: "spec-template", content: "<p>tmpl</p>" });
// Give the template a non-default mime so we can verify inheritance.
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index d66b9e9401..0a9353c244 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -25,7 +25,7 @@ import { getSql } from "./sql/index.js";
import type TaskContext from "./task_context.js";
import { decodeBase64 } from "./utils/binary.js";
import date_utils from "./utils/date.js";
-import { newEntityId, replaceAll, toMap, unescapeHtml } from "./utils/index.js";
+import { isValidEntityId, newEntityId, replaceAll, toMap, unescapeHtml } from "./utils/index.js";
import ws from "./ws.js";
interface FoundLink {
@@ -39,7 +39,7 @@ interface Attachment {
}
export interface NoteParams {
- /** optionally can force specific noteId */
+ /** Forces a specific noteId: 4 to 128 letters, digits or underscores. */
noteId?: string;
branchId?: string;
parentNoteId: string;
@@ -243,6 +243,12 @@ function createNewNote(params: NoteParams): {
throw new Error(error);
}
+ if (params.noteId !== undefined && params.noteId !== null
+ && (typeof params.noteId !== "string" || !isValidEntityId(params.noteId))) {
+ throw new ValidationError(`Note ID '${params.noteId}' is not valid. `
+ + "Only letters, digits and underscores are allowed, with a length of 4 to 128.");
+ }
+
// When creating from a template, inherit the template's type and mime if not explicitly provided.
// This ensures binary types (PDF, images, etc.) get the correct mime from the start.
if (params.templateNoteId) {
diff --git a/packages/trilium-core/src/services/utils/index.spec.ts b/packages/trilium-core/src/services/utils/index.spec.ts
index f288663cfd..f88c6f7804 100644
--- a/packages/trilium-core/src/services/utils/index.spec.ts
+++ b/packages/trilium-core/src/services/utils/index.spec.ts
@@ -46,6 +46,21 @@ describe("#newEntityId", () => {
});
});
+describe("#isValidEntityId", () => {
+ it("accepts only letters, digits and underscores with a length of 4 to 128", () => {
+ const accepted = [ "root", "_hidden", "abcDEF012345", "a".repeat(128) ];
+ for (const id of accepted) {
+ expect(utils.isValidEntityId(id), id).toBe(true);
+ }
+
+ const dashedId = "0d8949e4-6fe3-4f4b-82c1-679baf64fccb";
+ const rejected = [ "", "abc", "a".repeat(129), dashedId, "bad/id01", "bad id01" ];
+ for (const id of rejected) {
+ expect(utils.isValidEntityId(id), id).toBe(false);
+ }
+ });
+});
+
describe("#randomString", () => {
it("should return a string with a length as per argument", () => {
const stringLength = 5;
diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.ts
index f36b82c3bb..3e9e8d8b80 100644
--- a/packages/trilium-core/src/services/utils/index.ts
+++ b/packages/trilium-core/src/services/utils/index.ts
@@ -45,6 +45,14 @@ export function newEntityId() {
return randomString(12);
}
+/**
+ * Link parsing (`link.ts` on the client, `findInternalLinks()` on the server) matches only this
+ * character set, so a note ID outside it cannot be the target of a link.
+ */
+export function isValidEntityId(id: string) {
+ return /^[A-Za-z0-9_]{4,128}$/.test(id);
+}
+
export function hashedBlobId(content: string | Uint8Array) {
if (content === null || content === undefined) {
content = "";