Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fa8870f

Browse filesBrowse files
committed
[Validator] fix URL validator to detect non supported chars according to RFC 3986
1 parent b0f8a7f commit fa8870f
Copy full SHA for fa8870f

File tree

2 files changed

+8
-5
lines changed
Filter options

2 files changed

+8
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/UrlValidator.php
+7-5Lines changed: 7 additions & 5 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ 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>'),
166167
);
167168
}
168169

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.