Spot the bug in the CSP nonce middleware

from Security response headers
Node 24 intermediate 4 min 1 issue to find

Find the security bug in this generated CSP nonce middleware.

JavaScript
import { randomBytes } from 'node:crypto';

const nonce = randomBytes(16).toString('base64');

function addCsp(request, response, next) {
  response.locals.nonce = nonce;
  response.setHeader(
    'Content-Security-Policy',
    `default-src 'self'; script-src 'nonce-${nonce}'`,
  );
  next();
}
Open in playground
Report an error