From 8df7edcb75223f892319d2ed3e7953d5b18a9d70 Mon Sep 17 00:00:00 2001 From: Alphons Joseph <93847055+Crazypersonalph@users.noreply.github.com> Date: Tue, 1 Sep 2026 15:39:19 +0800 Subject: [PATCH] fix: regression caused by security hardening causing clickable icons to not work. --- jest.config.js | 2 +- src/seqta/utils/stringToHTML.test.ts | 13 +++++++++++++ src/seqta/utils/stringToHTML.ts | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 src/seqta/utils/stringToHTML.test.ts diff --git a/jest.config.js b/jest.config.js index a63ca1c1..97c8f4a3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,6 @@ export default { preset: 'ts-jest', - testEnvironment: 'node', + testEnvironment: 'jsdom', roots: ['/src'], testMatch: [ '**/__tests__/**/*.ts', diff --git a/src/seqta/utils/stringToHTML.test.ts b/src/seqta/utils/stringToHTML.test.ts new file mode 100644 index 00000000..2a331dde --- /dev/null +++ b/src/seqta/utils/stringToHTML.test.ts @@ -0,0 +1,13 @@ +import stringToHTML from "./stringToHTML"; + +describe("stringToHTML", () => { + it("preserves inline onclick handlers used by lesson action buttons", () => { + const body = stringToHTML( + `
Go
`, + ); + + const el = body.firstElementChild as HTMLElement | null; + expect(el).not.toBeNull(); + expect(el?.getAttribute("onclick")).toContain("location.href"); + }); +}); diff --git a/src/seqta/utils/stringToHTML.ts b/src/seqta/utils/stringToHTML.ts index c12c6f60..83cd7fbc 100644 --- a/src/seqta/utils/stringToHTML.ts +++ b/src/seqta/utils/stringToHTML.ts @@ -16,8 +16,9 @@ export default function stringToHTML(str: string, styles = false) { const parser = new DOMParser(); str = DOMPurify.sanitize(str, { + ADD_ATTR: ["onclick"], ALLOWED_URI_REGEXP: - /^(?:(?:https?|mailto|tel):|\/|#|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i, + /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|chrome-extension):|\/|#|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i, }); const doc = parser.parseFromString(str, "text/html");