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/components/ReviewList.jsx b/client/src/components/ReviewList.jsx
index 4638ba2..e95cfb4 100644
--- a/client/src/components/ReviewList.jsx
+++ b/client/src/components/ReviewList.jsx
@@ -16,6 +16,7 @@ export default function ReviewList({ reviews }) {
{'★'.repeat(r.rating || 0)}{'☆'.repeat(5 - (r.rating || 0))}
{' • '}by {r.author?.name || 'unknown'}
+
and onerror handlers — is injected into
// every viewer's page. Make this strip dangerous markup before it is rendered.
-export const sanitizeHtml = (dirty) => dirty;
+import DOMPurify from 'dompurify';
+
+export const sanitizeHtml = (dirty) =>
+ DOMPurify.sanitize(dirty, { USE_PROFILES: { html: true } });
\ No newline at end of file
diff --git a/jest.config.cjs b/jest.config.cjs
new file mode 100644
index 0000000..29cdc70
--- /dev/null
+++ b/jest.config.cjs
@@ -0,0 +1,28 @@
+// One jest run with two environments:
+// - "client" tests run in jsdom (they render React components)
+// - "server" tests run in node (they boot the Express app + in-memory Mongo)
+// `npm test` runs both.
+module.exports = {
+ projects: [
+ {
+ displayName: 'client',
+ testEnvironment: 'jsdom',
+ testMatch: ['
/tests/client/**/*.test.{js,jsx}'],
+ setupFilesAfterEnv: ['/tests/setup/jest.setup.js'],
+ moduleNameMapper: {
+ '\\.(css|less|scss|sass)$': 'identity-obj-proxy'
+ },
+ transform: {
+ '^.+\\.(js|jsx)$': 'babel-jest'
+ }
+ },
+ {
+ displayName: 'server',
+ testEnvironment: 'node',
+ testMatch: ['/tests/server/**/*.test.js'],
+ transform: {
+ '^.+\\.js$': 'babel-jest'
+ }
+ }
+ ]
+};
diff --git a/package-lock.json b/package-lock.json
index 0164e48..058c44f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -28,6 +28,7 @@
"version": "0.1.0",
"dependencies": {
"axios": "^1.7.4",
+ "dompurify": "^3.4.17",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.2"
@@ -4152,6 +4153,13 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/@types/trusted-types": {
+ "version": "2.0.7",
+ "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz",
+ "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==",
+ "license": "MIT",
+ "optional": true
+ },
"node_modules/@types/webidl-conversions": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz",
@@ -6751,6 +6759,15 @@
"license": "MIT",
"peer": true
},
+ "node_modules/dompurify": {
+ "version": "3.4.17",
+ "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.4.17.tgz",
+ "integrity": "sha512-B/pSlYVHQ58kqISD/yDjuJ1yfzI34m1gF4lAod2bwSRhiN4K5Cl3H8Af7hqB2PmTL911tdEudnnXn5FnhRoz0w==",
+ "license": "(MPL-2.0 OR Apache-2.0)",
+ "optionalDependencies": {
+ "@types/trusted-types": "^2.0.7"
+ }
+ },
"node_modules/dotenv": {
"version": "16.6.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz",
@@ -7625,6 +7642,18 @@
"node": ">= 0.4"
}
},
+ "node_modules/helmet": {
+ "version": "8.3.0",
+ "resolved": "https://registry.npmjs.org/helmet/-/helmet-8.3.0.tgz",
+ "integrity": "sha512-Qgpiaws3Sm30Av8Eah6sjMCZZwjlBu+E68rhpCWBshY1lb09HtLwj5GviX0OyQIn+ulUS0iX0AxN5n3tLZzz1w==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=18.0.0"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/EvanHahn"
+ }
+ },
"node_modules/html-encoding-sniffer": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-4.0.0.tgz",
@@ -12246,6 +12275,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
+ "helmet": "^8.3.0",
"joi": "^17.12.3",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.6.0",
diff --git a/server/package.json b/server/package.json
index 3fe9b91..76018b3 100644
--- a/server/package.json
+++ b/server/package.json
@@ -11,6 +11,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
+ "helmet": "^8.3.0",
"joi": "^17.12.3",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.6.0",
diff --git a/server/src/app.js b/server/src/app.js
index 81dffda..884f7a1 100644
--- a/server/src/app.js
+++ b/server/src/app.js
@@ -5,9 +5,16 @@ 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 helmet from 'helmet';
const app = express();
-
+app.use(
+ helmet.contentSecurityPolicy({
+ directives: {
+ scriptSrc: ["'self'"],
+ scriptSrcAttr: ["'none'"],
+ },
+ }),
+);
app.use(morgan('dev'));
app.use(cors({ origin: 'http://localhost:5175', credentials: false }));
app.use(express.json());
@@ -30,4 +37,5 @@ app.use((err, req, res, next) => {
res.status(err.status || 500).json({ message: err.message || 'Server Error' });
});
+
export default app;
diff --git a/tests/client/reviewList.test.jsx b/tests/client/reviewList.test.jsx
new file mode 100644
index 0000000..f75b27f
--- /dev/null
+++ b/tests/client/reviewList.test.jsx
@@ -0,0 +1,45 @@
+import { render } from '@testing-library/react';
+import ReviewList from '../../client/src/components/ReviewList';
+
+// A review body a malicious user could submit: it tries to run script that
+// reads the JWT out of localStorage (the app stores the token there).
+const XSS_BODY =
+ 'Nice item! ' +
+ '
' +
+ '' +
+ '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