Skip to content

Build your personal knowledge base with Trilium Notes

fix(tree): escape template titles in the insert menus

Elian Doranf0f7ae918329
diff --git a/apps/client/src/services/note_types.spec.ts b/apps/client/src/services/note_types.spec.ts
index 1194946deb..94c69d5407 100644
--- a/apps/client/src/services/note_types.spec.ts
+++ b/apps/client/src/services/note_types.spec.ts
@@ -480,6 +480,37 @@ describe("new template badges", () => {
}
});
+ it("escapes template titles for the menu, which renders HTML, and not for the list, which renders text", async () => {
+ const user = buildNote({ id: "userTemplateHtml", title: "<img src=x onerror=alert(1)>", "#template": "" });
+ withTemplates([ user.noteId ]);
+ const restore = withTemplatesRoot([
+ fakeTemplate("tpl-snippet", [ "template", "snippet" ], "A & <b>B</b>"),
+ fakeTemplate("tpl-coll", [ "template", "collection" ], "<i>Board</i>")
+ ]);
+ const all = (items: any[]): any[] => items.flatMap((i) => [ i, ...all(i.items ?? []) ]);
+ const titleOf = (items: any[], templateNoteId: string) =>
+ all(items).find((i) => i.templateNoteId === templateNoteId)?.title;
+ try {
+ const data = await noteTypesService.loadNoteTypeData();
+ const menu = noteTypesService.buildNoteTypeItems(data, "insertChildNote" as never);
+ /** What `Menu` shows for a title: the title as HTML, here as its elements and its text. */
+ const rendered = (title: string) => {
+ const element = document.createElement("span");
+ element.innerHTML = title;
+ return { elements: element.children.length, text: element.textContent };
+ };
+ expect(rendered(titleOf(menu, user.noteId))).toEqual({ elements: 0, text: "<img src=x onerror=alert(1)>" });
+ expect(rendered(titleOf(menu, "tpl-snippet"))).toEqual({ elements: 0, text: "A & <b>B</b>" });
+ expect(rendered(titleOf(menu, "tpl-coll"))).toEqual({ elements: 0, text: "<i>Board</i>" });
+
+ const list = await noteTypesService.getNoteTypeItems();
+ expect(titleOf(list, user.noteId)).toBe("<img src=x onerror=alert(1)>");
+ expect(titleOf(list, "tpl-snippet")).toBe("A & <b>B</b>");
+ } finally {
+ restore();
+ }
+ });
+
it("offers the enabled code languages in a submenu of Code, as they stand when the menu opens", async () => {
withTemplates();
const restore = withTemplatesRoot([]);
diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.ts
index fcf423c2cc..e6140ec62d 100644
--- a/apps/client/src/services/note_types.ts
+++ b/apps/client/src/services/note_types.ts
@@ -219,15 +219,17 @@ async function loadNoteTypeData(): Promise<NoteTypeData> {
return { builtInTemplateNotes, userTemplateNotes, newTemplates: new Set(newTemplateNoteIds) };
}
+type NoteTypeLayout = "menu" | "list";
+
/**
* The note type items. In a `"menu"` the snippets and the collections each form a submenu; a
* `"list"`, such as the note type chooser draws, cannot open one, so there they stay inline. The AI
* quick action template stands beside the AI chat note type, and is offered only alongside it.
*/
-function buildNoteTypeItems(data: NoteTypeData, command?: TreeCommandNames, layout: "menu" | "list" = "menu") {
+function buildNoteTypeItems(data: NoteTypeData, command?: TreeCommandNames, layout: NoteTypeLayout = "menu") {
const { builtInTemplateNotes, userTemplateNotes, newTemplates } = data;
const builtIn = (group: BuiltInTemplateGroup) =>
- getBuiltInTemplates(command, builtInTemplateNotes, group, newTemplates);
+ getBuiltInTemplates(command, builtInTemplateNotes, group, newTemplates, layout);
const snippets = builtIn("snippet");
const collections = builtIn("collection");
@@ -250,7 +252,7 @@ function buildNoteTypeItems(data: NoteTypeData, command?: TreeCommandNames, layo
if (collections.length > 0) items.push({ title: t("note_types.book"), uiIcon: "bx bx-book", items: collections });
}
- items.push(...getUserTemplates(command, userTemplateNotes, newTemplates));
+ items.push(...getUserTemplates(command, userTemplateNotes, newTemplates, layout));
return items;
}
@@ -307,7 +309,7 @@ function getCodeLanguageItems(command?: TreeCommandNames): MenuItem<TreeCommandN
];
}
-function getUserTemplates(command: TreeCommandNames | undefined, templateNotes: FNote[], newTemplates: Set<string>) {
+function getUserTemplates(command: TreeCommandNames | undefined, templateNotes: FNote[], newTemplates: Set<string>, layout: NoteTypeLayout) {
if (templateNotes.length === 0) {
return [];
}
@@ -321,7 +323,7 @@ function getUserTemplates(command: TreeCommandNames | undefined, templateNotes:
for (const templateNote of templateNotes) {
const item: MenuItem<TreeCommandNames> = {
- title: templateNote.title,
+ title: templateTitle(templateNote, layout),
uiIcon: templateNote.getIcon(),
command,
type: templateNote.type,
@@ -357,7 +359,7 @@ function builtInTemplateGroup(templateNote: FNote): BuiltInTemplateGroup {
return "other";
}
-function getBuiltInTemplates(command: TreeCommandNames | undefined, childNotes: FNote[], group: BuiltInTemplateGroup, newTemplates: Set<string>) {
+function getBuiltInTemplates(command: TreeCommandNames | undefined, childNotes: FNote[], group: BuiltInTemplateGroup, newTemplates: Set<string>, layout: NoteTypeLayout) {
const items: MenuItem<TreeCommandNames>[] = [];
for (const templateNote of childNotes) {
@@ -366,7 +368,7 @@ function getBuiltInTemplates(command: TreeCommandNames | undefined, childNotes:
}
const item: MenuItem<TreeCommandNames> = {
- title: templateNote.title,
+ title: templateTitle(templateNote, layout),
uiIcon: templateNote.getIcon(),
command,
type: templateNote.type,
@@ -389,6 +391,11 @@ function getBuiltInTemplates(command: TreeCommandNames | undefined, childNotes:
return items;
}
+/** A template's title as the layout renders it: `Menu` renders titles as HTML, a list as text. */
+function templateTitle(templateNote: FNote, layout: NoteTypeLayout) {
+ return layout === "menu" ? escapeHtml(templateNote.title) : templateNote.title;
+}
+
/** `items` led by `lead`, or nothing when there are no items to lead. */
function withLeading(lead: MenuItem<TreeCommandNames>, items: MenuItem<TreeCommandNames>[]) {
return items.length > 0 ? [ lead, ...items ] : [];