Skip to content

Commit 753bc7f

Browse files
committed
fix: add DOMPurify for sanitizing HTML to prevent XSS attacks
1 parent 417cb27 commit 753bc7f

File tree

3 files changed

+451
-6
lines changed

3 files changed

+451
-6
lines changed

app/(app)/articles/[slug]/page.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getPost } from "@/server/lib/posts";
1616
import { getCamelCaseFromLower } from "@/utils/utils";
1717
import { generateHTML } from "@tiptap/html";
1818
import { TiptapExtensions } from "@/components/editor/editor/extensions";
19+
import DOMPurify from 'isomorphic-dompurify';
1920

2021
type Props = { params: { slug: string } };
2122

@@ -68,10 +69,10 @@ const parseJSON = (str: string): any | null => {
6869
}
6970
};
7071

71-
const renderTiptapContent = (jsonContent: JSON) => {
72-
return generateHTML(jsonContent, [
73-
...TiptapExtensions,
74-
]);
72+
const renderSanitizedTiptapContent = (jsonContent: JSON) => {
73+
const rawHtml = generateHTML(jsonContent, [...TiptapExtensions]);
74+
// Sanitize the HTML
75+
return DOMPurify.sanitize(rawHtml);
7576
};
7677

7778
const ArticlePage = async ({ params }: Props) => {
@@ -83,7 +84,7 @@ const ArticlePage = async ({ params }: Props) => {
8384
const post = await getPost({ slug });
8485

8586
if (!post) {
86-
notFound();
87+
return notFound();
8788
}
8889

8990
const parsedBody = parseJSON(post.body);
@@ -93,7 +94,7 @@ const ArticlePage = async ({ params }: Props) => {
9394

9495
if (isTiptapContent && parsedBody) {
9596
const jsonContent = parsedBody;
96-
renderedContent = renderTiptapContent(jsonContent);
97+
renderedContent = renderSanitizedTiptapContent(jsonContent);
9798
} else {
9899
const ast = Markdoc.parse(post.body);
99100
const transformedContent = Markdoc.transform(ast, config);

0 commit comments

Comments
 (0)