File size: 12,944 Bytes
352fe2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
diff --git a/src/Protocols/Pusher/Http/Controllers/Controller.php b/src/Protocols/Pusher/Http/Controllers/Controller.php
index a583cf53..f92643fc 100644
--- a/src/Protocols/Pusher/Http/Controllers/Controller.php
+++ b/src/Protocols/Pusher/Http/Controllers/Controller.php
@@ -48,6 +48,7 @@ public function verify(RequestInterface $request, Connection $connection, $appId
 
         $this->setApplication($appId);
         $this->setChannels();
+        $this->verifySignature($request);
     }
 
     /**
@@ -100,8 +101,9 @@ protected function verifySignature(RequestInterface $request): void
         ]);
 
         $signature = hash_hmac('sha256', $signature, $this->application->secret());
+        $authSignature = $this->query['auth_signature'] ?? '';
 
-        if ($signature !== $this->query['auth_signature']) {
+        if ($signature !== $authSignature) {
             throw new HttpException(401, 'Authentication signature invalid.');
         }
     }
diff --git a/tests/Feature/Protocols/Pusher/Reverb/ChannelControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/ChannelControllerTest.php
index ea810525..5bf6dc57 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/ChannelControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/ChannelControllerTest.php
@@ -1,6 +1,7 @@
 <?php
 
 use Laravel\Reverb\Tests\ReverbTestCase;
+use React\Http\Message\ResponseException;
 
 use function React\Async\await;
 
@@ -141,3 +142,9 @@
 
     expect($response->getHeader('Content-Length'))->toBe(['40']);
 });
+
+it('fails when using an invalid signature', function () {
+    $response = await($this->request('channels/test-channel-one?info=user_count,subscription_count,cache'));
+
+    expect($response->getStatusCode())->toBe(401);
+})->throws(ResponseException::class, exceptionCode: 401);
diff --git a/tests/Feature/Protocols/Pusher/Reverb/ChannelUsersControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/ChannelUsersControllerTest.php
index 671b9b87..2fc46ae1 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/ChannelUsersControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/ChannelUsersControllerTest.php
@@ -13,11 +13,11 @@
 it('returns an error when presence channel not provided', function () {
     subscribe('test-channel');
     await($this->signedRequest('channels/test-channel/users'));
-})->throws(ResponseException::class);
+})->throws(ResponseException::class, exceptionCode: 400);
 
 it('returns an error when unoccupied channel provided', function () {
     await($this->signedRequest('channels/presence-test-channel/users'));
-})->throws(ResponseException::class);
+})->throws(ResponseException::class, exceptionCode: 404);
 
 it('returns the user data', function () {
     $channel = app(ChannelManager::class)
@@ -53,13 +53,13 @@
     subscribe('test-channel');
 
     await($this->signedRequest('channels/test-channel/users'));
-})->throws(ResponseException::class);
+})->throws(ResponseException::class, exceptionCode: 400);
 
 it('returns an error when gathering unoccupied channel provided', function () {
     $this->usingRedis();
 
     await($this->signedRequest('channels/presence-test-channel/users'));
-})->throws(ResponseException::class);
+})->throws(ResponseException::class, exceptionCode: 404);
 
 it('can send the content-length header', function () {
     $channel = app(ChannelManager::class)
@@ -120,3 +120,9 @@
 
     expect($response->getHeader('Content-Length'))->toBe(['38']);
 });
+
+it('fails when using an invalid signature', function () {
+    $response = await($this->request('channels/presence-test-channel/users'));
+
+    expect($response->getStatusCode())->toBe(401);
+})->throws(ResponseException::class, exceptionCode: 401);
diff --git a/tests/Feature/Protocols/Pusher/Reverb/ChannelsControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/ChannelsControllerTest.php
index 1eee2f75..1634e8f2 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/ChannelsControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/ChannelsControllerTest.php
@@ -2,6 +2,7 @@
 
 use Illuminate\Support\Arr;
 use Laravel\Reverb\Tests\ReverbTestCase;
+use React\Http\Message\ResponseException;
 
 use function React\Async\await;
 
@@ -122,3 +123,9 @@
 
     expect($response->getHeader('Content-Length'))->toBe(['81']);
 });
+
+it('fails when using an invalid signature', function () {
+    $response = await($this->request('channels?info=user_count'));
+
+    expect($response->getStatusCode())->toBe(401);
+})->throws(ResponseException::class, exceptionCode: 401);
diff --git a/tests/Feature/Protocols/Pusher/Reverb/ConnectionsControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/ConnectionsControllerTest.php
index 675c4be5..cd9b9146 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/ConnectionsControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/ConnectionsControllerTest.php
@@ -1,6 +1,7 @@
 <?php
 
 use Laravel\Reverb\Tests\ReverbTestCase;
+use React\Http\Message\ResponseException;
 
 use function React\Async\await;
 
@@ -71,3 +72,9 @@
 
     expect($response->getHeader('Content-Length'))->toBe(['17']);
 });
+
+it('fails when using an invalid signature', function () {
+    $response = await($this->request('connections'));
+
+    expect($response->getStatusCode())->toBe(401);
+})->throws(ResponseException::class, exceptionCode: 401);
diff --git a/tests/Feature/Protocols/Pusher/Reverb/EventsBatchControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/EventsBatchControllerTest.php
index efa6c631..ee6a4554 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/EventsBatchControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/EventsBatchControllerTest.php
@@ -171,3 +171,15 @@
 
     expect($response->getHeader('Content-Length'))->toBe(['12']);
 });
+
+it('fails when using an invalid signature', function () {
+    $response = await($this->postRequest('batch_events', ['batch' => [
+        [
+            'name' => 'NewEvent',
+            'channel' => 'test-channel',
+            'data' => json_encode(['some' => 'data']),
+        ],
+    ]]));
+
+    expect($response->getStatusCode())->toBe(401);
+})->throws(ResponseException::class, exceptionCode: 401);
diff --git a/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php
index a34487c0..a747e286 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php
@@ -182,7 +182,7 @@
         'name' => 'NewEvent',
         'channel' => 'test-channel',
         'data' => json_encode([str_repeat('a', 10_100)]),
-    ], appId: '654321'));
+    ], appId: '654321', key: 'reverb-key-2', secret: 'reverb-secret-2'));
 
     expect($response->getStatusCode())->toBe(200);
     expect($response->getBody()->getContents())->toBe('{}');
@@ -207,3 +207,13 @@
 
     expect($response->getHeader('Content-Length'))->toBe(['2']);
 });
+
+it('fails when using an invalid signature', function () {
+    $response = await($this->postRequest('events', [
+        'name' => 'NewEvent',
+        'channel' => 'test-channel',
+        'data' => json_encode(['some' => 'data']),
+    ]));
+
+    expect($response->getStatusCode())->toBe(401);
+})->throws(ResponseException::class, exceptionCode: 401);
diff --git a/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php b/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php
index 3b7cb9fa..86a5b8dc 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/ServerTest.php
@@ -465,7 +465,7 @@
         'name' => 'NewEvent',
         'channel' => 'test-channel',
         'data' => json_encode([str_repeat('a', 150_000)]),
-    ], appId: '654321'));
+    ], appId: '654321', key: 'reverb-key-2', secret: 'reverb-secret-2'));
 
     expect($response->getStatusCode())->toBe(200);
     expect($response->getBody()->getContents())->toBe('{}');
diff --git a/tests/Feature/Protocols/Pusher/Reverb/UsersTerminateControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/UsersTerminateControllerTest.php
index 99851799..5012966b 100644
--- a/tests/Feature/Protocols/Pusher/Reverb/UsersTerminateControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/UsersTerminateControllerTest.php
@@ -9,7 +9,7 @@
 
 it('returns an error when connection cannot be found', function () {
     await($this->signedPostRequest('channels/users/not-a-user/terminate_connections'));
-})->throws(ResponseException::class);
+})->throws(ResponseException::class, exceptionCode: 404);
 
 it('unsubscribes from all channels and terminates a user', function () {
     $connection = connect();
@@ -54,3 +54,9 @@
     expect(collect(channels()->all())->get('test-channel-two')->connections())->toHaveCount(1);
     expect($response->getHeader('Content-Length'))->toBe(['2']);
 });
+
+it('fails when using an invalid signature', function () {
+    $response = await($this->postRequest('users/987/terminate_connections'));
+
+    expect($response->getStatusCode())->toBe(401);
+})->throws(ResponseException::class, exceptionCode: 401);
diff --git a/tests/ReverbTestCase.php b/tests/ReverbTestCase.php
index a8ee272b..00b09c96 100644
--- a/tests/ReverbTestCase.php
+++ b/tests/ReverbTestCase.php
@@ -160,16 +160,29 @@ public function requestWithoutAppId(string $path, string $method = 'GET', mixed
     /**
      * Send a signed request to the server.
      */
