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..d0185c4 100644 --- a/client/package.json +++ b/client/package.json @@ -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" diff --git a/client/src/lib/sanitize.js b/client/src/lib/sanitize.js index 1c8ed3d..37e92d6 100644 --- a/client/src/lib/sanitize.js +++ b/client/src/lib/sanitize.js @@ -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 ' + + 'click me'; + +function renderReview(body) { + const reviews = [{ _id: 'r1', rating: 5, author: { name: 'attacker' }, body }]; + return render(); +} + +describe('ReviewList renders review HTML safely', () => { + test.each([ + '', + '', + '', + '', + '', + 'link' + ])('removes active markup from attack: %s', (payload) => { + const { container } = renderReview(payload); + expect(container.querySelector('script, img, svg, iframe, object, embed')).toBeNull(); + for (const element of container.querySelectorAll('.review-body *')) { + for (const attribute of element.attributes) { + expect(attribute.name).not.toMatch(/^on/i); + } + } + const link = container.querySelector('a'); + if (link) expect(link.hasAttribute('href')).toBe(false); + }); + + test('preserves legitimate formatting and safe links', () => { + const { container } = renderReview( + '

Great seller and service!
' + + 'Details

' + + '' + ); + expect(container.querySelector('b')).toHaveTextContent('seller'); + expect(container.querySelector('em')).toHaveTextContent('service'); + expect(container.querySelector('br')).not.toBeNull(); + expect(container.querySelector('li')).toHaveTextContent('Fast delivery'); + expect(container.querySelector('a')).toHaveAttribute('href', 'https://example.com/item'); + }); + + test('removes styles, forms and DOM-clobbering attributes', () => { + const { container } = renderReview( + '
' + + '

Review

' + ); + expect(container.querySelector('form, input, [style], [id], [data-payload]')).toBeNull(); + expect(container.textContent).toContain('Review'); + }); + + test('does not render any