Skip to content

Commit

Permalink
Disable CSRF on broadcast route
Browse files Browse the repository at this point in the history
This route does not particularly warrant or require CSRF protection and doing so increases the complication of setting up Laravel Echo in SPA environments.
  • Loading branch information
taylorotwell committed Nov 17, 2020
1 parent 0294433 commit acb4b77
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Broadcasting/BroadcastManager.php
Expand Up @@ -70,7 +70,7 @@ public function routes(array $attributes = null)
$router->match(
['get', 'post'], '/broadcasting/auth',
'\\'.BroadcastController::class.'@authenticate'
);
)->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Expand Up @@ -317,7 +317,7 @@ protected function retrieveChannelOptions($channel)
}

/**
* Check if channel name from request match a pattern from registered channels.
* Check if the channel name from the request matches a pattern from registered channels.
*
* @param string $channel
* @param string $pattern
Expand Down
Expand Up @@ -42,8 +42,9 @@ public function auth($request)
{
$channelName = $this->normalizeChannelName($request->channel_name);

if ($this->isGuardedChannel($request->channel_name) &&
! $this->retrieveUser($request, $channelName)) {
if (empty($request->channel_name) ||
($this->isGuardedChannel($request->channel_name) &&
! $this->retrieveUser($request, $channelName))) {
throw new AccessDeniedHttpException;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php
Expand Up @@ -60,8 +60,9 @@ public function auth($request)
str_replace($this->prefix, '', $request->channel_name)
);

if ($this->isGuardedChannel($request->channel_name) &&
! $this->retrieveUser($request, $channelName)) {
if (empty($request->channel_name) ||
($this->isGuardedChannel($request->channel_name) &&
! $this->retrieveUser($request, $channelName))) {
throw new AccessDeniedHttpException;
}

Expand Down
13 changes: 12 additions & 1 deletion src/Illuminate/Routing/Router.php
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use ReflectionClass;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

Expand Down Expand Up @@ -708,7 +709,17 @@ public function gatherRouteMiddleware(Route $route)
$middleware = collect($route->gatherMiddleware())->map(function ($name) {
return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups);
})->flatten()->reject(function ($name) use ($excluded) {
return in_array($name, $excluded, true);
if (empty($excluded)) {
return false;
} elseif (in_array($name, $excluded, true)) {
return true;
}

$reflection = new ReflectionClass($name);

return collect($excluded)->contains(function ($exclude) use ($reflection) {
return $reflection->isSubclassOf($exclude);
});
})->values();

return $this->sortMiddleware($middleware);
Expand Down

0 comments on commit acb4b77

Please sign in to comment.