svelte-adapter-azure-swa icon indicating copy to clipboard operation
svelte-adapter-azure-swa copied to clipboard

Generate CSP headers for prerendered pages

Open tlaundal opened this issue 2 years ago • 2 comments

This is just an initial stab at implementing #106 to start the discussion and see if it is viable.

From my tests it is working, though not locally with swa because of Azure/static-web-apps-cli#646.

For this to be ready for merge, we would need to add

  • [ ] Documentation
  • [ ] Tests

We should discuss if a config option for this should be opt-in or opt-out. If there is no http-equiv="content-security-policy" meta tag in a html file, no headers will be generated. I'm not sure there is a usecase where you would want CSP meta tags but not headers?

We might also want to consider adding support for turning any http-equiv meta tag into a header.

I entertained the thought of making a PR to SvelteKit for exposing any headers returned for pages during pre-rendering in builder.prerendered to the adapters, but I think this solution here is a better first step.

What do you think , @geoffrich?

tlaundal avatar Jan 26 '23 11:01 tlaundal

What is the advantage of having the CSP in a header as opposed to the meta tag? Won't they have the same effect?

The implementation seems relatively straightforward. Re: opt-in vs opt-out -- my only concern would be the impact on build speed, since you have to read each file to determine if they set a CSP.

geoffrich avatar Jan 28 '23 19:01 geoffrich

I was inspired by this note from google pagespeed:

you should define a CSP in an HTTP response header if you can. An injection before the meta tag will bypass the CSP. Additionally, frame-ancestors, sandbox and reporting are not supported in meta tag CSPs.

I'm sure in most cases it doesn't make too much of a difference, but I think it would be nice to have.

tlaundal avatar Jan 29 '23 17:01 tlaundal