内容安全策略(CSP)

        内容安全策略(Content Security Policy,CSP)是一种网络浏览器技术,用于减少或者消除某类型的网站漏洞,如跨站脚本(XSS)攻击等。CSP通过定义哪些资源可以被用户代理(如浏览器)加载和执行,从而提高网站的安全性。
        CSP是通过在网页的HTTP响应头中设置“Content-Security-Policy”来实施的。这个HTTP响应头指定了一个策略,明确了哪些内容可以被加载以及它们可以被从哪些域加载。

 一、常见CSP示例

 1、所有内容均来自站点的同一个源(不包括其子域名)

Content-Security-Policy: default-src 'self'

2、允许内容来自信任的域名及其子域名(域名不必须与 CSP 设置所在的域名相同)

Content-Security-Policy: default-src 'self' *.trusted.com

3、允许网页应用的用户在他们自己的内容中包含来自任何源的图片,但是限制音频或视频需从信任的资源提供者,所有脚本必须从特定主机服务器获取可信的代码

Content-Security-Policy: default-src 'self'; img-src *; media-src media1.com media2.com; script-src userscripts.example.com

4、确保网站的所有内容都要通过 SSL 方式获取,以避免攻击者窃听用户发出的请求

Content-Security-Policy: default-src https://onlinebanking.jumbobank.com

5、内容安全策略指令
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy

 二、使用Content-Security-Policy-Report-Only对策略进行测试

        CSP 可以部署为仅报告(report-only)模式。在此模式下,CSP 策略不是强制性的,任何违规行为将会报告给一个指定的 URI 地址。(仅报告标头可以用来测试对策略未来的修订,如果 Content-Security-Policy-Report-Only 标头和 Content-Security-Policy 同时出现在一个响应中,两个策略均有效)

1、使用 report-uri (已废弃,但是大多数浏览器支持)将报告发送到指定url

Content-Security-Policy-Report-Only "default-src https:; report-uri /report-endpoint";

2、使用 report-to (目前有的浏览器不支持)将报告发送到指定url

Report-To "{ "group": "default",
              "max_age": 10886400,
              "endpoints": [
                { "url": "https://example.com/report-endpoint" }
              ] }";
Content-Security-Policy "default-src https:; report-to default";

 三、执行CSP策略并将违规内容发送到指定服务器

 1、制定CSP策略,指定report-uri用以启用报告,策略如下:

Content-Security-Policy: "default-src 'none'; style-src cdn.example.com; report-uri http://www.hb.com:8088/report-endpoint"; //该策略禁止任何资源的加载,除了来自 cdn.example.com 的样式表。

2、编辑网站页面signup.html,将以下页面置于 http://example.com/signup.html

<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="UTF-8" />
    <title>Sign Up</title>
    <link rel="stylesheet" href="css/style.css" />
  </head>
  <body>
    Here be content.
  </body>
</html>

3、访问http://example.com/signup.html,由于CSP仅允许加载自 cdn.example.com 的样式表,而该页面企图从自己的源(http://example.com)加载。页面被访问时,一个兼容 CSP 的浏览器将以 POST 请求的形式发送违规报告到 http://example.com/_/csp-reports,内容如下

{
  "csp-report": {
    "blocked-uri": "http://example.com/css/style.css", //被 CSP 阻止的资源 URI。如果被阻止的 URI 来自不同的源而非 document-uri,那么被阻止的资源 URI 会被删减,仅保留协议、主机和端口号。
    "disposition": "report",//根据 Content-Security-Policy-Report-Only 和 Content-Security-Policy 标头使用情况的不同,值为 "enforce" 或 "report"。
    "document-uri": "http://example.com/signup.html",//发生违规的文档的 URI
    "effective-directive": "style-src-elem",//导致违规行为发生的指令。一些浏览器可能提供不同的值,例如 Chrome 提供 style-src-elem 或 style-src-attr,即使实际执行的指令是 style-src。
    "original-policy": "default-src 'none'; style-src cdn.example.com; report-to /_/csp-reports",//由 Content-Security-Policy HTTP 标头指定的原始策略值。
    "referrer": "",//违规发生处的文档引用(地址)。
    "status-code": 200,//全局对象被实例化的资源的 HTTP 状态代码。
    "violated-directive": "style-src-elem"//导致违反策略的指令。violated-directive 是 effective-directive 字段的历史名称,并包含相同的值。
  }
}

 四、nginx配置CSP策略,并接受违规报告

1、使用python配置接受报告的服务

pip3 install flask
vim report-endpoint.py
from flask import Flask, request
import json

app = Flask(__name__)

@app.route('/report-endpoint', methods=['POST'])
def report_endpoint():
    report = request.get_json()
    #print(json.dumps(report, indent=4))
    print(request.data)
    return '', 200

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8088)

2、修改nginx.conf 配置文件,在server中下添加:

add_header Content-Security-Policy "default-src 'none'; style-src cdn.example.com; report-uri http://www.hb.com:8088/report-endpoint";

3、重启 nginx 服务

systemctl restart nginx

五、使用<meta> 元素来配置CSP策略

据内容安全策略(CSP)的规定,某些CSP指令,包括report-uri,不能通过<meta>元素来设置在signup.html添加:

<meta
  http-equiv="Content-Security-Policy"
  content="default-src 'none'; style-src cdn.example.com;" />

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值