Skip to content

Commit 72ce732

Browse files
committed
[Validator] fix URL validator to detect non supported chars according to RFC 3986
1 parent b0f8a7f commit 72ce732

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Symfony/Component/Validator/Constraints/UrlValidator.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*/
2121
class UrlValidator extends ConstraintValidator
2222
{
23-
const PATTERN = '~^
24-
(%s):// # protocol
23+
const PATTERN = '<^
24+
(%protocol%):// # protocol
2525
(([\pL\pN-]+:)?([\pL\pN-]+)@)? # basic auth
2626
(
2727
([\pL\pN\pS-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
@@ -33,8 +33,10 @@ class UrlValidator extends ConstraintValidator
3333
\] # an IPv6 address
3434
)
3535
(:[0-9]+)? # a port (optional)
36-
(/?|/\S+|\?\S*|\#\S*) # a /, nothing, a / with something, a query or a fragment
37-
$~ixu';
36+
(?:/ (?:[\pL\pN\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )* # a path
37+
(?:\? (?:[\pL\pN\-._~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a query (optional)
38+
(?:\# (?:[\pL\pN\-._~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a fragment (optional)
39+
$>ixu';
3840

3941
/**
4042
* {@inheritdoc}
@@ -58,7 +60,7 @@ public function validate($value, Constraint $constraint)
5860
return;
5961
}
6062

61-
$pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
63+
$pattern = str_replace('%protocol%', implode('|', $constraint->protocols), static::PATTERN);
6264

6365
if (!preg_match($pattern, $value)) {
6466
$this->context->buildViolation($constraint->message)

src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ public function getInvalidUrls()
163163
array('http://:password@@symfony.com'),
164164
array('http://username:passwordsymfony.com'),
165165
array('http://usern@me:password@symfony.com'),
166+
array('http://example.com/exploit.html?<script>alert(1);</script>'),
167+
array('http://example.com/exploit.html?hel lo'),
166168
);
167169
}
168170

0 commit comments

Comments
 (0)