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 for styling under strict Content-Security-Policy #496

Closed
1 of 3 tasks
riovir opened this issue Feb 7, 2018 · 3 comments
Closed
1 of 3 tasks

Support for styling under strict Content-Security-Policy #496

riovir opened this issue Feb 7, 2018 · 3 comments
Labels
ionitron: stale issue This issue has not seen any activity for a long period of time

Comments

@riovir
Copy link

riovir commented Feb 7, 2018

Stencil version:

 @stencil/core@0.3.0

I'm submitting a:

Current behavior:
A components currently rely on inline <style> tags. When Content-Security-Policy headers forbid unsafe-inline the styling breaks.

Expected behavior:
Any solution that allows strict CSP policies and well styled Stencil components to coexist.

One approach could be for Stencil to generate CSS files at compilation time to which <style> tags would refer.

Steps to reproduce:

  1. Take any Stencil component with a styleUrl specifying some styles
  2. Compile it and serve it with the following response header added: Content-Security-Policy = "style-src 'self' ;"
  3. Notice that styles are broken due to the browser refusing to interpret them

Related code:

The following snippet spins up a proxy from port 5000 to 3333 banning inline style tags. (Assuming express and express-http-proxy are installed.)

const express = require('express');
const proxy = require('express-http-proxy');

const app = express();
app.use((req, res, next) => { res.set('Content-Security-Policy', "style-src 'self' ;"); next(); });
app.use('/', proxy('http://localhost:3333'));
app.listen(5000);
@peterpeterparker
Copy link
Contributor

peterpeterparker commented Feb 7, 2018

Couldn't that be solved by adding the sha value of the component in the CSP style-url list?

When I imported my component build with StencilJS in my Ionic project, I saw the CSP warning in the Chrome debugger, telling my to add unsafe-inline or to add the regarding 'sha256-xxxxx' value to the style-src of my CSP. I did added the sha value, restarted my project and the issue was solved.

@riovir
Copy link
Author

riovir commented Feb 8, 2018

Indeed adding the sha256 value provides a way to approach the problem. Unfortunately this angle is less than ideal in some scenarios:

  • For each styled component on the page an individual hash needs to be added to the header
  • These headers are often handled centrally by a proxy for multiple projects (as in our case) in order to enforce security across teams. This requires the careful management of multiple versions of each component in circulation.
  • One needs to also take care that the amount of hashes stays in the sane realm. At around 50 items we start to near close to a rather large 4k header which can start causing problems on its own.

A special entry or two per Stencil component collection would be acceptable though as a workaround if CSS files are not an option.

@ionitron-bot ionitron-bot bot added the ionitron: stale issue This issue has not seen any activity for a long period of time label Apr 9, 2018
@ionitron-bot
Copy link

ionitron-bot bot commented Apr 9, 2018

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 ionitron-bot bot closed this as completed Apr 9, 2018
@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ionitron: stale issue This issue has not seen any activity for a long period of time
Projects
None yet
Development

No branches or pull requests

3 participants