Skip to content

Commit f65875d

Browse files
committed
updates validation
1 parent 745fed1 commit f65875d

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

src/Protocols/Pusher/ClientEvent.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Reverb\Protocols\Pusher;
44

5+
use Illuminate\Support\Facades\Validator;
56
use Illuminate\Support\Str;
67
use Laravel\Reverb\Contracts\Connection;
78

@@ -12,6 +13,12 @@ class ClientEvent
1213
*/
1314
public static function handle(Connection $connection, array $event): ?ClientEvent
1415
{
16+
Validator::make($event, [
17+
'event' => ['required', 'string'],
18+
'channel' => ['required', 'string'],
19+
'data' => ['required', 'array'],
20+
])->validate();
21+
1522
if (! Str::startsWith($event['event'], 'client-')) {
1623
return null;
1724
}

src/Protocols/Pusher/EventHandler.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Reverb\Protocols\Pusher;
44

55
use Exception;
6+
use Illuminate\Support\Facades\Validator;
67
use Illuminate\Support\Str;
78
use Laravel\Reverb\Contracts\Connection;
89
use Laravel\Reverb\Protocols\Pusher\Channels\CacheChannel;
@@ -24,14 +25,11 @@ public function __construct(protected ChannelManager $channels)
2425
*/
2526
public function handle(Connection $connection, string $event, array $payload = []): void
2627
{
27-
match (Str::after($event, 'pusher:')) {
28+
$event = Str::after($event, 'pusher:');
29+
30+
match ($event) {
2831
'connection_established' => $this->acknowledge($connection),
29-
'subscribe' => $this->subscribe(
30-
$connection,
31-
$payload['channel'],
32-
$payload['auth'] ?? null,
33-
$payload['channel_data'] ?? null
34-
),
32+
'subscribe' => $this->subscribe($connection, $payload),
3533
'unsubscribe' => $this->unsubscribe($connection, $payload['channel']),
3634
'ping' => $this->pong($connection),
3735
'pong' => $connection->touch(),
@@ -53,13 +51,19 @@ public function acknowledge(Connection $connection): void
5351
/**
5452
* Subscribe to the given channel.
5553
*/
56-
public function subscribe(Connection $connection, string $channel, ?string $auth = null, ?string $data = null): void
54+
public function subscribe(Connection $connection, array $payload): void
5755
{
56+
$validated = Validator::make($payload, [
57+
'channel' => ['required', 'string'],
58+
'auth' => ['nullable', 'string'],
59+
'channel_data' => ['nullable', 'json'],
60+
])->validate();
61+
5862
$channel = $this->channels
5963
->for($connection->app())
60-
->findOrCreate($channel);
64+
->findOrCreate($channel = $validated['channel']);
6165

62-
$channel->subscribe($connection, $auth, $data);
66+
$channel->subscribe($connection, $validated['auth'] ?? null, $validated['channel_data'] ?? null);
6367

6468
$this->afterSubscribe($channel, $connection);
6569
}

src/Protocols/Pusher/Server.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Laravel\Reverb\Protocols\Pusher\Exceptions\PusherException;
1414
use Ratchet\RFC6455\Messaging\Frame;
1515
use Ratchet\RFC6455\Messaging\FrameInterface;
16+
use Throwable;
1617

1718
class Server
1819
{
@@ -55,29 +56,21 @@ public function message(Connection $from, string $message): void
5556
try {
5657
$event = json_decode($message, associative: true, flags: JSON_THROW_ON_ERROR);
5758

58-
Validator::make($event, [
59-
'event' => ['required', 'string'],
60-
'data' => ['nullable', 'array'],
61-
'channel' => ['nullable', 'string'],
62-
'data.channel' => ['required', 'string'],
63-
'data.auth' => ['nullable', 'string'],
64-
'data.channel_data' => ['nullable', 'json'],
65-
])->validate();
59+
Validator::make($event, ['event' => ['required', 'string']])->validate();
6660

6761
match (Str::startsWith($event['event'], 'pusher:')) {
6862
true => $this->handler->handle(
6963
$from,
7064
$event['event'],
7165
empty($event['data']) ? [] : $event['data'],
72-
$event['channel'] ?? null
7366
),
7467
default => ClientEvent::handle($from, $event)
7568
};
7669

7770
Log::info('Message Handled', $from->id());
7871

7972
MessageReceived::dispatch($from, $message);
80-
} catch (Exception $e) {
73+
} catch (Throwable $e) {
8174
$this->error($from, $e);
8275
}
8376
}
@@ -114,7 +107,7 @@ public function close(Connection $connection): void
114107
/**
115108
* Handle an error.
116109
*/
117-
public function error(Connection $connection, Exception $exception): void
110+
public function error(Connection $connection, Throwable $exception): void
118111
{
119112
if ($exception instanceof PusherException) {
120113
$connection->send(json_encode($exception->payload()));

tests/Unit/Protocols/Pusher/ServerTest.php

-6
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@
331331
]);
332332

333333
it('sends an error if something fails for event type', function () {
334-
335334
$this->server->message(
336335
$connection = new FakeConnection,
337336
json_encode([
@@ -349,7 +348,6 @@
349348
});
350349

351350
it('sends an error if something fails for data type', function () {
352-
353351
$this->server->message(
354352
$connection = new FakeConnection,
355353
json_encode([
@@ -368,7 +366,6 @@
368366
});
369367

370368
it('sends an error if something fails for data channel type', function () {
371-
372369
$this->server->message(
373370
$connection = new FakeConnection,
374371
json_encode([
@@ -407,7 +404,6 @@
407404
});
408405

409406
it('sends an error if something fails for data auth type', function () {
410-
411407
$this->server->message(
412408
$connection = new FakeConnection,
413409
json_encode([
@@ -429,7 +425,6 @@
429425
});
430426

431427
it('sends an error if something fails for data channel_data type', function () {
432-
433428
$this->server->message(
434429
$connection = new FakeConnection,
435430
json_encode([
@@ -472,7 +467,6 @@
472467
});
473468

474469
it('sends an error if something fails for channel type', function () {
475-
476470
$this->server->message(
477471
$connection = new FakeConnection,
478472
json_encode([

0 commit comments

Comments
 (0)