MediaWiki r64677 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r64676‎ | r64677 (on ViewVC)‎ | r64678 >
Date:00:05, 7 April 2010
Author:tstarling
Status:ok
Tags:
Comment:
* (bug 23076) Fixed login CSRF vulnerability. Logins now require a token to be submitted along with the user name and password. Patch by Roan Kattouw.
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/User.php
@@ -2850,7 +2850,7 @@
28512851 return EDIT_TOKEN_SUFFIX;
28522852 } else {
28532853 if( !isset( $_SESSION['wsEditToken'] ) ) {
2854 - $token = $this->generateToken();
 2854+ $token = self::generateToken();
28552855 $_SESSION['wsEditToken'] = $token;
28562856 } else {
28572857 $token = $_SESSION['wsEditToken'];
@@ -2868,7 +2868,7 @@
28692869 * @param $salt \string Optional salt value
28702870 * @return \string The new random token
28712871 */
2872 - function generateToken( $salt = '' ) {
 2872+ public static function generateToken( $salt = '' ) {
28732873 $token = dechex( mt_rand() ) . dechex( mt_rand() );
28742874 return md5( $token . $salt );
28752875 }
@@ -2967,7 +2967,7 @@
29682968 $now = time();
29692969 $expires = $now + 7 * 24 * 60 * 60;
29702970 $expiration = wfTimestamp( TS_MW, $expires );
2971 - $token = $this->generateToken( $this->mId . $this->mEmail . $expires );
 2971+ $token = self::generateToken( $this->mId . $this->mEmail . $expires );
29722972 $hash = md5( $token );
29732973 $this->load();
29742974 $this->mEmailToken = $hash;
Index: trunk/phase3/includes/api/ApiLogin.php
@@ -58,6 +58,7 @@
5959 'wpName' => $params['name'],
6060 'wpPassword' => $params['password'],
6161 'wpDomain' => $params['domain'],
 62+ 'wpLoginToken' => $params['token'],
6263 'wpRemember' => ''
6364 ) );
6465
@@ -86,6 +87,15 @@
8788 $result['cookieprefix'] = $wgCookiePrefix;
8889 $result['sessionid'] = session_id();
8990 break;
 91+
 92+ case LoginForm::NEED_TOKEN:
 93+ $result['result'] = 'NeedToken';
 94+ $result['token'] = $loginForm->getLoginToken();
 95+ break;
 96+
 97+ case LoginForm::WRONG_TOKEN:
 98+ $result['result'] = 'WrongToken';
 99+ break;
90100
91101 case LoginForm::NO_NAME:
92102 $result['result'] = 'NoName';
@@ -146,7 +156,8 @@
147157 return array(
148158 'name' => null,
149159 'password' => null,
150 - 'domain' => null
 160+ 'domain' => null,
 161+ 'token' => null,
151162 );
152163 }
153164
@@ -154,7 +165,8 @@
155166 return array(
156167 'name' => 'User Name',
157168 'password' => 'Password',
158 - 'domain' => 'Domain (optional)'
 169+ 'domain' => 'Domain (optional)',
 170+ 'token' => 'Login token obtained in first request',
159171 );
160172 }
161173
@@ -170,6 +182,8 @@
171183
172184 public function getPossibleErrors() {
173185 return array_merge( parent::getPossibleErrors(), array(
 186+ array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
 187+ array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
174188 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
175189 array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
176190 array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
Index: trunk/phase3/includes/specials/SpecialUserlogin.php
@@ -35,11 +35,13 @@
3636 const CREATE_BLOCKED = 9;
3737 const THROTTLED = 10;
3838 const USER_BLOCKED = 11;
 39+ const NEED_TOKEN = 12;
 40+ const WRONG_TOKEN = 13;
3941
4042 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
4143 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
4244 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
43 - var $mSkipCookieCheck, $mReturnToQuery;
 45+ var $mSkipCookieCheck, $mReturnToQuery, $mToken;
4446
4547 private $mExtUser = null;
4648
@@ -70,6 +72,7 @@
7173 $this->mRemember = $request->getCheck( 'wpRemember' );
7274 $this->mLanguage = $request->getText( 'uselang' );
7375 $this->mSkipCookieCheck = $request->getCheck( 'wpSkipCookieCheck' );
 76+ $this->mToken = $request->getVal( 'wpLoginToken' );
7477
7578 if ( $wgRedirectOnLogin ) {
7679 $this->mReturnTo = $wgRedirectOnLogin;
@@ -395,6 +398,21 @@
396399 return self::NO_NAME;
397400 }
398401
 402+ // We require a login token to prevent login CSRF
 403+ // Handle part of this before incrementing the throttle so
 404+ // token-less login attempts don't count towards the throttle
 405+ // but wrong-token attempts do.
 406+
 407+ // If the user doesn't have a login token yet, set one.
 408+ if ( !self::getLoginToken() ) {
 409+ self::setLoginToken();
 410+ return self::NEED_TOKEN;
 411+ }
 412+ // If the user didn't pass a login token, tell them we need one
 413+ if ( !$this->mToken ) {
 414+ return self::NEED_TOKEN;
 415+ }
 416+
399417 global $wgPasswordAttemptThrottle;
400418
401419 $throttleCount = 0;
@@ -413,6 +431,11 @@
414432 return self::THROTTLED;
415433 }
416434 }
 435+
 436+ // Validate the login token
 437+ if ( $this->mToken !== self::getLoginToken() ) {
 438+ return self::WRONG_TOKEN;
 439+ }
417440
418441 // Load $wgUser now, and check to see if we're logging in as the same
419442 // name. This is necessary because loading $wgUser (say by calling
@@ -575,6 +598,7 @@
576599 $wgUser->invalidateCache();
577600 }
578601 $wgUser->setCookies();
 602+ self::clearLoginToken();
