SessionTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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('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->assertSame('1,2,3', $result);
  110. $session->write('testing', ['1' => 'one', '2' => 'two', '3' => 'three']);
  111. $result = $session->read('testing.1');
  112. $this->assertSame('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->assertSame('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->assertSame('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->assertSame('value', $session->read('Some.string'));
  186. $value = $session->consume('Some.string');
  187. $this->assertSame('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 testCloseNotStarted()
  234. {
  235. $session = new Session();
  236. $this->assertTrue($session->start());
  237. $session->close();
  238. $this->assertFalse($session->started());
  239. }
  240. /**
  241. * testClear method
  242. *
  243. * @return void
  244. */
  245. public function testClear()
  246. {
  247. $session = new Session();
  248. $session->write('Delete.me', 'Clearing out');
  249. $session->clear();
  250. $this->assertFalse($session->check('Delete.me'));
  251. $this->assertFalse($session->check('Delete'));
  252. }
  253. /**
  254. * testDelete method
  255. *
  256. * @return void
  257. */
  258. public function testDelete()
  259. {
  260. $session = new Session();
  261. $session->write('Delete.me', 'Clearing out');
  262. $session->delete('Delete.me');
  263. $this->assertFalse($session->check('Delete.me'));
  264. $this->assertTrue($session->check('Delete'));
  265. $session->write('Clearing.sale', 'everything must go');
  266. $session->delete('');
  267. $this->assertTrue($session->check('Clearing.sale'));
  268. $session->delete('Clearing');
  269. $this->assertFalse($session->check('Clearing.sale'));
  270. $this->assertFalse($session->check('Clearing'));
  271. }
  272. /**
  273. * test delete
  274. *
  275. * @return void
  276. */
  277. public function testDeleteEmptyString()
  278. {
  279. $session = new Session();
  280. $session->write('', 'empty string');
  281. $session->delete('');
  282. $this->assertFalse($session->check(''));
  283. }
  284. /**
  285. * testDestroy method
  286. *
  287. * @return void
  288. */
  289. public function testDestroy()
  290. {
  291. $session = new Session();
  292. $session->start();
  293. $session->write('bulletProof', 'invincible');
  294. $session->id('foo');
  295. $session->destroy();
  296. $this->assertFalse($session->check('bulletProof'));
  297. }
  298. /**
  299. * testCheckingSavedEmpty method
  300. *
  301. * @return void
  302. */
  303. public function testCheckingSavedEmpty()
  304. {
  305. $session = new Session();
  306. $session->write('SessionTestCase', 0);
  307. $this->assertTrue($session->check('SessionTestCase'));
  308. $session->write('SessionTestCase', '0');
  309. $this->assertTrue($session->check('SessionTestCase'));
  310. $session->write('SessionTestCase', false);
  311. $this->assertTrue($session->check('SessionTestCase'));
  312. $session->write('SessionTestCase', null);
  313. $this->assertFalse($session->check('SessionTestCase'));
  314. }
  315. /**
  316. * testCheckKeyWithSpaces method
  317. *
  318. * @return void
  319. */
  320. public function testCheckKeyWithSpaces()
  321. {
  322. $session = new Session();
  323. $session->write('Session Test', 'test');
  324. $this->assertTrue($session->check('Session Test'));
  325. $session->delete('Session Test');
  326. $session->write('Session Test.Test Case', 'test');
  327. $this->assertTrue($session->check('Session Test.Test Case'));
  328. }
  329. /**
  330. * testCheckEmpty
  331. *
  332. * @return void
  333. */
  334. public function testCheckEmpty()
  335. {
  336. $session = new Session();
  337. $this->assertFalse($session->check());
  338. }
  339. /**
  340. * test key exploitation
  341. *
  342. * @return void
  343. */
  344. public function testKeyExploit()
  345. {
  346. $session = new Session();
  347. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  348. $session->write($key, 'haxored');
  349. $result = $session->read($key);
  350. $this->assertNull($result);
  351. }
  352. /**
  353. * testReadingSavedEmpty method
  354. *
  355. * @return void
  356. */
  357. public function testReadingSavedEmpty()
  358. {
  359. $session = new Session();
  360. $session->write('', 'empty string');
  361. $this->assertTrue($session->check(''));
  362. $this->assertSame('empty string', $session->read(''));
  363. $session->write('SessionTestCase', 0);
  364. $this->assertSame(0, $session->read('SessionTestCase'));
  365. $session->write('SessionTestCase', '0');
  366. $this->assertSame('0', $session->read('SessionTestCase'));
  367. $this->assertNotSame($session->read('SessionTestCase'), 0);
  368. $session->write('SessionTestCase', false);
  369. $this->assertFalse($session->read('SessionTestCase'));
  370. $session->write('SessionTestCase', null);
  371. $this->assertNull($session->read('SessionTestCase'));
  372. }
  373. /**
  374. * test using a handler from app/Http/Session.
  375. *
  376. * @preserveGlobalState disabled
  377. * @runInSeparateProcess
  378. * @return void
  379. */
  380. public function testUsingAppLibsHandler()
  381. {
  382. static::setAppNamespace();
  383. $config = [
  384. 'defaults' => 'cake',
  385. 'handler' => [
  386. 'engine' => 'TestAppLibSession',
  387. 'these' => 'are',
  388. 'a few' => 'options',
  389. ],
  390. ];
  391. $session = Session::create($config);
  392. $this->assertInstanceOf('TestApp\Http\Session\TestAppLibSession', $session->engine());
  393. $this->assertSame('user', ini_get('session.save_handler'));
  394. $this->assertEquals(['these' => 'are', 'a few' => 'options'], $session->engine()->options);
  395. }
  396. /**
  397. * test using a handler from a plugin.
  398. *
  399. * @preserveGlobalState disabled
  400. * @runInSeparateProcess
  401. * @return void
  402. */
  403. public function testUsingPluginHandler()
  404. {
  405. static::setAppNamespace();
  406. $this->loadPlugins(['TestPlugin']);
  407. $config = [
  408. 'defaults' => 'cake',
  409. 'handler' => [
  410. 'engine' => 'TestPlugin.TestPluginSession',
  411. ],
  412. ];
  413. $session = Session::create($config);
  414. $this->assertInstanceOf('TestPlugin\Http\Session\TestPluginSession', $session->engine());
  415. $this->assertSame('user', ini_get('session.save_handler'));
  416. }
  417. /**
  418. * Tests that it is possible to pass an already made instance as the session engine
  419. *
  420. * @preserveGlobalState disabled
  421. * @runInSeparateProcess
  422. * @return void
  423. */
  424. public function testEngineWithPreMadeInstance()
  425. {
  426. static::setAppNamespace();
  427. $engine = new TestAppLibSession();
  428. $session = new Session(['handler' => ['engine' => $engine]]);
  429. $this->assertSame($engine, $session->engine());
  430. $session = new Session();
  431. $session->engine($engine);
  432. $this->assertSame($engine, $session->engine());
  433. }
  434. /**
  435. * Tests instantiating a missing engine
  436. *
  437. * @return void
  438. */
  439. public function testBadEngine()
  440. {
  441. $this->expectException(InvalidArgumentException::class);
  442. $this->expectExceptionMessage('The class "Derp" does not exist and cannot be used as a session engine');
  443. $session = new Session();
  444. $session->engine('Derp');
  445. }
  446. /**
  447. * Test that cookieTimeout matches timeout when unspecified.
  448. *
  449. * @preserveGlobalState disabled
  450. * @runInSeparateProcess
  451. * @return void
  452. */
  453. public function testCookieTimeoutFallback()
  454. {
  455. $config = [
  456. 'defaults' => 'cake',
  457. 'timeout' => 400,
  458. ];
  459. new Session($config);
  460. $this->assertSame('0', ini_get('session.cookie_lifetime'));
  461. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  462. }
  463. /**
  464. * Tests that the cookie name can be changed with configuration
  465. *
  466. * @preserveGlobalState disabled
  467. * @runInSeparateProcess
  468. * @return void
  469. */
  470. public function testSessionName()
  471. {
  472. new Session(['cookie' => 'made_up_name']);
  473. $this->assertSame('made_up_name', session_name());
  474. }
  475. /**
  476. * Test that a call of check() starts the session when cookies are disabled in php.ini
  477. *
  478. * @preserveGlobalState disabled
  479. * @runInSeparateProcess
  480. */
  481. public function testCheckStartsSessionWithCookiesDisabled()
  482. {
  483. $_COOKIE = [];
  484. $_GET = [];
  485. $session = new TestWebSession([
  486. 'ini' => [
  487. 'session.use_cookies' => 0,
  488. 'session.use_trans_sid' => 0,
  489. ],
  490. ]);
  491. $this->assertFalse($session->started());
  492. $session->check('something');
  493. $this->assertTrue($session->started());
  494. }
  495. /**
  496. * Test that a call of check() starts the session when a cookie is already set
  497. */
  498. public function testCheckStartsSessionWithCookie()
  499. {
  500. $_COOKIE[session_name()] = '123abc';
  501. $_GET = [];
  502. $session = new TestWebSession([
  503. 'ini' => [
  504. 'session.use_cookies' => 1,
  505. 'session.use_trans_sid' => 0,
  506. ],
  507. ]);
  508. $this->assertFalse($session->started());
  509. $session->check('something');
  510. $this->assertTrue($session->started());
  511. }
  512. /**
  513. * Test that a call of check() starts the session when the session ID is passed via URL and session.use_trans_sid is enabled
  514. *
  515. * @preserveGlobalState disabled
  516. * @runInSeparateProcess
  517. * @return void
  518. */
  519. public function testCheckStartsSessionWithSIDinURL()
  520. {
  521. $_COOKIE = [];
  522. $_GET[session_name()] = '123abc';
  523. $session = new TestWebSession([
  524. 'ini' => [
  525. 'session.use_cookies' => 1,
  526. 'session.use_trans_sid' => 1,
  527. ],
  528. ]);
  529. $this->assertFalse($session->started());
  530. $session->check('something');
  531. $this->assertTrue($session->started());
  532. }
  533. /**
  534. * 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
  535. */
  536. public function testCheckDoesntStartSessionWithoutTransSID()
  537. {
  538. $_COOKIE = [];
  539. $_GET[session_name()] = '123abc';
  540. $session = new TestWebSession([
  541. 'ini' => [
  542. 'session.use_cookies' => 1,
  543. 'session.use_trans_sid' => 0,
  544. ],
  545. ]);
  546. $this->assertFalse($session->started());
  547. $session->check('something');
  548. $this->assertFalse($session->started());
  549. }
  550. }