SessionTest.php 17 KB

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