Skip to content

Commit a79b5e5

Browse files
MarvinG92chalasr
authored andcommitted
fix redirect uri validation to allow apps like: com.my.app:/
1 parent 7d890a6 commit a79b5e5

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/ValueObject/RedirectUri.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class RedirectUri
1616
*/
1717
public function __construct(string $redirectUri)
1818
{
19-
if (!filter_var($redirectUri, \FILTER_VALIDATE_URL)) {
19+
if (1 !== preg_match('/^[a-zA-Z][a-zA-Z0-9+.-]*:(?:\/\/[^\/\s?#]+(?:\/[^\s?#]*)?|\/[^\s?#]*)?(?:\?[^\s#]*)?(?:#[^\s]*)?$/', $redirectUri)) {
2020
throw new \RuntimeException(\sprintf('The \'%s\' string is not a valid URI.', $redirectUri));
2121
}
2222

tests/Unit/RedirectUriTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace League\Bundle\OAuth2ServerBundle\Tests\Unit;
6+
7+
use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class RedirectUriTest extends TestCase
11+
{
12+
public function exceptionRedirectUriProvider(): array
13+
{
14+
return [
15+
['invalid'],
16+
['http://invalid url'],
17+
['http:/invalid'],
18+
['http:/invalid.com'],
19+
['http:/invalid.com/test'],
20+
];
21+
}
22+
23+
/**
24+
* @dataProvider exceptionRedirectUriProvider
25+
*/
26+
public function testInvalidRedirectUris($data): void
27+
{
28+
$this->expectException(\RuntimeException::class);
29+
30+
new RedirectUri($data[0]);
31+
}
32+
33+
public function testValidRedirectUris(): void
34+
{
35+
// Test standard URIs
36+
$this->assertIsObject(new RedirectUri('http://github.com'));
37+
$this->assertIsObject(new RedirectUri('http://github.com/test'));
38+
$this->assertIsObject(new RedirectUri('http://github.com/test?query=test'));
39+
40+
// Test mobile URIs
41+
$this->assertIsObject(new RedirectUri('com.my.app:/'));
42+
$this->assertIsObject(new RedirectUri('com.my.app:/callback'));
43+
$this->assertIsObject(new RedirectUri('myapp://callback#token=123'));
44+
}
45+
}

0 commit comments

Comments
 (0)