SessionTest.php 17 KB

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