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

Sets max message size #24

Merged
merged 2 commits into from
Nov 27, 2023
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
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