Chris@0: container->get('session_handler.write_safe'); Chris@0: $this->assertTrue($session_handler->isSessionWritable(), 'session_handler->isSessionWritable() initially returns TRUE.'); Chris@0: $session_handler->setSessionWritable(FALSE); Chris@0: $this->assertFalse($session_handler->isSessionWritable(), '$session_handler->isSessionWritable() returns FALSE after disabling.'); Chris@0: $session_handler->setSessionWritable(TRUE); Chris@0: $this->assertTrue($session_handler->isSessionWritable(), '$session_handler->isSessionWritable() returns TRUE after enabling.'); Chris@0: Chris@0: // Test session hardening code from SA-2008-044. Chris@0: $user = $this->drupalCreateUser(); Chris@0: Chris@0: // Enable sessions. Chris@0: $this->sessionReset($user->id()); Chris@0: Chris@0: // Make sure the session cookie is set as HttpOnly. Chris@0: $this->drupalLogin($user); Chris@0: $this->assertTrue(preg_match('/HttpOnly/i', $this->drupalGetHeader('Set-Cookie', TRUE)), 'Session cookie is set as HttpOnly.'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Verify that the session is regenerated if a module calls exit Chris@0: // in hook_user_login(). Chris@0: $user->name = 'session_test_user'; Chris@0: $user->save(); Chris@0: $this->drupalGet('session-test/id'); Chris@0: $matches = []; Chris@0: preg_match('/\s*session_id:(.*)\n/', $this->getRawContent(), $matches); Chris@0: $this->assertTrue(!empty($matches[1]), 'Found session ID before logging in.'); Chris@0: $original_session = $matches[1]; Chris@0: Chris@0: // We cannot use $this->drupalLogin($user); because we exit in Chris@0: // session_test_user_login() which breaks a normal assertion. Chris@0: $edit = [ Chris@0: 'name' => $user->getUsername(), Chris@0: 'pass' => $user->pass_raw Chris@0: ]; Chris@0: $this->drupalPostForm('user/login', $edit, t('Log in')); Chris@0: $this->drupalGet('user'); Chris@0: $pass = $this->assertText($user->getUsername(), format_string('Found name: %name', ['%name' => $user->getUsername()]), 'User login'); Chris@0: $this->_logged_in = $pass; Chris@0: Chris@0: $this->drupalGet('session-test/id'); Chris@0: $matches = []; Chris@0: preg_match('/\s*session_id:(.*)\n/', $this->getRawContent(), $matches); Chris@0: $this->assertTrue(!empty($matches[1]), 'Found session ID after logging in.'); Chris@0: $this->assertTrue($matches[1] != $original_session, 'Session ID changed after login.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test data persistence via the session_test module callbacks. Chris@0: */ Chris@0: public function testDataPersistence() { Chris@0: $user = $this->drupalCreateUser([]); Chris@0: // Enable sessions. Chris@0: $this->sessionReset($user->id()); Chris@0: Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: $value_1 = $this->randomMachineName(); Chris@0: $this->drupalGet('session-test/set/' . $value_1); Chris@0: $this->assertText($value_1, 'The session value was stored.', 'Session'); Chris@0: $this->drupalGet('session-test/get'); Chris@0: $this->assertText($value_1, 'Session correctly returned the stored data for an authenticated user.', 'Session'); Chris@0: Chris@0: // Attempt to write over val_1. If drupal_save_session(FALSE) is working. Chris@0: // properly, val_1 will still be set. Chris@0: $value_2 = $this->randomMachineName(); Chris@0: $this->drupalGet('session-test/no-set/' . $value_2); Chris@0: $this->assertText($value_2, 'The session value was correctly passed to session-test/no-set.', 'Session'); Chris@0: $this->drupalGet('session-test/get'); Chris@0: $this->assertText($value_1, 'Session data is not saved for drupal_save_session(FALSE).', 'Session'); Chris@0: Chris@0: // Switch browser cookie to anonymous user, then back to user 1. Chris@0: $this->sessionReset(); Chris@0: $this->sessionReset($user->id()); Chris@0: $this->assertText($value_1, 'Session data persists through browser close.', 'Session'); Chris@0: Chris@0: // Logout the user and make sure the stored value no longer persists. Chris@0: $this->drupalLogout(); Chris@0: $this->sessionReset(); Chris@0: $this->drupalGet('session-test/get'); Chris@0: $this->assertNoText($value_1, "After logout, previous user's session data is not available.", 'Session'); Chris@0: Chris@0: // Now try to store some data as an anonymous user. Chris@0: $value_3 = $this->randomMachineName(); Chris@0: $this->drupalGet('session-test/set/' . $value_3); Chris@0: $this->assertText($value_3, 'Session data stored for anonymous user.', 'Session'); Chris@0: $this->drupalGet('session-test/get'); Chris@0: $this->assertText($value_3, 'Session correctly returned the stored data for an anonymous user.', 'Session'); Chris@0: Chris@0: // Try to store data when drupal_save_session(FALSE). Chris@0: $value_4 = $this->randomMachineName(); Chris@0: $this->drupalGet('session-test/no-set/' . $value_4); Chris@0: $this->assertText($value_4, 'The session value was correctly passed to session-test/no-set.', 'Session'); Chris@0: $this->drupalGet('session-test/get'); Chris@0: $this->assertText($value_3, 'Session data is not saved for drupal_save_session(FALSE).', 'Session'); Chris@0: Chris@0: // Login, the data should persist. Chris@0: $this->drupalLogin($user); Chris@0: $this->sessionReset($user->id()); Chris@0: $this->drupalGet('session-test/get'); Chris@0: $this->assertNoText($value_1, 'Session has persisted for an authenticated user after logging out and then back in.', 'Session'); Chris@0: Chris@0: // Change session and create another user. Chris@0: $user2 = $this->drupalCreateUser([]); Chris@0: $this->sessionReset($user2->id()); Chris@0: $this->drupalLogin($user2); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests storing data in Session() object. Chris@0: */ Chris@0: public function testSessionPersistenceOnLogin() { Chris@0: // Store information via hook_user_login(). Chris@0: $user = $this->drupalCreateUser(); Chris@0: $this->drupalLogin($user); Chris@0: // Test property added to session object form hook_user_login(). Chris@0: $this->drupalGet('session-test/get-from-session-object'); Chris@0: $this->assertText('foobar', 'Session data is saved in Session() object.', 'Session'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that empty anonymous sessions are destroyed. Chris@0: */ Chris@0: public function testEmptyAnonymousSession() { Chris@0: // Disable the dynamic_page_cache module; it'd cause session_test's debug Chris@0: // output (that is added in Chris@0: // SessionTestSubscriber::onKernelResponseSessionTest()) to not be added. Chris@0: $this->container->get('module_installer')->uninstall(['dynamic_page_cache']); Chris@0: Chris@0: // Verify that no session is automatically created for anonymous user when Chris@0: // page caching is disabled. Chris@0: $this->container->get('module_installer')->uninstall(['page_cache']); Chris@0: $this->drupalGet(''); Chris@0: $this->assertSessionCookie(FALSE); Chris@0: $this->assertSessionEmpty(TRUE); Chris@0: Chris@0: // The same behavior is expected when caching is enabled. Chris@0: $this->container->get('module_installer')->install(['page_cache']); Chris@0: $config = $this->config('system.performance'); Chris@0: $config->set('cache.page.max_age', 300); Chris@0: $config->save(); Chris@0: $this->drupalGet(''); Chris@0: $this->assertSessionCookie(FALSE); Chris@0: // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages. Chris@0: // $this->assertSessionEmpty(TRUE); Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); Chris@0: Chris@0: // Start a new session by setting a message. Chris@0: $this->drupalGet('session-test/set-message'); Chris@0: $this->assertSessionCookie(TRUE); Chris@0: $this->assertTrue($this->drupalGetHeader('Set-Cookie'), 'New session was started.'); Chris@0: Chris@0: // Display the message, during the same request the session is destroyed Chris@0: // and the session cookie is unset. Chris@0: $this->drupalGet(''); Chris@0: $this->assertSessionCookie(FALSE); Chris@0: $this->assertSessionEmpty(FALSE); Chris@0: $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.'); Chris@0: $this->assertText(t('This is a dummy message.'), 'Message was displayed.'); Chris@0: $this->assertTrue(preg_match('/SESS\w+=deleted/', $this->drupalGetHeader('Set-Cookie')), 'Session cookie was deleted.'); Chris@0: Chris@0: // Verify that session was destroyed. Chris@0: $this->drupalGet(''); Chris@0: $this->assertSessionCookie(FALSE); Chris@0: // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages. Chris@0: // $this->assertSessionEmpty(TRUE); Chris@0: $this->assertNoText(t('This is a dummy message.'), 'Message was not cached.'); Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); Chris@0: $this->assertFalse($this->drupalGetHeader('Set-Cookie'), 'New session was not started.'); Chris@0: Chris@0: // Verify that no session is created if drupal_save_session(FALSE) is called. Chris@0: $this->drupalGet('session-test/set-message-but-dont-save'); Chris@0: $this->assertSessionCookie(FALSE); Chris@0: $this->assertSessionEmpty(TRUE); Chris@0: Chris@0: // Verify that no message is displayed. Chris@0: $this->drupalGet(''); Chris@0: $this->assertSessionCookie(FALSE); Chris@0: // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages. Chris@0: // $this->assertSessionEmpty(TRUE); Chris@0: $this->assertNoText(t('This is a dummy message.'), 'The message was not saved.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that sessions are only saved when necessary. Chris@0: */ Chris@0: public function testSessionWrite() { Chris@0: $user = $this->drupalCreateUser([]); Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: $sql = 'SELECT u.access, s.timestamp FROM {users_field_data} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE u.uid = :uid'; Chris@0: $times1 = db_query($sql, [':uid' => $user->id()])->fetchObject(); Chris@0: Chris@0: // Before every request we sleep one second to make sure that if the session Chris@0: // is saved, its timestamp will change. Chris@0: Chris@0: // Modify the session. Chris@0: sleep(1); Chris@0: $this->drupalGet('session-test/set/foo'); Chris@0: $times2 = db_query($sql, [':uid' => $user->id()])->fetchObject(); Chris@0: $this->assertEqual($times2->access, $times1->access, 'Users table was not updated.'); Chris@0: $this->assertNotEqual($times2->timestamp, $times1->timestamp, 'Sessions table was updated.'); Chris@0: Chris@0: // Write the same value again, i.e. do not modify the session. Chris@0: sleep(1); Chris@0: $this->drupalGet('session-test/set/foo'); Chris@0: $times3 = db_query($sql, [':uid' => $user->id()])->fetchObject(); Chris@0: $this->assertEqual($times3->access, $times1->access, 'Users table was not updated.'); Chris@0: $this->assertEqual($times3->timestamp, $times2->timestamp, 'Sessions table was not updated.'); Chris@0: Chris@0: // Do not change the session. Chris@0: sleep(1); Chris@0: $this->drupalGet(''); Chris@0: $times4 = db_query($sql, [':uid' => $user->id()])->fetchObject(); Chris@0: $this->assertEqual($times4->access, $times3->access, 'Users table was not updated.'); Chris@0: $this->assertEqual($times4->timestamp, $times3->timestamp, 'Sessions table was not updated.'); Chris@0: Chris@0: // Force updating of users and sessions table once per second. Chris@0: $this->settingsSet('session_write_interval', 0); Chris@0: // Write that value also into the test settings.php file. Chris@0: $settings['settings']['session_write_interval'] = (object) [ Chris@0: 'value' => 0, Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: $this->writeSettings($settings); Chris@0: $this->drupalGet(''); Chris@0: $times5 = db_query($sql, [':uid' => $user->id()])->fetchObject(); Chris@0: $this->assertNotEqual($times5->access, $times4->access, 'Users table was updated.'); Chris@0: $this->assertNotEqual($times5->timestamp, $times4->timestamp, 'Sessions table was updated.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that empty session IDs are not allowed. Chris@0: */ Chris@0: public function testEmptySessionID() { Chris@0: $user = $this->drupalCreateUser([]); Chris@0: $this->drupalLogin($user); Chris@0: $this->drupalGet('session-test/is-logged-in'); Chris@0: $this->assertResponse(200, 'User is logged in.'); Chris@0: Chris@0: // Reset the sid in {sessions} to a blank string. This may exist in the Chris@0: // wild in some cases, although we normally prevent it from happening. Chris@0: db_query("UPDATE {sessions} SET sid = '' WHERE uid = :uid", [':uid' => $user->id()]); Chris@0: // Send a blank sid in the session cookie, and the session should no longer Chris@0: // be valid. Closing the curl handler will stop the previous session ID Chris@0: // from persisting. Chris@0: $this->curlClose(); Chris@0: $this->additionalCurlOptions[CURLOPT_COOKIE] = rawurlencode($this->getSessionName()) . '=;'; Chris@0: $this->drupalGet('session-test/id-from-cookie'); Chris@0: $this->assertRaw("session_id:\n", 'Session ID is blank as sent from cookie header.'); Chris@0: // Assert that we have an anonymous session now. Chris@0: $this->drupalGet('session-test/is-logged-in'); Chris@0: $this->assertResponse(403, 'An empty session ID is not allowed.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Reset the cookie file so that it refers to the specified user. Chris@0: * Chris@0: * @param $uid Chris@0: * User id to set as the active session. Chris@0: */ Chris@0: public function sessionReset($uid = 0) { Chris@0: // Close the internal browser. Chris@0: $this->curlClose(); Chris@0: $this->loggedInUser = FALSE; Chris@0: Chris@0: // Change cookie file for user. Chris@0: $this->cookieFile = \Drupal::service('stream_wrapper_manager')->getViaScheme('temporary')->getDirectoryPath() . '/cookie.' . $uid . '.txt'; Chris@0: $this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile; Chris@0: $this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE; Chris@0: $this->drupalGet('session-test/get'); Chris@0: $this->assertResponse(200, 'Session test module is correctly enabled.', 'Session'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert whether the SimpleTest browser sent a session cookie. Chris@0: */ Chris@0: public function assertSessionCookie($sent) { Chris@0: if ($sent) { Chris@0: $this->assertNotNull($this->sessionId, 'Session cookie was sent.'); Chris@0: } Chris@0: else { Chris@0: $this->assertNull($this->sessionId, 'Session cookie was not sent.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert whether $_SESSION is empty at the beginning of the request. Chris@0: */ Chris@0: public function assertSessionEmpty($empty) { Chris@0: if ($empty) { Chris@0: $this->assertIdentical($this->drupalGetHeader('X-Session-Empty'), '1', 'Session was empty.'); Chris@0: } Chris@0: else { Chris@0: $this->assertIdentical($this->drupalGetHeader('X-Session-Empty'), '0', 'Session was not empty.'); Chris@0: } Chris@0: } Chris@0: Chris@0: }