How to implement nonce in _headers file?

I am using pages with marko, how could I implement nonce from the _headers file?
11 Replies
James
James2w ago
If it's a dynamic nonce, you won't be able to and will have to set your headers via code using Functions
Rohan Vashisht
Rohan VashishtOP2w ago
but using a static nonce is just like not using a nonce right?
James
James2w ago
yeah, pretty much
Rohan Vashisht
Rohan VashishtOP2w ago
thanks But what if my website is ssg?
James
James2w ago
your framework should offer some way to run server-side code and set headers I'd imagine you would do this in vanilla Pages with Functions, but it might be a little different depending on your framework
Rohan Vashisht
Rohan VashishtOP2w ago
yes, how could I implement hash?
James
James2w ago
you could create a random UUID with crypto.randomUUID, or some random bytes + md5/sha it - lots of ways to generate a random hash
Rohan Vashisht
Rohan VashishtOP2w ago
CSP Hash Examples and Guide
Implementing a hash with Content Security Policy (CSP)
Rohan Vashisht
Rohan VashishtOP2w ago
this is the hash of the script itself do we have some software that could calculate the hash for all the scripts for me?
James
James2w ago
depends greatly on your framework and how you're building your site. It might expose some metadata about the build output
Rohan Vashisht
Rohan VashishtOP2w ago
ok, thanks

Did you find this page helpful?