mirror of
https://github.com/flarum/core.git
synced 2025-07-22 01:01:28 +02:00
Added CSRF Extender (#2095)
This commit is contained in:
committed by
GitHub
parent
6a7cac7704
commit
d627d01544
29
framework/core/src/Http/HttpServiceProvider.php
Normal file
29
framework/core/src/Http/HttpServiceProvider.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Http;
|
||||
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
|
||||
class HttpServiceProvider extends AbstractServiceProvider
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton('flarum.http.csrfExemptPaths', function () {
|
||||
return ['/api/token'];
|
||||
});
|
||||
|
||||
$this->app->bind(Middleware\CheckCsrfToken::class, function ($app) {
|
||||
return new Middleware\CheckCsrfToken($app->make('flarum.http.csrfExemptPaths'));
|
||||
});
|
||||
}
|
||||
}
|
@@ -17,8 +17,22 @@ use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class CheckCsrfToken implements Middleware
|
||||
{
|
||||
protected $exemptRoutes;
|
||||
|
||||
public function __construct(array $exemptRoutes)
|
||||
{
|
||||
$this->exemptRoutes = $exemptRoutes;
|
||||
}
|
||||
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
$path = $request->getAttribute('originalUri')->getPath();
|
||||
foreach ($this->exemptRoutes as $exemptRoute) {
|
||||
if (fnmatch($exemptRoute, $path)) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
||||
if (in_array($request->getMethod(), ['GET', 'HEAD', 'OPTIONS'])) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
Reference in New Issue
Block a user