0

I need to add security for ajax get method in Laravel 5. For POST, PUT and DELETE Laravel token is good. But How can I add security in ajax get request.

Security

If an hacker try to access an ajax page directly form another domain. I need to check the domain name and restrict.

I have added middleware but I am not sure Is this the correct way?

<?php

namespace App\Http\Middleware;

use Closure;

class AjaxMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ((!$request->ajax()) || ($request->url() != url()->current())){
            return response('Forbidden.', 403);
        }
        return $next($request);
    }
} 
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
Sarath TS
  • 2,432
  • 6
  • 32
  • 79

2 Answers2

0

Isn't that what CORS suppose to block? Check this out: Understanding AJAX CORS and security considerations

You don't have to put any extra layer of protection if you are afraid that someone from a different website might do ajax calls to your website. They will be blocked by browser, by default.

Community
  • 1
  • 1
neochief
  • 569
  • 4
  • 6
0

Just use the same CSFR technique for the GET requests, to verify the request comes from a valid session.

Eduardo Chongkan
  • 752
  • 7
  • 12