2015-07-06 4 views
1

В двух местах я обнаружил, что защита Laravel csrf можно обойти, установив переменную protected $except. Но это не кажется, работает в соответствии с док:Laravel: webhooks необходимо обходить проверку CSRF от Laravel

http://laravel.com/docs/5.1/billing#handling-stripe-webhooks

и в

http://laravel.com/docs/5.1/routing#csrf-protection

protected $except = [ 
    'stripe/*', 
]; 

Я использую 5,1

Вот в routes.php

Route::match(['post'], '/webhooks/provider/callback/{version}', [ 
    'as' => 'provider.webhooks.callback', 'uses' => '[email protected]' 
]); 
Route::match(['post'], '/webhooks/provider/fallback/{version}', [ 
    'as' => 'provider.webhooks.fallback', 'uses' => '[email protected]' 
]); 

А вот

<?php namespace App\Http\Middleware; 
use Closure; 
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier; 
class VerifyCsrfToken extends BaseVerifier { 
    protected $except = [ 
     'webhooks/*', 
     '/webhooks/*', 
    ]; 
    public function handle($request, Closure $next) 
    { 
     return parent::handle($request, $next); 
    } 
} 

А вот что в BaseVerifier, где я не вижу какой-либо $except чека:

<?php namespace Illuminate\Foundation\Http\Middleware; 
use Closure; 
use Illuminate\Contracts\Routing\Middleware; 
use Symfony\Component\HttpFoundation\Cookie; 
use Illuminate\Contracts\Encryption\Encrypter; 
use Illuminate\Session\TokenMismatchException; 
use Symfony\Component\Security\Core\Util\StringUtils; 
class VerifyCsrfToken implements Middleware { 
    public function handle($request, Closure $next) 
    { 
     if ($this->isReading($request) || $this->tokensMatch($request)) 
     { 
      return $this->addCookieToResponse($request, $next($request)); 
     } 

     throw new TokenMismatchException; 
    } 
} 

Однако я решил комментирование, но до сих пор установки $except должны были обработаны в соответствии с документом; не так ли ?:

<?php namespace App\Http; 
use Illuminate\Foundation\Http\Kernel as HttpKernel; 
class Kernel extends HttpKernel { 
    protected $middleware = [ 
     //'App\Http\Middleware\VerifyCsrfToken', 
    ]; 
} 

А вот в журнале ошибок:

[2015-07-06 09:40:34] production.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:46 
Stack trace: 
#0 /app/Http/Middleware/VerifyCsrfToken.php(26): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#1 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure)) 
+1

Вы можете разместить свой файл маршруты? Вы установили маршрут как '/ stripe/webhook'? – Laurence

+1

И используете ли вы Laravel 5.1? – Laurence

+0

да его 5.1 @TheShiftExchange – itsazzad

ответ

1

Модификация app/Http/Middleware/VerifyCsrfToken.php

//add an array of Routes to skip CSRF check 
private $openRoutes = ['free/route', 'free/too']; 

//modify this function 
public function handle($request, Closure $next) 
    { 
     //add this condition 
    foreach($this->openRoutes as $route) { 

     if ($request->is($route)) { 
     return $next($request); 
     } 
    } 

    return parent::handle($request, $next); 
    } 

source

В $openRoutes массиве дает ваши маршруты и это будет обойдено.

+2

Это * только *, если они используют 'Laravel 5.0'. Если они используют 'Laravel 5.1', это не нужно – Laurence

1

Итак, для Laravel 5.0 вы можете использовать это;

private $openRoutes = ['webhooks/free', 'webhooks/*']; 

public function handle($request, Closure $next) 
{ 
    if(in_array($request->path(), $this->openRoutes)){ 
    return $next($request); 
    } 

    return parent::handle($request, $next); 
} 

для Laravel 5.1 вы можете пользователю эта функция

<?php 

namespace App\Http\Middleware; 
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier; 

class VerifyCsrfToken extends BaseVerifier 
{ 
/** 
* The URIs that should be excluded from CSRF verification. 
* 
* @var array 
*/ 
protected $except = [ 
         'stripe/*', 
        ]; 
} 

источник на док http://laravel.com/docs/5.1/routing#csrf-excluding-uris

Смежные вопросы