.. ======================== Clickjacking Protection ======================== ======================== クリックジャッキング対策 ======================== .. module:: django.middleware.clickjacking .. :synopsis: Protects against Clickjacking :synopsis: クリックジャッキング対策 .. The clickjacking middleware and decorators provide easy-to-use protection against `clickjacking`_. This type of attack occurs when a malicious site tricks a user into clicking on a concealed element of another site which they have loaded in a hidden frame or iframe. クリックジャッキングミドルウェアとデコレータは、簡単に使える `クリックジャッキング`_ 攻撃に対する防御を提供します。このタイプの攻撃は、 悪意のあるサイトがユーザをだまし、隠された frame や iframe にロードした別サイト の要素をクリックさせることで起こります。 .. _clickjacking: http://en.wikipedia.org/wiki/Clickjacking .. _クリックジャッキング: http://ja.wikipedia.org/wiki/%E3%82%AF%E3%83%AA%E3%83%83%E3%82%AF%E3%82%B8%E3%83%A3%E3%83%83%E3%82%AD%E3%83%B3%E3%82%B0 .. An example of clickjacking ========================== クリックジャッキングの例 ======================== .. Suppose an online store has a page where a logged in user can click "Buy Now" to purchase an item. A user has chosen to stay logged into the store all the time for convenience. An attacker site might create an "I Like Ponies" button on one of their own pages, and load the store's page in a transparent iframe such that the "Buy Now" button is invisibly overlaid on the "I Like Ponies" button. If the user visits the attacker's site, clicking "I Like Ponies" will cause an inadvertent click on the "Buy Now" button and an unknowing purchase of the item. あるオンラインストアのサイトに、ログインユーザが「今すぐ購入」ボタンを押すことで 商品を買えるようなページがあるとしましょう。あるユーザが利便性のためログインし 続けることを選択していたとします。そして攻撃者が彼ら自身のサイトのページに 「私はポニーが好きです」ボタンを作り、さらに透明な iframe に先ほどのストアの ページをロードして『「今すぐ購入」ボタンが彼らの「私はポニーが好きです」ボタンを 真上に来るように』重ねたとします。もしそのユーザが攻撃者のサイトを訪ねて 「私はポニーが好きです」ボタンをクリックしてしまうと、無意識に「今すぐ購入」 ボタンをクリックすることとなり、身に覚えの無い品物を購入することに なってしまいます。 .. _clickjacking-prevention: .. Preventing clickjacking ======================= クリックジャッキングを防止する ============================== .. Modern browsers honor the `X-Frame-Options`_ HTTP header that indicates whether or not a resource is allowed to load within a frame or iframe. If the response contains the header with a value of ``SAMEORIGIN`` then the browser will only load the resource in a frame if the request originated from the same site. If the header is set to ``DENY`` then the browser will block the resource from loading in a frame no matter which site made the request. モダンなブラウザは、frame や iframe の中にあるリソースをロードして良いかどうかを 示す `X-Frame-Options`_ HTTP ヘッダの指定を尊重します。 もしサーバからのレスポンスに ``SAMEORIGIN`` という値を指定した `X-Frame-Options` ヘッダが含まれていた場合、ブラウザは frame 中のリソースが同一サイトに由来する 場合に限り、そのリソースをロードします。もしヘッダが ``DENY`` に設定されていた場合、どのサイトがリクエスト元であろうとブラウザは frame 中のリソースのロードを問答無用でブロックします。 .. _X-Frame-Options: https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header .. Django provides a few simple ways to include this header in responses from your site: Django はこのヘッダをレスポンスに含める簡単な方法をいくつか提供します: .. 1. A simple middleware that sets the header in all responses. 2. A set of view decorators that can be used to override the middleware or to only set the header for certain views. 1. すべてのレスポンスにこのヘッダを設定するシンプルなミドルウェア 2. そのミドルウェアの動作をオーバーライドする、または単純に特定のビューにだけ このヘッダを設定するデコレータのセット .. How to use it ============= 使用方法 ======== .. Setting X-Frame-Options for all responses ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ X-Frame-Options をすべてのレスポンスに設定する ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. To set the same ``X-Frame-Options`` value for all responses in your site, put ``'django.middleware.clickjacking.XFrameOptionsMiddleware'`` to :setting:`MIDDLEWARE_CLASSES`:: 同じ ``X-Frame-Options`` 値をサイトの全レスポンスに設定するには、 ``'django.middleware.clickjacking.XFrameOptionsMiddleware'`` を :setting:`MIDDLEWARE_CLASSES` に設定します:: MIDDLEWARE_CLASSES = ( ... 'django.middleware.clickjacking.XFrameOptionsMiddleware', ... ) .. This middleware is enabled in the settings file generated by :djadmin:`startproject`. このミドルウェアは :djadmin:`startproject` が生成する設定ファイルでは 最初から有効化されています。 .. By default, the middleware will set the ``X-Frame-Options`` header to ``SAMEORIGIN`` for every outgoing ``HttpResponse``. If you want ``DENY`` instead, set the :setting:`X_FRAME_OPTIONS` setting:: デフォルトでは、このミドルウェアはすべての ``HttpResponse`` の ``X-Frame-Options`` ヘッダを ``SAMEORIGIN`` に設定します。もし ``DENY`` にしたい場合は :setting:`X_FRAME_OPTIONS` の設定を次のように設定します:: X_FRAME_OPTIONS = 'DENY' .. When using the middleware there may be some views where you do **not** want the ``X-Frame-Options`` header set. For those cases, you can use a view decorator that tells the middleware not to set the header:: このミドルウェアを使うにあたって、いくつかのビューでは ``X-Frame-Options`` ヘッダを設定 **したくない** かもしれません。そのような場合、 ビューデコレータでミドルウェアにヘッダを設定しないよう指示することができます:: from django.http import HttpResponse from django.views.decorators.clickjacking import xframe_options_exempt @xframe_options_exempt def ok_to_load_in_a_frame(request): return HttpResponse("This page is safe to load in a frame on any site.") .. Setting X-Frame-Options per view ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ X-Frame-Options をビューごとに設定する ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. To set the ``X-Frame-Options`` header on a per view basis, Django provides these decorators:: ``X-Frame-Options`` ヘッダをビューごとに設定するために、Django は次のようなデコレータを提供しています:: from django.http import HttpResponse from django.views.decorators.clickjacking import xframe_options_deny from django.views.decorators.clickjacking import xframe_options_sameorigin @xframe_options_deny def view_one(request): return HttpResponse("I won't display in any frame!") @xframe_options_sameorigin def view_two(request): return HttpResponse("Display in a frame if it's from the same origin as me.") .. Note that you can use the decorators in conjunction with the middleware. Use of a decorator overrides the middleware. なお、これらのデコレータはミドルウェアとともに使うことができます。 デコレータの指定は、ミドルウェアの指定よりも優先されます。 .. Limitations =========== 制限事項 ======== .. The ``X-Frame-Options`` header will only protect against clickjacking in a modern browser. Older browsers will quietly ignore the header and need `other clickjacking prevention techniques`_. ``X-Frame-Options`` ヘッダによるクリックジャッキング対策はモダンなブラウザに 対してのみ有効です。古いブラウザの場合はこのヘッダを無視してしまうため、 `他のクリックジャッキング対策 `_ が必要です。 .. 翻訳者注:「他のクリックジャッキング対策」は原文では英語 Wikipedia の Clickjacking の Prevention 章を参照しています。しかし2014年10月23日現在、 日本語 Wikipedia のクリックジャッキングのページには Prevention 章に相当する 章が無いため、リンク先は英語版 Wikipedia のままにしてあります。 .. Browsers that support X-Frame-Options ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ X-Frame-Options をサポートするブラウザ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Internet Explorer 8+ * Firefox 3.6.9+ * Opera 10.5+ * Safari 4+ * Chrome 4.1+ .. See also ~~~~~~~~ 参考情報 ~~~~~~~~ .. A `complete list`_ of browsers supporting ``X-Frame-Options``. ``X-Frame-Options`` をサポートするブラウザの `完全な一覧`_ 。 .. .. _complete list: https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header#Browser_compatibility .. _完全な一覧: https://developer.mozilla.org/ja/docs/HTTP/X-Frame-Options .. _other clickjacking prevention techniques: http://en.wikipedia.org/wiki/Clickjacking#Prevention