Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.47% |
51 / 57 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AntiSpoofPreAuthenticationProvider | |
89.47% |
51 / 57 |
|
66.67% |
4 / 6 |
24.67 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getAuthenticationRequests | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| testForAccountCreation | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| testUserInternal | |
100.00% |
40 / 40 |
|
100.00% |
1 / 1 |
9 | |||
| testUserForCreation | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| getSpoofUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | */ |
| 18 | |
| 19 | namespace MediaWiki\Extension\AntiSpoof; |
| 20 | |
| 21 | use MediaWiki\Auth\AbstractPreAuthenticationProvider; |
| 22 | use MediaWiki\Auth\AuthenticationRequest; |
| 23 | use MediaWiki\Auth\AuthManager; |
| 24 | use MediaWiki\Context\RequestContext; |
| 25 | use MediaWiki\Language\RawMessage; |
| 26 | use MediaWiki\Message\Message; |
| 27 | use MediaWiki\Permissions\PermissionManager; |
| 28 | use MediaWiki\User\User; |
| 29 | use MediaWiki\User\UserIdentity; |
| 30 | use Psr\Log\LoggerInterface; |
| 31 | use Psr\Log\NullLogger; |
| 32 | use StatusValue; |
| 33 | |
| 34 | class AntiSpoofPreAuthenticationProvider extends AbstractPreAuthenticationProvider { |
| 35 | /** @var bool False effectively disables this provider, but spoofed names will still be logged. */ |
| 36 | protected bool $antiSpoofAccounts; |
| 37 | |
| 38 | /** |
| 39 | * @param PermissionManager $permissionManager |
| 40 | * @param array $params Options: |
| 41 | * - antiSpoofAccounts: (bool) stop spoofed accounts from being created. When false, only log. |
| 42 | */ |
| 43 | public function __construct( |
| 44 | private readonly PermissionManager $permissionManager, |
| 45 | array $params = [], |
| 46 | ) { |
| 47 | global $wgAntiSpoofAccounts; |
| 48 | |
| 49 | $params += [ 'antiSpoofAccounts' => $wgAntiSpoofAccounts ]; |
| 50 | |
| 51 | $this->antiSpoofAccounts = $params['antiSpoofAccounts']; |
| 52 | } |
| 53 | |
| 54 | /** @inheritDoc */ |
| 55 | public function getAuthenticationRequests( $action, array $options ) { |
| 56 | $needed = false; |
| 57 | if ( $action == AuthManager::ACTION_CREATE ) { |
| 58 | $user = User::newFromName( $options['username'] ) ?: new User(); |
| 59 | $needed = $this->antiSpoofAccounts && |
| 60 | $this->permissionManager->userHasAnyRight( $user, 'override-antispoof' ); |
| 61 | } |
| 62 | |
| 63 | return $needed ? [ new AntiSpoofAuthenticationRequest() ] : []; |
| 64 | } |
| 65 | |
| 66 | /** @inheritDoc */ |
| 67 | public function testForAccountCreation( $user, $creator, array $reqs ) { |
| 68 | /** @var AntiSpoofAuthenticationRequest $req */ |
| 69 | $req = AuthenticationRequest::getRequestByClass( $reqs, AntiSpoofAuthenticationRequest::class ); |
| 70 | $override = $req && $req->ignoreAntiSpoof && $creator->isAllowed( 'override-antispoof' ); |
| 71 | |
| 72 | return $this->testUserInternal( $user, $override, $this->logger ); |
| 73 | } |
| 74 | |
| 75 | private function testUserInternal( UserIdentity $user, bool $override, LoggerInterface $logger ): StatusValue { |
| 76 | $name = $user->getName(); |
| 77 | $spoofUser = $this->getSpoofUser( $user ); |
| 78 | $mode = !$this->antiSpoofAccounts ? 'LOGGING ' : ( $override ? 'OVERRIDE ' : '' ); |
| 79 | $active = $this->antiSpoofAccounts && !$override; |
| 80 | |
| 81 | if ( $spoofUser->isLegal() ) { |
| 82 | $normalized = $spoofUser->getNormalized(); |
| 83 | $conflicts = $spoofUser->getConflicts(); |
| 84 | if ( !$conflicts ) { |
| 85 | $logger->debug( "{mode}PASS new account '{name}' [{normalized}]", [ |
| 86 | 'mode' => $mode, |
| 87 | 'name' => $name, |
| 88 | 'normalized' => $normalized, |
| 89 | ] ); |
| 90 | } else { |
| 91 | $logger->info( "{mode}CONFLICT new account '{name}' [{normalized}] spoofs {spoofs}", [ |
| 92 | 'mode' => $mode, |
| 93 | 'name' => $name, |
| 94 | 'normalized' => $normalized, |
| 95 | 'spoofs' => $conflicts, |
| 96 | ] ); |
| 97 | |
| 98 | if ( $active ) { |
| 99 | $list = []; |
| 100 | foreach ( $conflicts as $simUser ) { |
| 101 | $list[] = "* " . wfMessage( 'antispoof-conflict-item', $simUser )->plain(); |
| 102 | } |
| 103 | $list = implode( "\n", $list ); |
| 104 | |
| 105 | return StatusValue::newFatal( |
| 106 | 'antispoof-conflict', |
| 107 | $name, |
| 108 | Message::numParam( count( $conflicts ) ), |
| 109 | // Avoid forced wikitext escaping for params in the Status class |
| 110 | new RawMessage( $list ) |
| 111 | ); |
| 112 | } |
| 113 | } |
| 114 | } else { |
| 115 | $error = $spoofUser->getErrorStatus(); |
| 116 | $logger->info( "{mode}ILLEGAL new account '{name}' {error}", [ |
| 117 | 'mode' => $mode, |
| 118 | 'name' => $name, |
| 119 | 'error' => $error->getMessage( false, false, 'en' )->text(), |
| 120 | ] ); |
| 121 | if ( $active ) { |
| 122 | return StatusValue::newFatal( 'antispoof-name-illegal', $name, |
| 123 | $error->getMessage() ); |
| 124 | } |
| 125 | } |
| 126 | return StatusValue::newGood(); |
| 127 | } |
| 128 | |
| 129 | /** @inheritDoc */ |
| 130 | public function testUserForCreation( $user, $autocreate, array $options = [] ) { |
| 131 | $sv = StatusValue::newGood(); |
| 132 | |
| 133 | // For "cancreate" checks via the API, test if the current user could |
| 134 | // create the username. |
| 135 | if ( $this->antiSpoofAccounts && !$autocreate && empty( $options['creating'] ) && |
| 136 | !$this->permissionManager->userHasAnyRight( RequestContext::getMain()->getUser(), 'override-antispoof' ) |
| 137 | ) { |
| 138 | $sv->merge( $this->testUserInternal( $user, false, new NullLogger ) ); |
| 139 | } |
| 140 | |
| 141 | return $sv; |
| 142 | } |
| 143 | |
| 144 | protected function getSpoofUser( UserIdentity $user ): SpoofUser { |
| 145 | return new SpoofUser( $user->getName() ); |
| 146 | } |
| 147 | } |