Skip to content

Fix Stored XSS #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@testing-library/user-event": "^13.5.0",
"axios": "^0.25.0",
"bootstrap": "^5.1.3",
"dompurify": "^3.0.6",
"react": "^17.0.2",
"react-bootstrap": "^2.1.1",
"react-copy-to-clipboard": "^5.0.4",
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/Blog/Blog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import axios from "axios";
import NavBar from "../NavBar/NavBar";
import Footer from "../Footer/Footer";
import "./Blog.css";
import DOMPurify from 'dompurify';

export default function Blog() {
const { id } = useParams();
Expand Down Expand Up @@ -180,7 +181,7 @@ export default function Blog() {
<h1>{blog.title}</h1>
<div
className="blog-content"
dangerouslySetInnerHTML={{ __html: blog.content }}
dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(blog.content) }}
></div>
</Card.Body>
</Card>
Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"express": "^4.17.2",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.1.8",
"multer": "^1.4.4"
"multer": "^1.4.4",
"sanitize-html": "^2.11.0"
}
}
8 changes: 6 additions & 2 deletions server/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const multer = require("multer");
const cloudinary = require("cloudinary").v2;
const User = require("../models/user.model");
const Blog = require("../models/blog.model");
const sanitizeHtml = require('sanitize-html');

const { CLOUD_NAME, API_KEY, API_SECRET } = process.env;

Expand Down Expand Up @@ -129,6 +130,9 @@ Router.post("/post/comment/:id", async (req, res) => {
Router.post("/create", upload.single("image"), async (req, res) => {
const { title, content, date, token } = req.body;

const sanitizedTitle = sanitizeHtml(title);
const sanitizedContent = sanitizeHtml(content);

if (req.file) {
cloudinary.uploader.upload(
req.file.path,
Expand All @@ -148,8 +152,8 @@ Router.post("/create", upload.single("image"), async (req, res) => {
return res.status(404).send({ error: "User not found" });
}
const blog = new Blog({
title,
content,
title: sanitizedTitle,
content: sanitizedContent,
image,
cloudinaryId,
author: user.name,
Expand Down