diff --git a/includes/session.inc b/includes/session.inc index 9589e06..67ba14e 100644 --- a/includes/session.inc +++ b/includes/session.inc @@ -87,19 +87,21 @@ function _drupal_session_read($sid) { // Otherwise, if the session is still active, we have a record of the // client's session in the database. If it's HTTPS then we are either have // a HTTPS session or we are about to log in so we check the sessions table - // for an anonymous session with the non-HTTPS-only cookie. + // for an anonymous session with the non-HTTPS-only cookie. The session ID + // that is in the user's cookie is hashed before being stored in the database + // as a security measure. Thus, we have to hash it to match the database. if ($is_https) { - $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.ssid = :ssid", array(':ssid' => $sid))->fetchObject(); + $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.ssid = :ssid", array(':ssid' => drupal_hash_base64($sid)))->fetchObject(); if (!$user) { if (isset($_COOKIE[$insecure_session_name])) { $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid AND s.uid = 0", array( - ':sid' => $_COOKIE[$insecure_session_name])) + ':sid' => drupal_hash_base64($_COOKIE[$insecure_session_name]))) ->fetchObject(); } } } else { - $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid", array(':sid' => $sid))->fetchObject(); + $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid", array(':sid' => drupal_hash_base64($sid)))->fetchObject(); } // We found the client's session record and they are an authenticated, @@ -185,17 +187,18 @@ function _drupal_session_write($sid, $value) { // Use the session ID as 'sid' and an empty string as 'ssid' by default. // _drupal_session_read() does not allow empty strings so that's a safe // default. - $key = array('sid' => $sid, 'ssid' => ''); + $key = array('sid' => drupal_hash_base64($sid), 'ssid' => ''); // On HTTPS connections, use the session ID as both 'sid' and 'ssid'. if ($is_https) { - $key['ssid'] = $sid; + $key['ssid'] = drupal_hash_base64($sid); // The "secure pages" setting allows a site to simultaneously use both // secure and insecure session cookies. If enabled and both cookies are - // presented then use both keys. + // presented then use both keys. The session ID from the cookie is + // hashed before being stored in the database as a security measure. if (variable_get('https', FALSE)) { $insecure_session_name = substr(session_name(), 1); if (isset($_COOKIE[$insecure_session_name])) { - $key['sid'] = $_COOKIE[$insecure_session_name]; + $key['sid'] = drupal_hash_base64($_COOKIE[$insecure_session_name]); } } } @@ -378,18 +381,18 @@ function drupal_session_regenerate() { $params = session_get_cookie_params(); $expire = $params['lifetime'] ? REQUEST_TIME + $params['lifetime'] : 0; setcookie(session_name(), session_id(), $expire, $params['path'], $params['domain'], $params['secure'], $params['httponly']); - $fields = array('sid' => session_id()); + $fields = array('sid' => drupal_hash_base64(session_id())); if ($is_https) { - $fields['ssid'] = session_id(); + $fields['ssid'] = drupal_hash_base64(session_id()); // If the "secure pages" setting is enabled, use the newly-created // insecure session identifier as the regenerated sid. if (variable_get('https', FALSE)) { - $fields['sid'] = $session_id; + $fields['sid'] = drupal_hash_base64($session_id); } } db_update('sessions') ->fields($fields) - ->condition($is_https ? 'ssid' : 'sid', $old_session_id) + ->condition($is_https ? 'ssid' : 'sid', drupal_hash_base64($old_session_id)) ->execute(); } elseif (isset($old_insecure_session_id)) { @@ -397,8 +400,8 @@ function drupal_session_regenerate() { // secure site but a session was active on the insecure site, update the // insecure session with the new session identifiers. db_update('sessions') - ->fields(array('sid' => $session_id, 'ssid' => session_id())) - ->condition('sid', $old_insecure_session_id) + ->fields(array('sid' => drupal_hash_base64($session_id), 'ssid' => drupal_hash_base64(session_id()))) + ->condition('sid', drupal_hash_base64($old_insecure_session_id)) ->execute(); } else { @@ -430,7 +433,7 @@ function _drupal_session_destroy($sid) { // Delete session data. db_delete('sessions') - ->condition($is_https ? 'ssid' : 'sid', $sid) + ->condition($is_https ? 'ssid' : 'sid', drupal_hash_base64($sid)) ->execute(); // Reset $_SESSION and $user to prevent a new session from being started diff --git a/modules/simpletest/tests/session.test b/modules/simpletest/tests/session.test index 097503b..2558b02 100644 --- a/modules/simpletest/tests/session.test +++ b/modules/simpletest/tests/session.test @@ -493,8 +493,8 @@ class SessionHttpsTestCase extends DrupalWebTestCase { */ protected function assertSessionIds($sid, $ssid, $assertion_text) { $args = array( - ':sid' => $sid, - ':ssid' => $ssid, + ':sid' => drupal_hash_base64($sid), + ':ssid' => !empty($ssid) ? drupal_hash_base64($ssid) : '', ); return $this->assertTrue(db_query('SELECT timestamp FROM {sessions} WHERE sid = :sid AND ssid = :ssid', $args)->fetchField(), $assertion_text); } diff --git a/modules/system/system.install b/modules/system/system.install index 43c7383..04fef31 100644 --- a/modules/system/system.install +++ b/modules/system/system.install @@ -1485,13 +1485,13 @@ function system_schema() { 'not null' => TRUE, ), 'sid' => array( - 'description' => "A session ID. The value is generated by Drupal's session handlers.", + 'description' => "A session ID (hashed). The value is generated by Drupal's session handlers.", 'type' => 'varchar', 'length' => 128, 'not null' => TRUE, ), 'ssid' => array( - 'description' => "Secure session ID. The value is generated by Drupal's session handlers.", + 'description' => "Secure session ID (hashed). The value is generated by Drupal's session handlers.", 'type' => 'varchar', 'length' => 128, 'not null' => TRUE, @@ -3151,6 +3151,13 @@ function system_update_7079() { } /** + * Truncate the session table with the unsecured (not hashed) session ids. + */ +function system_update_7080() { + db_truncate('sessions')->execute(); +} + +/** * @} End of "defgroup updates-7.x-extra". * The next series of updates should start at 8000. */