From 2a018d8377f12b507f7954a2eb16a501a76889cc Mon Sep 17 00:00:00 2001 From: Adel Hamed Date: Tue, 10 Nov 2026 13:53:44 +0300 Subject: [PATCH 1/2] feat: sanitized mongoose and Joi inputs to ensure noSQL injections and script attacks don't work (for the reviews at least) --- package-lock.json | 77 ++++++---------------- server/src/app.js | 35 +++++----- server/src/controllers/reviewController.js | 42 +++++++++--- 3 files changed, 71 insertions(+), 83 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0164e48..082b1e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -112,6 +112,7 @@ "integrity": "sha512-RgHBCvtjbOK2gXSNBNIkNoEc9qoVEtau3hj8gEqKQuL3HZAibKarWFEI3Lfm6EYKkLalOh8eSrj9b+ch9H/VBA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.7", "@babel/generator": "^7.29.7", @@ -2063,6 +2064,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" }, @@ -2086,6 +2088,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" } @@ -3276,9 +3279,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3300,9 +3300,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3324,9 +3321,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3348,9 +3342,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3372,9 +3363,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3396,9 +3384,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3911,7 +3896,6 @@ "integrity": "sha512-yzr2S9HyAIdhz2/6qHgbs665Q7PKVcDF05vsOlHPxG1mo36gKVesdYVeDLnXgfjJ03CrKRk08knc6+E/9m8v2Q==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/code-frame": "^7.10.4", "@babel/runtime": "^7.12.5", @@ -3932,7 +3916,6 @@ "integrity": "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=10" }, @@ -3946,7 +3929,6 @@ "integrity": "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", @@ -3961,8 +3943,7 @@ "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", "integrity": "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==", "dev": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/@testing-library/jest-dom": { "version": "6.9.1", @@ -4024,8 +4005,7 @@ "resolved": "https://registry.npmjs.org/@types/aria-query/-/aria-query-5.0.4.tgz", "integrity": "sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw==", "dev": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/@types/babel__core": { "version": "7.20.5", @@ -4122,6 +4102,7 @@ "integrity": "sha512-yOriVnggzrnQ3a9OKOCxaVuSug3w3/SbOj5i7VwXWZEyUNl3bLF9V3MfxGbZKuwqJOQyRfqXyROBB1CoZLFWzA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@types/node": "*", "@types/tough-cookie": "*", @@ -4297,9 +4278,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4314,9 +4292,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4331,9 +4306,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4348,9 +4320,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4365,9 +4334,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4382,9 +4348,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4399,9 +4362,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4416,9 +4376,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4433,9 +4390,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4450,9 +4404,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4901,6 +4852,7 @@ "integrity": "sha512-cIjZnSO+y89ykVX9x96OVnA6tGORBn7vGJ/e3PCbBHMEyfMNG/ixzOS0lLa6saMlWcs69bSKjxFyDwjK152OMg==", "dev": true, "license": "Apache-2.0", + "peer": true, "engines": { "bare": ">=1.20.0" } @@ -4911,6 +4863,7 @@ "integrity": "sha512-AIPKioV7/Y/8KfZ3AAhjPJxLLbY49S64Ym5DakZlUg75qQiTgUq9hEJoEwa4eUezPUlXRy/i5NpsKvo9jgKmoA==", "dev": true, "license": "Apache-2.0", + "peer": true, "peerDependencies": { "bare-abort-controller": "*" }, @@ -5126,6 +5079,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.11.26", "caniuse-lite": "^1.0.30001813", @@ -6748,8 +6702,7 @@ "resolved": "https://registry.npmjs.org/dom-accessibility-api/-/dom-accessibility-api-0.5.16.tgz", "integrity": "sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg==", "dev": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/dotenv": { "version": "16.6.1", @@ -8345,6 +8298,7 @@ "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -8467,6 +8421,7 @@ "integrity": "sha512-wprhLejRtwN6h8ZgaqC0eYjGJ1uMdGPT/+b3eFncbG4NWuuP9QL+vWwRinu9waw7hkOLcpMJGVJAMgBwsPssMQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "chalk": "^4.1.2", "graceful-fs": "^4.2.11", @@ -8714,6 +8669,7 @@ "integrity": "sha512-/imKNG4EbWNrVjoNC/1H5/9GFy+tqjGBHCaSsN+P2RnPqjsLmv6UD3Ej+Kj8nBWaRAwyk7kK5ZUc+OEatnTR3A==", "dev": true, "license": "MIT", + "peer": true, "bin": { "jiti": "bin/jiti.js" } @@ -8757,6 +8713,7 @@ "integrity": "sha512-Cvc9WUhxSMEo4McES3P7oK3QaXldCfNWp7pl2NNeiIFlCoLr3kfq9kb1fxftiwk1FLV7CvpvDfonxtzUDeSOPg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "cssstyle": "^4.2.1", "data-urls": "^5.0.0", @@ -9049,7 +9006,6 @@ "integrity": "sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==", "dev": true, "license": "MIT", - "peer": true, "bin": { "lz-string": "bin/bin.js" } @@ -10072,6 +10028,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "nanoid": "^3.3.18", "picocolors": "^1.1.1", @@ -10383,6 +10340,7 @@ "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", "license": "MIT", + "peer": true, "dependencies": { "loose-envify": "^1.1.0" }, @@ -10395,6 +10353,7 @@ "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.3.1.tgz", "integrity": "sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==", "license": "MIT", + "peer": true, "dependencies": { "loose-envify": "^1.1.0", "scheduler": "^0.23.2" @@ -11574,6 +11533,7 @@ "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -11905,6 +11865,7 @@ "integrity": "sha512-o5a9xKjbtuhY6Bi5S3+HvbRERmouabWbyUcpXXUA1u+GNUKoROi9byOJ8M0nHbHYHkYICiMlqxkg1KkYmm25Sw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "esbuild": "^0.21.3", "postcss": "^8.4.43", diff --git a/server/src/app.js b/server/src/app.js index 81dffda..0fb7f93 100644 --- a/server/src/app.js +++ b/server/src/app.js @@ -1,33 +1,36 @@ -import express from 'express'; -import morgan from 'morgan'; -import cors from 'cors'; -import authRoutes from './routes/auth.js'; -import listingRoutes from './routes/listings.js'; -import userRoutes from './routes/users.js'; -import reviewRoutes from './routes/reviews.js'; +import express from "express"; +import morgan from "morgan"; +import cors from "cors"; +import authRoutes from "./routes/auth.js"; +import listingRoutes from "./routes/listings.js"; +import userRoutes from "./routes/users.js"; +import reviewRoutes from "./routes/reviews.js"; const app = express(); -app.use(morgan('dev')); -app.use(cors({ origin: 'http://localhost:5175', credentials: false })); +app.use(morgan("dev")); +app.use(cors({ origin: "http://localhost:5175", credentials: false })); app.use(express.json()); +app.use(mongoSanitize()); // This is supposed to fix the issue, but I am not sure -app.get('/api/health', (req, res) => res.json({ ok: true })); +app.get("/api/health", (req, res) => res.json({ ok: true })); -app.use('/api/auth', authRoutes); -app.use('/api/listings', listingRoutes); -app.use('/api/users', userRoutes); -app.use('/api/reviews', reviewRoutes); +app.use("/api/auth", authRoutes); +app.use("/api/listings", listingRoutes); +app.use("/api/users", userRoutes); +app.use("/api/reviews", reviewRoutes); // Not found app.use((req, res) => { - res.status(404).json({ message: 'Not Found' }); + res.status(404).json({ message: "Not Found" }); }); // Error handler app.use((err, req, res, next) => { console.error(err); - res.status(err.status || 500).json({ message: err.message || 'Server Error' }); + res + .status(err.status || 500) + .json({ message: err.message || "Server Error" }); }); export default app; diff --git a/server/src/controllers/reviewController.js b/server/src/controllers/reviewController.js index 25a20b0..1a6b27f 100644 --- a/server/src/controllers/reviewController.js +++ b/server/src/controllers/reviewController.js @@ -1,27 +1,47 @@ -import Joi from 'joi'; -import { Review } from '../models/Review.js'; +import Joi from "joi"; +import { Review } from "../models/Review.js"; +import { sanitizeFilter } from "mongoose"; + +// This probably means that the way to solve this is to sanitize the input of the +// body, cause body can accept pure html +// Ok so there is an option using .custom() const reviewSchema = Joi.object({ listing: Joi.string().hex().length(24).required(), rating: Joi.number().min(1).max(5).default(5), // The body is free-form HTML (the reviewer can add bold text, links, etc.). - body: Joi.string().min(1).required(), + body: Joi.string().custom(sanitizeFilter).min(1).required(), // the author is always the logged-in user, never taken from the body - author: Joi.forbidden() + author: Joi.forbidden(), }); +const sanitize = (value, helpers) => { + const forbiddenStuff = [" value.includes(item))) { + return helpers.error("any.invalid"); + } + + return value; +}; + // GET /api/reviews?listing= (public) export async function getReviewsForListing(req, res, next) { try { const { listing } = req.query; - if (!listing) return res.status(400).json({ message: 'listing query param is required' }); + if (!listing) + return res + .status(400) + .json({ message: "listing query param is required" }); const reviews = await Review.find({ listing }) - .populate('author', 'name') + .populate("author", "name") .sort({ createdAt: -1 }) .lean(); res.json({ reviews }); - } catch (err) { next(err); } + } catch (err) { + next(err); + } } // POST /api/reviews (requires auth) @@ -31,7 +51,11 @@ export async function createReview(req, res, next) { if (error) return res.status(400).json({ message: error.message }); const doc = await Review.create({ ...value, author: req.user.id }); - const review = await Review.findById(doc._id).populate('author', 'name').lean(); + const review = await Review.findById(doc._id) + .populate("author", "name") + .lean(); res.status(201).json({ review }); - } catch (err) { next(err); } + } catch (err) { + next(err); + } } From 0f4e81b4889afee1fdfeecc512a83dee1960f2a4 Mon Sep 17 00:00:00 2001 From: Adel Hamed Date: Tue, 10 Nov 2026 14:31:08 +0300 Subject: [PATCH 2/2] actual fix --- .github/workflows/ci.yaml | 24 ++++++++++++ babel.config.cjs | 7 ++++ client/package.json | 1 + client/src/lib/sanitize.js | 4 +- jest.config.cjs | 28 +++++++++++++ package-lock.json | 42 ++++++++++++++++++++ package.json | 3 ++ server/package.json | 1 + server/src/app.js | 3 +- tests/client/reviewList.test.jsx | 45 +++++++++++++++++++++ tests/server/csp.test.js | 67 ++++++++++++++++++++++++++++++++ tests/setup/jest.setup.js | 2 + 12 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 babel.config.cjs create mode 100644 jest.config.cjs create mode 100644 tests/client/reviewList.test.jsx create mode 100644 tests/server/csp.test.js create mode 100644 tests/setup/jest.setup.js diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..9868ef7 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,24 @@ +name: CI + +on: + push: + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Check out the code + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Install dependencies + run: npm install + + - name: Run the test suite + run: npm test diff --git a/babel.config.cjs b/babel.config.cjs new file mode 100644 index 0000000..e448cc9 --- /dev/null +++ b/babel.config.cjs @@ -0,0 +1,7 @@ +// Lets jest load the project's ESM + JSX source through babel-jest. +module.exports = { + presets: [ + ['@babel/preset-env', { targets: { node: 'current' } }], + ['@babel/preset-react', { runtime: 'automatic' }] + ] +}; diff --git a/client/package.json b/client/package.json index d1b5a77..aee2fd7 100644 --- a/client/package.json +++ b/client/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "axios": "^1.7.4", + "dompurify": "^3.4.17", "react": "^18.3.1", "react-dom": "^18.3.1", "react-router-dom": "^6.26.2" diff --git a/client/src/lib/sanitize.js b/client/src/lib/sanitize.js index 1c8ed3d..62d16ae 100644 --- a/client/src/lib/sanitize.js +++ b/client/src/lib/sanitize.js @@ -1,3 +1,5 @@ +const purify = require("dompurify"); + // Reviews are rendered as HTML so people can use light formatting (bold, links). // This helper is the single place review HTML passes through before it is handed // to React's dangerouslySetInnerHTML. @@ -5,4 +7,4 @@ // TODO (security): right now this returns the HTML untouched, so whatever a // reviewer typed — including ' + + 'click me'; + +function renderReview(body) { + const reviews = [{ _id: 'r1', rating: 5, author: { name: 'attacker' }, body }]; + return render(); +} + +describe('ReviewList renders review HTML safely', () => { + test('does not render any