Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active June 6, 2017 05:09
Show Gist options
  • Save xgqfrms-GitHub/ecf7733d066d56723b00de41a849037a to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/ecf7733d066d56723b00de41a849037a to your computer and use it in GitHub Desktop.
CSP 内容安全策略

CSP 内容安全策略

https://developers.google.com/web/fundamentals/security/csp/

CSP 基于白名单来源,因为此方法可明确指示浏览器将特定的资源集视为可接受的资源,并拒绝其余资源。

https://developers.google.com/web/fundamentals/security/csp/

TL;DR

  • 使用白名单告诉客户端允许加载和不允许加载的内容。
  • 了解可使用哪些指令。
  • 了解这些指令接受哪些关键字。
  • 内联代码和 eval() 被视为是有害的。
  • 向服务器举报政策违规行为,以免执行这些行为。
    
<meta http-equiv="Content-Security-Policy" 
content="default-src https://cdn.example.net; child-src 'none'; object-src 'none'" />

https://github.com/xgqfrms-GitHub/webgeeker/blob/gh-pages/CSP/readme.md

@xgqfrms-GitHub
Copy link
Author

仅报告

如果您是刚刚开始使用 CSP,那么,在向您的用户部署严格的政策前,先评估您的应用的当前状态很重要。
作为完整部署的敲门砖,您可以要求浏览器监控某个政策,报告违规行为,但不强制执行限制。
发送 Content-Security-Policy-Report-Only 标头,而不是 Content-Security-Policy 标头。

Content-Security-Policy-Report-Only: default-src 'self'; ...; report-uri /my_amazing_csp_report_parser;

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Apr 4, 2017

Content Security Policy Level 2

http://caniuse.com/#search=csp

Content Security Policy 1.0

http://caniuse.com/#feat=contentsecuritypolicy

CSP 1通过将允许的脚本,样式和其他资源来源列入白名单来减轻跨站点脚本攻击。

CSP 2增加了hash-source,nonce-source和五个新的指令

@xgqfrms-GitHub
Copy link
Author

Content-Security-Policy: 
default-src 'none'; 
script-src https://cdn.mybank.net; style-src https://cdn.mybank.net;
img-src https://cdn.mybank.net; 
connect-src https://api.mybank.com; child-src 'self'
Content-Security-Policy: 
default-src https:; 
script-src https: 'unsafe-inline';
style-src https: 'unsafe-inline'

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Apr 4, 2017

Content Security Policy Level 3

W3C Working Draft, 13 September 2016

https://www.w3.org/TR/CSP3/

Content Security Policy Level 2

W3C Recommendation, 15 December 2016

https://www.w3.org/TR/CSP2/

@xgqfrms-GitHub
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment