I’m trying to allow the user to return to their previous screen when they get a 404 not found, but my CSP is stopping me.
Using nonce seems to be the way around it, but I can’t get the blessed thing to work.
Ideally, I’d like to add the nonce to my href, but I’m not sure that’s do-able. Alternatively, I’d like to run some javascript from the 404 file, but it doesn’t seem to be picking up the value of my nonce.
This is how I set up the nonce in helmet-csp:
app.locals.nonce = crypto.randomBytes(16).toString("hex");
app.use(csp({
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'"],
scriptSrc: ["'self'", `'nonce-${app.locals.nonce}'`],
imgSrc: ["'self'"],
fontSrc: ["'self'"]
}
}));
In the nunjucks 404 page, I’m trying to go back to previous page:
{% set n = "nonce-"+nonce %}
<a nonce={{n}} href="javascript:history.go(-1);">{{ t('404:goBack')</a>
And at the bottom, I have my script tags set up thusly:
<style nonce=app.locals.nonce>
console.log('inside 404')
</style>
On landing on my 404 page, Chrome spews up the following error: eligibilixxxty-sf-debt:38 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-a8ff45c6cc9b12df8111202f13d1c000'". Either the 'unsafe-inline' keyword, a hash ('sha256-+6WnXIl4mbFTCARd8N3COQmT3bJJmo32N8q8ZSQAIcU='), or a nonce ('nonce-...') is required to enable inline execution.
And clicking the href link gets this response: Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-a8ff45c6cc9b12df8111202f13d1c000'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
Any ideas where I’m going wrong?