Browse Source

Fixed UrlValidator pattern to improve matching

tags/2.0.7
SilverFire - Dmitry Naumenko 9 years ago
parent
commit
d12af259e0
  1. 1
      framework/CHANGELOG.md
  2. 3
      framework/validators/UrlValidator.php
  3. 17
      tests/framework/validators/UrlValidatorTest.php

1
framework/CHANGELOG.md

@ -55,6 +55,7 @@ Yii Framework 2 Change Log
- Bug #10580: Fixed `yii\grid\GridView::guessColumns()` to work with numeric column names (silverfire)
- Bug #10625: Fixed `activeForm.js` - when submit doesn't reload page, submit button value simulation with hidden input did not work (andrewnester)
- Bug #10629: Fixed `yii\helpers\BaseStringHelper` - BaseStringHelper::truncateHtml adds suffix regardless of the string length (andrewnester)
- Bug #10751: Fixed `yii\validators\UrlValidator` pattern to improve matching (silverfire)
- Bug: Fixed generation of canonical URLs for `ViewAction` pages (samdark)
- Bug: Fixed `mb_*` functions calls to use `UTF-8` or `Yii::$app->charset` (silverfire)
- Enh #3506: Added `yii\validators\IpValidator` to perform validation of IP addresses and subnets (SilverFire, samdark)

3
framework/validators/UrlValidator.php

@ -9,6 +9,7 @@ namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\web\JsExpression;
use yii\helpers\Json;
@ -28,7 +29,7 @@ class UrlValidator extends Validator
* The pattern may contain a `{schemes}` token that will be replaced
* by a regular expression which represents the [[validSchemes]].
*/
public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i';
public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])/i';
/**
* @var array list of URI schemes which should be considered valid. By default, http and https
* are considered to be valid schemes.

17
tests/framework/validators/UrlValidatorTest.php

@ -28,6 +28,23 @@ class UrlValidatorTest extends TestCase
.'&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr'));
$this->assertFalse($val->validate('ftp://ftp.ruhr-uni-bochum.de/'));
$this->assertFalse($val->validate('http://invalid,domain'));
$this->assertFalse($val->validate('http://example.com,'));
$this->assertFalse($val->validate('http://example.com*12'));
$this->assertTrue($val->validate('http://example.com/*12'));
$this->assertTrue($val->validate('http://example.com/?test'));
$this->assertTrue($val->validate('http://example.com/#test'));
$this->assertTrue($val->validate('http://example.com:80/#test'));
$this->assertTrue($val->validate('http://example.com:65535/#test'));
$this->assertTrue($val->validate('http://example.com:81/?good'));
$this->assertTrue($val->validate('http://example.com?test'));
$this->assertTrue($val->validate('http://example.com#test'));
$this->assertTrue($val->validate('http://example.com:81#test'));
$this->assertTrue($val->validate('http://example.com:81?good'));
$this->assertFalse($val->validate('http://example.com,?test'));
$this->assertFalse($val->validate('http://example.com:?test'));
$this->assertFalse($val->validate('http://example.com:test'));
$this->assertFalse($val->validate('http://example.com:123456/test'));
$this->assertFalse($val->validate('http://äüö?=!"§$%&/()=}][{³²€.edu'));
}

Loading…
Cancel
Save