CSRF 保护 Codeigniter 生成随机令牌
CSRF Protection Codeigniter generating Random token
我正在使用 codeigniter,并且我在 config.php 中启用了 csrf,如下所示。
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_token';
$config['csrf_cookie_name'] = 'csrf_cookie';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
然后为了避免错误 "An Error Was Encountered. The action you have requested is not allowed." 我在网络视图中的每个表单中添加了以下代码。
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
此代码生成一个令牌,错误为 cleared.But 由于 <?php echo $this->security->get_csrf_hash(); ?>
,每个页面的令牌都相同。
当我在网页中通过view source查看源代码时,token清晰可见。
我想知道这个方法能防止csrf吗?或者我必须生成一个随机令牌?或者通过 codeigniter 防止 csrf 的最佳方法是什么。
为 html 表格尝试 form_open('controller/method');
。
这将自动创建隐藏类型的随机 csrf 令牌,无需手动操作。
令牌是随机的。但是,Codeigniter 将使用相同的令牌值直到 CSRF cookie 过期,或者,如果 $config['csrf_regenerate'] = TRUE;
它将在每个 POST 请求上创建一个新的令牌值。
GET 请求(即导航到网站上的其他页面)不会生成新令牌。
我正在使用 codeigniter,并且我在 config.php 中启用了 csrf,如下所示。
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_token';
$config['csrf_cookie_name'] = 'csrf_cookie';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
然后为了避免错误 "An Error Was Encountered. The action you have requested is not allowed." 我在网络视图中的每个表单中添加了以下代码。
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
此代码生成一个令牌,错误为 cleared.But 由于 <?php echo $this->security->get_csrf_hash(); ?>
,每个页面的令牌都相同。
当我在网页中通过view source查看源代码时,token清晰可见。
我想知道这个方法能防止csrf吗?或者我必须生成一个随机令牌?或者通过 codeigniter 防止 csrf 的最佳方法是什么。
为 html 表格尝试 form_open('controller/method');
。
这将自动创建隐藏类型的随机 csrf 令牌,无需手动操作。
令牌是随机的。但是,Codeigniter 将使用相同的令牌值直到 CSRF cookie 过期,或者,如果 $config['csrf_regenerate'] = TRUE;
它将在每个 POST 请求上创建一个新的令牌值。
GET 请求(即导航到网站上的其他页面)不会生成新令牌。