2020-09-19 22:16:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
|
|
|
use ManualLogEntry;
|
2020-11-26 15:23:22 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\ChangeTags\ChangeTagsManager;
|
2020-12-18 14:05:33 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry;
|
2020-09-19 22:16:35 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Filter;
|
2021-01-01 17:28:36 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseFilter;
|
2022-05-27 09:41:54 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2022-06-27 20:39:37 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2020-09-19 22:16:35 +00:00
|
|
|
use Status;
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class FilterStore {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterFilterStore';
|
|
|
|
|
2020-11-29 23:10:14 +00:00
|
|
|
/** @var ConsequencesRegistry */
|
|
|
|
private $consequencesRegistry;
|
2020-09-19 22:16:35 +00:00
|
|
|
|
|
|
|
/** @var ILoadBalancer */
|
|
|
|
private $loadBalancer;
|
|
|
|
|
|
|
|
/** @var FilterProfiler */
|
|
|
|
private $filterProfiler;
|
|
|
|
|
|
|
|
/** @var FilterLookup */
|
|
|
|
private $filterLookup;
|
|
|
|
|
|
|
|
/** @var ChangeTagsManager */
|
|
|
|
private $tagsManager;
|
|
|
|
|
|
|
|
/** @var FilterValidator */
|
|
|
|
private $filterValidator;
|
|
|
|
|
|
|
|
/** @var FilterCompare */
|
|
|
|
private $filterCompare;
|
|
|
|
|
2021-02-10 13:13:03 +00:00
|
|
|
/** @var EmergencyCache */
|
|
|
|
private $emergencyCache;
|
|
|
|
|
2020-09-19 22:16:35 +00:00
|
|
|
/**
|
2020-11-29 23:10:14 +00:00
|
|
|
* @param ConsequencesRegistry $consequencesRegistry
|
2020-09-19 22:16:35 +00:00
|
|
|
* @param ILoadBalancer $loadBalancer
|
|
|
|
* @param FilterProfiler $filterProfiler
|
|
|
|
* @param FilterLookup $filterLookup
|
|
|
|
* @param ChangeTagsManager $tagsManager
|
|
|
|
* @param FilterValidator $filterValidator
|
|
|
|
* @param FilterCompare $filterCompare
|
2021-02-10 13:13:03 +00:00
|
|
|
* @param EmergencyCache $emergencyCache
|
2020-09-19 22:16:35 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2020-11-29 23:10:14 +00:00
|
|
|
ConsequencesRegistry $consequencesRegistry,
|
2020-09-19 22:16:35 +00:00
|
|
|
ILoadBalancer $loadBalancer,
|
|
|
|
FilterProfiler $filterProfiler,
|
|
|
|
FilterLookup $filterLookup,
|
|
|
|
ChangeTagsManager $tagsManager,
|
|
|
|
FilterValidator $filterValidator,
|
2021-02-10 13:13:03 +00:00
|
|
|
FilterCompare $filterCompare,
|
|
|
|
EmergencyCache $emergencyCache
|
2020-09-19 22:16:35 +00:00
|
|
|
) {
|
2020-11-29 23:10:14 +00:00
|
|
|
$this->consequencesRegistry = $consequencesRegistry;
|
2020-09-19 22:16:35 +00:00
|
|
|
$this->loadBalancer = $loadBalancer;
|
|
|
|
$this->filterProfiler = $filterProfiler;
|
|
|
|
$this->filterLookup = $filterLookup;
|
|
|
|
$this->tagsManager = $tagsManager;
|
|
|
|
$this->filterValidator = $filterValidator;
|
|
|
|
$this->filterCompare = $filterCompare;
|
2021-02-10 13:13:03 +00:00
|
|
|
$this->emergencyCache = $emergencyCache;
|
2020-09-19 22:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether user input for the filter editing form is valid and if so saves the filter.
|
|
|
|
* Returns a Status object which can be:
|
|
|
|
* - Good with [ new_filter_id, history_id ] as value if the filter was successfully saved
|
|
|
|
* - Good with value = false if everything went fine but the filter is unchanged
|
|
|
|
* - OK with errors if a validation error occurred
|
|
|
|
* - Fatal in case of a permission-related error
|
|
|
|
*
|
2022-05-27 09:41:54 +00:00
|
|
|
* @param Authority $performer
|
2022-04-26 15:39:04 +00:00
|
|
|
* @param int|null $filterId
|
2020-09-19 22:16:35 +00:00
|
|
|
* @param Filter $newFilter
|
|
|
|
* @param Filter $originalFilter
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
public function saveFilter(
|
2022-05-27 09:41:54 +00:00
|
|
|
Authority $performer,
|
2022-04-26 15:39:04 +00:00
|
|
|
?int $filterId,
|
2020-09-19 22:16:35 +00:00
|
|
|
Filter $newFilter,
|
|
|
|
Filter $originalFilter
|
2021-07-21 18:51:12 +00:00
|
|
|
): Status {
|
2022-05-27 09:41:54 +00:00
|
|
|
$validationStatus = $this->filterValidator->checkAll( $newFilter, $originalFilter, $performer );
|
2020-09-19 22:16:35 +00:00
|
|
|
if ( !$validationStatus->isGood() ) {
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for non-changes
|
|
|
|
$differences = $this->filterCompare->compareVersions( $newFilter, $originalFilter );
|
2022-04-26 15:46:01 +00:00
|
|
|
if ( !$differences ) {
|
2020-09-19 22:16:35 +00:00
|
|
|
return Status::newGood( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything went fine, so let's save the filter
|
|
|
|
$wasGlobal = $originalFilter->isGlobal();
|
2022-05-27 09:41:54 +00:00
|
|
|
[ $newID, $historyID ] = $this->doSaveFilter(
|
|
|
|
$performer->getUser(), $newFilter, $differences, $filterId, $wasGlobal );
|
2020-09-19 22:16:35 +00:00
|
|
|
return Status::newGood( [ $newID, $historyID ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves new filter's info to DB
|
|
|
|
*
|
2022-06-27 20:39:37 +00:00
|
|
|
* @param UserIdentity $userIdentity
|
2020-09-19 22:16:35 +00:00
|
|
|
* @param Filter $newFilter
|
|
|
|
* @param array $differences
|
2022-04-26 15:39:04 +00:00
|
|
|
* @param int|null $filterId
|
2020-09-19 22:16:35 +00:00
|
|
|
* @param bool $wasGlobal
|
|
|
|
* @return int[] first element is new ID, second is history ID
|
|
|
|
*/
|
|
|
|
private function doSaveFilter(
|
2022-06-27 20:39:37 +00:00
|
|
|
UserIdentity $userIdentity,
|
2020-09-19 22:16:35 +00:00
|
|
|
Filter $newFilter,
|
|
|
|
array $differences,
|
2022-04-26 15:39:04 +00:00
|
|
|
?int $filterId,
|
2020-09-19 22:16:35 +00:00
|
|
|
bool $wasGlobal
|
2021-07-21 18:51:12 +00:00
|
|
|
): array {
|
2021-05-02 06:41:53 +00:00
|
|
|
$dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
|
2022-04-30 10:16:45 +00:00
|
|
|
$newRow = $this->filterToDatabaseRow( $newFilter );
|
2020-09-19 22:16:35 +00:00
|
|
|
|
|
|
|
// Set last modifier.
|
|
|
|
$newRow['af_timestamp'] = $dbw->timestamp();
|
2022-06-27 20:39:37 +00:00
|
|
|
$newRow['af_user'] = $userIdentity->getId();
|
|
|
|
$newRow['af_user_text'] = $userIdentity->getName();
|
2020-09-19 22:16:35 +00:00
|
|
|
|
2022-04-26 15:39:04 +00:00
|
|
|
$isNew = $filterId === null;
|
2020-09-19 22:16:35 +00:00
|
|
|
|
|
|
|
// Preserve the old throttled status (if any) only if disabling the filter.
|
|
|
|
// TODO: It might make more sense to check what was actually changed
|
|
|
|
$newRow['af_throttled'] = ( $newRow['af_throttled'] ?? false ) && !$newRow['af_enabled'];
|
|
|
|
// This is null when creating a new filter, but the DB field is NOT NULL
|
|
|
|
$newRow['af_hit_count'] = $newRow['af_hit_count'] ?? 0;
|
2022-04-26 04:05:32 +00:00
|
|
|
$rowForInsert = array_diff_key( $newRow, [ 'af_id' => true ] );
|
2020-09-19 22:16:35 +00:00
|
|
|
|
|
|
|
$dbw->startAtomic( __METHOD__ );
|
2022-04-26 15:39:04 +00:00
|
|
|
if ( $filterId === null ) {
|
2022-04-26 04:05:32 +00:00
|
|
|
$dbw->insert( 'abuse_filter', $rowForInsert, __METHOD__ );
|
2022-04-26 15:39:04 +00:00
|
|
|
$filterId = $dbw->insertId();
|
2022-04-26 04:05:32 +00:00
|
|
|
} else {
|
2022-04-26 15:39:04 +00:00
|
|
|
$dbw->update( 'abuse_filter', $rowForInsert, [ 'af_id' => $filterId ], __METHOD__ );
|
2020-09-19 22:16:35 +00:00
|
|
|
}
|
2022-04-26 15:39:04 +00:00
|
|
|
$newRow['af_id'] = $filterId;
|
2020-09-19 22:16:35 +00:00
|
|
|
|
|
|
|
$actions = $newFilter->getActions();
|
|
|
|
$actionsRows = [];
|
2020-11-29 23:10:14 +00:00
|
|
|
foreach ( $this->consequencesRegistry->getAllEnabledActionNames() as $action ) {
|
2022-04-26 15:39:04 +00:00
|
|
|
if ( !isset( $actions[$action] ) ) {
|
|
|
|
continue;
|
2020-09-19 22:16:35 +00:00
|
|
|
}
|
2022-04-26 15:39:04 +00:00
|
|
|
|
|
|
|
$parameters = $actions[$action];
|
|
|
|
if ( $action === 'throttle' && $parameters[0] === null ) {
|
|
|
|
// FIXME: Do we really need to keep the filter ID inside throttle parameters?
|
|
|
|
// We'd save space, keep things simpler and avoid this hack. Note: if removing
|
|
|
|
// it, a maintenance script will be necessary to clean up the table.
|
|
|
|
$parameters[0] = $filterId;
|
|
|
|
}
|
|
|
|
|
|
|
|
$actionsRows[] = [
|
|
|
|
'afa_filter' => $filterId,
|
|
|
|
'afa_consequence' => $action,
|
|
|
|
'afa_parameters' => implode( "\n", $parameters ),
|
|
|
|
];
|
2020-09-19 22:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a history row
|
|
|
|
$afhRow = [];
|
|
|
|
|
|
|
|
foreach ( AbuseFilter::HISTORY_MAPPINGS as $afCol => $afhCol ) {
|
|
|
|
$afhRow[$afhCol] = $newRow[$afCol];
|
|
|
|
}
|
|
|
|
|
|
|
|
$afhRow['afh_actions'] = serialize( $actions );
|
|
|
|
|
|
|
|
$afhRow['afh_changed_fields'] = implode( ',', $differences );
|
|
|
|
|
|
|
|
$flags = [];
|
|
|
|
if ( $newRow['af_hidden'] ) {
|
|
|
|
$flags[] = 'hidden';
|
|
|
|
}
|
|
|
|
if ( $newRow['af_enabled'] ) {
|
|
|
|
$flags[] = 'enabled';
|
|
|
|
}
|
|
|
|
if ( $newRow['af_deleted'] ) {
|
|
|
|
$flags[] = 'deleted';
|
|
|
|
}
|
|
|
|
if ( $newRow['af_global'] ) {
|
|
|
|
$flags[] = 'global';
|
|
|
|
}
|
|
|
|
|
|
|
|
$afhRow['afh_flags'] = implode( ',', $flags );
|
|
|
|
|
2022-04-26 15:39:04 +00:00
|
|
|
$afhRow['afh_filter'] = $filterId;
|
2020-09-19 22:16:35 +00:00
|
|
|
|
|
|
|
// Do the update
|
|
|
|
$dbw->insert( 'abuse_filter_history', $afhRow, __METHOD__ );
|
|
|
|
$historyID = $dbw->insertId();
|
|
|
|
if ( !$isNew ) {
|
|
|
|
$dbw->delete(
|
|
|
|
'abuse_filter_action',
|
2022-04-26 15:39:04 +00:00
|
|
|
[ 'afa_filter' => $filterId ],
|
2020-09-19 22:16:35 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$dbw->insert( 'abuse_filter_action', $actionsRows, __METHOD__ );
|
|
|
|
|
|
|
|
$dbw->endAtomic( __METHOD__ );
|
|
|
|
|
|
|
|
// Invalidate cache if this was a global rule
|
|
|
|
if ( $wasGlobal || $newRow['af_global'] ) {
|
2021-02-26 16:47:50 +00:00
|
|
|
$this->filterLookup->purgeGroupWANCache( $newRow['af_group'] );
|
2020-09-19 22:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logging
|
2022-04-26 15:46:01 +00:00
|
|
|
$logEntry = new ManualLogEntry( 'abusefilter', $isNew ? 'create' : 'modify' );
|
2022-06-27 20:39:37 +00:00
|
|
|
$logEntry->setPerformer( $userIdentity );
|
2022-04-26 15:39:04 +00:00
|
|
|
$logEntry->setTarget( SpecialAbuseFilter::getTitleForSubpage( (string)$filterId ) );
|
2020-09-19 22:16:35 +00:00
|
|
|
$logEntry->setParameters( [
|
|
|
|
'historyId' => $historyID,
|
2022-04-26 15:39:04 +00:00
|
|
|
'newId' => $filterId
|
2020-09-19 22:16:35 +00:00
|
|
|
] );
|
|
|
|
$logid = $logEntry->insert( $dbw );
|
|
|
|
$logEntry->publish( $logid );
|
|
|
|
|
|
|
|
// Purge the tag list cache so the fetchAllTags hook applies tag changes
|
|
|
|
if ( isset( $actions['tag'] ) ) {
|
|
|
|
$this->tagsManager->purgeTagCache();
|
|
|
|
}
|
|
|
|
|
2022-04-26 15:39:04 +00:00
|
|
|
$this->filterProfiler->resetFilterProfile( $filterId );
|
2021-02-10 13:13:03 +00:00
|
|
|
if ( $newRow['af_enabled'] ) {
|
2022-04-26 15:39:04 +00:00
|
|
|
$this->emergencyCache->setNewForFilter( $filterId, $newRow['af_group'] );
|
2021-02-10 13:13:03 +00:00
|
|
|
}
|
2022-04-26 15:39:04 +00:00
|
|
|
return [ $filterId, $historyID ];
|
2020-09-19 22:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Perhaps add validation to ensure no null values remained.
|
|
|
|
* @param Filter $filter
|
2022-04-30 10:16:45 +00:00
|
|
|
* @return array
|
2020-09-19 22:16:35 +00:00
|
|
|
*/
|
2022-04-30 10:16:45 +00:00
|
|
|
private function filterToDatabaseRow( Filter $filter ): array {
|
2020-09-19 22:16:35 +00:00
|
|
|
// T67807: integer 1's & 0's might be better understood than booleans
|
2022-04-30 10:16:45 +00:00
|
|
|
return [
|
2020-09-19 22:16:35 +00:00
|
|
|
'af_id' => $filter->getID(),
|
|
|
|
'af_pattern' => $filter->getRules(),
|
|
|
|
'af_public_comments' => $filter->getName(),
|
|
|
|
'af_comments' => $filter->getComments(),
|
|
|
|
'af_group' => $filter->getGroup(),
|
|
|
|
'af_actions' => implode( ',', $filter->getActionsNames() ),
|
|
|
|
'af_enabled' => (int)$filter->isEnabled(),
|
|
|
|
'af_deleted' => (int)$filter->isDeleted(),
|
|
|
|
'af_hidden' => (int)$filter->isHidden(),
|
|
|
|
'af_global' => (int)$filter->isGlobal(),
|
|
|
|
'af_user' => $filter->getUserID(),
|
|
|
|
'af_user_text' => $filter->getUserName(),
|
|
|
|
'af_timestamp' => $filter->getTimestamp(),
|
|
|
|
'af_hit_count' => $filter->getHitCount(),
|
|
|
|
'af_throttled' => (int)$filter->isThrottled(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|