Skip to content
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

[1.x] Add support for wildcard allowed origins #233

Merged
merged 5 commits into from
Aug 1, 2024
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
8 changes: 6 additions & 2 deletions src/Protocols/Pusher/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ protected function verifyOrigin(Connection $connection): void

$origin = parse_url($connection->origin(), PHP_URL_HOST);

if (! $origin || ! in_array($origin, $allowedOrigins)) {
throw new InvalidOrigin;
foreach ($allowedOrigins as $allowedOrigin) {
if (Str::is($allowedOrigin, $origin)) {
return;
}
}

throw new InvalidOrigin;
}
}
12 changes: 3 additions & 9 deletions tests/FakeConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class FakeConnection extends BaseConnection
/**
* Create a new fake connection instance.
*/
public function __construct(?string $identifier = null)
public function __construct(?string $identifier = null, ?string $origin = null)
{
if ($identifier) {
$this->identifier = $identifier;
}

$this->origin = $origin ?? 'http://localhost';
}

/**
Expand Down Expand Up @@ -69,14 +71,6 @@ public function app(): Application
return app()->make(ApplicationProvider::class)->findByKey('reverb-key');
}

/**
* Get the origin of the connection.
*/
public function origin(): string
{
return 'http://localhost';
}

/**
* Set the connection last seen at timestamp.
*/
Expand Down
40 changes: 31 additions & 9 deletions tests/Unit/Protocols/Pusher/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@
->with($connection);
});

it('it rejects a connection from an invalid origin', function () {
$this->app['config']->set('reverb.apps.apps.0.allowed_origins', ['laravel.com']);
$this->server->open($connection = new FakeConnection);
it('it rejects a connection from an invalid origin', function (string $origin, array $allowedOrigins) {
$this->app['config']->set('reverb.apps.apps.0.allowed_origins', $allowedOrigins);
$this->server->open($connection = new FakeConnection(origin: $origin));

$connection->assertReceived([
'event' => 'pusher:error',
Expand All @@ -293,11 +293,24 @@
'message' => 'Origin not allowed',
]),
]);
});

it('accepts a connection from an valid origin', function () {
$this->app['config']->set('reverb.apps.0.allowed_origins', ['localhost']);
$this->server->open($connection = new FakeConnection);
})->with([
'localhost' => [
'http://localhost',
['laravel.com'],
],
'subdomain' => [
'http://sub.laravel.com',
['laravel.com'],
],
'wildcard' => [
'http://laravel.com',
['*.laravel.com'],
],
]);

it('accepts a connection from an valid origin', function (string $origin, array $allowedOrigins) {
$this->app['config']->set('reverb.apps.apps.0.allowed_origins', $allowedOrigins);
$this->server->open($connection = new FakeConnection(origin: $origin));

$connection->assertReceived([
'event' => 'pusher:connection_established',
Expand All @@ -306,4 +319,13 @@
'activity_timeout' => 30,
]),
]);
});
})->with([
'localhost' => [
'http://localhost',
['localhost'],
],
'wildcard' => [
'http://sub.localhost',
['localhost', '*.localhost'],
],
]);