CSRF Protection

提供: GeeklogJpWiki

目次

CSRF対策

トークン

GeeklogにおけるCSRF対策はトークンです。トークンはハッシュ値によるユニークなIDです。

通常、フォームが表示されたときに作成され、ポスト(またはGET )リクエストで送信、処理する際に、フォームをチェックをします。

トークンは、Geeklog 1.5.0で、gl_tokensテーブルに格納されています。

トークンの作成と利用

標準の練習としては、トークンの両方をトークンの名前と値は、フォームのテンプレートのように:

   $template->set_var('gltoken_name', CSRF_TOKEN);
   $template->set_var('gltoken', SEC_createToken());

hidden入力フィールドは次のようにします。

   <input type="hidden" name="{gltoken_name}" value="{gltoken}"{xhtml}>

CSRF_TOKEN は、定数であり、一方、sec_createtoken()の呼び出しで、新たなトークンを生成し、返します。

通常、1つのトークンには1フォームのみを使用するので、コールを確認してsec_createtoken()は一度だけです。この関数は、オプションのパラメータを定義する時間の長さ( "のTTL " 、の時間のライブ)は、トークンは有効です。デフォルトのTTLは1200秒、つまり20分です。

HTTPリクエストを処理した結果を、単にトークンが送った際のリクエストをチェックするために、SEC_checkToken()を呼びます。中止の操作の場合、関数はfalseを返します。

無効なトークンとIP、ユーザー名をログに記録できるよう、追跡しています。


配慮

  • Avoid creating new tokens when not required. One token per form or page is usually enough.
  • A call to SEC_checkToken() will invalidate the token. So if you display a new form after checking the token, you will need to create a new token. Consider this in the control flow of your form processing, e.g. when checking for missing required fields. Keep in mind that the user may use the back button which will display the form again - including a token that has just been invalidated.
  • Protect any operations that create, modify, or delete content. There is usually no need to use tokens when listing things (e.g. a list of articles). If the list has active elements, however, e.g. a checkbox to enable/disable something, then you should use tokens for those operations.



対策を追加する手法

  • 送信フォームにトークンのhiddenを設置。
  • 送信処理の内容チェックでトークンのチェックを追加。


1.4.1と1.5と兼用の場合

1.4.1にトークン用の関数がないので、それらがあるように偽装します。

functions.inc の先頭あたりのconfigファイルのインクルード後あたりに宣言

if (version_compare(VERSION, '1.5.0') < 0) {
    if (!function_exists('SEC_checkToken')) {
        function SEC_checkToken() {
            return true;
        }
    }
}

あとの使い方は1.5専用と同じように使います。 ただ、その追加やチェックは1.5.0以上のバージョンで行うといったif文を使っておく必要があります。

if (version_compare(VERSION, '1.5.0') >= 0) {
個人用ツール