SessionTest.php 17 KB

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