webcomponentsjs icon indicating copy to clipboard operation
webcomponentsjs copied to clipboard

Content security policy rule of Shopify CMS blocks script load in the FireFox

Open fuerte656 opened this issue 9 years ago • 4 comments

Hello there,

I am developing a plugin for Shopify CMS. This plugin uses webcomponents.js to load Polymer scripts via: script.src = "data:text/javascript;charset=utf-8," + encodeURIComponent(scriptContent); line 307

In FireFox I get the following errors:

Content Security Policy: The page’s settings blocked the loading of a resource at data:text/javascript;charset=utf-8,/**script code*/(“script-src https://domainurl https://* 'unsafe-inline' 'unsafe-eval'”)

Here is an original Content security policy header (I can't change this header on the server, because Shopify is a hosted platform and I don't have any control on it):

    Content-Security-Policy:
    default-src 'self' https://*;
    child-src 'self' https://* blob: data:; 
    connect-src 'self' https://* wss://*;
    font-src 'self' https://* blob: data:;
    img-src 'self' https://* blob: data:; 
    media-src'self' https://* blob: data:;
    object-src 'self' https://* blob: data:;
    script-src 'self' https://* 'unsafe-inline' 'unsafe-eval';
    style-src 'self' https://* 'unsafe-inline';

The script-src 'self' https://* 'unsafe-inline' 'unsafe-eval'; rule prevents loading js through script tags with DataURI.

I have found that I can load js in a script tag without a DataURI. In particular, this comment says that DataURI is not necessary:

// NOTE: execute scripts by injecting them and watching for the load/error // event. Inline scripts are handled via dataURL's because browsers tend to // provide correct parsing errors in this case. If this has any compatibility // issues, we can switch to injecting the inline script with textContent. parseScript: function(scriptElt) {.... line 226

At this moment, I have modified your script to use textContent instead of the DataURI, but I am worried that I have to use my own branch of webcomponents.js. I wonder whether you consider making this behavior as a standard feature? Or at least make it configurable?

my question on stackoverflow

Best regards Alex

fuerte656 avatar Feb 03 '17 06:02 fuerte656

although not suitable for this case, but if you have control over nginx, then add the following:

instead of

script-src 'self' https://* 'unsafe-inline' 'unsafe-eval';

use

script-src 'self' https://* 'unsafe-inline' 'unsafe-eval' data:;

sz332 avatar Jan 30 '18 14:01 sz332

No, I haven't any control of server side. We produce cms plugin and give this plugin to our customers. I have created a patch that changes the code of webcomponentsjs, and I applying this patch before a build process.

fuerte656 avatar Jan 31 '18 05:01 fuerte656

Yes, basically the question is whether this function could be changed, or made configurable in webcomponents so you don't have to patch it.

sz332 avatar Jan 31 '18 09:01 sz332

The HTML Imports polyfill no longer exists in version 1 of this polyfill. Therefore, this issue should be resolved. Note that we are tracking the other CSP compatibility issues in #188.

TimvdLippe avatar Sep 27 '18 11:09 TimvdLippe