SessionTest.php 18 KB

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