SessionTest.php 17 KB

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