lit-element icon indicating copy to clipboard operation
lit-element copied to clipboard

Support style-src 'nonce' security policy

Open redallen opened this issue 4 years ago • 3 comments

Description

Inline styles are not applied and the following console error is thrown when using static get styles:

Content Security Policy: The page’s settings blocked the loading of a resource at inline (“style-src”).

Live Demo

https://stackblitz.com/edit/lit-element-example-qpfhsi?file=index.html

Steps to Reproduce

  1. Add <meta http-equiv="Content-Security-Policy" content="style-src 'nonce-2726c7f26c'" > to <head> in a browser that does not support constructable stylesheets such as Firefox. For more details on this security policy, see MDN.

Expected Results

You can pass a nonce to the css tagged template function so it creates <style nonce="2726c7f26c> when inserting the <style> tag into the document.

Actual Results

There's no API available to set a nonce using static get styles.

Browsers Affected

  • [ ] Chrome
  • [X] Firefox
  • [X] Edge
  • [X] Safari 11
  • [X] Safari 10
  • [X] IE 11

Versions

  • lit-element: v2.2.1
  • webcomponents: v2.4.0

redallen avatar Dec 20 '19 15:12 redallen

I think we could handle this with a LitElement.styleNonce static that we apply to all injected <style> tags.

justinfagnani avatar Jan 22 '20 21:01 justinfagnani

Has there been any update on this? Or has the issue been fixed in the new lit repo? It's really stopping me adopt CSP while using lit-element

mdownes avatar Jul 06 '21 11:07 mdownes

has anybody found a reliable solution to this?

We have the same exact issues with lit elements.

We have an APP that consumes many lit elements as npm packages.

Those lit elements ship with their own incremental styles (in the shadow dom). (using style properties).

The app attaches nonce using a header and includes the nonce on all embedded styles and scripts.

however when lit components tries to add it's own style element the system blows due to unsafe style. as noted in this issue.

samaniehsan avatar Aug 13 '21 19:08 samaniehsan

Fixed via https://github.com/lit/lit/pull/2134

sorvell avatar Sep 08 '22 17:09 sorvell

@justinfagnani - Is there any chance the PR linked above could get back-ported to this repo? Our team is still running on lit-element@^2.5.1 and we're hitting this issue.

andy-blum avatar Nov 25 '22 17:11 andy-blum

@andy-blum yeah, I think we can do that.

Is there any reason you haven't upgraded to LitElement 3.x yet? Things are mostly compatible, so I'm curious what incompatibilities you've run into.

justinfagnani avatar Nov 25 '22 18:11 justinfagnani

I'm not 100% sure why we haven't upgraded, it may be in progress and it's just a long, deliberate process to ensure backwards compatibility. A minor/patch upgrade would likely be much quicker.

andy-blum avatar Nov 25 '22 19:11 andy-blum