Skip to content

Commit

Permalink
pkp#10342 added required middleware attachment check to ValidateCsrfT…
Browse files Browse the repository at this point in the history
…oken
  • Loading branch information
touhidurabir committed Aug 29, 2024
1 parent 97aa9f2 commit db0be8d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 19 additions & 1 deletion classes/middleware/ValidateCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,33 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use PKP\middleware\HasUser;
use PKP\middleware\traits\HasRequiredMiddleware;

class ValidateCsrfToken
{
use HasRequiredMiddleware;

/**
* @copydoc \PKP\middleware\traits\HasRequiredMiddleware::requiredMiddleware()
*/
public function requiredMiddleware(): array
{
return [
HasUser::class,
];
}

/**
* Determine and validate CSRF token
*
*/
public function handle(Request $request, Closure $next)
{
if (!$this->hasRequiredMiddleware($request)) {
// Required middleware not attached to target routes, move to next
return $next($request);
}

if($this->isApiRequest($request)) {
return $next($request);
}
Expand Down
10 changes: 5 additions & 5 deletions classes/middleware/traits/HasRequiredMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @class HasRequiredMiddleware
*
* @brief Helper trait to find the required middleware attachments
* @brief Trait to find the required middleware attachments in a middleware
*/

namespace PKP\middleware\traits;
Expand Down Expand Up @@ -56,10 +56,10 @@ public function hasRequiredMiddleware(Request $request, int $matchingCriteria =
$router = app('router'); /** @var \Illuminate\Routing\Router $router */
$routerMiddleware = $router->getMiddleware();

// need to replace the alish name with full class path
// need to replace the alias name with full class path
$routeMiddleware = $routeMiddleware->map(function (string $middleware) use($routerMiddleware): string {
// extract the middleware class or alias name if in formar
// as `has.roles:1|16|17` or `PKP\middleware\HasRoles:1|16|17`
// extract the middleware class or alias name if in format
// such as `has.roles:1|16|17` or `PKP\middleware\HasRoles:1|16|17`
$middleware = array_shift(array_pad(explode(":", $middleware, 2), 2, []));

if (class_exists($middleware)) {
Expand All @@ -76,4 +76,4 @@ public function hasRequiredMiddleware(Request $request, int $matchingCriteria =
=> $routeMiddleware->intersect($requiredMiddleware)->count() > 0,
};
}
}
}

0 comments on commit db0be8d

Please sign in to comment.