Skip to content
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
24 changes: 24 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -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' }]
]
};
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 @@
},
"dependencies": {
"axios": "^1.7.4",
"dompurify": "^3.4.17",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.2"
Expand Down
20 changes: 19 additions & 1 deletion client/src/lib/sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,22 @@
// TODO (security): right now this returns the HTML untouched, so whatever a
// reviewer typed — including <script> 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'

const CONFIG = {
ALLOWED_TAGS: ['b', 'strong', 'i', 'em', 'u', 'p', 'br', 'ul', 'ol', 'li', 'blockquote', 'code', 'a'],
ALLOWED_ATTR: ['href', 'title', 'target', 'rel'],
ALLOW_DATA_ATTR: false,
}

DOMPurify.addHook('afterSanitizeAttributes', (node) => {
if (node.tagName === 'A') {
node.setAttribute('rel', 'noopener noreferrer nofollow')
if (node.getAttribute('target')) node.setAttribute('target', '_blank')
}
})

export const sanitizeHtml = (dirty) => DOMPurify.sanitize(String(dirty ?? ''), CONFIG)

//export const sanitizeHtml = (dirty) => dirty;
28 changes: 28 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -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: ['<rootDir>/tests/client/**/*.test.{js,jsx}'],
setupFilesAfterEnv: ['<rootDir>/tests/setup/jest.setup.js'],
moduleNameMapper: {
'\\.(css|less|scss|sass)$': 'identity-obj-proxy'
},
transform: {
'^.+\\.(js|jsx)$': 'babel-jest'
}
},
{
displayName: 'server',
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/server/**/*.test.js'],
transform: {
'^.+\\.js$': 'babel-jest'
}
}
]
};
78 changes: 30 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 27 additions & 1 deletion server/src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import morgan from 'morgan';
import cors from 'cors';
import helmet from 'helmet';
import authRoutes from './routes/auth.js';
import listingRoutes from './routes/listings.js';
import userRoutes from './routes/users.js';
Expand All @@ -9,6 +10,31 @@ import reviewRoutes from './routes/reviews.js';
const app = express();

app.use(morgan('dev'));

// Security headers, including a strict Content-Security-Policy.
// Defense-in-depth: DOMPurify cleans the markup, and this policy tells the
// browser to refuse inline/injected script even if something slips through.
app.use(
helmet({
contentSecurityPolicy: {
useDefaults: false,
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'"],
scriptSrcAttr: ["'none'"], // blocks inline handlers such as onerror= / onclick=
objectSrc: ["'none'"],
baseUri: ["'self'"],
imgSrc: ["'self'", 'data:'],
styleSrc: ["'self'", "'unsafe-inline'"],
frameAncestors: ["'none'"],
formAction: ["'self'"],
},
},
// The client (:5175) calls this API cross-origin, so allow it to read responses.
crossOriginResourcePolicy: { policy: 'cross-origin' },
})
);

app.use(cors({ origin: 'http://localhost:5175', credentials: false }));
app.use(express.json());

Expand All @@ -30,4 +56,4 @@ app.use((err, req, res, next) => {
res.status(err.status || 500).json({ message: err.message || 'Server Error' });
});

export default app;
export default app;
8 changes: 7 additions & 1 deletion server/src/controllers/reviewController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import Joi from 'joi';
import { Review } from '../models/Review.js';

const CLEAN = {
ALLOWED_TAGS: ['b', 'strong', 'i', 'em', 'u', 'p', 'br', 'ul', 'ol', 'li', 'blockquote', 'code', 'a'],
ALLOWED_ATTR: ['href', 'title'],
ALLOW_DATA_ATTR: false,
};

const reviewSchema = Joi.object({
listing: Joi.string().hex().length(24).required(),
rating: Joi.number().min(1).max(5).default(5),
Expand All @@ -24,7 +30,7 @@ export async function getReviewsForListing(req, res, next) {
} catch (err) { next(err); }
}

// POST /api/reviews (requires auth)
// POST /api/reviews (requires auth).
export async function createReview(req, res, next) {
try {
const { value, error } = reviewSchema.validate(req.body);
Expand Down
Loading