facebookcloudflarefacebook-instant-games

Facebook Instant Game fails to load scripts from cloudflare.com due to Content Security Policy


In my Facebook Instant Game I try to statically load scripts from cloudflare.com such as:

<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.8.5/pixi.min.js"></script>

But getting Chrome browser error:

Refused to load the script 'https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' 'unsafe-eval' *.facebook.com connect.facebook.net cdn.mixpnl.com *.google-analytics.com web.localytics.com *.googletagmanager.com blob: *.cloudfront.net *.amazonaws.com *.googleapis.com *.firebaseapp.com *.firebaseio.com *.8686c.com *.cncovs.com *.aliyun.com *.aliyuncs.com *.wsdvs.com *.console.re *.kunlunar.com *.layabox.com *.windows.net *.msecnd.net *.anysdk.com cdn.trackjs.com cdn.firebase.com *.kochava.com *.akamaized.net *.cocos.com *.hinet.net *.playfab.com code.createjs.com *.zdassets.com websdk.appsflyer.com ". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

Is there a way I can load scripts from cloudflare?


Solution

  • Content Security Policy is one of the ways to mitigate XSS (cross-site scripting) attack on your site/app.

    To allow your site/app to load scripts from cdnjs.cloudflare.com, you'll need to add/append the domain in the script-src directive of your Content-Security-Policy HTTP response header.

    A few of example implementations:

    httpd.conf:

    Header set Content-Security-Policy "script-src 'self' ...(snipped)... cdnjs.cloudflare.com;"

    nginx.conf:

    add_header Content-Security-Policy "script-src 'self' ...(snipped)... cdnjs.cloudflare.com;";

    Then make sure to run checkconfig before reload or restart the httpd/nginx service.