2013.
12.
23
23:04:46
結論から書けば
■埋め込み
もしくは
■チェック
でOKです。
CSRF対策としてセキュリティトークンを使う方法は一般的ですが、
FuelPHPさんではあらかじめそれ用の部品が用意されているのだそうな。
まずトークン埋め込みの方は、フォーム内に
のような形で記述すれば埋め込まれます。
でトークンの名前(name属性)を取得して、
で実際のトークン値を取得している訳ですな。
トークンの名前は
か
の中に
とか書いてあるので好きに変えてください。
ちなみにトークンの埋め込みはFormクラスを使って
のように記述しても良いですよ。
トークンチェックの方は、受け取り側で
のように記述すればOKです。
これだけ。
おー、楽ですな(*´ェ`*)
いらないかもしれませんけど、一応サンプル置いておきますねφ(--)
■fuel\app\classes\controller\test08.php
■fuel\app\views\template_hoge08.php
完了\(--)/
■埋め込み
<input type="hidden" name="<?php echo Config::get('security.csrf_token_key'); ?>" value="<?php echo Security::fetch_token(); ?>" />
もしくは
<?php echo \Form::csrf(); ?>
■チェック
if(!\Security::check_token()){}
でOKです。
CSRF対策としてセキュリティトークンを使う方法は一般的ですが、
FuelPHPさんではあらかじめそれ用の部品が用意されているのだそうな。
まずトークン埋め込みの方は、フォーム内に
<input type="hidden" name="<?php echo Config::get('security.csrf_token_key'); ?>" value="<?php echo Security::fetch_token(); ?>" />
のような形で記述すれば埋め込まれます。
Config::get('security.csrf_token_key');
でトークンの名前(name属性)を取得して、
Security::fetch_token();
で実際のトークン値を取得している訳ですな。
トークンの名前は
fuel\core\config\config.php
か
fuel\app\config\config.php
の中に
'security' => array(
/**
* If true, every HTTP request of the type speficied in autoload_methods
* will be checked for a CSRF token. If not present or not valid, a
* security exception will be thrown.
*/
'csrf_autoload' => false,
'csrf_autoload_methods' => array('post', 'put', 'delete'),
/**
* Name of the form field that holds the CSRF token.
*/
'csrf_token_key' => 'fuel_csrf_token',
~~ 中略 ~~
),
/**
* If true, every HTTP request of the type speficied in autoload_methods
* will be checked for a CSRF token. If not present or not valid, a
* security exception will be thrown.
*/
'csrf_autoload' => false,
'csrf_autoload_methods' => array('post', 'put', 'delete'),
/**
* Name of the form field that holds the CSRF token.
*/
'csrf_token_key' => 'fuel_csrf_token',
~~ 中略 ~~
),
とか書いてあるので好きに変えてください。
ちなみにトークンの埋め込みはFormクラスを使って
<?php echo \Form::csrf(); ?>
のように記述しても良いですよ。
トークンチェックの方は、受け取り側で
if(!\Security::check_token()){
//トークンチェックNG
}
//トークンチェックNG
}
のように記述すればOKです。
これだけ。
おー、楽ですな(*´ェ`*)
いらないかもしれませんけど、一応サンプル置いておきますねφ(--)
■fuel\app\classes\controller\test08.php
<?php
class Controller_Test08 extends Controller_Template
{
public $template = 'template_hoge08';
public function action_index()
{
print "index";
}
public function action_hoge()
{
if(!\Security::check_token()){
//トークンチェックNG
die('Invalid');
}else{
//トークンチェックOK
print "hoge";
}
}
}
class Controller_Test08 extends Controller_Template
{
public $template = 'template_hoge08';
public function action_index()
{
print "index";
}
public function action_hoge()
{
if(!\Security::check_token()){
//トークンチェックNG
die('Invalid');
}else{
//トークンチェックOK
print "hoge";
}
}
}
■fuel\app\views\template_hoge08.php
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<form method="post" action="http://192.168.X.XXX/hoge/public/test08/hoge">
<?php // やり方その1 ?>
<input type="hidden" name="<?php echo Config::get('security.csrf_token_key'); ?>" value="<?php echo Security::fetch_token(); ?>" />
<?php /************************* ?>
<?php // やり方その2 ?>
<?php echo \Form::csrf(); ?>
<?php *************************/ ?>
<input type="submit" value="send">
</form>
</body>
</html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<form method="post" action="http://192.168.X.XXX/hoge/public/test08/hoge">
<?php // やり方その1 ?>
<input type="hidden" name="<?php echo Config::get('security.csrf_token_key'); ?>" value="<?php echo Security::fetch_token(); ?>" />
<?php /************************* ?>
<?php // やり方その2 ?>
<?php echo \Form::csrf(); ?>
<?php *************************/ ?>
<input type="submit" value="send">
</form>
</body>
</html>
完了\(--)/