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' }]
]
};
2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
},
"dependencies": {
"axios": "^1.7.4",
"dompurify": "^3.4.17",
"helmet": "^8.3.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.2"
Expand Down
22 changes: 14 additions & 8 deletions client/src/lib/sanitize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// 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.
//
// 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';

// Treat every stored review as untrusted, including reviews posted before the fix.
// Sanitize immediately before the HTML sink; escaping would lose the formatting.
export const sanitizeHtml = (dirty) => DOMPurify.sanitize(
typeof dirty === 'string' ? dirty : '',
{
ALLOWED_TAGS: ['p', 'br', 'b', 'strong', 'i', 'em', 'u', 's', 'a',
'ul', 'ol', 'li', 'blockquote', 'code', 'pre'],
ALLOWED_ATTR: ['href', 'title'],
ALLOW_DATA_ATTR: false,
ALLOW_ARIA_ATTR: false
}
);
39 changes: 37 additions & 2 deletions client/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import helmet from 'helmet'
import { createHash } from 'node:crypto'
import { contentSecurityPolicy } from '../server/src/security.js'

function securityHeaders() {
let base
return {
name: 'security-headers',
configResolved(config) { base = config.base },
configureServer(server) {
// Permit only React's exact development bootstrap, not arbitrary inline JS.
const hash = createHash('sha256')
.update(react.preambleCode.replace('__BASE__', base))
.digest('base64')
server.middlewares.use(helmet({
contentSecurityPolicy: contentSecurityPolicy({
development: true,
scriptHashes: [`'sha256-${hash}'`]
}),
strictTransportSecurity: false
}))
},
configurePreviewServer(server) {
server.middlewares.use(helmet({
contentSecurityPolicy: contentSecurityPolicy(),
strictTransportSecurity: false
}))
}
}
}

export default defineConfig({
plugins: [react()],
plugins: [react(), securityHeaders()],
server: {
port: 5175
port: 5175,
strictPort: true
},
preview: {
port: 5175,
strictPort: true
}
})
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'
}
}
]
};
Loading