2018-05-28 16:25:36 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Reset login/signup throttling for a specified user and/or IP.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Auth\Throttler;
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-04-27 15:42:24 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2019-06-25 18:53:15 +00:00
|
|
|
use Wikimedia\IPUtils;
|
2018-05-28 16:25:36 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-05-28 16:25:36 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2018-05-28 16:25:36 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset login/signup throttling for a specified user and/or IP.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
* @since 1.32
|
|
|
|
|
*/
|
|
|
|
|
class ResetAuthenticationThrottle extends Maintenance {
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addDescription( 'Reset login/signup throttling for a specified user and/or IP. '
|
|
|
|
|
. "\n\n"
|
2024-03-03 19:16:41 +00:00
|
|
|
. 'When resetting signup or temp account, provide the IP. When resetting login (or both), provide '
|
2018-05-28 16:25:36 +00:00
|
|
|
. 'both username (as entered in login screen) and IP. An easy way to obtain them is '
|
|
|
|
|
. "the 'throttler' log channel." );
|
|
|
|
|
$this->addOption( 'login', 'Reset login throttle' );
|
|
|
|
|
$this->addOption( 'signup', 'Reset account creation throttle' );
|
2024-03-03 19:16:41 +00:00
|
|
|
$this->addOption( 'tempaccount', 'Reset temp account creation throttle' );
|
2024-04-08 09:56:01 +00:00
|
|
|
$this->addOption( 'tempaccountnameacquisition', 'Reset temp account name acquisition throttle' );
|
2024-08-24 14:52:06 +00:00
|
|
|
$this->addOption( 'user', 'Username to reset (when using --login)', false, true );
|
2018-05-28 16:25:36 +00:00
|
|
|
$this->addOption( 'ip', 'IP to reset', false, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$forLogin = (bool)$this->getOption( 'login' );
|
|
|
|
|
$forSignup = (bool)$this->getOption( 'signup' );
|
2024-03-03 19:16:41 +00:00
|
|
|
$forTempAccount = (bool)$this->getOption( 'tempaccount' );
|
2024-04-08 09:56:01 +00:00
|
|
|
$forTempAccountNameAcquisition = (bool)$this->getOption( 'tempaccountnameacquisition' );
|
2018-05-28 16:25:36 +00:00
|
|
|
$username = $this->getOption( 'user' );
|
|
|
|
|
$ip = $this->getOption( 'ip' );
|
|
|
|
|
|
2024-04-08 09:56:01 +00:00
|
|
|
if ( !$forLogin && !$forSignup && !$forTempAccount && !$forTempAccountNameAcquisition ) {
|
|
|
|
|
$this->fatalError(
|
|
|
|
|
'At least one of --login, --signup, --tempaccount, or --tempaccountnameacquisition is required!'
|
|
|
|
|
);
|
2024-08-24 14:52:06 +00:00
|
|
|
} elseif ( $ip === null ) {
|
|
|
|
|
$this->fatalError( '--ip is required!' );
|
|
|
|
|
} elseif ( !IPUtils::isValid( $ip ) ) {
|
2018-05-28 16:25:36 +00:00
|
|
|
$this->fatalError( "Not a valid IP: $ip" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $forLogin ) {
|
|
|
|
|
$this->clearLoginThrottle( $username, $ip );
|
|
|
|
|
}
|
|
|
|
|
if ( $forSignup ) {
|
|
|
|
|
$this->clearSignupThrottle( $ip );
|
|
|
|
|
}
|
2024-03-03 19:16:41 +00:00
|
|
|
if ( $forTempAccount ) {
|
|
|
|
|
$this->clearTempAccountCreationThrottle( $ip );
|
|
|
|
|
}
|
2024-04-08 09:56:01 +00:00
|
|
|
if ( $forTempAccountNameAcquisition ) {
|
|
|
|
|
$this->clearTempAccountNameAcquisitionThrottle( $ip );
|
|
|
|
|
}
|
2018-05-28 16:25:36 +00:00
|
|
|
|
deferred,jobqueue,rdbms: Change logger->notice to info or warning
MediaWiki generally only uses four levels:
* 'debug': Verbose information, e.g. during local dev, CI, and
in "Verbose" mode in production via WikimediaDebug.
* 'info': Diagnostics. Enabled for most channels even for production
traffic, though typically hidden in Logstash along with 'debug',
and instead reviewed in combination with other messages from the
same request when the request is investigated for a different reason.
* 'warning': Diagnostics,. Typically hidden in Logstash, except for
dashboards for component's own maintainers, to assess system health.
Warnings are rare but expected to some degree at scale.
* 'error': Something has gone wrong that should in theory not happen.
Typically included even in system-wide dashboards and sometimes used
for alerting.
Afaik we have no meaning associated with 'notice'. At WMF I found
no dashboards selecting it, no alerts using it, and no channels used
it as the threshold for including or discarding messages.
Instead, lower them to info(), or raise them to warning().
Change-Id: Ie927643d774ba696ec33c7e6b7023b1a1b831d12
2023-01-06 21:42:32 +00:00
|
|
|
LoggerFactory::getInstance( 'throttler' )->info( 'Manually cleared {type} throttle', [
|
2018-05-28 16:25:36 +00:00
|
|
|
'type' => implode( ' and ', array_filter( [
|
|
|
|
|
$forLogin ? 'login' : null,
|
|
|
|
|
$forSignup ? 'signup' : null,
|
2024-03-03 19:16:41 +00:00
|
|
|
$forTempAccount ? 'tempaccount' : null,
|
2024-04-08 09:56:01 +00:00
|
|
|
$forTempAccountNameAcquisition ? 'tempaccountnameacquisition' : null,
|
2018-05-28 16:25:36 +00:00
|
|
|
] ) ),
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'ipKey' => $ip,
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|null $rawUsername
|
|
|
|
|
* @param string|null $ip
|
|
|
|
|
*/
|
|
|
|
|
protected function clearLoginThrottle( $rawUsername, $ip ) {
|
2021-07-21 16:34:05 +00:00
|
|
|
$this->output( 'Clearing login throttle...' );
|
2018-05-28 16:25:36 +00:00
|
|
|
|
2022-04-27 15:42:24 +00:00
|
|
|
$passwordAttemptThrottle = $this->getConfig()->get( MainConfigNames::PasswordAttemptThrottle );
|
2018-05-28 16:25:36 +00:00
|
|
|
if ( !$passwordAttemptThrottle ) {
|
|
|
|
|
$this->output( "none set\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 18:44:20 +00:00
|
|
|
$objectCacheFactory = $this->getServiceContainer()->getInstance()->getObjectCacheFactory();
|
|
|
|
|
|
2018-05-28 16:25:36 +00:00
|
|
|
$throttler = new Throttler( $passwordAttemptThrottle, [
|
|
|
|
|
'type' => 'password',
|
2024-06-05 18:44:20 +00:00
|
|
|
'cache' => $objectCacheFactory->getLocalClusterInstance(),
|
2018-05-28 16:25:36 +00:00
|
|
|
] );
|
|
|
|
|
if ( $rawUsername !== null ) {
|
2023-08-31 09:21:12 +00:00
|
|
|
$usernames = $this->getServiceContainer()->getAuthManager()
|
2020-03-31 18:51:49 +00:00
|
|
|
->normalizeUsername( $rawUsername );
|
2018-05-28 16:25:36 +00:00
|
|
|
if ( !$usernames ) {
|
|
|
|
|
$this->fatalError( "Not a valid username: $rawUsername" );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$usernames = [ null ];
|
|
|
|
|
}
|
|
|
|
|
foreach ( $usernames as $username ) {
|
|
|
|
|
$throttler->clear( $username, $ip );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$botPasswordThrottler = new Throttler( $passwordAttemptThrottle, [
|
|
|
|
|
'type' => 'botpassword',
|
2024-06-05 18:44:20 +00:00
|
|
|
'cache' => $objectCacheFactory->getLocalClusterInstance(),
|
2018-05-28 16:25:36 +00:00
|
|
|
] );
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
|
2018-05-28 16:25:36 +00:00
|
|
|
$botPasswordThrottler->clear( $username, $ip );
|
|
|
|
|
|
|
|
|
|
$this->output( "done\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $ip
|
|
|
|
|
*/
|
|
|
|
|
protected function clearSignupThrottle( $ip ) {
|
2021-07-21 16:34:05 +00:00
|
|
|
$this->output( 'Clearing signup throttle...' );
|
2018-05-28 16:25:36 +00:00
|
|
|
|
2022-04-27 15:42:24 +00:00
|
|
|
$accountCreationThrottle = $this->getConfig()->get( MainConfigNames::AccountCreationThrottle );
|
2018-05-28 16:25:36 +00:00
|
|
|
if ( !is_array( $accountCreationThrottle ) ) {
|
|
|
|
|
$accountCreationThrottle = [ [
|
|
|
|
|
'count' => $accountCreationThrottle,
|
|
|
|
|
'seconds' => 86400,
|
|
|
|
|
] ];
|
|
|
|
|
}
|
|
|
|
|
if ( !$accountCreationThrottle ) {
|
|
|
|
|
$this->output( "none set\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$throttler = new Throttler( $accountCreationThrottle, [
|
|
|
|
|
'type' => 'acctcreate',
|
2024-09-04 12:59:26 +00:00
|
|
|
'cache' => $this->getServiceContainer()->getObjectCacheFactory()
|
2024-06-05 18:44:20 +00:00
|
|
|
->getLocalClusterInstance(),
|
2018-05-28 16:25:36 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$throttler->clear( null, $ip );
|
|
|
|
|
|
|
|
|
|
$this->output( "done\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 19:16:41 +00:00
|
|
|
protected function clearTempAccountCreationThrottle( string $ip ): void {
|
|
|
|
|
$this->output( 'Clearing temp account creation throttle...' );
|
|
|
|
|
|
|
|
|
|
$tempAccountCreationThrottle = $this->getConfig()->get( MainConfigNames::TempAccountCreationThrottle );
|
|
|
|
|
if ( !$tempAccountCreationThrottle ) {
|
|
|
|
|
$this->output( "none set\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$throttler = new Throttler( $tempAccountCreationThrottle, [
|
|
|
|
|
'type' => 'tempacctcreate',
|
2024-09-04 12:59:26 +00:00
|
|
|
'cache' => $this->getServiceContainer()->getObjectCacheFactory()
|
2024-06-05 18:44:20 +00:00
|
|
|
->getLocalClusterInstance(),
|
2024-03-03 19:16:41 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$throttler->clear( null, $ip );
|
|
|
|
|
|
|
|
|
|
$this->output( "done\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 09:56:01 +00:00
|
|
|
protected function clearTempAccountNameAcquisitionThrottle( string $ip ): void {
|
|
|
|
|
$this->output( 'Clearing temp account name acquisition throttle...' );
|
|
|
|
|
|
|
|
|
|
$tempAccountNameAcquisitionThrottle = $this->getConfig()->get(
|
|
|
|
|
MainConfigNames::TempAccountNameAcquisitionThrottle
|
|
|
|
|
);
|
|
|
|
|
if ( !$tempAccountNameAcquisitionThrottle ) {
|
|
|
|
|
$this->output( "none set\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$throttler = new Throttler( $tempAccountNameAcquisitionThrottle, [
|
|
|
|
|
'type' => 'tempacctnameacquisition',
|
2024-09-04 12:59:26 +00:00
|
|
|
'cache' => $this->getServiceContainer()->getObjectCacheFactory()
|
2024-06-05 18:44:20 +00:00
|
|
|
->getLocalClusterInstance(),
|
2024-04-08 09:56:01 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$throttler->clear( null, $ip );
|
|
|
|
|
|
|
|
|
|
$this->output( "done\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-05-28 16:25:36 +00:00
|
|
|
$maintClass = ResetAuthenticationThrottle::class;
|
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|