Skip to content

[Form] Do not fix URL protocol for relative URLs #42253

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
Jan 31, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function onSubmit(FormEvent $event)
{
$data = $event->getData();

if ($this->defaultProtocol && $data && \is_string($data) && !preg_match('~^([\w+.-]+://|[^:/?@#]++@)~', $data)) {
if ($this->defaultProtocol && $data && \is_string($data) && !preg_match('~^(?:[/.]|[\w+.-]+://|[^:/?@#]++@)~', $data)) {
$event->setData($this->defaultProtocol.'://'.$data);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,25 @@

class FixUrlProtocolListenerTest extends TestCase
{
/**
* @dataProvider provideUrlToFix
*/
public function testFixUrl($data)
Copy link
Member

Choose a reason for hiding this comment

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

I suggest avoiding to swap the test and its provider, to avoid merge conflicts between branches

Copy link
Member

Choose a reason for hiding this comment

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

I'm going to deal with the conflicts :)

{
$form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
$event = new FormEvent($form, $data);

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

$this->assertSame('http://'.$data, $event->getData());
}

public function provideUrlToFix()
{
return [
['www.symfony.com'],
['symfony.com/doc'],
['twitter.com/@symfony'],
['symfony.com?foo@bar'],
['symfony.com#foo@bar'],
Expand All @@ -32,17 +47,17 @@ public function provideUrlToFix()
}

/**
* @dataProvider provideUrlToFix
* @dataProvider provideUrlToSkip
*/
public function testFixUrl($data)
public function testSkipUrl($url)
{
$form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
$event = new FormEvent($form, $data);
$event = new FormEvent($form, $url);

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

$this->assertEquals('http://'.$data, $event->getData());
$this->assertSame($url, $event->getData());
}

public function provideUrlToSkip()
Expand All @@ -56,20 +71,9 @@ public function provideUrlToSkip()
['iris.beep://foo'],
['foo+bar://foo'],
['fabien@symfony.com'],
['//relative/url'],
['/relative/url'],
['./relative/url'],
];
}

/**
* @dataProvider provideUrlToSkip
*/
public function testSkipUrl($url)
{
$form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
$event = new FormEvent($form, $url);

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

$this->assertEquals($url, $event->getData());
}
}