Fix #17878: Detect CORS AJAX requests without X-Requested-With in Request::getIsAjax()

This commit is contained in:
Igor Tarasov
2020-03-24 21:01:52 +05:00
committed by GitHub
parent 4b6d3c0290
commit 7f88acb313
4 changed files with 29 additions and 4 deletions

View File

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.33 under development
------------------------
- Bug #17878: Detect CORS AJAX requests without `X-Requested-With` in `Request::getIsAjax()` (dicrtarasov, samdark)
- Enh #17929: Actions can now have bool typed params bound (alex-code)
- Enh #17827: Add `StringValidator::$strict` that can be turned off to allow any scalars (adhayward, samdark)
- Bug #16145: Fix `Html` helper `checkboxList()`, `radioList()`, `renderSelectOptions()`, `dropDownList()`, `listBox()` methods to work properly with traversable selection (samdark)

View File

@ -471,8 +471,8 @@ class Request extends \yii\base\Request
/**
* Returns whether this is an AJAX (XMLHttpRequest) request.
*
* Note that jQuery doesn't set the header in case of cross domain
* requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header
* Note that in case of cross domain requests, browser doesn't set the X-Requested-With header by default:
* https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header
*
* In case you are using `fetch()`, pass header manually:
*
@ -487,7 +487,13 @@ class Request extends \yii\base\Request
*/
public function getIsAjax()
{
return $this->headers->get('X-Requested-With') === 'XMLHttpRequest';
$origin = $this->headers->get('Origin');
return
($this->headers->get('X-Requested-With') === 'XMLHttpRequest') ||
($this->headers->get('Sec-Fetch-Mode') === 'cors') ||
($this->headers->get('Sec-Fetch-Site') === 'cross-site') ||
($origin !== null && $origin !== $this->getHostInfo());
}
/**