8000 [Validator] support protocolless urls validation by MyDigitalLife · Pull Request #24308 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] su 8000 pport protocolless urls validation #24308

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

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Url extends Constraint
* @deprecated since Symfony 4.1, to be removed in 5.0
*/
public $checkDNS = self::CHECK_DNS_TYPE_NONE;
public $relativeProtocol = false;

public function __construct($options = null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Validator/Constraints/UrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function validate($value, Constraint $constraint)
return;
}

$pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
$pattern = $constraint->relativeProtocol ? str_replace('(%s):', '(?:(%s):)?', static::PATTERN) : static::PATTERN;
$pattern = sprintf($pattern, implode('|', $constraint->protocols));

if (!preg_match($pattern, $value)) {
$this->context->buildViolation($constraint->message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ public function testValidUrls($url)
$this->assertNoViolation();
}

/**
* @dataProvider getValidRelativeUrls
* @dataProvider getValidUrls
*/
public function testValidRelativeUrl($url)
{
$constraint = new Url(array(
'relativeProtocol' => true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should also test absolute urls are still valid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test has both getValidRelativeUrls and getValidUrls as a data provider. That covers both absolute and relative URL's. Or am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MyDigitalLife nice :) didnt noticed that.

));

$this->validator->validate($url, $constraint);

$this->assertNoViolation();
}

public function getValidRelativeUrls()
{
return array(
array('//google.com'),
array('//symfony.fake/blog/'),
array('//symfony.com/search?type=&q=url+validator'),
);
}

public function getValidUrls()
{
return array(
Expand Down Expand Up @@ -147,6 +171,46 @@ public function testInvalidUrls($url)
->assertRaised();
}

/**
* @dataProvider getInvalidRelativeUrls
* @dataProvider getInvalidUrls
*/
public function testInvalidRelativeUrl($url)
{
$constraint = new Url(array(
'message' => 'myMessage',
'relativeProtocol' => true,
));

$this->validator->validate($url, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$url.'"')
->setCode(Url::INVALID_URL_ERROR)
->assertRaised();
}

public function getInvalidRelativeUrls()
{
return array(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide comment

Any reason why we are not using [] instead of array

Copy link
Member
@javiereguiluz javiereguiluz Sep 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because Symfony coding standards. We can't easily change the existing code from array() to [] ... so using [] only for the new code would be inconsistent and that's why we use array() everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This started as a patch for 2.8 so I needed to use the array keyword to not break code in PHP 5.3 seeing this is supported with Symfony 2.x. I definitely prefer the brackets and could changes it.

array('/google.com'),
array('//goog_le.com'),
array('//google.com::aa'),
array('//google.com:aa'),
array('//127.0.0.1:aa/'),
array('//[::1'),
array('//hello.☎/'),
array('//:password@symfony.com'),
array('//:password@@symfony.com'),
array('//username:passwordsymfony.com'),
array('//usern@me:password@symfony.com'),
array('//example.com/exploit.html?<script>alert(1);</script>'),
array('//example.com/exploit.html?hel lo'),
array('//example.com/exploit.html?not_a%hex'),
array('//'),
);
}

public function getInvalidUrls()
{
return array(
Expand Down
0