Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
0.00% |
0 / 69 |
|
0.00% |
0 / 4 |
650 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIPFromUser | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| onGetUserPermissionsErrorsExpensive | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
272 | |||
| onOtherBlockLogLink | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| 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 | * https://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Extension\StopForumSpam; |
| 22 | |
| 23 | use MediaWiki\Config\Config; |
| 24 | use MediaWiki\Context\RequestContext; |
| 25 | use MediaWiki\Hook\OtherBlockLogLinkHook; |
| 26 | use MediaWiki\Html\Html; |
| 27 | use MediaWiki\Logger\LoggerFactory; |
| 28 | use MediaWiki\MediaWikiServices; |
| 29 | use MediaWiki\Permissions\Hook\GetUserPermissionsErrorsExpensiveHook; |
| 30 | use MediaWiki\Title\Title; |
| 31 | use MediaWiki\User\User; |
| 32 | use Wikimedia\IPUtils; |
| 33 | |
| 34 | class Hooks implements |
| 35 | GetUserPermissionsErrorsExpensiveHook, |
| 36 | OtherBlockLogLinkHook |
| 37 | { |
| 38 | |
| 39 | public function __construct( private readonly Config $config ) { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get an IP address for a User if possible |
| 44 | * |
| 45 | * @param User $user |
| 46 | * @return bool|string IP address or false |
| 47 | */ |
| 48 | public static function getIPFromUser( User $user ) { |
| 49 | $context = RequestContext::getMain(); |
| 50 | if ( !$context->getUser()->isSafeToLoad() ) { |
| 51 | return false; |
| 52 | } |
| 53 | if ( $context->getUser()->getName() === $user->getName() ) { |
| 54 | // Only use the main context if the users are the same |
| 55 | return $context->getRequest()->getIP(); |
| 56 | } |
| 57 | |
| 58 | // Couldn't figure out an IP address |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * If an IP address is deny-listed, don't let them edit. |
| 64 | * |
| 65 | * @param Title $title Title being acted upon |
| 66 | * @param User $user User performing the action |
| 67 | * @param string $action Action being performed |
| 68 | * @param array &$result Will be filled with block status if blocked |
| 69 | * @return bool |
| 70 | */ |
| 71 | public function onGetUserPermissionsErrorsExpensive( $title, $user, $action, &$result ) { |
| 72 | if ( !$this->config->get( 'SFSIPListLocation' ) ) { |
| 73 | // Not configured |
| 74 | return true; |
| 75 | } |
| 76 | if ( $action === 'read' ) { |
| 77 | return true; |
| 78 | } |
| 79 | if ( $this->config->get( 'BlockAllowsUTEdit' ) && $title->equals( $user->getTalkPage() ) ) { |
| 80 | // Let a user edit their talk page |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | $exemptReasons = []; |
| 85 | $logger = LoggerFactory::getInstance( 'StopForumSpam' ); |
| 86 | $ip = self::getIPFromUser( $user ); |
| 87 | |
| 88 | // attempt to get ip from user |
| 89 | if ( $ip === false ) { |
| 90 | $exemptReasons[] = "Unable to obtain IP information for {user}"; |
| 91 | } |
| 92 | |
| 93 | // allow if user has sfsblock-bypass |
| 94 | if ( $user->isAllowed( 'sfsblock-bypass' ) ) { |
| 95 | $exemptReasons[] = "{user} is exempt from SFS blocks"; |
| 96 | } |
| 97 | |
| 98 | // allow if user is exempted from autoblocks (borrowed from TorBlock) |
| 99 | if ( MediaWikiServices::getInstance()->getAutoblockExemptionList()->isExempt( $ip ) ) { |
| 100 | $exemptReasons[] = "{clientip} is in autoblock exemption list, exempting from SFS blocks"; |
| 101 | } |
| 102 | |
| 103 | $denyListManager = DenyListManager::singleton(); |
| 104 | if ( !$this->config->get( 'SFSReportOnly' ) ) { |
| 105 | // enforce mode + ip not deny-listed = allow action |
| 106 | if ( !$denyListManager->isIpDenyListed( $ip ) || count( $exemptReasons ) > 0 ) { |
| 107 | return true; |
| 108 | } |
| 109 | } elseif ( |
| 110 | $denyListManager->isIpDenyListed( $ip ) && |
| 111 | count( $exemptReasons ) > 0 |
| 112 | ) { |
| 113 | // report-only mode + ip deny-listed = allow action and log |
| 114 | $exemptReasonsStr = implode( ', ', $exemptReasons ); |
| 115 | $logger->info( |
| 116 | $exemptReasonsStr, |
| 117 | [ |
| 118 | 'action' => $action, |
| 119 | 'clientip' => $ip, |
| 120 | 'title' => $title->getPrefixedText(), |
| 121 | 'user' => $user->getName(), |
| 122 | 'reportonly' => $this->config->get( 'SFSReportOnly' ) |
| 123 | ] |
| 124 | ); |
| 125 | |
| 126 | return true; |
| 127 | |
| 128 | } elseif ( !$denyListManager->isIpDenyListed( $ip ) ) { |
| 129 | // report-only mode + ip NOT deny-listed |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | // log blocked action, regardless of report-only mode |
| 134 | $blockVerb = ( $this->config->get( 'SFSReportOnly' ) ) ? 'would have been' : 'was'; |
| 135 | $logger->info( |
| 136 | "{user} {$blockVerb} blocked by SFS from doing {action} " |
| 137 | . "by using {clientip} on \"{title}\".", |
| 138 | [ |
| 139 | 'action' => $action, |
| 140 | 'clientip' => $ip, |
| 141 | 'title' => $title->getPrefixedText(), |
| 142 | 'user' => $user->getName(), |
| 143 | 'reportonly' => $this->config->get( 'SFSReportOnly' ) |
| 144 | ] |
| 145 | ); |
| 146 | |
| 147 | // final catch-all for report-only mode |
| 148 | if ( $this->config->get( 'SFSReportOnly' ) ) { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | // default: set error msg result and return false |
| 153 | $result = [ 'stopforumspam-blocked', $ip ]; |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @param array &$msg |
| 159 | * @param string $ip |
| 160 | * @return bool |
| 161 | */ |
| 162 | public function onOtherBlockLogLink( &$msg, $ip ) { |
| 163 | if ( |
| 164 | !$this->config->get( 'SFSIPListLocation' ) || |
| 165 | $this->config->get( 'SFSReportOnly' ) |
| 166 | ) { |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | $denyListManager = DenyListManager::singleton(); |
| 171 | if ( IPUtils::isIPAddress( $ip ) && $denyListManager->isIpDenyListed( $ip ) ) { |
| 172 | $msg[] = Html::rawElement( |
| 173 | 'span', |
| 174 | [ 'class' => 'mw-stopforumspam-denylisted' ], |
| 175 | wfMessage( 'stopforumspam-is-blocked', $ip )->parse() |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | } |