session_regenerate_id和安全性属性

时间:2018-11-06 13:04:00

标签: php session session-cookies

我遇到一个奇怪的问题,在我使用

重新生成会话ID之后
session_regenerate_id(true);

cookie似乎丢失了其“ Secure,HttpOnly”标志。

我可以使用

重置Cookie
$params = session_get_cookie_params();
setcookie("PHPSESSID", session_id(), 0, $params["path"], $params["domain"],
    true,  // this is the secure flag you need to set. Default is false.
    true  // this is the httpOnly flag you need to set

);

但是veracode(我们用于安全性测试的人)不确定地对其进行标记,因为第一个cookie(重新生成的cookie)在标头中没有安全的HttpOnly标记。

这是示例标题

Cache-Control: no-store, no-cache, must-revalidate
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Tue, 06 Nov 2018 12:56:41 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=98
Location: home.php
Pragma: no-cache
Server: Apache
Set-Cookie: PHPSESSID=18a289a6c8d34b0df72dafc9d5e12c92; path=/
Set-Cookie: PHPSESSID=18a289a6c8d34b0df72dafc9d5e12c92; path=/; secure; HttpOnly

Veracode正在标记该问题,因为第一个cookie-没有安全的httpOnly标签。我猜它只读第一个,或者感觉默认情况下它们不显示是不安全的。我该如何在重新生成的会话中强制使用这些标签?还是有更好的方法来实现他们的要求?这是我的代码。

session_start();

$_SESSION = array();
session_unset();
session_destroy();
session_start(); //Not sure if this is needed

session_regenerate_id(true);
$params = session_get_cookie_params();
setcookie("PHPSESSID", session_id(), 0, $params["path"], $params["domain"],
    true,  // this is the secure flag you need to set. Default is false.
    true  // this is the httpOnly flag you need to set

);

2 个答案:

答案 0 :(得分:2)

在本地文件夹PHP.ini设置(通常称为user.ini,位于网站帐户的根HTML目录中)中,您可以设置PHP.ini值:

session.cookie_secure=1
session.cookie_httponly=1
session.use_only_cookies=1

,这意味着此帐户(此网站)对会话cookie的任何使用都将符合上述要求。

这比将这些需求编码到脚本中要好得多,因为很容易错过或忽略这些内容。

您的脚本可以是:

session_start();
...
session_regenerate_id(true);

您将知道其他所有内容都会自动处理。


您可以阅读有关会话安全性HERE的更多信息。

答案 1 :(得分:1)

您可以

session_set_cookie_params ( int $lifetime [, string $path 
       [, string $domain [, bool $secure = FALSE [, bool $httponly = FALSE ]]]] )

session_start()之前

则不需要session_unset, destroy and start。另外,在覆盖会话数据时,请勿为$_SESSION分配值。

https://secure.php.net/manual/en/function.session-set-cookie-params.php

相关问题