Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add secure and httponly flags to cookies and also add samsite 'l… #461

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Web/scripts/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,23 @@ function Schedule(opts, resourceGroups) {
this.initRotateSchedule = function () {
$('#schedule-actions .schedule-style').click(function (e) {
e.preventDefault();
createCookie(opts.cookieName, $(this).attr('schedule-display'), 30, opts.scriptUrl);
window.location.reload();

var scheduleDisplay = $(this).attr('schedule-display');

// Validate if schedule-display is an integer
var isInteger = /^[0-9]+$/.test(scheduleDisplay);

if (isInteger) {

// If is valid cerate a normal cookie
createCookie(opts.cookieName, parseInt(scheduleDisplay, 10), 30, opts.scriptUrl);
window.location.reload();
} else {

// Otherwise create a cookie with value 0
createCookie(opts.cookieName, 0, 30, opts.scriptUrl);
window.location.reload();
}
});
};

Expand Down
8 changes: 7 additions & 1 deletion lib/Server/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ class Cookie
public $Value;
public $Expiration;
public $Path;
public $Secure;
public $HttpOnly;
public $SameSite;

public function __construct($name, $value, $expiration = null, $path = null)
public function __construct($name, $value, $expiration = null, $path = null, $secure = true, $httpOnly = true, $sameSite = 'Lax')
{
if (is_null($expiration)) {
$expiration = Date::Now()->AddDays(30)->TimeStamp();
Expand All @@ -29,6 +32,9 @@ public function __construct($name, $value, $expiration = null, $path = null)
$this->Value = $value;
$this->Expiration = $expiration; // date(DATE_COOKIE, $expiration);
$this->Path = $path;
$this->Secure = $secure;
$this->HttpOnly = $httpOnly;
$this->SameSite = $sameSite;
}

public function Delete()
Expand Down
13 changes: 11 additions & 2 deletions lib/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ public function __construct()

public function SetCookie(Cookie $cookie)
{
setcookie($cookie->Name, $cookie->Value, $cookie->Expiration, $cookie->Path);
}
setcookie(
$cookie->Name,
$cookie->Value,
[
'expires' => $cookie->Expiration,
'path' => $cookie->Path,
'secure' => $cookie->Secure,
'httponly' => $cookie->HttpOnly,
'samesite' => $cookie->SameSite
]
); }

public function DeleteCookie(Cookie $cookie)
{
Expand Down
Loading