Skip to content

Commit b122a19

Browse files
committed
fix(client&server): implemented Dompurify and helmet to protect against xss
1 parent d6b8fd7 commit b122a19

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

client/package-lock.json

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

client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@testing-library/react": "^13.4.0",
88
"@testing-library/user-event": "^13.5.0",
99
"axios": "^1.7.7",
10+
"dompurify": "^3.1.7",
1011
"react": "^18.3.1",
1112
"react-dom": "^18.3.1",
1213
"react-scripts": "5.0.1",

client/src/SearchMovies.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState } from "react";
22
import axios from "axios";
3+
import { sanitize } from "dompurify";
34

45
const SearchMovies = () => {
56
const [title, setTitle] = useState("");
@@ -8,13 +9,13 @@ const SearchMovies = () => {
89
const handleSearch = async (e) => {
910
e.preventDefault();
1011
const sanitisedTitle = title.replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "");
11-
console.log(sanitisedTitle);
1212
try {
1313
const response = await axios.get(
1414
`http://localhost:3001/search?title=${sanitisedTitle}`
1515
);
16-
console.log(response);
17-
setResults(response.data);
16+
let stringifiedRes = sanitize(JSON.stringify(response.data));
17+
stringifiedRes = JSON.parse(stringifiedRes);
18+
setResults(stringifiedRes);
1819
setTitle("");
1920
} catch (error) {
2021
console.error("Error fetching data:", error);

server/app.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require("express");
22
const cors = require("cors");
33
const pgp = require("pg-promise")();
4+
const helmet = require("helmet");
45
const logger = require("morgan");
56

67
const app = express();
@@ -22,6 +23,7 @@ const corsOptions = {
2223
credentials: true,
2324
};
2425
app.use(cors(corsOptions));
26+
app.use(helmet());
2527
app.use(logger("dev"));
2628
app.use(express.json());
2729

@@ -58,6 +60,7 @@ app.get("/movies", async (req, res) => {
5860

5961
app.get("/search", async (req, res) => {
6062
const title = req.query["title"].replace(/[.,/#!$%^&*;:'{}=\-_`~()]/g, "");
63+
const sanitisedTitle = title.replace(/</g, "").replace(/>/g, "");
6164
console.log("===");
6265
console.log(title);
6366
try {
@@ -67,7 +70,7 @@ app.get("/search", async (req, res) => {
6770
WHERE title ILIKE $1;
6871
`;
6972

70-
const result = await db.any(query, title);
73+
const result = await db.any(query, sanitisedTitle);
7174
console.log(result);
7275
res.json(result);
7376
} catch (err) {

server/package-lock.json

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

server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"cors": "^2.8.5",
1717
"express": "^4.21.0",
18+
"helmet": "^8.0.0",
1819
"morgan": "^1.10.0",
1920
"pg": "^8.12.0",
2021
"pg-promise": "^11.9.1"

0 commit comments

Comments
 (0)