[英]Security cookie_secure CSRF not working
我目前正在使用Codeigniter,并希望在config.php中启用:
$config['cookie_secure'] = TRUE;
我也在使用:
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = '***';
$config['csrf_cookie_name'] = '***';
$config['csrf_expire'] = 7200;
连接仅是https。 但是,当我使用cookie_secure选项时,CSRF不再起作用,并给出以下信息:
The action you have requested is not allowed.
由于这种措施,Codeigniter无法将CSRF存储在cookie中。 我该如何解决? 我喜欢同时使用两种安全措施。
<---编辑--->
<form action="<?php echo base_url().'login/'; ?>" method="post">
<?php echo form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash()); ?>
我也陷入了同样的问题,当我有两个cookie_secure
设置为true, csrf_protection
设置为true。
我首先注意到的是没有设置CSRF cookie。
纵观csrf_set_cookie()
函数/system/core/Security.php
,你会看到:
/**
* Set Cross Site Request Forgery Protection Cookie
*
* @return object
*/
public function csrf_set_cookie()
{
$expire = time() + $this->_csrf_expire;
$secure_cookie = (config_item('cookie_secure') === TRUE) ? 1 : 0;
if ($secure_cookie && (empty($_SERVER['HTTPS']) OR strtolower($_SERVER['HTTPS']) === 'off'))
{
return FALSE;
}
setcookie($this->_csrf_cookie_name, $this->_csrf_hash, $expire, config_item('cookie_path'), config_item('cookie_domain'), $secure_cookie);
log_message('debug', "CRSF cookie Set");
return $this;
}
尽管像大多数人一样,我使用负载平衡器来终止SSL。 各个服务器不知道流量是通过HTTPS传输的,因为流量是通过内部网络上的端口80发送到每个服务器的。 这就是HTTP_X_FORWARDED_PROTO
目的。
我最终在/application/core/MY_Security.php
覆盖了csrf_set_cookie()
来解决此问题:
/**
* Set Cross Site Request Forgery Protection Cookie
*
* @return object
*/
public function csrf_set_cookie()
{
$expire = time() + $this->_csrf_expire;
$secure_cookie = (config_item('cookie_secure') === TRUE) ? 1 : 0;
if (
$secure_cookie &&
(empty($_SERVER['HTTPS']) OR strtolower($_SERVER['HTTPS']) === 'off') &&
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] != 'https')
) {
return FALSE;
}
setcookie($this->_csrf_cookie_name, $this->_csrf_hash, $expire, config_item('cookie_path'), config_item('cookie_domain'), $secure_cookie);
log_message('debug', "CRSF cookie Set");
return $this;
}
看起来这可能可以解决问题,请确保您的代码中包含form_open()
。 根据codeigniter表单帮助程序上的文档 ,
创建带有基本URL的开始表单标签...
这也应该在没有form_hidden()
情况下自动添加csrf。
如果要解决此CODEIGNITER BUG,可以进行如下设置:
$config['sess_cookie_name'] = 'ci_projectname_session';
...
...
$config['cookie_prefix'] = 'ci_projectname_';
如上面的示例,cookie前缀和cookie名称必须以相同的开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.