I'm moving my NodeJS(http+websocket)/vanillaJS to express+websocket/(hopefully React). I want to implement CSP-nonce for script-src with ExpressJS, for the static files. I've managed to set the header dynamically with crypto and helmet-csp.
However, I'm stuck at setting the nonce value on each script tag and still using the express.static function (can't find anything on the docs/web/stacko also...) With the raw http module i was only replacing the '<script type="text/javascript"' parts with the nonce generated value.
Is there anyway to do this and still use express-static? (I'm aware that it says static, but this seems to be a useful functionality even for static files). If not (since I'm new to express) is there a standard/best practice way to do it? Or a well known used module that does this? Thanks!
You'll have to set a special string in your scripts which you'll replace with the random nonce you'll be generating. In my case I set it to 'random-csp-nonce'. So I'll be reading my file and substituting 'random-csp-nonce' with random generated nonce. Unfortunately, I guess you'll have to write such routes for each file.
Reference - https://codereview.stackexchange.com/questions/180251/send-html-with-nonce-for-each-script-and-style-tag-on-each-request