Skip to content

Commit 5a1b8af

Browse files
authored
Merge pull request #294 from binaryfire/fix-broadcasting-middleware
fix: exclude CSRF verification from broadcast auth routes
2 parents 9099a79 + 67b59ce commit 5a1b8af

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/broadcasting/src/BroadcastManager.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Hypervel\Bus\UniqueLock;
2525
use Hypervel\Cache\Contracts\Factory as Cache;
2626
use Hypervel\Foundation\Http\Kernel;
27+
use Hypervel\Foundation\Http\Middleware\VerifyCsrfToken;
2728
use Hypervel\ObjectPool\Traits\HasPoolProxy;
2829
use Hypervel\Queue\Contracts\Factory as Queue;
2930
use InvalidArgumentException;
@@ -73,7 +74,10 @@ public function __construct(
7374
public function routes(array $attributes = []): void
7475
{
7576
if ($this->app->has(Kernel::class)) {
76-
$attributes = $attributes ?: ['middleware' => ['web']];
77+
$attributes = $attributes ?: [
78+
'middleware' => ['web'],
79+
'without_middleware' => [VerifyCsrfToken::class],
80+
];
7781
}
7882

7983
$kernels = $this->app->get(ConfigInterface::class)
@@ -95,7 +99,10 @@ public function routes(array $attributes = []): void
9599
*/
96100
public function userRoutes(?array $attributes = null): void
97101
{
98-
$attributes = $attributes ?: ['middleware' => ['web']];
102+
$attributes = $attributes ?: [
103+
'middleware' => ['web'],
104+
'without_middleware' => [VerifyCsrfToken::class],
105+
];
99106

100107
$this->app->get(RouterDispatcherFactory::class)->getRouter()
101108
->addRoute(

tests/Broadcasting/BroadcastManagerTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hypervel\Tests\Broadcasting;
66

77
use Hyperf\Contract\ConfigInterface;
8+
use Hyperf\HttpServer\Router\DispatcherFactory as RouterDispatcherFactory;
89
use Hypervel\Broadcasting\BroadcastEvent;
910
use Hypervel\Broadcasting\BroadcastManager;
1011
use Hypervel\Broadcasting\Channel;
@@ -19,6 +20,8 @@
1920
use Hypervel\Container\DefinitionSource;
2021
use Hypervel\Context\ApplicationContext;
2122
use Hypervel\Foundation\Application;
23+
use Hypervel\Foundation\Http\Kernel;
24+
use Hypervel\Foundation\Http\Middleware\VerifyCsrfToken;
2225
use Hypervel\Queue\Contracts\Factory as QueueFactoryContract;
2326
use Hypervel\Support\Facades\Broadcast;
2427
use Hypervel\Support\Facades\Bus;
@@ -117,6 +120,66 @@ public function testThrowExceptionWhenUnknownStoreIsUsed()
117120

118121
$broadcastManager->connection('alien_connection');
119122
}
123+
124+
public function testRoutesExcludesCsrfMiddleware(): void
125+
{
126+
$capturedAttributes = null;
127+
128+
$router = m::mock('router');
129+
$router->shouldReceive('addRoute')
130+
->once()
131+
->withArgs(function ($methods, $path, $handler, $attributes) use (&$capturedAttributes) {
132+
$capturedAttributes = $attributes;
133+
return true;
134+
});
135+
136+
$routerFactory = m::mock('routerFactory');
137+
$routerFactory->shouldReceive('getRouter')
138+
->with('http')
139+
->andReturn($router);
140+
141+
$config = m::mock(ConfigInterface::class);
142+
$config->shouldReceive('get')
143+
->with('server.kernels', [])
144+
->andReturn(['http' => []]);
145+
146+
$app = m::mock(ContainerInterface::class);
147+
$app->shouldReceive('has')->with(Kernel::class)->andReturn(true);
148+
$app->shouldReceive('get')->with(ConfigInterface::class)->andReturn($config);
149+
$app->shouldReceive('get')->with(RouterDispatcherFactory::class)->andReturn($routerFactory);
150+
151+
$broadcastManager = new BroadcastManager($app);
152+
$broadcastManager->routes();
153+
154+
$this->assertSame(['web'], $capturedAttributes['middleware']);
155+
$this->assertSame([VerifyCsrfToken::class], $capturedAttributes['without_middleware']);
156+
}
157+
158+
public function testUserRoutesExcludesCsrfMiddleware(): void
159+
{
160+
$capturedAttributes = null;
161+
162+
$router = m::mock('router');
163+
$router->shouldReceive('addRoute')
164+
->once()
165+
->withArgs(function ($methods, $path, $handler, $attributes) use (&$capturedAttributes) {
166+
$capturedAttributes = $attributes;
167+
return true;
168+
});
169+
170+
$routerFactory = m::mock('routerFactory');
171+
$routerFactory->shouldReceive('getRouter')
172+
->andReturn($router);
173+
174+
$app = m::mock(ContainerInterface::class);
175+
$app->shouldReceive('get')->with(RouterDispatcherFactory::class)->andReturn($routerFactory);
176+
177+
$broadcastManager = new BroadcastManager($app);
178+
$broadcastManager->userRoutes();
179+
180+
$this->assertSame(['web'], $capturedAttributes['middleware']);
181+
$this->assertSame([VerifyCsrfToken::class], $capturedAttributes['without_middleware']);
182+
}
120183
}
121184

122185
class TestEvent implements ShouldBroadcast

0 commit comments

Comments
 (0)