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: validate & sanitize inbox items Signed-off-by: 01zulfi <[email protected]>
01zulfi7b6165c8f064
diff --git a/packages/core/package-lock.json b/packages/core/package-lock.jsonindex 3efc9c59c..1bc65ed22 100644--- a/packages/core/package-lock.json+++ b/packages/core/package-lock.json@@ -23,6 +23,7 @@"dayjs": "1.11.13","dom-serializer": "^2.0.0","domhandler": "^5.0.3",+ "dompurify": "^3.3.3","domutils": "^3.1.0","entities": "5.0.0","fuzzyjs": "^5.0.1",@@ -35,7 +36,8 @@"prismjs": "^1.29.0","qclone": "^1.2.0","rfdc": "^1.3.0",- "spark-md5": "^3.0.2"+ "spark-md5": "^3.0.2",+ "zod": "^4.3.6"},"devDependencies": {"@notesnook/crypto": "file:../crypto",@@ -1184,6 +1186,13 @@"dev": true,"license": "MIT"},+ "node_modules/@types/trusted-types": {+ "version": "2.0.7",+ "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz",+ "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==",+ "license": "MIT",+ "optional": true+ },"node_modules/@types/unist": {"version": "3.0.3","resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.3.tgz",@@ -1881,6 +1890,15 @@"url": "https://github.com/fb55/domhandler?sponsor=1"}},+ "node_modules/dompurify": {+ "version": "3.3.3",+ "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.3.3.tgz",+ "integrity": "sha512-Oj6pzI2+RqBfFG+qOaOLbFXLQ90ARpcGG6UePL82bJLtdsa6CYJD7nmiU8MW9nQNOtCHV3lZ/Bzq1X0QYbBZCA==",+ "license": "(MPL-2.0 OR Apache-2.0)",+ "optionalDependencies": {+ "@types/trusted-types": "^2.0.7"+ }+ },"node_modules/domutils": {"version": "3.2.2","resolved": "https://registry.npmjs.org/domutils/-/domutils-3.2.2.tgz",@@ -4231,6 +4249,15 @@"optional": true}}+ },+ "node_modules/zod": {+ "version": "4.3.6",+ "resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",+ "integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",+ "license": "MIT",+ "funding": {+ "url": "https://github.com/sponsors/colinhacks"+ }}}}diff --git a/packages/core/package.json b/packages/core/package.jsonindex f4df81446..2101db463 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -78,6 +78,7 @@"dayjs": "1.11.13","dom-serializer": "^2.0.0","domhandler": "^5.0.3",+ "dompurify": "^3.3.3","domutils": "^3.1.0","entities": "5.0.0","fuzzyjs": "^5.0.1",@@ -90,7 +91,8 @@"prismjs": "^1.29.0","qclone": "^1.2.0","rfdc": "^1.3.0",- "spark-md5": "^3.0.2"+ "spark-md5": "^3.0.2",+ "zod": "^4.3.6"},"overrides": {"htmlparser2": "^10.0.0"diff --git a/packages/core/src/api/sync/merger.ts b/packages/core/src/api/sync/merger.tsindex a35258d18..d87c4b76e 100644--- a/packages/core/src/api/sync/merger.ts+++ b/packages/core/src/api/sync/merger.ts@@ -28,7 +28,9 @@ import {Note,isDeleted} from "../../types.js";-import { ParsedInboxItem, SyncInboxItem } from "./types.js";+import { SyncInboxItem } from "./types.js";+import { z } from "zod";+import DOMPurify from "dompurify";const THRESHOLD = process.env.NODE_ENV === "test" ? 2 * 1000 : 60 * 1000;class Merger {@@ -168,6 +170,25 @@ export function isContentConflicted(}}+const RawInboxItemSchema = z.object({+ title: z.string().min(1, "Title is required"),+ pinned: z.boolean().optional(),+ favorite: z.boolean().optional(),+ readonly: z.boolean().optional(),+ archived: z.boolean().optional(),+ notebookIds: z.array(z.string()).optional(),+ tagIds: z.array(z.string()).optional(),+ type: z.enum(["note"]),+ source: z.string(),+ version: z.literal(1),+ content: z+ .object({+ type: z.enum(["html"]),+ data: z.string()+ })+ .optional()+});+export async function handleInboxItems(inboxItems: SyncInboxItem[],db: Database@@ -190,34 +211,37 @@ export async function handleInboxItems(const decryptedItem = await db.storage().decryptPGPMessage(inboxKeys.privateKey, item.cipher);- const parsed = JSON.parse(decryptedItem) as ParsedInboxItem;-- if (parsed.type !== "note") {- continue;- }- if (parsed.version !== 1) {+ const validation = RawInboxItemSchema.safeParse(+ JSON.parse(decryptedItem)+ );+ if (!validation.success) {+ logger.info("Failed to validate inbox item.", {+ inboxItem: item,+ errors: validation.error.issues+ });continue;}+ const data = validation.data;await db.notes.add({id: item.id,- title: parsed.title,- favorite: parsed.favorite,- pinned: parsed.pinned,- readonly: parsed.readonly,+ title: data.title,+ favorite: data.favorite,+ pinned: data.pinned,+ readonly: data.readonly,content: {- data: parsed?.content?.data ?? "",+ data: DOMPurify.sanitize(data?.content?.data ?? ""),type: "tiptap"}});- if (parsed.archived !== undefined) {- await db.notes.archive(parsed.archived, item.id);+ if (data.archived !== undefined) {+ await db.notes.archive(data.archived, item.id);}- for (const notebookId of parsed.notebookIds || []) {+ for (const notebookId of data.notebookIds || []) {if (!(await db.notebooks.exists(notebookId))) continue;await db.notes.addToNotebook(notebookId, item.id);}- for (const tagId of parsed.tagIds || []) {+ for (const tagId of data.tagIds || []) {if (!(await db.tags.exists(tagId))) continue;await db.relations.add({ type: "tag", id: tagId },diff --git a/packages/core/src/api/sync/types.ts b/packages/core/src/api/sync/types.tsindex f7e10ddb8..efba6eb92 100644--- a/packages/core/src/api/sync/types.ts+++ b/packages/core/src/api/sync/types.ts@@ -64,20 +64,3 @@ export type SyncInboxItem = {cipher: string;alg: string;};--export type ParsedInboxItem = {- title: string;- pinned?: boolean;- favorite?: boolean;- readonly?: boolean;- archived?: boolean;- notebookIds?: string[];- tagIds?: string[];- type: "note";- source: string;- version: 1;- content?: {- type: "html";- data: string;- };-};