簡體   English   中英

session_regenerate_id和安全性屬性

[英]session_regenerate_id and security attributes

我有一個奇怪的問題,在我使用

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

);

在本地文件夾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);

您將知道其他所有內容將自動得到處理。


您可以在此處閱讀有關會話安全性的更多信息。

您可以

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

session_start()之前

session_unset, destroy and start不需要session_unset, destroy and start 另外,在覆蓋會話數據時,請勿為$_SESSION分配值。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM