Laravel: Excluding routes from VerifyCsrfToken middleware programmatically - laravel

I would like to know if there's a way I can programmatically add route to $except variable in VerifyCsrfToken class.
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* #var array
*/
protected $except = [
//
];
}
I am developing a package that listens to webhooks/postback from a payment gateway. It would be nicer if my custom route would be added to $except variable upon installation of the package.
Thanks

I decided to add my custom route to api.php
file_put_contents(
base_path('routes/api.php'),
file_get_contents(__DIR__.'/stubs/make/routes.stub'),
FILE_APPEND
);
VerifyCsrfToken middleware does not intercept routes in api.php

Related

Laravel 5.8 VerifyCsrfToken Exception not working

I'm trying to exclude all requests on an endpoint which I call test.com/code so I do like this in the VerifyCsrfToken.php file.
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* Indicates whether the XSRF-TOKEN cookie should be set on the response.
*
* #var bool
*/
protected $addHttpCookie = true;
/**
* The URIs that should be excluded from CSRF verification.
*
* #var array
*/
protected $except = [
'code',
'code/*'
];
}
But it does not solve the issue, even after I try to run "php artisan route:clear && php artisan config:clear", Anyone knows why I can't exclude specific routes? (Laravel 5.8)
My route is something like this:
Route::get('code/testing', 'CodeController#testing');
I have try this and it didn't work either.
protected $except = [
'https://test.com/code/*',
];
Till now the only way I figure out is remove the line "\App\Http\Middleware\VerifyCsrfToken::class," from app/Http/Kernel.php to disable Csrf feature which is not a good solution.
Use / (slash) at the beggining :
protected $except = [
'/code/*'
];
You can try '/code/*'. It works for me with a slash / in the front of the url.

Laravel auto run middleware for everypage without add Route::group in route

Route::group(['middleware' => ['web']], function(){
}
I have a middleware need to get user's point when everytime page load.
Is anyway to let this middleware run without add route group inside of route?
You can add your middleware in the app/Http/Kernel.php
If you add it to the property protected $middleware it will run for every request.
Instead added to the proterty protected $middlewareGroups it will run only for web or api requests.
The code in Kernel.php is commented and pretty self explanatory.
In your App\Http\Kernel.php file, add your middleware in $middleware array as:
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* #var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\AppAfterMiddleware::class,
];
Docs

REST API patch method URI having token mismatch in laravel

I am trying to write a method in laravel 5.2 for the patch HTTP verb for REST, but it's showing a token mismatch. This is my VerifyCsrfToken class:
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification
*
* #var array
*/
protected $except = ['v0.1/api/mc-api','v0.1/api/mc-api/{mc_api}'];
}
In my routes, I have the following:
Route::group(array('prefix'=>'v0.1/api'),function(){
Route::resource('mc-api','ApiController');
});
Route::any('v0.1/api/mc-api/*',['nocsrf'=>'true','uses'=>'ApiController#update']);
Based on chat I have tried changing my routes to the following but it doesn't help:
Route::any('v0.1/api/mc-api/*',['nocsrf'=>true,'uses'=>'ApiController#update']);
Nor does:
Route::any('v0.1/api/mc-api/{mc_api}', 'ApiController#u‌​pdate');
In your class VerifyCsrfToken $except array
change
'v0.1/api/mc-api/{mc_api}'
to
'v0.1/api/mc-api/*'

define post route without csrf token

I want to call my functions from android side.
now, I want to send post values from android side.
How can I disable csrf token for some functions ?
because without it I got error message .
like this:
Route::post('mobile/android/getlastnews','NewsController#getLastNews');
For Laravel 5.2, inside app/Http/Middleware/VerifyCsrfToken.php you just need to add it to the $except array.
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* #var array
*/
protected $except = [
'mobile/android/getlastnews'
];
}
If you wanted to allow all routes for mobile/ you can do the following
protected $except = [
'mobile/*'
];
Open app/Http/Middleware/VerifyCsrfToken.php and in the protected $except = []; array add your URL that you want to be skipped for CSRF validation.

How to disable CSRF Token in Laravel and why we have to disable it?

I want to see how I can disable CSRF token in Laravel and where I have to disable it. Is this good to disable it or not?
You can Disable CSRF on few routes by editing.
App\Http\Middleware\VerifyCsrfToken
and add your own routes name in protected
$except = [] array.
It does not seems to be good practice as by doing this we are removing security feature of Laravel.
In laravel 9.
Open file \App\Http\Middleware\VerifyCsrfToken.php
Disable for all routes
protected $except = [
'*',
];
Disable for some routes
protected $except = [
'mobile/*',
'news/articles',
];
I searched for a long time how to disable CSRF completely, there are many identical examples but they do not help
Many people explain how to do it, but they do not explain what the url should look like.
edit app/Http/Middleware/VerifyCsrfToken.php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* Indicates whether the XSRF-TOKEN cookie should be set on the response.
*
* #var bool
*/
protected $addHttpCookie = true;
/**
* The URIs that should be excluded from CSRF verification.
*
* #var array
*/
protected $except = [
'/user/my_function'
];
}
In the $except array(); we add a url with just a simple string. This points to a controller usually depending on how your route is setup.
For example I have a UserController.php file in my Controller folder. I have a route like. In the web.php routes file.
Route::post('/user', 'UserController#my_function')->name('my_function');
Also alternatively, if you came to this question simply because you don't know how to use the CSRF and you don't actually need to disable it, or make the URL except. You can use this method.
Add these lines to your app.blade.php if it is used for ajax related calls.
<script>
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});
});
</script>
You can disable it in app/http/Kernel.php in the web middleware group.
Is this good to disable it or not?
No it's not. Read the Wikipedia page about CSRF to understand what CSRF is, the CSRF-Token prevents CSRF.
The CSRF token protects your application and it's users against cross-site request forgery. For more information on that, have a read here:
https://en.wikipedia.org/wiki/Cross-site_request_forgery
The token is validated via Middleware in Laravel. If you take a look at the file app/Http/Middleware/VerifyCsrfToken.php, you will see it gives you the option to add URLs that should be exempt from CSRF verification.
<?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 = [
//
];
}
If you want to disable it entirely, you can find it in the Middleware group named web in app/Http/Kernel.php. Those are the middlewares that fire by default over HTTP requests.
I wouldn't recommend disabling it where possible though.
Hi just go to app/Http/Kernel.php file simply commented out line no 31
// \App\Http\Middleware\VerifyCsrfToken::class,
If you want to block csrf token verification easily head over to app/Middleware/VerifyCsrfToken.php to disable it for all routes
protected $except = ['*'];
or specify routes like this
protected $except = ['/api/route/one', 'api/route/two'];
(Temporary fix. Not Recommended)
Just Open kernel.php (app/http)
and disable
App\Http\Middleware\VerifyCsrfToken::class,

Resources