579603
580604 // Reset the throttle
581605 $key = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mName ) );
@@ -593,7 +617,11 @@
594618 return $this->cookieRedirectCheck( 'login' );
595619 }
596620 break;
597 -
 621+
 622+ case self::NEED_TOKEN:
 623+ case self::WRONG_TOKEN:
 624+ $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
 625+ break;
598626 case self::NO_NAME:
599627 case self::ILLEGAL:
600628 $this->mainLoginForm( wfMsg( 'noname' ) );
@@ -937,6 +965,11 @@
938966 $template->set( 'canremember', ( $wgCookieExpiration > 0 ) );
939967 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
940968
 969+ if ( !self::getLoginToken() ) {
 970+ self::setLoginToken();
 971+ }
 972+ $template->set( 'token', self::getLoginToken() );
 973+
941974 # Prepare language selection links as needed
942975 if( $wgLoginLanguageSelector ) {
943976 $template->set( 'languages', $this->makeLanguageSelector() );
@@ -991,6 +1024,32 @@
9921025 global $wgDisableCookieCheck, $wgRequest;
9931026 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
9941027 }
 1028+
 1029+ /**
 1030+ * Get the login token from the current session
 1031+ */
 1032+ public static function getLoginToken() {
 1033+ global $wgRequest;
 1034+ return $wgRequest->getSessionData( 'wsLoginToken' );
 1035+ }
 1036+
 1037+ /**
 1038+ * Generate a new login token and attach it to the current session
 1039+ */
 1040+ public static function setLoginToken() {
 1041+ global $wgRequest;
 1042+ // Use User::generateToken() instead of $user->editToken()
 1043+ // because the latter reuses $_SESSION['wsEditToken']
 1044+ $wgRequest->setSessionData( 'wsLoginToken', User::generateToken() );
 1045+ }
 1046+
 1047+ /**
 1048+ * Remove any login token attached to the current session
 1049+ */
 1050+ public static function clearLoginToken() {
 1051+ global $wgRequest;
 1052+ $wgRequest->setSessionData( 'wsLoginToken', null );
 1053+ }
9951054
9961055 /**
9971056 * @private
Index: trunk/phase3/includes/templates/Userlogin.php
@@ -111,6 +111,7 @@
112112 </tr>
113113 </table>
114114 <?php if( @$this->haveData( 'uselang' ) ) { ?><input type="hidden" name="uselang" value="<?php $this->text( 'uselang' ); ?>" /><?php } ?>
 115+<?php if( @$this->haveData( 'token' ) ) { ?><input type="hidden" name="wpLoginToken" value="<?php $this->text( 'token' ); ?>" /><?php } ?>
115116 </form>
116117 </div>
117118 <div id="loginend"><?php $this->msgWiki( 'loginend' ); ?></div>

Follow-up revisions

Rev.Commit summaryAuthorDate
r64678(bug 23076) MFT r64677: fixed login CSRF vulnerability.tstarling00:10, 7 April 2010
r64679(bug 23076) MFT r64677: fixed login CSRF vulnerability.tstarling00:12, 7 April 2010
r64680(bug 23076) MFT r64677: fixed login CSRF vulnerability.tstarling00:13, 7 April 2010
r64694Fix for r64677: as reported on mediawiki-api, I forgot about clients that bui...catrope08:50, 7 April 2010
r65760Bug 23371: Fix CSRF similar to r64677 covering the other three execute()...platonides20:16, 1 May 2010

Status & tagging log

  • 20:09, 2 November 2010 ^demon (talk | contribs) changed the status of r64677 [removed: new added: ok]