Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
2deff126
Commit
2deff126
authored
Sep 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Supports sending CSRF token via HTTP header.
parent
2db91187
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
4 deletions
+22
-4
Request.php
framework/yii/web/Request.php
+22
-4
No files found.
framework/yii/web/Request.php
View file @
2deff126
...
...
@@ -68,19 +68,28 @@ use yii\helpers\Security;
class
Request
extends
\yii\base\Request
{
/**
* The name of the HTTP header for sending CSRF token.
*/
const
CSRF_HEADER
=
'X-CSRF-TOKEN'
;
/**
* @var boolean whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to false.
*
By setting this property to true
, forms submitted to an Yii Web application must be originated
*
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 [[csrfVar]].
* You may use [[\yii\web\Html::beginForm()]] to generate his hidden input.
*
* In JavaScript, you may get the values of [[csrfVar]] and [[csrfToken]] via `yii.getCsrfVar()` and
* `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered.
*
* @see http://en.wikipedia.org/wiki/Cross-site_request_forgery
*/
public
$enableCsrfValidation
=
false
;
/**
* @var string the name of the token used to prevent CSRF. Defaults to '_csrf'.
* This property is
effectively
only when [[enableCsrfValidation]] is true.
* This property is
used
only when [[enableCsrfValidation]] is true.
*/
public
$csrfVar
=
'_csrf'
;
/**
...
...
@@ -986,6 +995,14 @@ class Request extends \yii\base\Request
}
/**
* @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent.
*/
public
function
getCsrfTokenFromHeader
()
{
return
isset
(
$_SERVER
[
self
::
CSRF_HEADER
])
?
$_SERVER
[
self
::
CSRF_HEADER
]
:
null
;
}
/**
* Creates a cookie with a randomly generated CSRF token.
* Initial values specified in [[csrfCookie]] will be applied to the generated cookie.
* @return Cookie the generated cookie
...
...
@@ -1012,7 +1029,7 @@ class Request extends \yii\base\Request
}
$method
=
$this
->
getMethod
();
if
(
$method
===
'POST'
||
$method
===
'PUT'
||
$method
===
'PATCH'
||
$method
===
'DELETE'
)
{
$
cookies
=
$this
->
getCookies
(
);
$
trueToken
=
$this
->
getCookies
()
->
getValue
(
$this
->
csrfVar
);
switch
(
$method
)
{
case
'POST'
:
$token
=
$this
->
getPost
(
$this
->
csrfVar
);
...
...
@@ -1027,7 +1044,8 @@ class Request extends \yii\base\Request
$token
=
$this
->
getDelete
(
$this
->
csrfVar
);
}
if
(
empty
(
$token
)
||
$cookies
->
getValue
(
$this
->
csrfVar
)
!==
$token
)
{
$valid
=
!
empty
(
$token
)
&&
$token
===
$trueToken
||
$this
->
getCsrfTokenFromHeader
()
===
$trueToken
;
if
(
!
$valid
)
{
throw
new
HttpException
(
400
,
Yii
::
t
(
'yii'
,
'Unable to verify your data submission.'
));
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment