A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: fix escaped html turning into markup in search results (#10519)
Abdullah Atta87a058e3c0fc
diff --git a/packages/core/__tests__/lookup.test.js b/packages/core/__tests__/lookup.test.jsindex 30d593c56..1ba083674 100644--- a/packages/core/__tests__/lookup.test.js+++ b/packages/core/__tests__/lookup.test.js@@ -184,6 +184,29 @@ describe("notesWithHighlighting", () => {expect(item.item).toBeDefined();}));+ test("search should keep escaped html escaped in highlighted content", () =>+ databaseTest().then(async (db) => {+ await db.notes.add({+ title: "escaped",+ content: {+ type: "tiptap",+ data: `<p><img src=x onerror=alert(1)> hello</p><p><a href="https://example.com/?a=1&b="2"">hello link</a></p>`+ }+ });++ const filtered = await db.lookup.notesWithHighlighting(+ "hello",+ db.notes.all+ );+ const { item } = await filtered.item(0);+ expect(item.rawContent).not.toContain("<img");+ expect(item.rawContent).toContain("<img src=x onerror=alert(1)> ");+ expect(item.rawContent).toContain(+ `href="https://example.com/?a=1&b="2""`+ );+ expect(item.content[0][0].prefix).toBe("<img src=x onerror=alert(1)> ");+ }));+test("search should be diacritic agnostic", () =>databaseTest().then(async (db) => {await db.notes.add({ title: "outdoor café" });diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.tsindex 71130041e..c502d46ac 100644--- a/packages/core/src/api/lookup.ts+++ b/packages/core/src/api/lookup.ts@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.*/import { match } from "fuzzyjs";-import { decodeHTML } from "entities";+import { decodeHTML, escapeAttribute, escapeText } from "entities";import Database from "./index.js";import {HighlightedResult,@@ -982,11 +982,14 @@ function highlightHtmlContent(html: string, queries: string[]): string {{ontext(text) {const normalizedText = removeDiacritics(text);+ // text is decoded by the parser so it must be escaped again+ // otherwise escaped text (e.g. <) would turn into markupconst highlighted = highlightRegexMatches(text,normalizedText,searchRegex,- matchIdCounter+ matchIdCounter,+ escapeText);matchIdCounter = highlighted.nextId;@@ -1039,7 +1042,7 @@ function highlightHtmlContent(html: string, queries: string[]): string {continue;}- html += ` ${key}="${value}"`;+ html += ` ${key}="${escapeAttribute(value)}"`;}html += `>${element.buffer}</${element.name}>`;@@ -1193,7 +1196,8 @@ function highlightRegexMatches(text: string,normalizedText: string,regex: RegExp,- startId: number+ startId: number,+ encode: (text: string) => string = (text) => text): { result: string; hasMatches: boolean; nextId: number } {let matchIdCounter = startId;let hasMatches = false;@@ -1203,15 +1207,15 @@ function highlightRegexMatches(while ((m = regex.exec(normalizedText)) !== null) {hasMatches = true;- result += text.slice(lastIndex, m.index);+ result += encode(text.slice(lastIndex, m.index));result += createSearchResultTag(- text.slice(m.index, m.index + m[0].length),+ encode(text.slice(m.index, m.index + m[0].length)),`match-${++matchIdCounter}`);lastIndex = m.index + m[0].length;}- result += text.slice(lastIndex);+ result += encode(text.slice(lastIndex));return { result, hasMatches, nextId: matchIdCounter };}