ionic-team / stencil

A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
https://stenciljs.com
Other
12.56k stars 787 forks source link

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

Closed riovir closed 6 years ago

riovir commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

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[bot] commented 6 years ago

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!