您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

让你的web应用更安全

2024/2/21 22:33:29发布23次查看
x-frame-options cookie of secure and httponly 设置x-frame-options https://developer.mozilla.org/en-us/docs/web/http/x-frame-options
x-frame-options 主要是为了防止点击劫持(clickjacking)点击劫持(clickjacking)是一种在网页中将恶意代码等隐藏在看似无害的内容(如按钮)之下,并诱使用户点击的手段。x-frame-options http 头部字段用来指示传输的资源是否可以被包含在 或 中,服务端可以声明这个策略来确保自己的网页内容不会被嵌入到其他的页面中.x-frame-options 具有3个具体的值:
deny表明网页内容不可以被嵌入到任何frame中
sameorigin同源策略,声明网页内容可以被同一域下面的frame嵌入,但不能被不在同一域下面的frame嵌入.同源: 协议,域名和端口全部相同,即使是ip与域名对应,也认为是不同域下,下面说明了具体的情况,与: http://www.example.com/dir/page.html对比:
compared url outcome reason
http://www.example.com/dir/page2.html 同源 协议主机端口相同
http://www.example.com/dir2/other.html 同源 协议主机端口相同
http://username:password@www.example.com/dir2/other.html 同源 协议主机端口相同
http://www.example.com:81/dir/other.html 不同源 端口不同
https://www.example.com/dir/other.html 不同源 协议不同
http://en.example.com/dir/other.html 不同源 主机名不同
http://example.com/dir/other.html 不同源 主机不同,必须完全匹配
http://v2.www.example.com/dir/other.html 不同源 主机不同,必须完全匹配
http://www.example.com:80/dir/other.html depends port explicit. depends on implementation in browser
allow-from指定具体的来源 服务器配置 apache 添加站点配置:
header always append x-frame-options sameorigin
nginx 添加 http , server 或者 location 配置
add_header x-frame-options sameorigin;
iis 添加到站点的 web.config
... ...
haproxy 添加到 frontend, listen, 或者 backend 配置中:
rspadd x-frame-options:\ sameorigin
更安全的cookie cookie 是浏览器储存在客户端的小的文本文件,用于客户端与服务器之间互传.web服务器通过指定 http header 的 set-cookie, 其结构如下:
set-cookie: name=value[; expires=date][; domain=domain][; path=path][; secure][; httponly]
可以看到,cookie主要包含一下几个字段:
name value expire domain path secure httponly 这里主要讲secure 和 httponly
httponly标识 用来告诉浏览器不能通过javascript的 document.cookie 来访问 cookie, 目的是避免 跨站脚本攻击 (xss)
secure标识 强制应用通过https来传输 cookie
php yii2中设置cookie的 httponly 和 secure 设置 _csrf yii2中的 cookie yii\web\cookie默认是 httponly的,
class cookie extends \yii\base\object{ public $name; public $value = ''; public $domain = ''; public $expire = 0; public $path = '/'; public $secure = false; public $httponly = true;}
使用 就会生成一个 name=_csrf 的 cookie, 默认是 httponly, 通过注入request来改变:
在 config/main.php 中
... 'components' => [ 'request' => [ 'csrfcookie' => [ 'httponly' => true, 'secure' => secure_cookie, ], ], ... ] ...
注入 csrfcookie 的 httponly 和 secure 属性值
生成secure的 cookie $cookies = yii::$app->response->cookies;$cookies->add(new cookie([ 'name' => 'accesstoken', 'value' => $accesstoken, 'expire' => time() + token::expire_time, 'secure' => secure_cookie ]));
注意更新 cookie 的时候也需要更新 secure 属性
$cookies = yii::$app->request->cookies;if (($cookie = $cookies->get('accesstoken')) !== null) { $cookie->secure = secure_cookie; // 这里很重要, 不然就会丢失 $cookie->expire = time() + token::expire_time; yii::$app->response->cookies->add($cookie);}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product