イントロダクションIntroduction
Laravelでは、クロス・サイト・リクエスト・フォージェリ(CSRF)からアプリケーションを簡単に守れます。クロス・サイト・リクエスト・フォージェリは悪意のあるエクスプロイトの一種であり、信頼できるユーザーになり代わり、認められていないコマンドを実行します。Laravel makes it easy to protect your application from cross-site request forgery[https://en.wikipedia.org/wiki/Cross-site_request_forgery] (CSRF) attacks. Cross-site request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of an authenticated user.
Laravelは、アプリケーションにより管理されているアクティブなユーザーの各セッションごとに、CSRF「トークン」を自動的に生成しています。このトークンを認証済みのユーザーが、実装にアプリケーションに対してリクエストを送信しているのかを確認するために利用します。Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.
アプリケーションでHTMLフォームを定義する場合はいつでも、隠しCSRFトークンフィールドをフォームに埋め込み、CSRF保護ミドルウェアがリクエストの有効性をチェックできるようにしなければなりません。トークン隠しフィールドを生成するには、csrf_field
ヘルパ関数を使ってください。Anytime you define a HTML form in
your application, you should include a hidden CSRF
token field in the form so that the CSRF protection
middleware can validate the request. You may use the
csrf_field
helper to generate the token
field:
<form method="POST" action="/profile">
{{ csrf_field() }}
...
</form>
web
ミドルウェアグループに含まれている、VerifyCsrfToken
ミドルウェアが、リクエスト中のトークンとセッションに保存されているトークンが一致するか、確認しています。The VerifyCsrfToken
middleware[/docs/{{version}}/middleware],
which is included in the web
middleware
group, will automatically verify that the token in
the request input matches the token stored in the
session.
URIの除外Excluding URIs From CSRF Protection
一連のURIをCSRF保護より除外したい場合もあります。たとえば、Stripeを課金処理に採用しており、そのWebフックシステムを利用している時、LaravelのCSRF保護よりWebフック処理ルートを除外する必要があるでしょう。なぜならルートに送るべきCSRFトークンがどんなものか、Stripeは知らないからです。Sometimes you may wish to exclude a set of URIs from CSRF protection. For example, if you are using Stripe[https://stripe.com] to process payments and are utilizing their webhook system, you will need to exclude your Stripe webhook handler route from CSRF protection since Stripe will not know what CSRF token to send to your routes.
通常、この種のルートはRouteServiceProvider
がroutes/web.php
ファイル中の全ルートへ適用する、web
ミドルウェアから外しておくべきです。しかし、VerifyCsrfToken
ミドルウェアの$except
プロパティへ、そうしたURIを追加することによっても、ルートを除外することができます。Typically, you should place these
kinds of routes outside of the web
middleware group that the
RouteServiceProvider
applies to all
routes in the routes/web.php
file.
However, you may also exclude the routes by adding
their URIs to the $except
property of
the VerifyCsrfToken
middleware:
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* CSRFバリデーションから除外するURI
*
* @var array
*/
protected $except = [
'stripe/*',
];
}
X-CSRF-TOKENX-CSRF-TOKEN
更に追加でPOSTパラメーターとしてCSRFトークンを確認したい場合は、LaravelのVerifyCsrfToken
ミドルウェアがX-CSRF-TOKEN
リクエストヘッダもチェックします。たとえば、HTML中のmeta
タグにトークンを保存します。In addition to checking for the
CSRF token as a POST parameter, the
VerifyCsrfToken
middleware will also
check for the X-CSRF-TOKEN
request
header. You could, for example, store the token in a
HTML meta
tag:
<meta name="csrf-token" content="{{ csrf_token() }}">
meta
タグを作成したら、jQueryのようなライブラリーで、全リクエストヘッダにトークンを追加できます。この手法によりAJAXベースのアプリケーションにシンプルで便利なCSRF保護を提供できます。Then, once you have created the
meta
tag, you can instruct a library
like jQuery to automatically add the token to all
request headers. This provides simple, convenient
CSRF protection for your AJAX based
applications:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
X-XSRF-TOKENX-XSRF-TOKEN
LaravelはCSRFトークンをフレームワークにより生成されるリクエストに含まれる、XSRF-TOKEN
クッキーの中に保存します。このクッキーの値をX-XSRF-TOKEN
リクエストヘッダにセットすることが可能です。Laravel stores the current CSRF
token in a XSRF-TOKEN
cookie that is
included with each response generated by the
framework. You can use the cookie value to set the
X-XSRF-TOKEN
request header.
AngularのようなJavaScriptフレームワークでは、自動的に値をX-XSRF-TOKEN
ヘッダに設定するので、利便性を主な目的として、このクッキーを送ります。This cookie is primarily sent as
a convenience since some JavaScript frameworks, like
Angular, automatically place its value in the
X-XSRF-TOKEN
header.