Skip to content

fix redirect uri validation to allow apps like: com.my.app:/ #243

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/ValueObject/RedirectUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RedirectUri
*/
public function __construct(string $redirectUri)
{
if (!filter_var($redirectUri, \FILTER_VALIDATE_URL)) {
if (1 !== preg_match('/^[a-zA-Z][a-zA-Z0-9+.-]*:(?:\/\/[^\/\s?#]+(?:\/[^\s?#]*)?|\/[^\s?#]*)?(?:\?[^\s#]*)?(?:#[^\s]*)?$/', $redirectUri)) {
throw new \RuntimeException(\sprintf('The \'%s\' string is not a valid URI.', $redirectUri));
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/RedirectUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace League\Bundle\OAuth2ServerBundle\Tests\Unit;

use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri;
use PHPUnit\Framework\TestCase;

final class RedirectUriTest extends TestCase
{
public function exceptionRedirectUriProvider(): array
{
return [
['invalid'],
['http://invalid url'],
['http:/invalid'],
['http:/invalid.com'],
['http:/invalid.com/test'],
];
}

/**
* @dataProvider exceptionRedirectUriProvider
*/
public function testInvalidRedirectUris($data): void
{
$this->expectException(\RuntimeException::class);

new RedirectUri($data[0]);
}

public function testValidRedirectUris(): void
{
// Test standard URIs
$this->assertIsObject(new RedirectUri('http://github.com'));
$this->assertIsObject(new RedirectUri('http://github.com/test'));
$this->assertIsObject(new RedirectUri('http://github.com/test?query=test'));

// Test mobile URIs
$this->assertIsObject(new RedirectUri('com.my.app:/'));
$this->assertIsObject(new RedirectUri('com.my.app:/callback'));
$this->assertIsObject(new RedirectUri('myapp://callback#token=123'));
}
}