SessionTest.php 17 KB

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