Skip to content

Commit 49e18b8

Browse files
authored
Merge pull request #51 from drupalauth/drupalauth4ssp-3020308
Avoid possible length extension attack.
2 parents 65f9291 + 7b62cc5 commit 49e18b8

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/Auth/Source/External.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SimpleSAML\Module\drupalauth\Auth\Source;
44

5+
use Drupal\Component\Utility\Crypt;
56
use Drupal\user\Entity\User;
67
use SimpleSAML\Auth\Source;
78
use SimpleSAML\Auth\State;
@@ -122,14 +123,20 @@ private function getUser()
122123
$cookie_name = $this->config->getCookieName();
123124
if (isset($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name]) {
124125
$strCookie = $_COOKIE[$cookie_name];
125-
list($hash, $uid) = explode(':', $strCookie);
126+
list($cookie_hash, $uid) = explode(':', $strCookie);
126127

127128
// make sure the hash matches
128129
// make sure the UID is passed
129-
if ((isset($hash) && !empty($hash)) && (isset($uid) && !empty($uid))) {
130+
if ((isset($cookie_hash) && !empty($cookie_hash)) && (isset($uid) && !empty($uid))) {
131+
$drupalHelper = new DrupalHelper();
132+
$drupalHelper->bootDrupal($this->config->getDrupalroot());
133+
130134
// Make sure no one manipulated the hash or the uid in the cookie before we trust the uid
131-
$cookie_salt = $this->config->getCookieSalt();
132-
if (sha1($cookie_salt . $uid) !== $hash) {
135+
$hash = Crypt::hmacBase64(
136+
$uid,
137+
$this->config->getCookieSalt() . \Drupal::service('private_key')->get()
138+
);
139+
if (!Crypt::hashEquals($hash, $cookie_hash)) {
133140
throw new Exception(
134141
'Cookie hash invalid. This indicates either tampering or an out of date drupal4ssp module.'
135142
);
@@ -145,9 +152,6 @@ private function getUser()
145152
}
146153

147154
if (!empty($drupaluid)) {
148-
$drupalHelper = new DrupalHelper();
149-
$drupalHelper->bootDrupal($this->config->getDrupalroot());
150-
151155
// Load the user object from Drupal.
152156
$drupaluser = User::load($uid);
153157
if ($drupaluser->isBlocked()) {

0 commit comments

Comments
 (0)