Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CSP with Nonces #312

Open
aKzenT opened this issue Jan 11, 2021 · 0 comments
Open

Support CSP with Nonces #312

aKzenT opened this issue Jan 11, 2021 · 0 comments

Comments

@aKzenT
Copy link

aKzenT commented Jan 11, 2021

Currently using element-queries fails when used with Content Security Policy Level 2 Nonces.

In CSP Level 2 using Nonces require all <script> and <style> elements to have a "nonce" attribute with a random number which is then used in the CSP response header to whitelist the script or style element.

"css-element-queries" creates a style element in its "init" function which of course does not have this "nonce" attribute and will therefore not be allowed to execute.

I propose a simple change that I tested locally which would solve this issue. Instead of always creating a new style element on init the script could look for an existing style-element with a specific identifier and modify this element. So instead of:

cssStyleElement = document.createElement('style');

The script should do something like:

cssStyleElement = document.getElementById('element-queries') || document.createElement('style');

If there is an element with the known id, then it is used, otherwise the existing logic will be used.

This allows users of the package to supply an existing (empty) style-element with a suitable nonce which would be picked up by the libary.

<style id="element-queries" nonce="..."></style>

I tested this successfully locally and did not see any issues with this approach so far. I can create a PR with the change above, if this is suitable for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant