SessionTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Network;
  16. use Cake\Http\Session;
  17. use Cake\Http\Session\CacheSession;
  18. use Cake\Http\Session\DatabaseSession;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * TestCacheSession
  22. */
  23. class TestCacheSession extends CacheSession
  24. {
  25. protected function _writeSession()
  26. {
  27. return true;
  28. }
  29. }
  30. /**
  31. * TestDatabaseSession
  32. */
  33. class TestDatabaseSession extends DatabaseSession
  34. {
  35. protected function _writeSession()
  36. {
  37. return true;
  38. }
  39. }
  40. /**
  41. * Overwrite Session to simulate a web session even if the test runs on CLI.
  42. */
  43. class TestWebSession extends Session
  44. {
  45. protected function _hasSession()
  46. {
  47. $isCLI = $this->_isCLI;
  48. $this->_isCLI = false;
  49. $result = parent::_hasSession();
  50. $this->_isCLI = $isCLI;
  51. return $result;
  52. }
  53. }
  54. /**
  55. * SessionTest class
  56. */
  57. class SessionTest extends TestCase
  58. {
  59. protected static $_gcDivisor;
  60. /**
  61. * Fixtures used in the SessionTest
  62. *
  63. * @var array
  64. */
  65. public $fixtures = ['core.cake_sessions', 'core.sessions'];
  66. /**
  67. * tearDown method
  68. *
  69. * @return void
  70. */
  71. public function tearDown()
  72. {
  73. unset($_SESSION);
  74. parent::tearDown();
  75. }
  76. /**
  77. * test setting ini properties with Session configuration.
  78. *
  79. * @preserveGlobalState disabled
  80. * @runInSeparateProcess
  81. * @return void
  82. */
  83. public function testSessionConfigIniSetting()
  84. {
  85. $_SESSION = null;
  86. $config = [
  87. 'cookie' => 'test',
  88. 'checkAgent' => false,
  89. 'timeout' => 86400,
  90. 'ini' => [
  91. 'session.referer_check' => 'example.com',
  92. 'session.use_trans_sid' => false
  93. ]
  94. ];
  95. Session::create($config);
  96. $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
  97. $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
  98. $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
  99. }
  100. /**
  101. * test session cookie path setting
  102. *
  103. * @preserveGlobalState disabled
  104. * @runInSeparateProcess
  105. * @return void
  106. */
  107. public function testCookiePath()
  108. {
  109. ini_set('session.cookie_path', '/foo');
  110. new Session();
  111. $this->assertEquals('/', ini_get('session.cookie_path'));
  112. new Session(['cookiePath' => '/base']);
  113. $this->assertEquals('/base', ini_get('session.cookie_path'));
  114. }
  115. /**
  116. * testCheck method
  117. *
  118. * @return void
  119. */
  120. public function testCheck()
  121. {
  122. $session = new Session();
  123. $session->write('SessionTestCase', 'value');
  124. $this->assertTrue($session->check('SessionTestCase'));
  125. $this->assertFalse($session->check('NotExistingSessionTestCase'));
  126. $this->assertFalse($session->check('Crazy.foo'));
  127. $session->write('Crazy.foo', ['bar' => 'baz']);
  128. $this->assertTrue($session->check('Crazy.foo'));
  129. $this->assertTrue($session->check('Crazy.foo.bar'));
  130. }
  131. /**
  132. * test read with simple values
  133. *
  134. * @return void
  135. */
  136. public function testReadSimple()
  137. {
  138. $session = new Session();
  139. $session->write('testing', '1,2,3');
  140. $result = $session->read('testing');
  141. $this->assertEquals('1,2,3', $result);
  142. $session->write('testing', ['1' => 'one', '2' => 'two', '3' => 'three']);
  143. $result = $session->read('testing.1');
  144. $this->assertEquals('one', $result);
  145. $result = $session->read('testing');
  146. $this->assertEquals(['1' => 'one', '2' => 'two', '3' => 'three'], $result);
  147. $result = $session->read();
  148. $this->assertArrayHasKey('testing', $result);
  149. $session->write('This.is.a.deep.array.my.friend', 'value');
  150. $result = $session->read('This.is.a.deep.array');
  151. $this->assertEquals(['my' => ['friend' => 'value']], $result);
  152. }
  153. /**
  154. * testReadEmpty
  155. *
  156. * @return void
  157. */
  158. public function testReadEmpty()
  159. {
  160. $session = new Session();
  161. $this->assertNull($session->read(''));
  162. }
  163. /**
  164. * Test writing simple keys
  165. *
  166. * @return void
  167. */
  168. public function testWriteSimple()
  169. {
  170. $session = new Session();
  171. $session->write('', 'empty');
  172. $this->assertEquals('empty', $session->read(''));
  173. $session->write('Simple', ['values']);
  174. $this->assertEquals(['values'], $session->read('Simple'));
  175. }
  176. /**
  177. * test writing a hash of values
  178. *
  179. * @return void
  180. */
  181. public function testWriteArray()
  182. {
  183. $session = new Session();
  184. $session->write([
  185. 'one' => 1,
  186. 'two' => 2,
  187. 'three' => ['something'],
  188. 'null' => null
  189. ]);
  190. $this->assertEquals(1, $session->read('one'));
  191. $this->assertEquals(['something'], $session->read('three'));
  192. $this->assertNull($session->read('null'));
  193. }
  194. /**
  195. * Test overwriting a string value as if it were an array.
  196. *
  197. * @return void
  198. */
  199. public function testWriteOverwriteStringValue()
  200. {
  201. $session = new Session();
  202. $session->write('Some.string', 'value');
  203. $this->assertEquals('value', $session->read('Some.string'));
  204. $session->write('Some.string.array', ['values']);
  205. $this->assertEquals(['values'], $session->read('Some.string.array'));
  206. }
  207. /**
  208. * Test consuming session data.
  209. *
  210. * @return void
  211. */
  212. public function testConsume()
  213. {
  214. $session = new Session();
  215. $session->write('Some.string', 'value');
  216. $session->write('Some.array', ['key1' => 'value1', 'key2' => 'value2']);
  217. $this->assertEquals('value', $session->read('Some.string'));
  218. $value = $session->consume('Some.string');
  219. $this->assertEquals('value', $value);
  220. $this->assertFalse($session->check('Some.string'));
  221. $value = $session->consume('');
  222. $this->assertNull($value);
  223. $value = $session->consume(null);
  224. $this->assertNull($value);
  225. $value = $session->consume('Some.array');
  226. $expected = ['key1' => 'value1', 'key2' => 'value2'];
  227. $this->assertEquals($expected, $value);
  228. $this->assertFalse($session->check('Some.array'));
  229. }
  230. /**
  231. * testId method
  232. *
  233. * @preserveGlobalState disabled
  234. * @runInSeparateProcess
  235. * @return void
  236. */
  237. public function testId()
  238. {
  239. $session = new Session();
  240. $session->start();
  241. $result = $session->id();
  242. $this->assertNotEmpty($result);
  243. $this->assertSame(session_id(), $result);
  244. $session->id('MySessionId');
  245. $this->assertSame('MySessionId', $session->id());
  246. $this->assertSame('MySessionId', session_id());
  247. $session->id('');
  248. $this->assertSame('', session_id());
  249. }
  250. /**
  251. * testStarted method
  252. *
  253. * @return void
  254. */
  255. public function testStarted()
  256. {
  257. $session = new Session();
  258. $this->assertFalse($session->started());
  259. $this->assertTrue($session->start());
  260. $this->assertTrue($session->started());
  261. }
  262. /**
  263. * testClear method
  264. *
  265. * @return void
  266. */
  267. public function testClear()
  268. {
  269. $session = new Session();
  270. $session->write('Delete.me', 'Clearing out');
  271. $session->clear();
  272. $this->assertFalse($session->check('Delete.me'));
  273. $this->assertFalse($session->check('Delete'));
  274. }
  275. /**
  276. * testDelete method
  277. *
  278. * @return void
  279. */
  280. public function testDelete()
  281. {
  282. $session = new Session();
  283. $session->write('Delete.me', 'Clearing out');
  284. $session->delete('Delete.me');
  285. $this->assertFalse($session->check('Delete.me'));
  286. $this->assertTrue($session->check('Delete'));
  287. $session->write('Clearing.sale', 'everything must go');
  288. $session->delete('');
  289. $this->assertTrue($session->check('Clearing.sale'));
  290. $session->delete(null);
  291. $this->assertTrue($session->check('Clearing.sale'));
  292. $session->delete('Clearing');
  293. $this->assertFalse($session->check('Clearing.sale'));
  294. $this->assertFalse($session->check('Clearing'));
  295. }
  296. /**
  297. * test delete
  298. *
  299. * @return void
  300. */
  301. public function testDeleteEmptyString()
  302. {
  303. $session = new Session();
  304. $session->write('', 'empty string');
  305. $session->delete('');
  306. $this->assertFalse($session->check(''));
  307. }
  308. /**
  309. * testDestroy method
  310. *
  311. * @return void
  312. */
  313. public function testDestroy()
  314. {
  315. $session = new Session();
  316. $session->start();
  317. $session->write('bulletProof', 'invincible');
  318. $session->id('foo');
  319. $session->destroy();
  320. $this->assertFalse($session->check('bulletProof'));
  321. }
  322. /**
  323. * testCheckingSavedEmpty method
  324. *
  325. * @return void
  326. */
  327. public function testCheckingSavedEmpty()
  328. {
  329. $session = new Session();
  330. $session->write('SessionTestCase', 0);
  331. $this->assertTrue($session->check('SessionTestCase'));
  332. $session->write('SessionTestCase', '0');
  333. $this->assertTrue($session->check('SessionTestCase'));
  334. $session->write('SessionTestCase', false);
  335. $this->assertTrue($session->check('SessionTestCase'));
  336. $session->write('SessionTestCase', null);
  337. $this->assertFalse($session->check('SessionTestCase'));
  338. }
  339. /**
  340. * testCheckKeyWithSpaces method
  341. *
  342. * @return void
  343. */
  344. public function testCheckKeyWithSpaces()
  345. {
  346. $session = new Session();
  347. $session->write('Session Test', 'test');
  348. $this->assertTrue($session->check('Session Test'));
  349. $session->delete('Session Test');
  350. $session->write('Session Test.Test Case', 'test');
  351. $this->assertTrue($session->check('Session Test.Test Case'));
  352. }
  353. /**
  354. * testCheckEmpty
  355. *
  356. * @return void
  357. */
  358. public function testCheckEmpty()
  359. {
  360. $session = new Session();
  361. $this->assertFalse($session->check());
  362. }
  363. /**
  364. * test key exploitation
  365. *
  366. * @return void
  367. */
  368. public function testKeyExploit()
  369. {
  370. $session = new Session();
  371. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  372. $session->write($key, 'haxored');
  373. $result = $session->read($key);
  374. $this->assertNull($result);
  375. }
  376. /**
  377. * testReadingSavedEmpty method
  378. *
  379. * @return void
  380. */
  381. public function testReadingSavedEmpty()
  382. {
  383. $session = new Session();
  384. $session->write('', 'empty string');
  385. $this->assertTrue($session->check(''));
  386. $this->assertEquals('empty string', $session->read(''));
  387. $session->write('SessionTestCase', 0);
  388. $this->assertEquals(0, $session->read('SessionTestCase'));
  389. $session->write('SessionTestCase', '0');
  390. $this->assertEquals('0', $session->read('SessionTestCase'));
  391. $this->assertNotSame($session->read('SessionTestCase'), 0);
  392. $session->write('SessionTestCase', false);
  393. $this->assertFalse($session->read('SessionTestCase'));
  394. $session->write('SessionTestCase', null);
  395. $this->assertNull($session->read('SessionTestCase'));
  396. }
  397. /**
  398. * test using a handler from app/Http/Session.
  399. *
  400. * @preserveGlobalState disabled
  401. * @runInSeparateProcess
  402. * @return void
  403. */
  404. public function testUsingAppLibsHandler()
  405. {
  406. static::setAppNamespace();
  407. $config = [
  408. 'defaults' => 'cake',
  409. 'handler' => [
  410. 'engine' => 'TestAppLibSession',
  411. 'these' => 'are',
  412. 'a few' => 'options'
  413. ]
  414. ];
  415. $session = Session::create($config);
  416. $this->assertInstanceOf('TestApp\Http\Session\TestAppLibSession', $session->engine());
  417. $this->assertEquals('user', ini_get('session.save_handler'));
  418. $this->assertEquals(['these' => 'are', 'a few' => 'options'], $session->engine()->options);
  419. }
  420. /**
  421. * test using a handler from a plugin.
  422. *
  423. * @preserveGlobalState disabled
  424. * @runInSeparateProcess
  425. * @return void
  426. */
  427. public function testUsingPluginHandler()
  428. {
  429. static::setAppNamespace();
  430. \Cake\Core\Plugin::load('TestPlugin');
  431. $config = [
  432. 'defaults' => 'cake',
  433. 'handler' => [
  434. 'engine' => 'TestPlugin.TestPluginSession'
  435. ]
  436. ];
  437. $session = Session::create($config);
  438. $this->assertInstanceOf('TestPlugin\Http\Session\TestPluginSession', $session->engine());
  439. $this->assertEquals('user', ini_get('session.save_handler'));
  440. }
  441. /**
  442. * Tests that it is possible to pass an already made instance as the session engine
  443. *
  444. * @preserveGlobalState disabled
  445. * @runInSeparateProcess
  446. * @return void
  447. */
  448. public function testEngineWithPreMadeInstance()
  449. {
  450. static::setAppNamespace();
  451. $engine = new \TestApp\Http\Session\TestAppLibSession;
  452. $session = new Session(['handler' => ['engine' => $engine]]);
  453. $this->assertSame($engine, $session->engine());
  454. $session = new Session();
  455. $session->engine($engine);
  456. $this->assertSame($engine, $session->engine());
  457. }
  458. /**
  459. * Tests instantiating a missing engine
  460. *
  461. * @return void
  462. */
  463. public function testBadEngine()
  464. {
  465. // Not actually deprecated, but we need to supress the deprecation warning.
  466. $this->deprecated(function () {
  467. $this->expectException(\InvalidArgumentException::class);
  468. $this->expectExceptionMessage('The class "Derp" does not exist and cannot be used as a session engine');
  469. $session = new Session();
  470. $session->engine('Derp');
  471. });
  472. }
  473. /**
  474. * Test that cookieTimeout matches timeout when unspecified.
  475. *
  476. * @preserveGlobalState disabled
  477. * @runInSeparateProcess
  478. * @return void
  479. */
  480. public function testCookieTimeoutFallback()
  481. {
  482. $config = [
  483. 'defaults' => 'cake',
  484. 'timeout' => 400,
  485. ];
  486. new Session($config);
  487. $this->assertEquals(0, ini_get('session.cookie_lifetime'));
  488. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  489. }
  490. /**
  491. * Tests that the cookie name can be changed with configuration
  492. *
  493. * @preserveGlobalState disabled
  494. * @runInSeparateProcess
  495. * @return void
  496. */
  497. public function testSessionName()
  498. {
  499. new Session(['cookie' => 'made_up_name']);
  500. $this->assertEquals('made_up_name', session_name());
  501. }
  502. /**
  503. * Test that a call of check() starts the session when cookies are disabled in php.ini
  504. *
  505. * @preserveGlobalState disabled
  506. * @runInSeparateProcess
  507. */
  508. public function testCheckStartsSessionWithCookiesDisabled()
  509. {
  510. $_COOKIE = [];
  511. $_GET = [];
  512. $session = new TestWebSession([
  513. 'ini' => [
  514. 'session.use_cookies' => 0,
  515. 'session.use_trans_sid' => 0,
  516. ]
  517. ]);
  518. $this->assertFalse($session->started());
  519. $session->check('something');
  520. $this->assertTrue($session->started());
  521. }
  522. /**
  523. * Test that a call of check() starts the session when a cookie is already set
  524. */
  525. public function testCheckStartsSessionWithCookie()
  526. {
  527. $_COOKIE[session_name()] = '123abc';
  528. $_GET = [];
  529. $session = new TestWebSession([
  530. 'ini' => [
  531. 'session.use_cookies' => 1,
  532. 'session.use_trans_sid' => 0,
  533. ]
  534. ]);
  535. $this->assertFalse($session->started());
  536. $session->check('something');
  537. $this->assertTrue($session->started());
  538. }
  539. /**
  540. * Test that a call of check() starts the session when the session ID is passed via URL and session.use_trans_sid is enabled
  541. *
  542. * @preserveGlobalState disabled
  543. * @runInSeparateProcess
  544. * @return void
  545. */
  546. public function testCheckStartsSessionWithSIDinURL()
  547. {
  548. $_COOKIE = [];
  549. $_GET[session_name()] = '123abc';
  550. $session = new TestWebSession([
  551. 'ini' => [
  552. 'session.use_cookies' => 1,
  553. 'session.use_trans_sid' => 1,
  554. ]
  555. ]);
  556. $this->assertFalse($session->started());
  557. $session->check('something');
  558. $this->assertTrue($session->started());
  559. }
  560. /**
  561. * Test that a call of check() does not start the session when the session ID is passed via URL and session.use_trans_sid is disabled
  562. */
  563. public function testCheckDoesntStartSessionWithoutTransSID()
  564. {
  565. $_COOKIE = [];
  566. $_GET[session_name()] = '123abc';
  567. $session = new TestWebSession([
  568. 'ini' => [
  569. 'session.use_cookies' => 1,
  570. 'session.use_trans_sid' => 0,
  571. ]
  572. ]);
  573. $this->assertFalse($session->started());
  574. $session->check('something');
  575. $this->assertFalse($session->started());
  576. }
  577. }