SessionTest.php 17 KB

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