Skip to content

Commit

Permalink
Sets max message size (#24)
Browse files Browse the repository at this point in the history
* sets max message size

* Fix code styling
  • Loading branch information
joedixon authored Nov 27, 2023
1 parent 411d4a8 commit 2b6d622
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/reverb.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'secret' => env('PUSHER_APP_SECRET'),
'allowed_origins' => ['*'],
'ping_interval' => env('REVERB_APP_PING_INTERVAL', 5),
'max_message_size' => env('REVERB_APP_MAX_MESSAGE_SIZE', 10000),
],
],

Expand Down
11 changes: 10 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public function __construct(
protected string $key,
protected string $secret,
protected int $pingInterval,
protected array $allowedOrigins
protected array $allowedOrigins,
protected int $maxMessageSize,
) {
}

Expand Down Expand Up @@ -52,4 +53,12 @@ public function pingInterval(): int
{
return $this->pingInterval;
}

/**
* Get the maximum message size allowed from the client.
*/
public function maxMessageSize(): int
{
return $this->maxMessageSize;
}
}
1 change: 1 addition & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function find(string $key, $value): Application
$app['secret'],
$app['ping_interval'],
$app['allowed_origins'],
$app['max_message_size'],
);
}
}
1 change: 1 addition & 0 deletions src/Servers/Reverb/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __invoke(RequestInterface $request, WsConnection $connection, st
return;
}

$connection->withMaxMessageSize($reverbConnection->app()->maxMessageSize());
$connection->onMessage(fn (string $message) => $this->server->message($reverbConnection, $message));
$connection->onClose(fn () => $this->server->close($reverbConnection));
$connection->openBuffer();
Expand Down
16 changes: 16 additions & 0 deletions src/WebSockets/WsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class WsConnection extends EventEmitter
*/
protected $onClose;

/**
* The maximum number of allowed bytes for each message.
*
* @var int
*/
protected $maxMessageSize;

public function __construct(public Connection $connection)
{
//
Expand All @@ -45,6 +52,7 @@ public function openBuffer(): void
{
$this->buffer = new MessageBuffer(
new CloseFrameChecker,
maxMessagePayloadSize: $this->maxMessageSize,
onMessage: $this->onMessage ?: fn () => null,
onControl: fn (FrameInterface $message) => $this->control($message),
sender: [$this->connection, 'send']
Expand Down Expand Up @@ -94,6 +102,14 @@ public function onClose(callable $callback): void
$this->onClose = $callback;
}

/**
* Set the maximum number of allowed bytes for each message from the client.
*/
public function withMaxMessageSize(int $size): void
{
$this->maxMessageSize = $size;
}

/**
* Close the connection.
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/Reverb/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@
$this->connect();
})->throws('Connection closed before handshake');

it('limits the size of messages', function () {
$connection = $this->connect(key: 'pusher-key-3', headers: ['Origin' => 'http://laravel.com']);
$message = $this->send(['This message is waaaaaay longer than the 1 byte limit'], $connection);

expect($message)->toBe('Maximum message size exceeded');
});

it('clears application state between requests', function () {
$this->subscribe('test-channel');

Expand Down
6 changes: 6 additions & 0 deletions tests/ReverbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function defineEnvironment($app)
'capacity' => null,
'allowed_origins' => ['*'],
'ping_interval' => 10,
'max_message_size' => 1000000,
]);

$app['config']->set('reverb.apps.apps.2', [
Expand All @@ -76,6 +77,7 @@ protected function defineEnvironment($app)
'capacity' => null,
'allowed_origins' => ['laravel.com'],
'ping_interval' => 10,
'max_message_size' => 1,
]);
}

Expand Down Expand Up @@ -177,6 +179,10 @@ public function send(array $message, WebSocket $connection = null): string
$promise->resolve((string) $message);
});

$connection->on('close', function ($code, $message) use ($promise) {
$promise->resolve((string) $message);
});

$connection->send(json_encode($message));

return await($promise->promise());
Expand Down

0 comments on commit 2b6d622

Please sign in to comment.