Prevent redirection with new line character in the route (#19800)

* Prevent redirection with new line character in the route

* Remove escape

* Remove import

* Changelog

* Remove \
This commit is contained in:
Bizley
2023-03-31 14:40:31 +02:00
committed by GitHub
parent aa1e4432f1
commit 87f05afb8a
3 changed files with 19 additions and 6 deletions

View File

@ -23,6 +23,7 @@ Yii Framework 2 Change Log
- Bug #19735: Fix `yii\validators\NumberValidator` to use programmable message for the value validation (bizley)
- Bug #19770: Fix `yii\mutex\MysqlMutex` `keyPrefix` expression param binding (kamarton)
- Enh #19794: Add caching in `yii\web\Request` for `getUserIP()` and `getSecureForwardedHeaderTrustedParts()` (rhertogh)
- Bug #19795: Fix `yii\web\Response::redirect()` to prevent setting headers with URL containing new line character (bizley)
2.0.47 November 18, 2022
------------------------

View File

@ -10,6 +10,7 @@ namespace yii\web;
use Yii;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\base\InvalidRouteException;
use yii\helpers\FileHelper;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
@ -886,12 +887,13 @@ class Response extends \yii\base\Response
}
$request = Yii::$app->getRequest();
$normalizedUrl = Url::to($url);
if (
$normalizedUrl !== null
&& strncmp($normalizedUrl, '/', 1) === 0
&& strncmp($normalizedUrl, '//', 2) !== 0
) {
$normalizedUrl = $request->getHostInfo() . $normalizedUrl;
if ($normalizedUrl !== null) {
if (preg_match('/\n/', $normalizedUrl)) {
throw new InvalidRouteException('Route with new line character detected "' . $normalizedUrl . '".');
}
if (strncmp($normalizedUrl, '/', 1) === 0 && strncmp($normalizedUrl, '//', 2) !== 0) {
$normalizedUrl = $request->getHostInfo() . $normalizedUrl;
}
}
if ($checkAjax && $request->getIsAjax()) {