stencil icon indicating copy to clipboard operation
stencil copied to clipboard

CSP Nonce Support for generated files

Open MrMcGibblets opened this issue 7 years ago • 14 comments

Stencil version:

 @stencil/[email protected]

I'm submitting a: [ ] bug report [x] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior: Same as the closed issue - https://github.com/ionic-team/stencil/issues/496

When bundling we automatically create an inline style which violates the strict CSP usage

Whilst a workaround is to add each individual sha to the CSP policy it isn't ideal and not a nice dev experience.

In webpack this was achieved by adding a top-level nonce value which would then be added to all at bundle time. - https://github.com/styled-components/styled-components/issues/887

Expected behavior: To be able to provide a nonce for styles (should be for all scripts, img etc...) that will be added to all build-generated scripts, styles etc... Allowing apps to be CSP ready out of the box.

Steps to reproduce: Add CSP Policy for style-src 'self' and run app.

MrMcGibblets avatar Feb 27 '19 21:02 MrMcGibblets

Working in an organization this is a critical feature for us. It's quite a hazzle to add sha values for our styles.

Was there any internal discussion in the ionic team about this feature?

BorntraegerMarc avatar Jul 16 '19 09:07 BorntraegerMarc

Is there any update from the Stencil team in regard of this feature? We also have some concern that limit some of our teams to move ahead with the stencilJs web-components used by our design system.

FrancoisDF avatar Apr 07 '20 19:04 FrancoisDF

Hi, Any update on this one? I can't believe nothing have been done yet to handle CSP in stencil libraries. Angular and react have added support for CSP in 2015-2016!

I'm also working in a big organization and CSP is a basic security requirement for us. It's a blocker right now!

Any update would be appreciated! 👍 @adamdbradley ?

Thanks

ptrot001 avatar Oct 28 '20 14:10 ptrot001

Hi, Any update on this?

alexhhn avatar Apr 07 '21 08:04 alexhhn

We also need that quite urgently

sandersaspera avatar Apr 08 '21 07:04 sandersaspera

any updates about this? Also our organisation requires to use a CSP.

erwingeiger avatar Jul 28 '21 13:07 erwingeiger

any updates about this? Also our organisation requires to use a CSP.

In case that helps: I found out that the inline styles for hydrated components (components are invisible or inherit visibility when having the "hydrated" class) can be suppressed by setting "hydratedFlag: null" in the stencil configuration. Those hydrated styles are supposed to prevent the components from "flickering". Maybe a custom output target could be used to write those css rules into a proper css file and thus not violating the corresponding CSP. There is a corresponding suggestion here (https://github.com/ionic-team/stencil/issues/2245)

sandersaspera avatar Aug 02 '21 13:08 sandersaspera

Hey everyone! Thank you so much for the feedback. We'll be taking this into consideration.

splitinfinities avatar Aug 03 '21 19:08 splitinfinities

For your information. Our best solution for now was to fork the source files of Stencil and made adjustments to the files src/runtime/styles.ts src/runtime/bootstrap-lazy.ts and a few others , to add the nonce attribute to the generated style tags.
Then we used this fork as a dependency in our project.

Not the best solution, but it works for now. I hope Stencil will come with a better solution in the near future.

erwingeiger avatar Aug 03 '21 19:08 erwingeiger

@erwingeiger Would you be able to make a PR into Stencil for that? Or do you believe there's a better approach than what you've done?

splitinfinities avatar Aug 03 '21 20:08 splitinfinities

@splitinfinities I would like, but this is a quick and dirty solution. So I think others can make better approaches. But I can tell what I did.

In the file src/runtime/bootstrap-lazy.ts after the rule visibilityStyle.setAttribute('data-styles', ''); I placed another setAttribute but now with the nonce attribute.

I did this in the files: src/client/polyfills/css-shim/load-link-styles.ts src/runtime/bootstrap-lazy.ts src/runtime/styles.ts

erwingeiger avatar Aug 03 '21 20:08 erwingeiger

Thanks for the issue! This issue is being closed due to inactivity. If this is still an issue with the latest version of Stencil, please create a new issue and ensure the template is fully filled out.

Thank you for using Stencil!

ionitron-bot[bot] avatar Sep 02 '21 22:09 ionitron-bot[bot]

Hey ionitron-bot, we didn't mean to close this. Bad bot!

rwaskiewicz avatar Sep 03 '21 12:09 rwaskiewicz

Initial support for CSP Nonces have been added in today's release of Stencil 2.22.1, with our official guidance on nonce usage with Stencil published here. As a result, I'm going to close this issue. Should anyone run into any issues with the feature, please feel free to open a new issue. Thanks!

rwaskiewicz avatar Jan 17 '23 21:01 rwaskiewicz