A couple of problems introduced after adding a CSP configuration #4952
Unanswered
kelvindecosta
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Just encountered this issue when using
This works well and prevents FOUC, unless a dev has also defined either the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Overview
Hello everyone!
I've added a simple CSP configuration.
Configuration (
kit.csp
) [click to expand]I'd like to clarify that there is no problem with the CSP configuration.
Rather, it brought up two interesting problems, both with the
script-src-elem
directive:This is because a
handle
hook performs HTML minification.This script is an IIFE that is meant to run immediately.
Problem 1: Svelte-generated inline scripts are blocked
As mentioned before, any
<script>
tag generated by Svelte is seemingly blocked.At first, this was weird because the documentation states that:
After a closer look, I realized that the
<meta http-equiv>
tag for CSP was indeed present.This implied that the hashes for the
<script>
tags were not matching.The culprit is definitely the following
handle
hook that performshtml-minification
: [click to expand]Disabling this hook unblocked the scripts.
I'm assuming that I can modify the options passed to the minifier to rectify the problem.
Still, I wonder if there's a way to let SvelteKit inject the CSP information after minification /
handle
hooks.Problem 2: Custom inline scripts are blocked
A bit of context; I'm working on a site that has a dark mode.
The custom script in question prevents a flash of white when the user's preferred them is dark.
Here's the code: [click to expand]
There was no hash in the CSP
script-src
directive for this custom<script>
tag.Is there a way I can get SvelteKit to treat custom
<script>
tags as if they were generated by Svelte? Or, is there a "Svelte" way to generate a custom<script>
tag?Conclusion
Adding a CSP configuration introduced two problems that might be solved if:
handle
hooks to run before CSP directives are injected.<script>
tagsAny help is appreciated!
Thank you so much for your time!
Beta Was this translation helpful? Give feedback.
All reactions