r24695 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24694‎ | r24695 | r24696 >
Date:09:53, 9 August 2007
Author:yurik
Status:old
Tags:
Comment:
API: fixed bad login throttling bug (i hope)
Modified paths:
  • /trunk/phase3/includes/api/ApiLogin.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiLogin.php
@@ -37,16 +37,16 @@
3838 class ApiLogin extends ApiBase {
3939
4040 /**
41 - * The amount of time a user must wait after submitting
 41+ * Time (in seconds) a user must wait after submitting
4242 * a bad login (will be multiplied by the THROTTLE_FACTOR for each bad attempt)
4343 */
44 - const THROTTLE_TIME = 10;
 44+ const THROTTLE_TIME = 1;
4545
4646 /**
4747 * The factor by which the wait-time in between authentication
4848 * attempts is increased every failed attempt.
4949 */
50 - const THROTTLE_FACTOR = 1.5;
 50+ const THROTTLE_FACTOR = 2;
5151
5252 /**
5353 * The maximum number of failed logins after which the wait increase stops.
@@ -160,10 +160,11 @@
161161 $val['count'] = 1 + $val['count'];
162162 }
163163
164 - $delay = ApiLogin::calculateDelay($val);
 164+ $delay = ApiLogin::calculateDelay($val['count']);
165165
166166 $wgMemc->delete($key);
167 - $wgMemc->add( $key, $val, $delay );
 167+ // Cache expiration should be the maximum timeout - to prevent a "try and wait" attack
 168+ $wgMemc->add( $key, $val, ApiLogin::calculateDelay(ApiLogin::THOTTLE_MAX_COUNT) );
168169
169170 return $delay;
170171 }
@@ -178,8 +179,8 @@
179180
180181 $val = $wgMemc->get($this->getMemCacheKey());
181182
182 - $elapse = (time() - $val['lastReqTime']) / 1000; // in seconds
183 - $canRetryIn = ApiLogin::calculateDelay($val) - $elapse;
 183+ $elapse = (time() - $val['lastReqTime']); // in seconds
 184+ $canRetryIn = ApiLogin::calculateDelay($val['count']) - $elapse;
184185
185186 return $canRetryIn < 0 ? 0 : $canRetryIn;
186187 }
@@ -188,9 +189,9 @@
189190 * Based on the number of previously attempted logins, returns
190191 * the delay (in seconds) when the next login attempt will be allowed.
191192 */
192 - private static function calculateDelay($val) {
 193+ private static function calculateDelay($count) {
193194 // Defensive programming
194 - $count = $val['count'];
 195+ $count = intval($count);
195196 $count = $count < 1 ? 1 : $count;
196197 $count = $count > self::THOTTLE_MAX_COUNT ? self::THOTTLE_MAX_COUNT : $count;
197198

Follow-up revisions

RevisionCommit summaryAuthorDate
r24755Merged revisions 24694-24754 via svnmerge from...david19:48, 13 August 2007

Status & tagging log