8000 bug #20342 [Form] Fix UrlType transforms valid protocols (ogizanagi) · symfony/symfony@620ea20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 620ea20

Browse files
committed
bug #20342 [Form] Fix UrlType transforms valid protocols (ogizanagi)
This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes #20342). Discussion ---------- [Form] Fix UrlType transforms valid protocols | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A According to https://tools.ietf.org/html/rfc3986#section-3.1: <details> <summary>`scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )`</summary> > Each URI begins with a scheme name that refers to a specification for assigning identifiers within that scheme. As such, the URI syntax is a federated and extensible naming system wherein each scheme's specification may further restrict the syntax and semantics of identifiers using that scheme. > Scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). Although schemes are case- insensitive, the canonical form is lowercase and documents that specify schemes must do so with lowercase letters. An implementation should accept uppercase letters as equivalent to lowercase in scheme names (e.g., allow "HTTP" as well as "http") for the sake of robustness but should only produce lowercase scheme names for consistency. </details> ~~Fixing the regex to add missing chars could solve the issue. However according to the RFC, we should not allow underscores. But `\w+` permits it (removing it would be a minor BC break anyway).~~ ~~IMHO, we should not care in this listener if the scheme is valid or not (a validator should be used instead), so I'd suggest to simply check if a scheme is provided or not.~~ I'm not using `parse_url($string, PHP_URL_SCHEME)` because `http:/symfony.com` or `http:symfony.com` is considered valid as containing a scheme. Actually, I changed my mind about previous fix (28f816a) and went back to the regex, in order to have strings like `symfony.com?uri=http://example.com` properly transformed to `symfony.com?uri=http://example.com` Commits ------- 46dd3b9 [Form] Fix UrlType transforms valid protocols
2 parents 16b29a1 + 46dd3b9 commit 620ea20

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function onSubmit(FormEvent $event)
3838
{
3939
$data = $event->getData();
4040

41-
if ($this->defaultProtocol && $data && !preg_match('~^\w+://~', $data)) {
41+
if ($this->defaultProtocol && $data && !preg_match('~^[\w+.-]+://~', $data)) {
4242
$event->setData($this->defaultProtocol.'://'.$data);
4343
}
4444
}

src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,28 @@ public function testSkipKnownUrl()
4040
$this->assertEquals('http://www.symfony.com', $event->getData());
4141
}
4242

43-
public function testSkipOtherProtocol()
43+
public function provideUrlsWithSupportedProtocols()
44+
{
45+
return array(
46+
array('ftp://www.symfony.com'),
47+
array('chrome-extension://foo'),
48+
array('h323://foo'),
49+
array('iris.beep://foo'),
50+
array('foo+bar://foo'),
51+
);
52+
}
53+
54+
/**
55+
* @dataProvider provideUrlsWithSupportedProtocols
56+
*/
57+
public function testSkipOtherProtocol($url)
4458
{
45-
$data = 'ftp://www.symfony.com';
4659
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
47-
$event = new FormEvent($form, $data);
60+
$event = new FormEvent($form, $url);
4861

4962
$filter = new FixUrlProtocolListener('http');
5063
$filter->onSubmit($event);
5164

52-
$this->assertEquals('ftp://www.symfony.com', $event->getData());
65+
$this->assertEquals($url, $event->getData());
5366
}
5467
}

0 commit comments

Comments
 (0)
0