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: make dom-purify mobile compatible
Ammar Ahmed61a56662eb4e
diff --git a/packages/core/src/api/sync/merger.ts b/packages/core/src/api/sync/merger.tsindex d87c4b76e..0ac222341 100644--- a/packages/core/src/api/sync/merger.ts+++ b/packages/core/src/api/sync/merger.ts@@ -30,7 +30,7 @@ import {} from "../../types.js";import { SyncInboxItem } from "./types.js";import { z } from "zod";-import DOMPurify from "dompurify";+import { sanitizeHtml } from "../../utils/html-parser.js";const THRESHOLD = process.env.NODE_ENV === "test" ? 2 * 1000 : 60 * 1000;class Merger {@@ -230,7 +230,7 @@ export async function handleInboxItems(pinned: data.pinned,readonly: data.readonly,content: {- data: DOMPurify.sanitize(data?.content?.data ?? ""),+ data: sanitizeHtml(data?.content?.data ?? ""),type: "tiptap"}});diff --git a/packages/core/src/utils/dom-purify.ts b/packages/core/src/utils/dom-purify.tsnew file mode 100644index 000000000..eeed63a29--- /dev/null+++ b/packages/core/src/utils/dom-purify.ts@@ -0,0 +1,102 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import DOMPurify, { WindowLike } from "dompurify";++const parseHTML = (input: string) =>+ "DOMParser" in globalThis+ ? new globalThis.DOMParser().parseFromString(input, "text/html")+ : null;++function ensureNodeFilter(win: any) {+ if (win.NodeFilter) return;++ function NodeFilter() {}++ NodeFilter.FILTER_ACCEPT = 1;+ NodeFilter.FILTER_REJECT = 2;+ NodeFilter.FILTER_SKIP = 3;++ NodeFilter.SHOW_ALL = 0xff_ff_ff_ff;+ NodeFilter.SHOW_ELEMENT = 0x1;+ NodeFilter.SHOW_ATTRIBUTE = 0x2;+ NodeFilter.SHOW_TEXT = 0x4;+ NodeFilter.SHOW_CDATA_SECTION = 0x8;+ NodeFilter.SHOW_ENTITY_REFERENCE = 0x10;+ NodeFilter.SHOW_ENTITY = 0x20;+ NodeFilter.SHOW_PROCESSING_INSTRUCTION = 0x40;+ NodeFilter.SHOW_COMMENT = 0x80;+ NodeFilter.SHOW_DOCUMENT = 0x1_00;+ NodeFilter.SHOW_DOCUMENT_TYPE = 0x2_00;+ NodeFilter.SHOW_DOCUMENT_FRAGMENT = 0x4_00;+ NodeFilter.SHOW_NOTATION = 0x8_00;++ win.NodeFilter = NodeFilter;+}++function createPurifyWindow() {+ const document = parseHTML(+ "<!doctype html><html><head><title>dompurify</title></head><body></body></html>"+ );+ const win = document?.defaultView as any;+ const doc = win.document;++ // DOMPurify calls new DOMParser().parseFromString(...)+ if (!win.DOMParser) {+ win.DOMParser = DOMParser;+ }++ ensureNodeFilter(win);++ // DOMPurify checks existence AND later relies on callable behavior.+ if (!doc.implementation) {+ doc.implementation = {};+ }++ if (typeof doc.implementation.createHTMLDocument !== "function") {+ doc.implementation.createHTMLDocument = (title = "") => {+ const document = parseHTML(+ "<!doctype html><html><head><title>dompurify</title></head><body></body></html>"+ );+ return document;+ };+ }++ if (typeof doc.implementation.createDocument !== "function") {+ doc.implementation.createDocument = () => {+ // Keep this as HTML doc because DOMPurify asks for body/html via getElementsByTagName.+ return parseHTML(+ "<!doctype html><html><head></head><body></body></html>"+ );+ };+ }++ return win as WindowLike;+}++let domPurify: DOMPurify.DOMPurify | undefined = undefined;+function getDomPurify() {+ if (DOMPurify.isSupported) return DOMPurify;+ if (!domPurify) {+ domPurify = DOMPurify(createPurifyWindow());+ }+ return domPurify;+}++export { getDomPurify };diff --git a/packages/core/src/utils/html-parser.ts b/packages/core/src/utils/html-parser.tsindex 5f8ea6a5a..82a90932a 100644--- a/packages/core/src/utils/html-parser.ts+++ b/packages/core/src/utils/html-parser.ts@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { decodeHTML5 } from "entities";import { Parser } from "htmlparser2";+import { getDomPurify } from "./dom-purify";export const parseHTML = (input: string) =>"DOMParser" in globalThis@@ -28,6 +29,13 @@ export const parseHTML = (input: string) =>): null;+export const sanitizeHtml = (html: string) => {+ const inputHtml = html.includes("</html>")+ ? html+ : `<html><body>${html}</body></html>`;+ return getDomPurify().sanitize(inputHtml);+};+export function getDummyDocument() {const doc = parseHTML("<div></div>");return doc;