-    public function signedRequest(string $path, string $method = 'GET', mixed $data = '', string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface
+    public function signedRequest(string $path, string $method = 'GET', mixed $data = '', string $host = '0.0.0.0', string $port = '8080', string $appId = '123456', string $key = 'reverb-key', string $secret = 'reverb-secret'): PromiseInterface
     {
-        $hash = md5(json_encode($data));
         $timestamp = time();
-        $query = "auth_key=reverb-key&auth_timestamp={$timestamp}&auth_version=1.0&body_md5={$hash}";
-        $string = "POST\n/apps/{$appId}/{$path}\n$query";
-        $signature = hash_hmac('sha256', $string, 'reverb-secret');
-        $path = Str::contains($path, '?') ? "{$path}&{$query}" : "{$path}?{$query}";
 
-        return $this->request("{$path}&auth_signature={$signature}", $method, $data, $host, $port, $appId);
+        $query = Str::contains($path, '?') ? Str::after($path, '?') : '';
+        $auth = "auth_key={$key}&auth_timestamp={$timestamp}&auth_version=1.0";
+        $query = $query ? "{$query}&{$auth}" : $auth;
+
+        $query = explode('&', $query);
+        sort($query);
+        $query = implode('&', $query);
+        
+        $path = Str::before($path, '?');
+
+        if ($data) {
+            $hash = md5(json_encode($data));
+            $query .= "&body_md5={$hash}";
+        }
+
+        $string = "{$method}\n/apps/{$appId}/{$path}\n$query";
+        $signature = hash_hmac('sha256', $string, $secret);
+
+        return $this->request("{$path}?{$query}&auth_signature={$signature}", $method, $data, $host, $port, $appId);
     }
 
     /**
@@ -183,30 +196,8 @@ public function postRequest(string $path, ?array $data = [], string $host = '0.0
     /**
      * Send a signed POST request to the server.
      */
-    public function signedPostRequest(string $path, ?array $data = [], string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface
+    public function signedPostRequest(string $path, ?array $data = [], string $host = '0.0.0.0', string $port = '8080', string $appId = '123456', $key = 'reverb-key', $secret = 'reverb-secret'): PromiseInterface
     {
-        $hash = md5(json_encode($data));
-        $timestamp = time();
-        $query = "auth_key=reverb-key&auth_timestamp={$timestamp}&auth_version=1.0&body_md5={$hash}";
-        $string = "POST\n/apps/{$appId}/{$path}\n$query";
-        $signature = hash_hmac('sha256', $string, 'reverb-secret');
-
-        return $this->postRequest("{$path}?{$query}&auth_signature={$signature}", $data, $host, $port, $appId);
-    }
-
-    /**
-     * Send a signed GET request to the server.
-     */
-    public function getWithSignature(string $path, array $data = [], string $host = '0.0.0.0', string $port = '8080', string $appId = '123456'): PromiseInterface
-    {
-        $hash = md5(json_encode($data));
-        $timestamp = time();
-        $query = "auth_key=reverb-key&auth_timestamp={$timestamp}&auth_version=1.0&body_md5={$hash}";
-        $string = "POST\n/apps/{$appId}/{$path}\n$query";
-        $signature = hash_hmac('sha256', $string, 'reverb-secret');
-
-        $path = Str::contains($path, '?') ? "{$path}&{$query}" : "{$path}?{$query}";
-
-        return $this->request("{$path}&auth_signature={$signature}", 'GET', '', $host, $port, $appId);
+        return $this->signedRequest($path, 'POST', $data, $host, $port, $appId, $key, $secret);
     }
 }