SessionTest.php 19 KB

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