Home > PHP Framework > Laravel > Laravel's form forgery and CSRF protection

Laravel's form forgery and CSRF protection

齐天大圣
Release: 2020-12-14 13:51:18
Original
1652 people have browsed it

We know that the most popular API design specification currently is restFul API design. Restful has five common HTTP methods, namely: get, post, put, patch and delete. It is very easy to construct the get or post method using the html form, but the other three methods are not supported. But in laravel, you can use the other three methods mentioned above through form forgery technology.

Recommended tutorial: "laravel framework"

Preparation work

First we have to prepare Work well done. We need to create two routes: a form route and a route that accepts forms.

// 表单页
Route::get('form', function () {
    return view('form');
});

// 接受表单请求
Route::any('getform', function () {
    return \Illuminate\Support\Facades\Request::method();
});
Copy after login

At the beginning, we made the simplest get request form, the content is as follows:

<form method="get" action="/getform">
    <input type="submit" value="sub" />
</form>
Copy after login

After clicking the submit button, 'GET' appears in the browser, indicating that the get request was successfully sent and accepted. .

CSRF protection

Then, we change to the post method, then refresh and click the submit button to see what happens. You will find an error with "page expired" and status code 419. Why can't laravel accept post requests? Here we will introduce laravel's default CSRF protection mechanism.

In order to prevent cross-site request forgery attacks, laravel provides CSRF token protection. Therefore, for all methods except the get method request, the reader needs to add the CSRF token to the form, as follows:

<input type="hidden" name="_token" value="{{csrf_token()}}">
Copy after login

It also has an abbreviation method, as follows:

@csrf
Copy after login

Turn off the CSRF protection function

It is generally not recommended to turn off the CSRF function of the entire site. It is very simple to turn it off, just comment out the

\App\Http\Middleware\VerifyCsrfToken::class
Copy after login

line in the Kernel.php file.

CSRF whitelist

Often we need to set a group of URLs that do not require CSRF protection, such as API interfaces provided by third parties, we It is hoped that all external API interfaces will not require CSRF protection. Then, you can use the CSRF whitelist function to set the whitelist in the app/Http/Middleware/VerifyCsrfToken.php file. As follows:

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        /* 这里是白名单列表 */
        &#39;http://example.com/api/*&#39;,
        &#39;api/*&#39;,
        &#39;a/b/*&#39;
    ];
}
Copy after login

Note: To facilitate testing, the csrf function will be automatically turned off when testing the environment

##Form forgery

After learning the CSRF protection mechanism, let’s take a look at how to forge the form. It is also very simple to forge a form. You only need to add

<input type="hidden" name="_method" value="PUT">
Copy after login

or abbreviated to

@method(&#39;PUT&#39;)
Copy after login

in the form. The following is a form for forging a put request

@csrf @method(&#39;PUT&#39;)
Copy after login

The above is the detailed content of Laravel's form forgery and CSRF protection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template