【问题标题】:Session cookies http & secure flag - how do you set these?会话 cookie http 和安全标志 - 你如何设置这些?
【发布时间】:2014-04-08 22:20:24
【问题描述】:

刚刚收到安全审核的结果 - 除了两件事之外,一切都很清楚

没有 http 标志的会话 cookie。

没有设置安全标志的会话 cookie。

应用程序是用 php 编码的,修复的建议是:

  1. 使用仅 http 标志设置会话 cookie
  2. 使用安全标志设置会话 cookie

我查看了示例,但不完全了解如何在 Linux 服务器上实现。我无权访问 .ini 文件。是否可以在 htaccess 文件中设置这些?

或者,我在代码中如何以及在何处实现?

【问题讨论】:

  • 您将会话 cookie 发送到哪里,或者在您的应用程序中设置它的机制是什么?这就是你应该实现它的地方。
  • 如果你在谈论默认的 PHPSESSID cookie,这个问题听起来像是 this 的副本
  • 它在代码中设置,用于登录和检索用户数据。可以在htaccess文件中设置吗?
  • 取决于。它是 PHP 会话系统使用的默认 PHPSESSID cookie,还是自定义的?
  • 这是默认的

标签: php linux security


【解决方案1】:

您可以在发送标头之前设置它们。只需在您的代码下面添加这些行。

<?php
// **PREVENTING SESSION HIJACKING**
// Prevents javascript XSS attacks aimed to steal the session ID
ini_set('session.cookie_httponly', 1);

// **PREVENTING SESSION FIXATION**
// Session ID cannot be passed through URLs
ini_set('session.use_only_cookies', 1);

// Uses a secure connection (HTTPS) if possible
ini_set('session.cookie_secure', 1);

【讨论】:

    【解决方案2】:

    由于您要求使用 .htaccess,并且此设置为 PHP_INI_ALL,因此只需将其放入您的 .htaccess:

    php_value session.cookie_httponly 1
    php_value session.cookie_secure 1
    

    请注意,会话 cookie 只会在之后随 https 请求一起发送。如果您在不安全的 http 页面中丢失会话,这可能会让人感到意外(但就像在 cmets 中指出的那样,这确实是首先配置的重点......)。

    【讨论】:

    • 是“令人讨厌的惊喜”,当然它总是值得警告人们 :) 但是防止会话 cookie 通过 http 发送是这里练习的重点。
    • 我正在检查非 https 页面...多么愚蠢的我 0_0。谢谢@els
    【解决方案3】:

    我知道这明确表示他们无权访问 .ini 文件,但对于那些通过搜索结果到达此处的人来说,.ini 设置如下所示:

    session.cookie_httponly = 1
    session.cookie_secure = 1
    

    默认情况下,cookie_secure 已存在于大多数 ini 文件中,但已被注释掉。所以取消注释该行并设置 1。 httponly 行也已经存在,但没有被注释掉,但默认为 0。所以你必须找到它并设置它。

    【讨论】:

      【解决方案4】:

      在使用session_start 开始会话之前,您也可以使用session_set_cookie_params 设置这些参数。

      这是我的 php 会话类的一部分/开始,它自动将一些参数设置为正确的值,而将其他参数设置为一些默认值。您可以通过使用参数$moreoptions 覆盖它们来更改它们。

      class Session {
      
      /**
      * The flag to define if we work under SSL
      * @var bool
      * @access private
      */
      private static bool $IS_SSL;
      
      /**
      * The session cookie parameters
      * @var array<string,mixed>
      * @access private
      */
      private static array $cookieparams = array('lifetime' => 86400,
                                                 'path' => '/',
                                                 'httponly' => true,
                                                 'samesite' => 'Strict');
      
      /**
      * Starts the session with session_start()
      *
      * Note: If the session already has started nothing will happen
      * @param array<string,mixed> $moreoptions   Optional: Array with cookie params to overrule the defaults
      * @param string $sessionname                Optional: Another name for the session
      * @return void
      * @access public
      */
      public static function start(array $moreoptions = array(), string $sessionname = '') : void {
          if (!self::hasStarted()) {
              self::$IS_SSL = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
      
              if (!empty($sessionname)) {
                  session_name($sessionname);
              } elseif (self::$IS_SSL) {
                  session_name('__Secure-PHPSESSID');
              }
      
              self::$cookieparams['domain'] = $_SERVER['SERVER_NAME'];
              self::$cookieparams['secure'] = self::$IS_SSL;
      
              session_set_cookie_params(array_merge(self::$cookieparams, $moreoptions)); 
              session_start();
          }
      }
      
      /**
      * Tests if a session was started
      * @return bool True if a session is running
      * @access public
      */
      public static function hasStarted() : bool {
          return session_status() === PHP_SESSION_ACTIVE;
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-29
        • 2013-01-08
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-13
        • 2022-01-07
        • 1970-01-01
        相关资源
        最近更新 更多