session_regenerate_id 和安全属性

session_regenerate_id and security attributes

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

重新生成 session 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(重新生成的)在 header.[=15 中没有安全的 HttpOnly 标签=]

这是示例header

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 上强制使用这些标签?还是有更好的方法来实现他们的要求?这是我的代码。

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

);

你可以

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

在您的本地文件夹 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