-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add CSRF validation by custom HTTP header #20280
Open
olegbaturin
wants to merge
3
commits into
yiisoft:master
Choose a base branch
from
olegbaturin:20279-custom-csrf-header
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -42,7 +42,7 @@ | |||||
* not available. | ||||||
* @property-read CookieCollection $cookies The cookie collection. | ||||||
* @property-read string $csrfToken The token used to perform CSRF validation. | ||||||
* @property-read string|null $csrfTokenFromHeader The CSRF token sent via [[CSRF_HEADER]] by browser. Null is | ||||||
* @property-read string|null $csrfTokenFromHeader The CSRF token sent via [[csrfHeader]] by browser. Null is | ||||||
* returned if no such header is sent. | ||||||
* @property-read array $eTags The entity tags. | ||||||
* @property-read HeaderCollection $headers The header collection. | ||||||
|
@@ -91,7 +91,7 @@ | |||||
class Request extends \yii\base\Request | ||||||
{ | ||||||
/** | ||||||
* The name of the HTTP header for sending CSRF token. | ||||||
* Default name of the HTTP header for sending CSRF token. | ||||||
*/ | ||||||
const CSRF_HEADER = 'X-CSRF-Token'; | ||||||
/** | ||||||
|
@@ -113,10 +113,40 @@ | |||||
* `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. | ||||||
* You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. | ||||||
* | ||||||
* For SPA, you can use CSRF validation by custom header with a random or an empty value. | ||||||
* Include a header with the name specified by [[csrfHeader]] to requests that must be validated. | ||||||
* Warning! CSRF validation by custom header can be used only for same-origin requests or | ||||||
* with CORS configured to allow requests from the list of specific origins only. | ||||||
* | ||||||
* @see Controller::enableCsrfValidation | ||||||
* @see https://en.wikipedia.org/wiki/Cross-site_request_forgery | ||||||
*/ | ||||||
public $enableCsrfValidation = true; | ||||||
/** | ||||||
* @var string the name of the HTTP header for sending CSRF token. Defaults [[CSRF_HEADER]]. | ||||||
* This property can be changed for Yii API applications only. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* Don't change this property for Yii Web application. | ||||||
*/ | ||||||
public $csrfHeader = self::CSRF_HEADER; | ||||||
/** | ||||||
* @var array the name of the HTTP header for sending CSRF token. | ||||||
* by default validate CSRF token on non-"safe" methods only | ||||||
* This property is used only when [[enableCsrfValidation]] is true. | ||||||
* @see https://tools.ietf.org/html/rfc2616#section-9.1.1 | ||||||
*/ | ||||||
public $csrfTokenSafeMethods = ['GET', 'HEAD', 'OPTIONS']; | ||||||
/** | ||||||
* @var array "unsafe" methods not triggered a CORS-preflight request | ||||||
* This property is used only when both [[enableCsrfValidation]] and [[validateCsrfHeaderOnly]] are true. | ||||||
* @see https://fetch.spec.whatwg.org/#http-cors-protocol | ||||||
*/ | ||||||
public $csrfHeaderUnafeMethods = ['GET', 'HEAD', 'POST']; | ||||||
/** | ||||||
* @var bool whether to use custom header only to CSRF validation of SPA. Defaults to false. | ||||||
* If false and [[enableCsrfValidation]] is true, CSRF validation by token will used. | ||||||
* @see https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#employing-custom-request-headers-for-ajaxapi | ||||||
*/ | ||||||
public $validateCsrfHeaderOnly = false; | ||||||
/** | ||||||
* @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. | ||||||
* This property is used only when [[enableCsrfValidation]] is true. | ||||||
|
@@ -1772,10 +1802,14 @@ | |||||
* along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. | ||||||
* @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time | ||||||
* this method is called, a new CSRF token will be generated and persisted (in session or cookie). | ||||||
* @return string the token used to perform CSRF validation. | ||||||
* @return null|string the token used to perform CSRF validation. Null is returned if the [[validateCsrfHeaderOnly]] is true. | ||||||
*/ | ||||||
public function getCsrfToken($regenerate = false) | ||||||
{ | ||||||
if ($this->validateCsrfHeaderOnly) { | ||||||
return null; | ||||||
} | ||||||
|
||||||
if ($this->_csrfToken === null || $regenerate) { | ||||||
$token = $this->loadCsrfToken(); | ||||||
if ($regenerate || empty($token)) { | ||||||
|
@@ -1819,11 +1853,11 @@ | |||||
} | ||||||
|
||||||
/** | ||||||
* @return string|null the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. | ||||||
* @return string|null the CSRF token sent via [[csrfHeader]] by browser. Null is returned if no such header is sent. | ||||||
*/ | ||||||
public function getCsrfTokenFromHeader() | ||||||
{ | ||||||
return $this->headers->get(static::CSRF_HEADER); | ||||||
return $this->headers->get($this->csrfHeader); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -1860,8 +1894,14 @@ | |||||
public function validateCsrfToken($clientSuppliedToken = null) | ||||||
{ | ||||||
$method = $this->getMethod(); | ||||||
// only validate CSRF token on non-"safe" methods https://tools.ietf.org/html/rfc2616#section-9.1.1 | ||||||
if (!$this->enableCsrfValidation || in_array($method, ['GET', 'HEAD', 'OPTIONS'], true)) { | ||||||
|
||||||
if ($this->validateCsrfHeaderOnly) { | ||||||
return in_array($method, $this->csrfHeaderUnafeMethods, true) | ||||||
? $this->headers->has($this->csrfHeader) | ||||||
: true; | ||||||
} | ||||||
|
||||||
if (!$this->enableCsrfValidation || in_array($method, $this->csrfTokenSafeMethods, true)) { | ||||||
return true; | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.