SessionTest.php 18 KB

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