Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 01e3b67

Browse files
authored
Merge pull request #634 from beyondcode/fix/prevent-collecting-if-stats-disable
[fix] Prevent collecting stats if disabled
2 parents de7e358 + 9c41cf3 commit 01e3b67

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/API/Controller.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ abstract class Controller implements HttpServerInterface
5151
*/
5252
protected $channelManager;
5353

54+
/**
55+
* The app attached with this request.
56+
*
57+
* @var \BeyondCode\LaravelWebSockets\Apps\App|null
58+
*/
59+
protected $app;
60+
5461
/**
5562
* Initialize the request.
5663
*
@@ -176,8 +183,7 @@ protected function handleRequest(ConnectionInterface $connection)
176183

177184
$laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest));
178185

179-
$this
180-
->ensureValidAppId($laravelRequest->appId)
186+
$this->ensureValidAppId($laravelRequest->get('appId'))
181187
->ensureValidSignature($laravelRequest);
182188

183189
// Invoke the controller action
@@ -220,7 +226,7 @@ protected function sendAndClose(ConnectionInterface $connection, $response)
220226
*/
221227
public function ensureValidAppId($appId)
222228
{
223-
if (! App::findById($appId)) {
229+
if (! $appId || ! $this->app = App::findById($appId)) {
224230
throw new HttpException(401, "Unknown app id `{$appId}` provided.");
225231
}
226232

@@ -252,9 +258,7 @@ protected function ensureValidSignature(Request $request)
252258

253259
$signature = "{$request->getMethod()}\n/{$request->path()}\n".Pusher::array_implode('=', '&', $params);
254260

255-
$app = App::findById($request->get('appId'));
256-
257-
$authSignature = hash_hmac('sha256', $signature, $app->secret);
261+
$authSignature = hash_hmac('sha256', $signature, $this->app->secret);
258262

259263
if ($authSignature !== $request->get('auth_signature')) {
260264
throw new HttpException(401, 'Invalid auth signature provided.');

src/API/TriggerEvent.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public function __invoke(Request $request)
4949
$request->appId, $request->socket_id, $channelName, (object) $payload
5050
);
5151

52-
StatisticsCollector::apiMessage($request->appId);
52+
if ($this->app->statisticsEnabled) {
53+
StatisticsCollector::apiMessage($request->appId);
54+
}
5355

5456
DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [
5557
'event' => $request->name,

src/Server/WebSocketHandler.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public function onOpen(ConnectionInterface $connection)
5656
/** @var \GuzzleHttp\Psr7\Request $request */
5757
$request = $connection->httpRequest;
5858

59-
StatisticsCollector::connection($connection->app->id);
59+
if ($connection->app->statisticsEnabled) {
60+
StatisticsCollector::connection($connection->app->id);
61+
}
6062

6163
$this->channelManager->subscribeToApp($connection->app->id);
6264

@@ -88,7 +90,9 @@ public function onMessage(ConnectionInterface $connection, MessageInterface $mes
8890
$message, $connection, $this->channelManager
8991
)->respond();
9092

91-
StatisticsCollector::webSocketMessage($connection->app->id);
93+
if ($connection->app->statisticsEnabled) {
94+
StatisticsCollector::webSocketMessage($connection->app->id);
95+
}
9296

9397
WebSocketMessageReceived::dispatch(
9498
$connection->app->id,
@@ -109,7 +113,9 @@ public function onClose(ConnectionInterface $connection)
109113
->unsubscribeFromAllChannels($connection)
110114
->then(function (bool $unsubscribed) use ($connection) {
111115
if (isset($connection->app)) {
112-
StatisticsCollector::disconnection($connection->app->id);
116+
if ($connection->app->statisticsEnabled) {
117+
StatisticsCollector::disconnection($connection->app->id);
118+
}
113119

114120
$this->channelManager->unsubscribeFromApp($connection->app->id);
115121

0 commit comments

Comments
 (0)