Setting headers in Vite
Hystrelius December 30, 2022 Updated: April 01, 2023 [Features] #js #vueThis post covers how to turn your Mozilla Observatory scores from D- to A+ on your Vite website.
Mozilla Observatory measures the security of your website using a range of factors to determine how safe it is for your users to interact with. In an age of ever-increasing hacking exploits, it is becoming more important to create safer websites.
It recommends the creation of more headers to secure your website:
- Content Security Policy
- HSTS
- X “options”:
Out of these three options, X options are the easiest to implement (and least likely to break your site).
In your vite.config.js
you can adjust your header settings:
// FILE: vite.config.js
;
;
// https://vitejs.dev/config/
;
Make sure to test all header attributes as they can severely break websites if you are not careful.
CSP header
This header has personally caused me strife (I left it out of the base config).
As it restricts what files can load into your website.
If you want to add it I recommend reading through the documentation.
// --snip--
headers:
This policy should not break anything on your website, but using policies like default-src 'self'
can easily break your website.
So, just test what works.
Hashes
Some CSP policies (script-src
and style-src
) prevent the inline loading of resources, which a vue file does natively. So you can define specific hashes that are allowed for the CSP to pass.
// --snip--
headers:
Generating Hashes
To generate said hashes, you can use openssl
, you can either:
- Ingest the file:
openssl dgst -sha256 -binary example.js | openssl base64 -A
- Ingest text:
echo -n "Example text" | openssl dgst -sha256 -binary | openssl base64 -A
These hashes are then put into the CSP like above, if this produces errors. I recommend using Chrome, it tells you the hash you need to put in your CSP in the error console.