从Yii2的Request看其CSRF防范策略
When CSRF validation is enabled, forms submitted to an Yii Web application must be originated
from the same application. If not, a 400 HTTP exception will be raised.
Note, this feature requires that the user client accepts cookie. Also, to use this feature, forms submitted
via POST method must contain a hidden input whose name is specified by [[csrfParam]].
You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input.
In javaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via 'yii.getCsrfParam()' and
'yii.getCsrfToken()', respectively. The [[\yii\web\YiiAsset]] asset must be registerd.
You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]].
meta标签有信息
<meta name="csrf-param" content="_csrf">
<meta name="csrf-token" content="d0hZYjNXdnI5FzAjfBNCQkN/ahBDGiQ4DjAAK1wEAz4bJQgOfGQlXw==">
form表单中有隐藏域
<input type="hidden" value="d0hZYjNXdnI5FzAjfBNCQkN/ahBDGiQ4DjAAK1wEAz4bJQgOfGQlXw==" name="_csrf">
Cookie中有记录
Name(_csrf)
Value(很长的字符串)
为什么表单页面有csrf隐藏域?
yii\helpers\Html,实际在BaseHtml.php中的 beginForm()方法中,找到了生成表单隐藏域的代码。
$hiddenInputs[] = static::hiddenInput($request->csrfParam, $request->getCsrfToken());
为什么表单页,head中有含csrf的meta?
app\views\layouts\main.php中,用了 <?= Html::csrfMetaTags() ?>
yii\helpers\Html中,实际在BaseHtml.php中的 csrfMetaTags()方法中,找到了生成meta标签的代码。
示例:
攻击通过在授权用户访问的页面中包含链接或者脚本的方式工作
************
备注 2017年2月3日周五
之前做项目,用到ajax请求的地方,都是用jquery的$.ajax()去处理。对于这时的post请求,我没有人为加上csrf数据,但是也能通过yii2后端的csrfValidation。
实际是因为我们默认的Asset配置中引入了yii.js,它有个功能,就是把csrf数据放到请求头中,后台校验的时候,如果发现请求头中有csrf数据,也会拿到并校验。
************
When CSRF validation is enabled, forms submitted to an Yii Web application must be originated
from the same application. If not, a 400 HTTP exception will be raised.
Note, this feature requires that the user client accepts cookie. Also, to use this feature, forms submitted
via POST method must contain a hidden input whose name is specified by [[csrfParam]].
You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input.
In javaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via 'yii.getCsrfParam()' and
'yii.getCsrfToken()', respectively. The [[\yii\web\YiiAsset]] asset must be registerd.
You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]].
meta标签有信息
<meta name="csrf-param" content="_csrf">
<meta name="csrf-token" content="d0hZYjNXdnI5FzAjfBNCQkN/ahBDGiQ4DjAAK1wEAz4bJQgOfGQlXw==">
form表单中有隐藏域
<input type="hidden" value="d0hZYjNXdnI5FzAjfBNCQkN/ahBDGiQ4DjAAK1wEAz4bJQgOfGQlXw==" name="_csrf">
Cookie中有记录
Name(_csrf)
Value(很长的字符串)
为什么表单页面有csrf隐藏域?
yii\helpers\Html,实际在BaseHtml.php中的 beginForm()方法中,找到了生成表单隐藏域的代码。
$hiddenInputs[] = static::hiddenInput($request->csrfParam, $request->getCsrfToken());
为什么表单页,head中有含csrf的meta?
app\views\layouts\main.php中,用了 <?= Html::csrfMetaTags() ?>
yii\helpers\Html中,实际在BaseHtml.php中的 csrfMetaTags()方法中,找到了生成meta标签的代码。
示例:
攻击通过在授权用户访问的页面中包含链接或者脚本的方式工作
************
备注 2017年2月3日周五
之前做项目,用到ajax请求的地方,都是用jquery的$.ajax()去处理。对于这时的post请求,我没有人为加上csrf数据,但是也能通过yii2后端的csrfValidation。
实际是因为我们默认的Asset配置中引入了yii.js,它有个功能,就是把csrf数据放到请求头中,后台校验的时候,如果发现请求头中有csrf数据,也会拿到并校验。
************