| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Auth\AuthManager; |
| 4 |
use MediaWiki\Auth\UsernameAuthenticationRequest; |
| 5 |
use MediaWiki\Context\RequestContext; |
| 6 |
use MediaWiki\Extension\ConfirmEdit\Auth\CaptchaAuthenticationRequest; |
| 7 |
use MediaWiki\Extension\ConfirmEdit\Auth\CaptchaPreAuthenticationProvider; |
| 8 |
use MediaWiki\Extension\ConfirmEdit\Auth\LoginAttemptCounter; |
| 9 |
use MediaWiki\Extension\ConfirmEdit\Hooks; |
| 10 |
use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha; |
| 11 |
use MediaWiki\Extension\ConfirmEdit\Store\CaptchaHashStore; |
| 12 |
use MediaWiki\Extension\ConfirmEdit\Store\CaptchaStore; |
| 13 |
use MediaWiki\Request\FauxRequest; |
| 14 |
use MediaWiki\Tests\Unit\Auth\AuthenticationProviderTestTrait; |
| 15 |
use MediaWiki\User\User; |
| 16 |
use Wikimedia\TestingAccessWrapper; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class CaptchaPreAuthenticationProviderTest extends MediaWikiIntegrationTestCase { |
| 23 |
use AuthenticationProviderTestTrait; |
| 24 |
|
| 25 |
public function setUp(): void { |
| 26 |
parent::setUp(); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
$this->clearHook( 'ConfirmEditTriggersCaptcha' ); |
| 31 |
|
| 32 |
$this->overrideConfigValues( [ |
| 33 |
'CaptchaClass' => SimpleCaptcha::class, |
| 34 |
'CaptchaBadLoginAttempts' => 1, |
| 35 |
'CaptchaBadLoginPerUserAttempts' => 1, |
| 36 |
'CaptchaStorageClass' => CaptchaHashStore::class, |
| 37 |
] ); |
| 38 |
CaptchaStore::unsetInstanceForTests(); |
| 39 |
CaptchaStore::get()->clearAll(); |
| 40 |
} |
| 41 |
|
| 42 |
public function tearDown(): void { |
| 43 |
parent::tearDown(); |
| 44 |
|
| 45 |
$req = TestingAccessWrapper::newFromClass( Hooks::class ); |
| 46 |
|
| 47 |
$req->instance = null; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function testGetAuthenticationRequests( |
| 54 |
$action, $useExistingUserOrNull, $triggers, $needsCaptcha, $preTestCallback = null |
| 55 |
) { |
| 56 |
if ( $useExistingUserOrNull === true ) { |
| 57 |
$username = $this->getTestSysop()->getUserIdentity()->getName(); |
| 58 |
} elseif ( $useExistingUserOrNull === false ) { |
| 59 |
$username = 'Foo'; |
| 60 |
} else { |
| 61 |
$username = null; |
| 62 |
} |
| 63 |
$this->setTriggers( $triggers ); |
| 64 |
if ( $preTestCallback ) { |
| 65 |
$fn = array_shift( $preTestCallback ); |
| 66 |
call_user_func_array( [ $this, $fn ], $preTestCallback ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
$request = RequestContext::getMain()->getRequest(); |
| 71 |
$request->setCookie( 'UserName', $username ); |
| 72 |
|
| 73 |
$provider = new CaptchaPreAuthenticationProvider(); |
| 74 |
$this->initProvider( $provider, null, null, $this->getServiceContainer()->getAuthManager() ); |
| 75 |
$reqs = $provider->getAuthenticationRequests( $action, [ 'username' => $username ] ); |
| 76 |
if ( $needsCaptcha ) { |
| 77 |
$this->assertCount( 1, $reqs ); |
| 78 |
$this->assertInstanceOf( CaptchaAuthenticationRequest::class, $reqs[0] ); |
| 79 |
} else { |
| 80 |
$this->assertSame( [], $reqs ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public static function provideGetAuthenticationRequests() { |
| 85 |
return [ |
| 86 |
[ AuthManager::ACTION_LOGIN, null, [], false ], |
| 87 |
[ AuthManager::ACTION_LOGIN, null, [ 'badlogin' ], false ], |
| 88 |
[ AuthManager::ACTION_LOGIN, null, [ 'badlogin' ], true, [ 'blockLogin', 'Foo' ] ], |
| 89 |
[ AuthManager::ACTION_LOGIN, null, [ 'badloginperuser' ], false, [ 'blockLogin', 'Foo' ] ], |
| 90 |
[ AuthManager::ACTION_LOGIN, false, [ 'badloginperuser' ], false, [ 'blockLogin', 'Bar' ] ], |
| 91 |
[ AuthManager::ACTION_LOGIN, false, [ 'badloginperuser' ], true, [ 'blockLogin', 'Foo' ] ], |
| 92 |
[ AuthManager::ACTION_LOGIN, null, [ 'badloginperuser' ], true, [ 'flagSession' ] ], |
| 93 |
[ AuthManager::ACTION_CREATE, null, [], false ], |
| 94 |
[ AuthManager::ACTION_CREATE, null, [ 'createaccount' ], true ], |
| 95 |
[ AuthManager::ACTION_CREATE, true, [ 'createaccount' ], false ], |
| 96 |
[ AuthManager::ACTION_LINK, null, [], false ], |
| 97 |
[ AuthManager::ACTION_CHANGE, null, [], false ], |
| 98 |
[ AuthManager::ACTION_REMOVE, null, [], false ], |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
public function testGetAuthenticationRequests_store() { |
| 103 |
$this->setTriggers( [ 'createaccount' ] ); |
| 104 |
$captcha = new SimpleCaptcha(); |
| 105 |
$provider = new CaptchaPreAuthenticationProvider(); |
| 106 |
$this->initProvider( $provider, null, null, $this->getServiceContainer()->getAuthManager() ); |
| 107 |
|
| 108 |
$reqs = $provider->getAuthenticationRequests( AuthManager::ACTION_CREATE, |
| 109 |
[ 'username' => 'Foo' ] ); |
| 110 |
|
| 111 |
$this->assertCount( 1, $reqs ); |
| 112 |
|
| 113 |
$req = $reqs[0]; |
| 114 |
$this->assertInstanceOf( CaptchaAuthenticationRequest::class, $req ); |
| 115 |
|
| 116 |
$id = $req->captchaId; |
| 117 |
$data = $req->captchaData; |
| 118 |
$this->assertEquals( $captcha->retrieveCaptcha( $id ), $data + [ 'index' => $id ] ); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function testTestForAuthentication( $req, $isBadLoginTriggered, |
| 125 |
$isBadLoginPerUserTriggered, $result |
| 126 |
) { |
| 127 |
$this->setTemporaryHook( 'PingLimiter', static function ( $user, $action, &$result ) { |
| 128 |
$result = false; |
| 129 |
return false; |
| 130 |
} ); |
| 131 |
CaptchaStore::get()->store( '345', [ 'question' => '2+2', 'answer' => '4' ] ); |
| 132 |
$loginAttemptCounter = $this->getMockBuilder( LoginAttemptCounter::class ) |
| 133 |
->onlyMethods( [ 'isBadLoginTriggered', 'isBadLoginPerUserTriggered' ] ) |
| 134 |
->disableOriginalConstructor() |
| 135 |
->getMock(); |
| 136 |
$loginAttemptCounter->expects( $this->any() )->method( 'isBadLoginTriggered' ) |
| 137 |
->willReturn( $isBadLoginTriggered ); |
| 138 |
$loginAttemptCounter->expects( $this->any() )->method( 'isBadLoginPerUserTriggered' ) |
| 139 |
->willReturn( $isBadLoginPerUserTriggered ); |
| 140 |
$provider = $this->getProvider(); |
| 141 |
$provider->loginAttemptCounter = $loginAttemptCounter; |
| 142 |
$this->initProvider( $provider, null, null, $this->getServiceContainer()->getAuthManager() ); |
| 143 |
|
| 144 |
$status = $provider->testForAuthentication( $req ? [ $req ] : [] ); |
| 145 |
|
| 146 |
$this->assertEquals( $result, $status->isGood() ); |
| 147 |
} |
| 148 |
|
| 149 |
public static function provideTestForAuthentication() { |
| 150 |
$fallback = new UsernameAuthenticationRequest(); |
| 151 |
$fallback->username = 'Foo'; |
| 152 |
return [ |
| 153 |
|
| 154 |
'no need to check' => [ $fallback, false, false, true ], |
| 155 |
'badlogin' => [ $fallback, true, false, false ], |
| 156 |
'badloginperuser, no username' => [ null, false, true, true ], |
| 157 |
'badloginperuser' => [ $fallback, false, true, false ], |
| 158 |
'non-existent captcha' => [ self::getCaptchaRequest( '123', '4' ), true, true, false ], |
| 159 |
'wrong captcha' => [ self::getCaptchaRequest( '345', '6' ), true, true, false ], |
| 160 |
'correct captcha' => [ self::getCaptchaRequest( '345', '4' ), true, true, true ], |
| 161 |
]; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function testTestForAccountCreation( $req, $creatorIsSysop, $result, $disableTrigger = false ) { |
| 168 |
$this->setTemporaryHook( 'PingLimiter', static function ( $user, $action, &$result ) { |
| 169 |
$result = false; |
| 170 |
return false; |
| 171 |
} ); |
| 172 |
$this->setTriggers( $disableTrigger ? [] : [ 'createaccount' ] ); |
| 173 |
CaptchaStore::get()->store( '345', [ 'question' => '2+2', 'answer' => '4' ] ); |
| 174 |
$user = User::newFromName( 'Foo' ); |
| 175 |
$provider = new CaptchaPreAuthenticationProvider(); |
| 176 |
$this->initProvider( $provider, null, null, $this->getServiceContainer()->getAuthManager() ); |
| 177 |
|
| 178 |
$creator = $creatorIsSysop ? $this->getTestSysop()->getUser() : User::newFromName( 'Bar' ); |
| 179 |
$status = $provider->testForAccountCreation( $user, $creator, $req ? [ $req ] : [] ); |
| 180 |
$this->assertEquals( $result, $status->isGood() ); |
| 181 |
} |
| 182 |
|
| 183 |
public static function provideTestForAccountCreation() { |
| 184 |
return [ |
| 185 |
|
| 186 |
'no captcha' => [ null, false, false ], |
| 187 |
'non-existent captcha' => [ self::getCaptchaRequest( '123', '4' ), false, false ], |
| 188 |
'wrong captcha' => [ self::getCaptchaRequest( '345', '6' ), false, false ], |
| 189 |
'correct captcha' => [ self::getCaptchaRequest( '345', '4' ), false, true ], |
| 190 |
'user is exempt' => [ null, true, true ], |
| 191 |
'disabled' => [ null, false, true, 'disable' ], |
| 192 |
]; |
| 193 |
} |
| 194 |
|
| 195 |
public function testPostAuthentication() { |
| 196 |
$this->setTriggers( [ 'badlogin', 'badloginperuser' ] ); |
| 197 |
$captcha = new SimpleCaptcha(); |
| 198 |
$user = User::newFromName( 'Foo' ); |
| 199 |
$anotherUser = User::newFromName( 'Bar' ); |
| 200 |
$provider = $this->getProvider(); |
| 201 |
$loginAttemptCounter = new LoginAttemptCounter( $captcha ); |
| 202 |
$provider->loginAttemptCounter = $loginAttemptCounter; |
| 203 |
$this->initProvider( $provider, null, null, $this->getServiceContainer()->getAuthManager() ); |
| 204 |
|
| 205 |
$this->assertFalse( $loginAttemptCounter->isBadLoginTriggered() ); |
| 206 |
$this->assertFalse( $loginAttemptCounter->isBadLoginPerUserTriggered( $user ) ); |
| 207 |
|
| 208 |
$provider->postAuthentication( $user, \MediaWiki\Auth\AuthenticationResponse::newFail( |
| 209 |
wfMessage( '?' ) ) ); |
| 210 |
|
| 211 |
$this->assertTrue( $loginAttemptCounter->isBadLoginTriggered() ); |
| 212 |
$this->assertTrue( $loginAttemptCounter->isBadLoginPerUserTriggered( $user ) ); |
| 213 |
$this->assertFalse( $loginAttemptCounter->isBadLoginPerUserTriggered( $anotherUser ) ); |
| 214 |
|
| 215 |
$provider->postAuthentication( $user, \MediaWiki\Auth\AuthenticationResponse::newPass( 'Foo' ) ); |
| 216 |
|
| 217 |
$this->assertFalse( $loginAttemptCounter->isBadLoginPerUserTriggered( $user ) ); |
| 218 |
} |
| 219 |
|
| 220 |
public function testPostAuthentication_disabled() { |
| 221 |
$this->setTriggers( [] ); |
| 222 |
$captcha = new SimpleCaptcha(); |
| 223 |
$loginAttemptCounter = new LoginAttemptCounter( $captcha ); |
| 224 |
$user = User::newFromName( 'Foo' ); |
| 225 |
$provider = $this->getProvider(); |
| 226 |
$provider->loginAttemptCounter = $loginAttemptCounter; |
| 227 |
$this->initProvider( $provider, null, null, $this->getServiceContainer()->getAuthManager() ); |
| 228 |
|
| 229 |
$this->assertFalse( $loginAttemptCounter->isBadLoginTriggered() ); |
| 230 |
$this->assertFalse( $loginAttemptCounter->isBadLoginPerUserTriggered( $user ) ); |
| 231 |
|
| 232 |
$provider->postAuthentication( $user, \MediaWiki\Auth\AuthenticationResponse::newFail( |
| 233 |
wfMessage( '?' ) ) ); |
| 234 |
|
| 235 |
$this->assertFalse( $loginAttemptCounter->isBadLoginTriggered() ); |
| 236 |
$this->assertFalse( $loginAttemptCounter->isBadLoginPerUserTriggered( $user ) ); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function testPingLimiter( array $attempts ) { |
| 243 |
$this->mergeMwGlobalArrayValue( |
| 244 |
'wgRateLimits', |
| 245 |
[ |
| 246 |
'badcaptcha' => [ |
| 247 |
'user' => [ 1, 1 ], |
| 248 |
], |
| 249 |
] |
| 250 |
); |
| 251 |
$provider = new CaptchaPreAuthenticationProvider(); |
| 252 |
$this->initProvider( $provider, null, null, $this->getServiceContainer()->getAuthManager() ); |
| 253 |
|
| 254 |
$providerAccess = TestingAccessWrapper::newFromObject( $provider ); |
| 255 |
|
| 256 |
$disablePingLimiter = false; |
| 257 |
$this->setTemporaryHook( 'PingLimiter', |
| 258 |
static function ( &$user, $action, &$result ) use ( &$disablePingLimiter ) { |
| 259 |
if ( $disablePingLimiter ) { |
| 260 |
$result = false; |
| 261 |
return false; |
| 262 |
} |
| 263 |
return null; |
| 264 |
} |
| 265 |
); |
| 266 |
foreach ( $attempts as $attempt ) { |
| 267 |
$disablePingLimiter = !empty( $attempts[3] ); |
| 268 |
$captcha = new SimpleCaptcha(); |
| 269 |
CaptchaStore::get()->store( '345', [ 'question' => '7+7', 'answer' => '14' ] ); |
| 270 |
$success = $providerAccess->verifyCaptcha( $captcha, [ $attempts[0] ], $attempts[1] ); |
| 271 |
$this->assertEquals( $attempts[2], $success ); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
public static function providePingLimiter() { |
| 276 |
$sysop = User::newFromName( 'UTSysop' ); |
| 277 |
return [ |
| 278 |
|
| 279 |
'no failure' => [ |
| 280 |
[ self::getCaptchaRequest( '345', '14' ), new User(), true ], |
| 281 |
[ self::getCaptchaRequest( '345', '14' ), new User(), true ], |
| 282 |
], |
| 283 |
'limited' => [ |
| 284 |
[ self::getCaptchaRequest( '345', '33' ), new User(), false ], |
| 285 |
[ self::getCaptchaRequest( '345', '14' ), new User(), false ], |
| 286 |
], |
| 287 |
'exempt user' => [ |
| 288 |
[ self::getCaptchaRequest( '345', '33' ), $sysop, false ], |
| 289 |
[ self::getCaptchaRequest( '345', '14' ), $sysop, true ], |
| 290 |
], |
| 291 |
'pinglimiter disabled' => [ |
| 292 |
[ self::getCaptchaRequest( '345', '33' ), new User(), false, 'disable' ], |
| 293 |
[ self::getCaptchaRequest( '345', '14' ), new User(), true, 'disable' ], |
| 294 |
], |
| 295 |
]; |
| 296 |
} |
| 297 |
|
| 298 |
protected static function getCaptchaRequest( $id, $word, $username = null ) { |
| 299 |
$req = new CaptchaAuthenticationRequest( $id, [ 'question' => '?', 'answer' => $word ] ); |
| 300 |
$req->captchaWord = $word; |
| 301 |
$req->username = $username; |
| 302 |
return $req; |
| 303 |
} |
| 304 |
|
| 305 |
protected function blockLogin( $username ) { |
| 306 |
$counter = new LoginAttemptCounter( new SimpleCaptcha() ); |
| 307 |
$counter->increaseBadLoginCounter( $username ); |
| 308 |
} |
| 309 |
|
| 310 |
protected function flagSession() { |
| 311 |
RequestContext::getMain()->getRequest()->getSession() |
| 312 |
->set( 'ConfirmEdit:loginCaptchaPerUserTriggered', true ); |
| 313 |
} |
| 314 |
|
| 315 |
protected function setTriggers( $triggers ) { |
| 316 |
$types = [ 'edit', 'create', 'sendemail', 'addurl', 'createaccount', 'badlogin', |
| 317 |
'badloginperuser' ]; |
| 318 |
$captchaTriggers = array_combine( $types, array_map( static function ( $type ) use ( $triggers ) { |
| 319 |
return in_array( $type, $triggers, true ); |
| 320 |
}, $types ) ); |
| 321 |
$this->overrideConfigValue( 'CaptchaTriggers', $captchaTriggers ); |
| 322 |
} |
| 323 |
|
| 324 |
private function getProvider(): CaptchaPreAuthenticationProvider { |
| 325 |
return new class() extends CaptchaPreAuthenticationProvider { |
| 326 |
public ?LoginAttemptCounter $loginAttemptCounter = null; |
| 327 |
|
| 328 |
protected function getLoginAttemptCounter( SimpleCaptcha $captcha ): LoginAttemptCounter { |
| 329 |
return $this->loginAttemptCounter ?: parent::getLoginAttemptCounter( $captcha ); |
| 330 |
} |
| 331 |
}; |
| 332 |
} |
| 333 |
|
| 334 |
} |