Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 104cc1d

Browse files
committed
Merge pull request #40 from pine3ree/add-create-session-cookie
parse boolean ini settings when creating the response cookie
2 parents 5ebdb3c + bc0edcb commit 104cc1d

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

src/PhpSessionPersistence.php

+32-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
use function sprintf;
3232
use function time;
3333

34+
use const FILTER_VALIDATE_BOOLEAN;
35+
use const FILTER_NULL_ON_FAILURE;
36+
3437
/**
3538
* Session persistence using ext-session.
3639
*
@@ -119,12 +122,7 @@ public function persistSession(SessionInterface $session, ResponseInterface $res
119122
return $response;
120123
}
121124

122-
$sessionCookie = SetCookie::create(session_name())
123-
->withValue($id)
124-
->withPath(ini_get('session.cookie_path'))
125-
->withDomain(ini_get('session.cookie_domain'))
126-
->withSecure(ini_get('session.cookie_secure'))
127-
->withHttpOnly(ini_get('session.cookie_httponly'));
125+
$sessionCookie = $this->createSessionCookie(session_name(), $id);
128126

129127
if ($cookieLifetime = $this->getCookieLifetime($session)) {
130128
$sessionCookie = $sessionCookie->withExpires(time() + $cookieLifetime);
@@ -182,6 +180,34 @@ private function generateSessionId() : string
182180
return bin2hex(random_bytes(16));
183181
}
184182

183+
/**
184+
* Build a SetCookie parsing boolean ini settings
185+
*
186+
* @param string $name The session name as the cookie name
187+
* @param string $id The session id as the cookie value
188+
* @return SetCookie
189+
*/
190+
private function createSessionCookie(string $name, string $id) : SetCookie
191+
{
192+
$secure = filter_var(
193+
ini_get('session.cookie_secure'),
194+
FILTER_VALIDATE_BOOLEAN,
195+
FILTER_NULL_ON_FAILURE
196+
);
197+
$httpOnly = filter_var(
198+
ini_get('session.cookie_httponly'),
199+
FILTER_VALIDATE_BOOLEAN,
200+
FILTER_NULL_ON_FAILURE
201+
);
202+
203+
return SetCookie::create($name)
204+
->withValue($id)
205+
->withPath(ini_get('session.cookie_path'))
206+
->withDomain(ini_get('session.cookie_domain'))
207+
->withSecure($secure)
208+
->withHttpOnly($httpOnly);
209+
}
210+
185211
/**
186212
* Generate cache http headers for this instance's session cache_limiter and
187213
* cache_expire values

test/PhpSessionPersistenceTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,52 @@ public function testOnlyOneSessionFileIsCreatedIfNoSessionCookiePresentINFirstRe
746746

747747
$this->restoreOriginalSessionIniSettings($ini);
748748
}
749+
750+
/**
751+
* @dataProvider cookieSettingsProvider
752+
*/
753+
public function testThatSetCookieCorrectlyInterpretsIniSettings(
754+
$secureIni,
755+
$httpOnlyIni,
756+
$expectedSecure,
757+
$expectedHttpOnly
758+
) {
759+
$ini = $this->applyCustomSessionOptions([
760+
'cookie_secure' => $secureIni,
761+
'cookie_httponly' => $httpOnlyIni,
762+
]);
763+
764+
$persistence = new PhpSessionPersistence();
765+
766+
$createSessionCookie = new ReflectionMethod($persistence, 'createSessionCookie');
767+
$createSessionCookie->setAccessible(true);
768+
769+
$setCookie = $createSessionCookie->invokeArgs(
770+
$persistence,
771+
['SETCOOKIESESSIONID', 'set-cookie-test-value']
772+
);
773+
774+
$this->assertSame($expectedSecure, $setCookie->getSecure());
775+
$this->assertSame($expectedHttpOnly, $setCookie->getHttpOnly());
776+
777+
$this->restoreOriginalSessionIniSettings($ini);
778+
}
779+
780+
public function cookieSettingsProvider()
781+
{
782+
// obvious input/results data are left (commented out) for reference
783+
return [
784+
//[false, false, false, false],
785+
//[0, 0, false, false],
786+
//['0', '0', false, false],
787+
//['', '', false, false],
788+
['off', 'off', false, false],
789+
['Off', 'Off', false, false],
790+
//[true, true, true, true],
791+
//[1, 1, true, true],
792+
//['1', '1', true, true],
793+
//['on', 'on', true, true],
794+
//['On', 'On', true, true],
795+
];
796+
}
749797
}

0 commit comments

Comments
 (0)