Skip to content

Commit

Permalink
AD-231 Add samesite option feature to cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
Dani Esteban committed May 9, 2022
1 parent bfe92a5 commit 5346814
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
24 changes: 17 additions & 7 deletions src/Sifo/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ static private function _initDomain()
self::$path = '/';
}

static public function set( $name, $value, $days = 14, $domain = false, $secure = false, $httpOnly = false )
static public function set( $name, $value, $days = 14, $domain = false, $secure = false, $httpOnly = false, string $samesite = null )
{
$domain ?: self::_initDomain();

$expires = 0 == $days
? 0
: time() + ( 86400 * $days );
$expires = 0 == $days ? 0 : time() + ( 86400 * $days );

$result = static::setCookie( $name, $value, $expires, self::$path, self::$domain, $secure, $httpOnly );
$result = static::setCookie( $name, $value, $expires, self::$path, self::$domain, $secure, $httpOnly, $samesite );

if ( !$result )
{
Expand Down Expand Up @@ -138,9 +136,21 @@ static private function _sanitizeCookie( $cookie )
return false;
}

static protected function setCookie(string $name, $value = "", $expires_or_options = 0, $path = "", $domain = "", $secure = false, $httponly = false): bool
static protected function setCookie(string $name, $value = "", $expires_or_options = 0, $path = "", $domain = "", $secure = false, $httponly = false, string $samesite = null): bool
{
return setcookie( $name, $value, $expires_or_options, $path, $domain, $secure, $httponly );
$options = [
'expires' => $expires_or_options,
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'httponly' => $httponly,
];

if ( $samesite !== null) {
$options['samesite'] = $samesite;
}

return setcookie( $name, $value, $options );
}

static protected function domain()
Expand Down
8 changes: 7 additions & 1 deletion test/Sifo/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testCookieIsSetWithExpectedDefaults(): void
TestCookie::getPath($cookieName),
"Path doesn't match with expected."
);
$this->assertNull(TestCookie::getSameSite($cookieName));
$this->assertFalse(TestCookie::isSecure($cookieName));
$this->assertFalse(TestCookie::isHttpOnly($cookieName));
}
Expand All @@ -53,7 +54,7 @@ public function testCookieIsSetWithCustomParameters(): void
$expirationDays = 7;
$defaultExpiration = time() + ( 86400 * $expirationDays );

TestCookie::set($cookieName, 'chips_ahoy', $expirationDays, false, true, true);
TestCookie::set($cookieName, 'chips_ahoy', $expirationDays, false, true, true, 'Lax');

$this->assertEquals(
$defaultExpiration,
Expand All @@ -70,6 +71,11 @@ public function testCookieIsSetWithCustomParameters(): void
TestCookie::getPath($cookieName),
"Path doesn't match with expected."
);
$this->assertSame(
'Lax',
TestCookie::getSameSite($cookieName),
"Same site doesn't match with expected."
);
$this->assertTrue(TestCookie::isSecure($cookieName));
$this->assertTrue(TestCookie::isHttpOnly($cookieName));
}
Expand Down
14 changes: 10 additions & 4 deletions test/Sifo/TestCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ protected static function setCookie(
$path = "",
$domain = "",
$secure = false,
$httponly = false
$httponly = false,
string $samesite = null
): bool {
if ($value === "") {
unset(self::$cookies[$name]);
Expand All @@ -29,10 +30,10 @@ protected static function setCookie(
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'httponly' => $httponly
'httponly' => $httponly,
'samesite' => $samesite,
];


return true;
}

Expand All @@ -43,7 +44,7 @@ public static function getCookieParam(string $name, string $param)
}

if (!array_key_exists($param, self::$cookies[$name] ?? [])) {
throw new InvalidArgumentException("Param $param does not exist in a cookie.");
return null;
}

return self::$cookies[$name][$param];
Expand Down Expand Up @@ -79,6 +80,11 @@ public static function isHttpOnly(string $name): bool
return (bool) self::getCookieParam($name, 'httponly');
}

public static function getSameSite(string $name): ?string
{
return self::getCookieParam($name, 'samesite');
}

public static function clearCookies(): void
{
self::$cookies = [];
Expand Down

0 comments on commit 5346814

Please sign in to comment.