Skip to content

Commit cde1396

Browse files
author
Programming-Sai
committed
Added some security to the quill editor with DOMpurity and fixed the error in the editor (keywords section).
1 parent 96cc215 commit cde1396

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

package-lock.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@fortawesome/free-regular-svg-icons": "^6.6.0",
1515
"@fortawesome/free-solid-svg-icons": "^6.6.0",
1616
"@fortawesome/react-fontawesome": "^0.2.2",
17+
"dompurify": "^3.1.7",
1718
"eslint": "8.48.0",
1819
"eslint-config-next": "13.4.19",
1920
"html-react-parser": "^5.1.18",

src/app/admin/editor/page.jsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
faUpload,
1515
} from "@fortawesome/free-solid-svg-icons";
1616
import dynamic from "next/dynamic";
17+
import BASE_PATH from "../../../../base";
1718
const ImageUploader = dynamic(
1819
() => import("@/components/imageuploader/ImageUploader"),
1920
{
@@ -90,14 +91,15 @@ const Editor = () => {
9091
calculateReadingTime(blogContent)
9192
);
9293
const [date, setDate] = useState(getCurrentDate());
93-
const [keywords, setKeywords] = useState("");
94+
const [keywords, setKeywords] = useState([]);
9495

9596
const handleTitleChange = (event) => {
9697
const inputTitle = event.target.value;
9798
setTitle(inputTitle);
9899
};
99100

100101
const [previewData, setPreviewData] = useLocalStorage("previewData", {
102+
// Initialize with existing values from localStorage
101103
image: "",
102104
title: "",
103105
slug: "",
@@ -106,7 +108,7 @@ const Editor = () => {
106108
readingTime: "",
107109
date: "",
108110
draft: true,
109-
keywords: [],
111+
keywords: [], // Ensure this is initialized properly
110112
description: "",
111113
comments: 0,
112114
views: 0,
@@ -126,7 +128,7 @@ const Editor = () => {
126128
keywords:
127129
typeof keywords === "string"
128130
? keywords.split(",").map((keyword) => keyword.trim())
129-
: [],
131+
: keywords,
130132
description,
131133
comments: 0,
132134
views: 0,
@@ -160,7 +162,7 @@ const Editor = () => {
160162
const handleKeyDown = (e) => {
161163
if ((e.ctrlKey || e.metaKey) && e.key === "y") {
162164
e.preventDefault();
163-
window.open(`/preview/${slug || "preview"}`, "_blank");
165+
window.open(`${BASE_PATH}/preview/${slug || "preview"}`, "_blank");
164166
}
165167
};
166168
window.addEventListener("keydown", handleKeyDown);

src/components/editorone/EditorOne.jsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ReactQuill from "react-quill";
44
import "react-quill/dist/quill.snow.css";
55
import "react-quill/dist/quill.bubble.css";
66
import styles from "./editorone.module.css";
7+
import DOMPurify from "dompurify";
78

89
const EditorOne = ({ blogContent, setBlogContent, quillTheme }) => {
910
const modules = {
@@ -44,6 +45,12 @@ const EditorOne = ({ blogContent, setBlogContent, quillTheme }) => {
4445
"video",
4546
];
4647

48+
const handleChange = (content) => {
49+
// Sanitize the HTML content
50+
const cleanContent = DOMPurify.sanitize(content);
51+
setBlogContent(cleanContent);
52+
};
53+
4754
useEffect(() => {
4855
if (typeof document !== "undefined") {
4956
const adjustBubblePosition = () => {
@@ -59,7 +66,6 @@ const EditorOne = ({ blogContent, setBlogContent, quillTheme }) => {
5966
tooltip.style.left = `${screenWidth - rect.width - 10}px`; // Adjust for overflow on the right
6067
}
6168
if (rect.left < 0) {
62-
// tooltip.style.right = `20px`;
6369
tooltip.style.right = `${screenWidth - rect.width - 10}px`; // Adjust for overflow on the right
6470
}
6571

@@ -85,7 +91,7 @@ const EditorOne = ({ blogContent, setBlogContent, quillTheme }) => {
8591
<div className={styles.blogContent} key={quillTheme}>
8692
<ReactQuill
8793
value={blogContent}
88-
onChange={setBlogContent}
94+
onChange={handleChange} // Use the new handler
8995
modules={modules}
9096
formats={formats}
9197
theme={quillTheme}

0 commit comments

Comments
 (0)