Skip to content

Build your personal knowledge base with Trilium Notes

fix(notes): validate a forced noteId of any type

matt wilkie71ed20bd1968
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.ts
index 8f312c211f..2df6d42b96 100644
--- a/packages/trilium-core/src/services/notes.spec.ts
+++ b/packages/trilium-core/src/services/notes.spec.ts
@@ -187,7 +187,7 @@ describe("notes service (real DB)", () => {
expect(becca.notes[noteId]).toBe(note);
}
- for (const noteId of [ null as unknown as string, "" ]) {
+ for (const noteId of [ null as unknown as string, "", undefined as unknown as string ]) {
expect(createNote("root", { noteId }).note.noteId).toMatch(/^[A-Za-z0-9]{12}$/);
}
@@ -202,6 +202,10 @@ describe("notes service (real DB)", () => {
const numericId = 12345 as unknown as string;
expect(() => createNote("root", { noteId: numericId })).toThrow(ValidationError);
+
+ for (const noteId of [ 0 as unknown as string, false as unknown as string ]) {
+ expect(() => createNote("root", { noteId }), String(noteId)).toThrow(/is not valid/);
+ }
});
it("inherits the template's mime and adds a template relation when creating from a template", () => {
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index 4cb7faa546..b654869b1d 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -243,8 +243,12 @@ function createNewNote(params: NoteParams): {
throw new Error(error);
}
- if (params.noteId && (typeof params.noteId !== "string" || !isValidEntityId(params.noteId))) {
- throw new ValidationError(`Note ID '${params.noteId}' is not valid. `
+ const forcedNoteId: unknown = params.noteId;
+ if (
+ forcedNoteId !== undefined && forcedNoteId !== null && forcedNoteId !== ""
+ && (typeof forcedNoteId !== "string" || !isValidEntityId(forcedNoteId))
+ ) {
+ throw new ValidationError(`Note ID '${forcedNoteId}' is not valid. `
+ "Only letters, digits and underscores are allowed, with a length of 4 to 128.");
}