在codeigniter中使用HTTPOnly标志设置cookie

时间:2012-05-09 20:45:36

标签: codeigniter cookies codeigniter-2 xss

我想知道如何在Codeigniter中将cookie设置为HTTPOnly。我查看了文档,但没有看到如何设置此标志。

我还想设置cookie的安全标志,并在config.php文件中找到它:

$config['cookie_secure'] = TRUE;

但是config.php中没有HTTPOnly选项。

如何将所有Cookie设置为HTTPOnly?如果它在框架中完成,那么知道该代码在我自己的学习中的位置(以及默认值是什么)会很有帮助。

4 个答案:

答案 0 :(得分:27)

幸运的是,您可以在GitHub上查看Session.php的源代码

在功能_set_cookie中,您会看到:

// Set the cookie
setcookie(
    $this->sess_cookie_name,
    $cookie_data,
    $expire,
    $this->cookie_path,
    $this->cookie_domain,
    $this->cookie_secure,
    $this->cookie_httponly
);

$this->cookie_httponly的值在__construct中分配,默认值为FALSE,但您可以通过config.php将其设置为TRUE,如下所示:

$config['cookie_httponly'] = TRUE;

这将使您的框架内的cookie成为HTTPOnly。

答案 1 :(得分:9)

我发现他们是一个drwaback会话mgmt的codeigniter,因为

-

  1. 2.1.3版本他们没有cookie_httponly标志。
    解决方案:
    步骤1:您需要在库中创建自己的My_session.php并扩展系统会话文件或修改Session.php(在system-> library-> Session.php文件中)
    第2步:找到_set_cookie($ cookie_data = NULL)函数,转到setcookie()并修改为

    setcookie( $this->sess_cookie_name, $cookie_data, $expire,
    $this->cookie_path, $this->cookie_domain, $this->cookie_secure, TRUE ); // add True flag.

  2. 即使您要添加" TRUE"如果使用OWASP ZAP工具进行测试,有一段时间它会给你CSRF问题,即:HTTPONLY不成立,
    安全标志不启用。

    原因:因为在sess_destroy时,他们将HTTPONLY和Secure标志设置为none。
    解决方案:将Session.php中的sess_destroy函数更新为

     // Kill the cookie 
    `setcookie(
              $this->sess_cookie_name,
             addslashes(serialize(array())),
             ($this->now - 31500000),
             $this->cookie_path,
             $this->cookie_domain,
             TRUE,
             TRUE 
         );`
    
  3. 由于这些问题给了我不眠之夜,所以我希望这对你也有帮助。 :)

答案 2 :(得分:3)

2.1.3在foreach中不包含cookie_httponly,因此不会设置它。

foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key)

也没有在这里设置...

    setcookie(
                $this->sess_cookie_name,
                $cookie_data,
                $expire,
                $this->cookie_path,
                $this->cookie_domain,
                $this->cookie_secure
            );

答案 3 :(得分:0)

该功能现在在v2中确实存在,请参见以下config.php:

  

| 'cookie_httponly'= Cookie仅可通过HTTP(S)访问(无javascript)

     

$ config ['cookie_httponly'] = TRUE;

相关问题