Open wangming1993 opened 8 years ago
https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options
X-Frame-Options 主要是为了防止点击劫持(clickjacking) _点击劫持(clickjacking)_是一种在网页中将恶意代码等隐藏在看似无害的内容(如按钮)之下,并诱使用户点击的手段。 X-Frame-Options HTTP 头部字段用来指示传输的资源是否可以被包含在 <frame> 或 <iframe> 中,服务端可以声明这个策略来确保自己的网页内容不会被嵌入到其他的页面中. X-Frame-Options 具有3个具体的值:
点击劫持
<frame>
<iframe>
frame
http://www.example.com/dir/page.html
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
添加站点配置:
Header always append X-Frame-Options SAMEORIGIN
添加 http , server 或者 location 配置
http
server
location
add_header X-Frame-Options SAMEORIGIN;
添加到站点的 Web.config
<system.webServer> ... <httpProtocol> <customHeaders> <add name="X-Frame-Options" value="SAMEORIGIN" /> </customHeaders> </httpProtocol> ... </system.webServer>
添加到 frontend, listen, 或者 backend 配置中:
frontend
listen
backend
rspadd X-Frame-Options:\ SAMEORIGIN
Cookie 是浏览器储存在客户端的小的文本文件,用于客户端与服务器之间互传. Web服务器通过指定 http header 的 Set-Cookie, 其结构如下:
http header
Set-Cookie
Set-Cookie: name=value[; expires=date][; domain=domain][; path=path][; secure][; httpOnly]
可以看到,_Cookie_主要包含一下几个字段:
这里主要讲secure 和 httpOnly
secure
httpOnly
用来告诉浏览器不能通过JavaScript的 document.cookie 来访问 cookie, 目的是避免 _跨站脚本攻击 (XSS)_
JavaScript
document.cookie
强制应用通过Https来传输 Cookie
Https
Yii2中的 Cookie yii\web\Cookie默认是 httpOnly的,
yii\web\Cookie
class Cookie extends \yii\base\Object { public $name; public $value = ''; public $domain = ''; public $expire = 0; public $path = '/'; public $secure = false; public $httpOnly = true; }
使用 <?= Html::csrfMetaTags() ?> 就会生成一个 name=_csrf 的 Cookie, 默认是 httpOnly, 通过注入request来改变:
<?= Html::csrfMetaTags() ?>
name=_csrf
request
在 config/main.php 中
config/main.php
... 'components' => [ 'request' => [ 'csrfCookie' => [ 'httpOnly' => true, 'secure' => SECURE_COOKIE, ], ], ... ] ...
注入 csrfCookie 的 httpOnly 和 secure 属性值
csrfCookie
$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); }
让你的web应用更安全
设置X-Frame-Options
X-Frame-Options 主要是为了防止
点击劫持
(clickjacking) _点击劫持(clickjacking)_是一种在网页中将恶意代码等隐藏在看似无害的内容(如按钮)之下,并诱使用户点击的手段。 X-Frame-Options HTTP 头部字段用来指示传输的资源是否可以被包含在<frame>
或<iframe>
中,服务端可以声明这个策略来确保自己的网页内容不会被嵌入到其他的页面中. X-Frame-Options 具有3个具体的值:frame
中frame
嵌入,但不能被不在同一域下面的frame
嵌入. 同源: 协议,域名和端口全部相同,即使是ip与域名对应,也认为是不同域下, 下面说明了具体的情况,与:http://www.example.com/dir/page.html
对比: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
服务器配置
Apache
添加站点配置:
Nginx
添加
http
,server
或者location
配置IIS
添加到站点的 Web.config
HAProxy
添加到
frontend
,listen
, 或者backend
配置中:更安全的Cookie
Cookie 是浏览器储存在客户端的小的文本文件,用于客户端与服务器之间互传. Web服务器通过指定
http header
的Set-Cookie
, 其结构如下:可以看到,_Cookie_主要包含一下几个字段:
这里主要讲
secure
和httpOnly
httpOnly标识
用来告诉浏览器不能通过
JavaScript
的document.cookie
来访问 cookie, 目的是避免 _跨站脚本攻击 (XSS)_secure标识
强制应用通过
Https
来传输 CookiePHP Yii2中设置Cookie的 httpOnly 和 secure
设置 _csrf
Yii2中的 Cookie
yii\web\Cookie
默认是 httpOnly的,使用
<?= Html::csrfMetaTags() ?>
就会生成一个name=_csrf
的 Cookie, 默认是 httpOnly, 通过注入request
来改变:在
config/main.php
中注入
csrfCookie
的 httpOnly 和 secure 属性值生成secure的 Cookie
注意更新 Cookie 的时候也需要更新 secure 属性