问题 在使用http-mock的Ember CLI应用程序中配置CSP


我按照建议在Ember CLI上使用http-mock http://www.ember-cli.com/#ember-data。我理解CSP的基本概念,但我不了解它在Ember CLI应用程序中的配置。

如何配置我的应用程序以接受请求 localhost:4200/api/ 在开发期间避免这种情况:

Content Security Policy violation: {
    "csp-report": {
        "document-uri":"http://localhost:4200/products",
        "referrer":"",
        "violated-directive":"style-src 'self'",
        "effective-directive":"style-src",
        "original-policy":"default-src 'none'; script-src 'self' 'unsafe-eval' localhost:35729 0.0.0.0:35729; font-src 'self'; connect-src 'self' ws://localhost:35729 ws://0.0.0.0:35729 http://0.0.0.0:4200/csp-report; img-src 'self'; style-src 'self'; media-src 'self'; report-uri http://0.0.0.0:4200/csp-report;",
        "blocked-uri":"",
        "source-file":"chrome-extension://alelhddbbhepgpmgidjdcjakblofbmce",
        "line-number":1,"column-number":20481,"status-code":200
    }
}

9058
2018-03-28 16:28


起源



答案:


您可以通过编辑调整内容安全策略 config/environment.js。我相信你的情况, connect-src 与抛出的错误有关 (编辑:看起来像 style-src 正在被侵犯,可能是Chrome Extension Awesome截图)。添加 * 将允许它连接到任何东西。

var ENV = {

  ...

  contentSecurityPolicy: {
    'default-src': "'none'",
    'script-src': "'self'",
    'font-src': "'self'",
    'connect-src': "'self' *",
    'img-src': "'self'",
    'style-src': "'self' *",
    'media-src': "'self'"
  }
}

或者更具体地说,您可以添加:

...
'connect-src': "'self' 'localhost:4200'",
...

此外,如果您只想将其添加到您的开发环境中,请将其放入:

if (environment === 'development') {
  ENV.contentSecurityPolicy = {
    ...(policies)...
  }
}

有关CSP的更多信息 ember-clihttps://www.npmjs.com/package/ember-cli-content-security-policy

有关CSP的更多信息: http://content-security-policy.com/


13
2018-03-28 17:40



我尝试了两个版本,但它们不起作用。我仍然得到相同的错误消息。 - wintermeyer
仔细观察你看到的错误信息 "violated-directive":"style-src 'self'" ..尝试添加 'style-src': "'self' *"。看起来Chrome扩展程序正在注入样式表?谷歌搜索它表明“真棒截图” - 尝试禁用? - rog
它可能是内联样式导致警告。加 'unsafe-inline' 至 style-src 解决这个问题。 - rog
它确实是Chrome扩展程序。我从来没有想过这个。谢谢! - wintermeyer


答案:


您可以通过编辑调整内容安全策略 config/environment.js。我相信你的情况, connect-src 与抛出的错误有关 (编辑:看起来像 style-src 正在被侵犯,可能是Chrome Extension Awesome截图)。添加 * 将允许它连接到任何东西。

var ENV = {

  ...

  contentSecurityPolicy: {
    'default-src': "'none'",
    'script-src': "'self'",
    'font-src': "'self'",
    'connect-src': "'self' *",
    'img-src': "'self'",
    'style-src': "'self' *",
    'media-src': "'self'"
  }
}

或者更具体地说,您可以添加:

...
'connect-src': "'self' 'localhost:4200'",
...

此外,如果您只想将其添加到您的开发环境中,请将其放入:

if (environment === 'development') {
  ENV.contentSecurityPolicy = {
    ...(policies)...
  }
}

有关CSP的更多信息 ember-clihttps://www.npmjs.com/package/ember-cli-content-security-policy

有关CSP的更多信息: http://content-security-policy.com/


13
2018-03-28 17:40



我尝试了两个版本,但它们不起作用。我仍然得到相同的错误消息。 - wintermeyer
仔细观察你看到的错误信息 "violated-directive":"style-src 'self'" ..尝试添加 'style-src': "'self' *"。看起来Chrome扩展程序正在注入样式表?谷歌搜索它表明“真棒截图” - 尝试禁用? - rog
它可能是内联样式导致警告。加 'unsafe-inline' 至 style-src 解决这个问题。 - rog
它确实是Chrome扩展程序。我从来没有想过这个。谢谢! - wintermeyer