2020-10-24 11:58:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
|
|
|
use DeferredUpdates;
|
|
|
|
use ExtensionRegistry;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use ManualLogEntry;
|
2021-02-11 19:26:23 +00:00
|
|
|
use MediaWiki\CheckUser\Hooks;
|
2020-10-24 11:58:46 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2021-01-02 14:01:00 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
|
2021-01-02 13:41:31 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariablesBlobStore;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager;
|
2023-08-16 01:54:05 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-07-15 11:22:29 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
2020-10-24 11:58:46 +00:00
|
|
|
use User;
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2023-02-26 12:51:08 +00:00
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
2020-10-24 11:58:46 +00:00
|
|
|
|
|
|
|
class AbuseLogger {
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
'AbuseFilterLogIP',
|
|
|
|
'AbuseFilterNotifications',
|
|
|
|
'AbuseFilterNotificationsPrivate',
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var Title */
|
|
|
|
private $title;
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2021-01-02 14:01:00 +00:00
|
|
|
/** @var VariableHolder */
|
2020-10-24 11:58:46 +00:00
|
|
|
private $vars;
|
|
|
|
/** @var string */
|
|
|
|
private $action;
|
|
|
|
|
|
|
|
/** @var CentralDBManager */
|
|
|
|
private $centralDBManager;
|
|
|
|
/** @var FilterLookup */
|
|
|
|
private $filterLookup;
|
2020-09-29 14:52:05 +00:00
|
|
|
/** @var VariablesBlobStore */
|
|
|
|
private $varBlobStore;
|
2020-10-18 22:25:05 +00:00
|
|
|
/** @var VariablesManager */
|
|
|
|
private $varManager;
|
2021-01-05 13:33:45 +00:00
|
|
|
/** @var EditRevUpdater */
|
|
|
|
private $editRevUpdater;
|
2023-02-26 12:51:08 +00:00
|
|
|
/** @var LBFactory */
|
|
|
|
private $lbFactory;
|
2020-10-24 11:58:46 +00:00
|
|
|
/** @var ServiceOptions */
|
|
|
|
private $options;
|
|
|
|
/** @var string */
|
|
|
|
private $wikiID;
|
|
|
|
/** @var string */
|
|
|
|
private $requestIP;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CentralDBManager $centralDBManager
|
|
|
|
* @param FilterLookup $filterLookup
|
2020-09-29 14:52:05 +00:00
|
|
|
* @param VariablesBlobStore $varBlobStore
|
2020-10-18 22:25:05 +00:00
|
|
|
* @param VariablesManager $varManager
|
2021-01-05 13:33:45 +00:00
|
|
|
* @param EditRevUpdater $editRevUpdater
|
2023-02-26 12:51:08 +00:00
|
|
|
* @param LBFactory $lbFactory
|
2020-10-24 11:58:46 +00:00
|
|
|
* @param ServiceOptions $options
|
|
|
|
* @param string $wikiID
|
|
|
|
* @param string $requestIP
|
|
|
|
* @param Title $title
|
|
|
|
* @param User $user
|
2021-01-02 14:01:00 +00:00
|
|
|
* @param VariableHolder $vars
|
2020-10-24 11:58:46 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
CentralDBManager $centralDBManager,
|
|
|
|
FilterLookup $filterLookup,
|
2020-09-29 14:52:05 +00:00
|
|
|
VariablesBlobStore $varBlobStore,
|
2020-10-18 22:25:05 +00:00
|
|
|
VariablesManager $varManager,
|
2021-01-05 13:33:45 +00:00
|
|
|
EditRevUpdater $editRevUpdater,
|
2023-02-26 12:51:08 +00:00
|
|
|
LBFactory $lbFactory,
|
2020-10-24 11:58:46 +00:00
|
|
|
ServiceOptions $options,
|
|
|
|
string $wikiID,
|
|
|
|
string $requestIP,
|
|
|
|
Title $title,
|
|
|
|
User $user,
|
2021-01-02 14:01:00 +00:00
|
|
|
VariableHolder $vars
|
2020-10-24 11:58:46 +00:00
|
|
|
) {
|
|
|
|
if ( !$vars->varIsSet( 'action' ) ) {
|
|
|
|
throw new InvalidArgumentException( "The 'action' variable is not set." );
|
|
|
|
}
|
|
|
|
$this->centralDBManager = $centralDBManager;
|
|
|
|
$this->filterLookup = $filterLookup;
|
2020-09-29 14:52:05 +00:00
|
|
|
$this->varBlobStore = $varBlobStore;
|
2020-10-18 22:25:05 +00:00
|
|
|
$this->varManager = $varManager;
|
2021-01-05 13:33:45 +00:00
|
|
|
$this->editRevUpdater = $editRevUpdater;
|
2023-02-26 12:51:08 +00:00
|
|
|
$this->lbFactory = $lbFactory;
|
2020-10-24 11:58:46 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
$this->options = $options;
|
|
|
|
$this->wikiID = $wikiID;
|
|
|
|
$this->requestIP = $requestIP;
|
|
|
|
$this->title = $title;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->vars = $vars;
|
2020-10-18 22:25:05 +00:00
|
|
|
$this->action = $vars->getComputedVariable( 'action' )->toString();
|
2020-10-24 11:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and publish log entries for taken actions
|
|
|
|
*
|
|
|
|
* @param array[] $actionsTaken
|
|
|
|
* @return array Shape is [ 'local' => int[], 'global' => int[] ], IDs of logged filters
|
|
|
|
* @phan-return array{local:int[],global:int[]}
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function addLogEntries( array $actionsTaken ): array {
|
2023-02-26 12:51:08 +00:00
|
|
|
$dbw = $this->lbFactory->getPrimaryDatabase();
|
2020-10-24 11:58:46 +00:00
|
|
|
$logTemplate = $this->buildLogTemplate();
|
|
|
|
$centralLogTemplate = [
|
|
|
|
'afl_wiki' => $this->wikiID,
|
|
|
|
];
|
|
|
|
|
|
|
|
$logRows = [];
|
|
|
|
$centralLogRows = [];
|
|
|
|
$loggedLocalFilters = [];
|
|
|
|
$loggedGlobalFilters = [];
|
|
|
|
|
|
|
|
foreach ( $actionsTaken as $filter => $actions ) {
|
2020-12-02 22:47:40 +00:00
|
|
|
list( $filterID, $global ) = GlobalNameUtils::splitGlobalName( $filter );
|
2020-10-24 11:58:46 +00:00
|
|
|
$thisLog = $logTemplate;
|
2019-02-06 16:07:01 +00:00
|
|
|
$thisLog['afl_filter_id'] = $filterID;
|
|
|
|
$thisLog['afl_global'] = (int)$global;
|
2020-10-24 11:58:46 +00:00
|
|
|
$thisLog['afl_actions'] = implode( ',', $actions );
|
|
|
|
|
|
|
|
// Don't log if we were only throttling.
|
|
|
|
// TODO This check should be removed or rewritten using Consequence objects
|
|
|
|
if ( $thisLog['afl_actions'] !== 'throttle' ) {
|
|
|
|
$logRows[] = $thisLog;
|
|
|
|
// Global logging
|
|
|
|
if ( $global ) {
|
|
|
|
$centralLog = $thisLog + $centralLogTemplate;
|
2019-02-06 16:07:01 +00:00
|
|
|
$centralLog['afl_filter_id'] = $filterID;
|
|
|
|
$centralLog['afl_global'] = 0;
|
2020-10-24 11:58:46 +00:00
|
|
|
$centralLog['afl_title'] = $this->title->getPrefixedText();
|
|
|
|
$centralLog['afl_namespace'] = 0;
|
|
|
|
|
|
|
|
$centralLogRows[] = $centralLog;
|
|
|
|
$loggedGlobalFilters[] = $filterID;
|
|
|
|
} else {
|
|
|
|
$loggedLocalFilters[] = $filterID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !count( $logRows ) ) {
|
|
|
|
return [ 'local' => [], 'global' => [] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
$localLogIDs = $this->insertLocalLogEntries( $logRows, $dbw );
|
|
|
|
|
|
|
|
$globalLogIDs = [];
|
|
|
|
if ( count( $loggedGlobalFilters ) ) {
|
2021-05-02 06:41:53 +00:00
|
|
|
$fdb = $this->centralDBManager->getConnection( DB_PRIMARY );
|
2020-10-24 11:58:46 +00:00
|
|
|
$globalLogIDs = $this->insertGlobalLogEntries( $centralLogRows, $fdb );
|
|
|
|
}
|
|
|
|
|
2021-01-05 13:33:45 +00:00
|
|
|
$this->editRevUpdater->setLogIdsForTarget(
|
|
|
|
$this->title,
|
|
|
|
[ 'local' => $localLogIDs, 'global' => $globalLogIDs ]
|
|
|
|
);
|
2020-10-24 11:58:46 +00:00
|
|
|
|
|
|
|
return [ 'local' => $loggedLocalFilters, 'global' => $loggedGlobalFilters ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a template to use for logging taken actions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function buildLogTemplate(): array {
|
2020-10-24 11:58:46 +00:00
|
|
|
// If $this->user isn't safe to load (e.g. a failure during
|
|
|
|
// AbortAutoAccount), create a dummy anonymous user instead.
|
|
|
|
$user = $this->user->isSafeToLoad() ? $this->user : new User;
|
|
|
|
// Create a template
|
|
|
|
$logTemplate = [
|
|
|
|
'afl_user' => $user->getId(),
|
|
|
|
'afl_user_text' => $user->getName(),
|
2023-02-26 12:51:08 +00:00
|
|
|
'afl_timestamp' => $this->lbFactory->getReplicaDatabase()->timestamp(),
|
2020-10-24 11:58:46 +00:00
|
|
|
'afl_namespace' => $this->title->getNamespace(),
|
|
|
|
'afl_title' => $this->title->getDBkey(),
|
|
|
|
'afl_action' => $this->action,
|
|
|
|
'afl_ip' => $this->options->get( 'AbuseFilterLogIP' ) ? $this->requestIP : ''
|
|
|
|
];
|
|
|
|
// Hack to avoid revealing IPs of people creating accounts
|
|
|
|
if ( ( $this->action === 'createaccount' || $this->action === 'autocreateaccount' ) && !$user->getId() ) {
|
2020-10-18 22:25:05 +00:00
|
|
|
$logTemplate['afl_user_text'] = $this->vars->getComputedVariable( 'accountname' )->toString();
|
2020-10-24 11:58:46 +00:00
|
|
|
}
|
|
|
|
return $logTemplate;
|
|
|
|
}
|
|
|
|
|
2022-07-15 20:20:21 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @return ManualLogEntry
|
|
|
|
*/
|
|
|
|
private function newLocalLogEntryFromData( array $data ): ManualLogEntry {
|
|
|
|
// Give grep a chance to find the usages:
|
|
|
|
// logentry-abusefilter-hit
|
|
|
|
$entry = new ManualLogEntry( 'abusefilter', 'hit' );
|
2022-07-15 11:22:29 +00:00
|
|
|
$user = new UserIdentityValue( $data['afl_user'], $data['afl_user_text'] );
|
2022-07-15 20:20:21 +00:00
|
|
|
$entry->setPerformer( $user );
|
|
|
|
$entry->setTarget( $this->title );
|
|
|
|
$filterName = GlobalNameUtils::buildGlobalName(
|
|
|
|
$data['afl_filter_id'],
|
|
|
|
$data['afl_global'] === 1
|
|
|
|
);
|
|
|
|
// Additional info
|
|
|
|
$entry->setParameters( [
|
|
|
|
'action' => $data['afl_action'],
|
|
|
|
'filter' => $filterName,
|
|
|
|
'actions' => $data['afl_actions'],
|
|
|
|
'log' => $data['afl_id'],
|
|
|
|
] );
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
2020-10-24 11:58:46 +00:00
|
|
|
/**
|
|
|
|
* @param array[] $logRows
|
|
|
|
* @param IDatabase $dbw
|
2021-01-05 13:33:45 +00:00
|
|
|
* @return int[]
|
2020-10-24 11:58:46 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function insertLocalLogEntries( array $logRows, IDatabase $dbw ): array {
|
2020-09-29 14:52:05 +00:00
|
|
|
$varDump = $this->varBlobStore->storeVarDump( $this->vars );
|
2020-10-24 11:58:46 +00:00
|
|
|
|
|
|
|
$loggedIDs = [];
|
|
|
|
foreach ( $logRows as $data ) {
|
|
|
|
$data['afl_var_dump'] = $varDump;
|
|
|
|
$dbw->insert( 'abuse_filter_log', $data, __METHOD__ );
|
|
|
|
$loggedIDs[] = $data['afl_id'] = $dbw->insertId();
|
|
|
|
|
|
|
|
// Send data to CheckUser if installed and we
|
|
|
|
// aren't already sending a notification to recentchanges
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'CheckUser' )
|
|
|
|
&& strpos( $this->options->get( 'AbuseFilterNotifications' ), 'rc' ) === false
|
|
|
|
) {
|
|
|
|
global $wgCheckUserLogAdditionalRights;
|
|
|
|
$wgCheckUserLogAdditionalRights[] = 'abusefilter-view';
|
2022-07-15 11:22:29 +00:00
|
|
|
$entry = $this->newLocalLogEntryFromData( $data );
|
|
|
|
$user = $entry->getPerformerIdentity();
|
|
|
|
// Invert the hack from ::buildLogTemplate because CheckUser attempts
|
|
|
|
// to assign an actor id to the non-existing user
|
|
|
|
if (
|
|
|
|
( $this->action === 'createaccount' || $this->action === 'autocreateaccount' )
|
|
|
|
&& !$user->getId()
|
|
|
|
) {
|
|
|
|
$entry->setPerformer( new UserIdentityValue( 0, $this->requestIP ) );
|
|
|
|
}
|
2020-10-24 11:58:46 +00:00
|
|
|
$rc = $entry->getRecentChange();
|
2021-02-11 19:26:23 +00:00
|
|
|
Hooks::updateCheckUserData( $rc );
|
2020-10-24 11:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->options->get( 'AbuseFilterNotifications' ) !== false ) {
|
2019-02-06 16:07:01 +00:00
|
|
|
$filterID = $data['afl_filter_id'];
|
|
|
|
$global = $data['afl_global'];
|
2020-10-24 11:58:46 +00:00
|
|
|
if (
|
|
|
|
!$this->options->get( 'AbuseFilterNotificationsPrivate' ) &&
|
|
|
|
$this->filterLookup->getFilter( $filterID, $global )->isHidden()
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-15 11:22:29 +00:00
|
|
|
$entry = $this->newLocalLogEntryFromData( $data );
|
2020-10-24 11:58:46 +00:00
|
|
|
$this->publishEntry( $dbw, $entry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $loggedIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array[] $centralLogRows
|
|
|
|
* @param IDatabase $fdb
|
2021-01-05 13:33:45 +00:00
|
|
|
* @return int[]
|
2020-10-24 11:58:46 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function insertGlobalLogEntries( array $centralLogRows, IDatabase $fdb ): array {
|
2020-10-18 22:25:05 +00:00
|
|
|
$this->varManager->computeDBVars( $this->vars );
|
2020-09-29 14:52:05 +00:00
|
|
|
$globalVarDump = $this->varBlobStore->storeVarDump( $this->vars, true );
|
2020-10-24 11:58:46 +00:00
|
|
|
foreach ( $centralLogRows as $index => $data ) {
|
|
|
|
$centralLogRows[$index]['afl_var_dump'] = $globalVarDump;
|
|
|
|
}
|
|
|
|
|
|
|
|
$loggedIDs = [];
|
|
|
|
foreach ( $centralLogRows as $row ) {
|
|
|
|
$fdb->insert( 'abuse_filter_log', $row, __METHOD__ );
|
|
|
|
$loggedIDs[] = $fdb->insertId();
|
|
|
|
}
|
|
|
|
return $loggedIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like ManualLogEntry::publish, but doesn't require an ID (which we don't have) and skips the
|
|
|
|
* tagging part
|
|
|
|
*
|
|
|
|
* @param IDatabase $dbw To cancel the callback if the log insertion fails
|
|
|
|
* @param ManualLogEntry $entry
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function publishEntry( IDatabase $dbw, ManualLogEntry $entry ): void {
|
2020-10-24 11:58:46 +00:00
|
|
|
DeferredUpdates::addCallableUpdate(
|
|
|
|
function () use ( $entry ) {
|
|
|
|
$rc = $entry->getRecentChange();
|
|
|
|
$to = $this->options->get( 'AbuseFilterNotifications' );
|
|
|
|
|
|
|
|
if ( $to === 'rc' || $to === 'rcandudp' ) {
|
|
|
|
$rc->save( $rc::SEND_NONE );
|
|
|
|
}
|
|
|
|
if ( $to === 'udp' || $to === 'rcandudp' ) {
|
|
|
|
$rc->notifyRCFeeds();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DeferredUpdates::POSTSEND,
|
|
|
|
$dbw
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|