在ember-cli 0.0.47升级后违反内容安全策略指令

时间:2014-10-04 11:22:20

标签: ember.js ember-cli content-security-policy

我将我的ember-cli应用程序升级到0.0.47,现在我的浏览器控制台中出现了一系列与内容安全策略相关的错误。我该如何解决这个问题?

Refused to load the script 'http://use.typekit.net/abcdef.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' localhost:35729".
 login:1
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' localhost:35729". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
 login:20
Refused to load the script 'http://connect.facebook.net/en_US/all.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' localhost:35729".
 login:1
Refused to load the script 'http://maps.googleapis.com/maps/api/js?libraries=places' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval' localhost:35729".

以下是我的app / index.html文件中的行:

<script type="text/javascript" src="//use.typekit.net/abcdef.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>

2 个答案:

答案 0 :(得分:126)

http://content-security-policy.com/https://github.com/rwjblue/ember-cli-content-security-policy阅读了一些文档后,我在config / environment.js文件中添加了一些策略,如下所示:

module.exports = function(environment) {
  var ENV = {
    contentSecurityPolicy: {
      'default-src': "'none'",
      'script-src': "'self' 'unsafe-inline' 'unsafe-eval' use.typekit.net connect.facebook.net maps.googleapis.com maps.gstatic.com",
      'font-src': "'self' data: use.typekit.net",
      'connect-src': "'self'",
      'img-src': "'self' www.facebook.com p.typekit.net",
      'style-src': "'self' 'unsafe-inline' use.typekit.net",
      'frame-src': "s-static.ak.facebook.com static.ak.facebook.com www.facebook.com"
    },

  // ...
};

这使得所有即时错误都消失了,但是当我开始浏览我的应用程序时,新的应用程序似乎与S3媒体源有关。

我确信这适用于不包含任何外部资源的应用,但我决定从我的package.json文件中删除“”ember-cli-content-security-policy“。

答案 1 :(得分:18)

从谷歌链接到字体时我不得不使用它:

<link rel='stylesheet' href='http://fonts.googleapis.com/css?family=Lato:400,700,900'>

在我使用的config/environment.js文件中

contentSecurityPolicy: {
  'font-src': "'self' data: fonts.gstatic.com",
  'style-src': "'self' 'unsafe-inline' fonts.googleapis.com"
},