A simple low-code package to generate and expose nonces in express apps.
npm install nonce-expressnonce-express!bzfS2qzo/Q==!
nonce-express is a simple low-code express middleware to help generate nonces. It uses the built-in crypto module to generate "cryptographically strong pseudorandom data" to be exposed at res.locals.nonce.
bash
npm install --save nonce-express
`
2. Use the middleware
`js
// ...
const nonce = require("nonce-express");
app.use(nonce({ // These are the defaults
varName: "nonce",
size: 16
}));
// ...
`
3. Use the generated nonce
`js
// ...
// In helmet
app.use(helmet({
contentSecurityPolicy: {
useDefaults: true,
directives: {
scriptSrc: [
"'self'",
(req, res) => 'nonce-${res.locals["nonce"]}',
]
}
}
}));
// In your app
app.get("/", (req,res) => res.send());
// ...
`
Final words
Now that this readme is officially longer than the actual nonce.js` file, I think it's time to end it here.