在代码点火器中使用 HTTPOnly 标志设置 cookie



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

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

$config['cookie_secure'] = TRUE;

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

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

幸运的是,

您可以在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。

我发现在编码点火器的会话管理中有一个drwaback,因为

-

  1. 2.1.3版本没有cookie_httponly标志。
    溶液:
    步骤1:您需要在库中创建自己的My_session.php并扩展系统会话文件或修改会话.php(在系统>库>会话.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和安全标志设置为none。
    解决方案:更新会话中的sess_destroy函数.php为

     // Kill the cookie 
    `setcookie(
              $this->sess_cookie_name,
             addslashes(serialize(array())),
             ($this->now - 31500000),
             $this->cookie_path,
             $this->cookie_domain,
             TRUE,
             TRUE 
         );`
    

由于这些问题让我不眠之夜,所以我希望这也能对您有所帮助。 :)

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
            );

此功能现在在 v2 中确实存在,请参阅此配置.php:

|'cookie_httponly' = Cookie只能通过HTTP(S)访问(没有javascript)

$config['cookie_httponly'] = 真;

最新更新