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 5fe210c

Browse filesBrowse files
author
Emmanuel BORGES
committed
allow brackets in the optional query string
1 parent 19c6639 commit 5fe210c
Copy full SHA for 5fe210c

File tree

4 files changed

+24
-2
lines changed
Filter options

4 files changed

+24
-2
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ CHANGELOG
99
* added option `allowNull` to NotBlank constraint
1010
* added `Json` constraint
1111
* added `Unique` constraint
12-
12+
* Added the `allowBrackets` option to the `Url` constraint to allow brackets in the optional query string.
13+
1314
4.2.0
1415
-----
1516

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Url.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Url extends Constraint
9494

9595
public $message = 'This value is not a valid URL.';
9696

97+
public $allowBrackets = false;
9798
/**
9899
* @deprecated since Symfony 4.1
99100
*/

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/UrlValidator.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ public function validate($value, Constraint $constraint)
6262
return;
6363
}
6464

65-
$pattern = $constraint->relativeProtocol ? str_replace('(%s):', '(?:(%s):)?', static::PATTERN) : static::PATTERN;
65+
$pattern = static::PATTERN;
66+
if ($constraint->allowBrackets) {
67+
$pattern = str_replace('(?:\? (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )?', '(?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )?', $pattern);
68+
}
69+
70+
$pattern = $constraint->relativeProtocol ? str_replace('(%s):', '(?:(%s):)?', $pattern) : $pattern;
6671
$pattern = sprintf($pattern, implode('|', $constraint->protocols));
6772

6873
if (!preg_match($pattern, $value)) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ public function testInvalidUrls($url)
171171
->assertRaised();
172172
}
173173

174+
public function testUrlWithAllowedExtraChars()
175+
{
176+
$constraint = new Url([
177+
'message' => 'myMessage',
178+
'allowBrackets' => true,
179+
]);
180+
181+
$url = 'http://example.com/exploit.html?test[0]=symfony';
182+
$this->validator->validate($url, $constraint);
183+
184+
$this->assertNoViolation();
185+
}
186+
174187
/**
175188
* @dataProvider getInvalidRelativeUrls
176189
* @dataProvider getInvalidUrls
@@ -206,6 +219,7 @@ public function getInvalidRelativeUrls()
206219
['//usern@me:password@symfony.com'],
207220
['//example.com/exploit.html?<script>alert(1);</script>'],
208221
['//example.com/exploit.html?hel lo'],
222+
['//example.com/exploit.html?test[0]=symfony'],
209223
['//example.com/exploit.html?not_a%hex'],
210224
['//'],
211225
];
@@ -233,6 +247,7 @@ public function getInvalidUrls()
233247
['http://usern@me:password@symfony.com'],
234248
['http://example.com/exploit.html?<script>alert(1);</script>'],
235249
['http://example.com/exploit.html?hel lo'],
250+
['http://example.com/exploit.html?test[0]=symfony'],
236251
['http://example.com/exploit.html?not_a%hex'],
237252
['http://'],
238253
];

0 commit comments

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