Session cookies http & secure flag - how do you set these?

后端 未结 3 1051
生来不讨喜
生来不讨喜 2020-12-23 10:16

Just received the results of a security audit - everything clear apart from two things

Session cookie without http flag.

Session cookie without secure flag s

相关标签:
3条回答
  • 2020-12-23 10:26

    You can set them before you send the header. Just add these line below in you code.

    <?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);
    
    0 讨论(0)
  • 2020-12-23 10:32

    Since you asked for .htaccess, and this setting is PHP_INI_ALL, just put this in your .htaccess:

    php_value session.cookie_httponly 1
    php_value session.cookie_secure 1
    

    Note that session cookies will only be sent with https requests after that. This might come as a surprise if you lose a session in non-secured http page (but like pointed out in the comments, is really the point of the configuration in the first place...).

    0 讨论(0)
  • 2020-12-23 10:43

    I know this specifically said they do not have access to the .ini file but for those who get here via search results the .ini settings look like:

    session.cookie_httponly = 1
    session.cookie_secure = 1
    

    The cookie_secure is already present by default in most ini files but commented out. So uncomment that line and set the 1. The httponly line is also already present but not commented out but defaults to 0. So you must hunt it down and set it.

    0 讨论(0)
提交回复
热议问题