2018-07-08 16:06:19 +00:00
|
|
|
<?php
|
2018-08-23 18:53:55 +00:00
|
|
|
|
2019-05-31 15:08:19 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2018-08-23 18:53:55 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-06-23 17:19:28 +00:00
|
|
|
use MediaWiki\Storage\PageEditStash;
|
2019-08-26 13:01:09 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-23 18:53:55 +00:00
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
/**
|
|
|
|
* Complete tests where filters are saved, actions are executed and the right
|
|
|
|
* consequences are expected to be taken
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Test
|
|
|
|
* @group AbuseFilter
|
|
|
|
* @group AbuseFilterConsequences
|
|
|
|
* @group Database
|
2018-12-27 19:27:25 +00:00
|
|
|
* @group Large
|
2018-07-08 16:06:19 +00:00
|
|
|
*
|
|
|
|
* @covers AbuseFilter
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @covers AbuseFilterRunner
|
2018-08-22 11:27:50 +00:00
|
|
|
* @covers AbuseFilterHooks
|
2020-01-11 17:05:30 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGenerator
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\VariableGenerator\RunVariableGenerator
|
2018-12-27 19:27:25 +00:00
|
|
|
* @covers AbuseFilterPreAuthenticationProvider
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @covers AbuseFilterParser
|
2018-12-27 19:27:25 +00:00
|
|
|
* @todo Add upload actions everywhere
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
|
|
|
class AbuseFilterConsequencesTest extends MediaWikiTestCase {
|
2018-08-23 18:53:55 +00:00
|
|
|
/**
|
|
|
|
* @var User The user performing actions
|
|
|
|
*/
|
2019-08-26 13:01:09 +00:00
|
|
|
private $user;
|
2018-08-24 14:22:58 +00:00
|
|
|
/** To be used as fake timestamp in several tests */
|
2020-01-21 11:13:11 +00:00
|
|
|
private const MAGIC_TIMESTAMP = 2051222400;
|
2018-08-25 12:44:01 +00:00
|
|
|
/** Prefix for tables to emulate an external DB */
|
2020-01-21 11:13:11 +00:00
|
|
|
public const DB_EXTERNAL_PREFIX = 'external_';
|
2018-08-25 12:44:01 +00:00
|
|
|
/** Tables to create in the external DB */
|
|
|
|
public static $externalTables = [
|
|
|
|
'abuse_filter',
|
|
|
|
'abuse_filter_action',
|
|
|
|
'abuse_filter_log',
|
|
|
|
'text',
|
|
|
|
];
|
2018-07-08 16:06:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array This tables will be deleted in parent::tearDown
|
|
|
|
*/
|
|
|
|
protected $tablesUsed = [
|
|
|
|
'abuse_filter',
|
|
|
|
'abuse_filter_action',
|
|
|
|
'abuse_filter_history',
|
|
|
|
'abuse_filter_log',
|
2018-11-09 21:58:40 +00:00
|
|
|
'page',
|
|
|
|
'ipblocks',
|
2018-12-27 19:27:25 +00:00
|
|
|
'logging',
|
2019-02-08 05:39:51 +00:00
|
|
|
'change_tag',
|
2018-08-25 12:44:01 +00:00
|
|
|
'user',
|
|
|
|
'text'
|
2018-07-08 16:06:19 +00:00
|
|
|
];
|
|
|
|
|
2019-02-23 09:01:57 +00:00
|
|
|
// phpcs:disable Generic.Files.LineLength
|
2018-07-08 16:06:19 +00:00
|
|
|
// Filters that may be created, their key is the ID.
|
|
|
|
protected static $filters = [
|
|
|
|
1 => [
|
|
|
|
'af_pattern' => 'added_lines irlike "foo"',
|
|
|
|
'af_public_comments' => 'Mock filter for edit',
|
|
|
|
'actions' => [
|
|
|
|
'warn' => [
|
|
|
|
'abusefilter-my-warning'
|
|
|
|
],
|
|
|
|
'tag' => [
|
|
|
|
'filtertag'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
2 => [
|
2018-08-21 14:46:18 +00:00
|
|
|
'af_pattern' => 'action = "move" & moved_to_title contains "test" & moved_to_title === moved_to_text',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter for move',
|
|
|
|
'af_hidden' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'8 hours',
|
|
|
|
'infinity'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
3 => [
|
2018-08-21 14:46:18 +00:00
|
|
|
'af_pattern' => 'action = "delete" & "test" in lcase(page_prefixedtitle) & page_prefixedtitle === article_prefixedtext',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter for delete',
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_global' => 1,
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
|
|
|
'degroup' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
4 => [
|
2018-08-21 14:46:18 +00:00
|
|
|
'af_pattern' => 'action contains "createaccount" & accountname rlike "user" & page_title === article_text',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter for createaccount',
|
|
|
|
'af_hidden' => 1,
|
|
|
|
'actions' => []
|
|
|
|
],
|
|
|
|
5 => [
|
|
|
|
'af_pattern' => 'user_name == "FilteredUser"',
|
|
|
|
'af_public_comments' => 'Mock filter',
|
|
|
|
'actions' => [
|
|
|
|
'tag' => [
|
|
|
|
'firstTag',
|
|
|
|
'secondTag'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
6 => [
|
|
|
|
'af_pattern' => 'edit_delta === 7',
|
|
|
|
'af_public_comments' => 'Mock filter with edit_delta',
|
|
|
|
'af_hidden' => 1,
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_global' => 1,
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
2018-10-12 08:51:45 +00:00
|
|
|
'disallow' => [
|
|
|
|
'abusefilter-disallowed-really'
|
|
|
|
]
|
2018-07-08 16:06:19 +00:00
|
|
|
]
|
|
|
|
],
|
|
|
|
7 => [
|
|
|
|
'af_pattern' => 'timestamp === int(timestamp)',
|
|
|
|
'af_public_comments' => 'Mock filter with timestamp',
|
|
|
|
'actions' => [
|
|
|
|
'degroup' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
8 => [
|
|
|
|
'af_pattern' => 'added_lines_pst irlike "\\[\\[Link\\|Link\\]\\]"',
|
|
|
|
'af_public_comments' => 'Mock filter with pst',
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'NoTalkBlockSet',
|
|
|
|
'4 hours',
|
|
|
|
'4 hours'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
9 => [
|
|
|
|
'af_pattern' => 'new_size > old_size',
|
|
|
|
'af_public_comments' => 'Mock filter with size',
|
|
|
|
'af_hidden' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'3 hours',
|
|
|
|
'3 hours'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
10 => [
|
|
|
|
'af_pattern' => '1 == 1',
|
|
|
|
'af_public_comments' => 'Mock throttled filter',
|
|
|
|
'af_hidden' => 1,
|
|
|
|
'af_throttled' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'tag' => [
|
|
|
|
'testTag'
|
|
|
|
],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'infinity',
|
|
|
|
'infinity'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
11 => [
|
|
|
|
'af_pattern' => '1 == 1',
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_public_comments' => 'Catch-all filter which throttles',
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
|
|
|
'throttle' => [
|
|
|
|
11,
|
|
|
|
'1,3600',
|
2018-12-27 19:27:25 +00:00
|
|
|
'site'
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
|
|
|
'disallow' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
12 => [
|
2018-08-21 14:46:18 +00:00
|
|
|
'af_pattern' => 'page_title == user_name & user_name === page_title',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter for userpage',
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'8 hours',
|
|
|
|
'1 day'
|
|
|
|
],
|
|
|
|
'degroup' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
13 => [
|
|
|
|
'af_pattern' => '2 == 2',
|
|
|
|
'af_public_comments' => 'Another throttled mock filter',
|
|
|
|
'af_throttled' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'8 hours',
|
|
|
|
'1 day'
|
|
|
|
],
|
|
|
|
'degroup' => []
|
|
|
|
]
|
2018-08-20 14:24:23 +00:00
|
|
|
],
|
|
|
|
14 => [
|
|
|
|
'af_pattern' => '5/int(article_text) == 3',
|
|
|
|
'af_public_comments' => 'Filter with a possible division by zero',
|
2018-12-27 19:27:25 +00:00
|
|
|
'actions' => [
|
|
|
|
'disallow' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
15 => [
|
|
|
|
'af_pattern' => 'action contains "createaccount"',
|
|
|
|
'af_public_comments' => 'Catch-all for account creations',
|
|
|
|
'af_hidden' => 1,
|
2018-08-20 14:24:23 +00:00
|
|
|
'actions' => [
|
|
|
|
'disallow' => []
|
|
|
|
]
|
2018-08-23 18:53:55 +00:00
|
|
|
],
|
|
|
|
16 => [
|
|
|
|
'af_pattern' => 'random := "adruhaoerihouhae"; added_lines contains random | ' .
|
|
|
|
'edit_diff_pst contains random | new_pst contains random | new_html contains random |' .
|
|
|
|
'1=1 | /*Superfluous condition to set a lazyLoader but not compute*/all_links contains random',
|
|
|
|
'af_public_comments' => 'Filter computing several non-lazy variables',
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => []
|
|
|
|
]
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
|
|
|
17 => [
|
|
|
|
'af_pattern' => 'timestamp = "' . self::MAGIC_TIMESTAMP . '" | 3 = 2 | 1 = 4 | 5 = 7 | 6 = 3',
|
|
|
|
'af_comments' => 'This will normally consume 5 conditions, unless the timestamp is set to' .
|
|
|
|
'the magic value of self::MAGIC_TIMESTAMP.',
|
|
|
|
'af_public_comments' => 'Test with variable conditions',
|
|
|
|
'actions' => [
|
|
|
|
'tag' => [
|
|
|
|
'testTagProfiling'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
18 => [
|
2018-08-25 12:44:01 +00:00
|
|
|
'af_pattern' => '1 == 1',
|
|
|
|
'af_public_comments' => 'Global filter',
|
|
|
|
'af_global' => 1,
|
2018-08-24 14:22:58 +00:00
|
|
|
'actions' => [
|
2018-08-25 12:44:01 +00:00
|
|
|
'warn' => [
|
|
|
|
'abusefilter-warning'
|
|
|
|
],
|
|
|
|
'disallow' => []
|
2018-08-24 14:22:58 +00:00
|
|
|
]
|
|
|
|
],
|
2018-08-25 12:44:01 +00:00
|
|
|
19 => [
|
|
|
|
'af_pattern' => 'user_name === "FilteredUser"',
|
|
|
|
'af_public_comments' => 'Another global filter',
|
|
|
|
'af_global' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'tag' => [
|
|
|
|
'globalTag'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
20 => [
|
|
|
|
'af_pattern' => 'page_title === "Cellar door"',
|
|
|
|
'af_public_comments' => 'Yet another global filter',
|
|
|
|
'af_global' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
]
|
2018-07-16 12:10:36 +00:00
|
|
|
],
|
|
|
|
21 => [
|
|
|
|
'af_pattern' => '1==1',
|
|
|
|
'af_public_comments' => 'Dangerous filter',
|
|
|
|
'actions' => [
|
|
|
|
'blockautopromote' => []
|
|
|
|
]
|
2018-08-25 12:44:01 +00:00
|
|
|
]
|
2018-07-08 16:06:19 +00:00
|
|
|
];
|
2019-02-23 09:01:57 +00:00
|
|
|
// phpcs:enable Generic.Files.LineLength
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-08-25 12:44:01 +00:00
|
|
|
/**
|
|
|
|
* Add tables for global filters to the list of used tables
|
|
|
|
*
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function __construct( $name = null, array $data = [], $dataName = '' ) {
|
|
|
|
$prefixedTables = array_map(
|
|
|
|
function ( $table ) {
|
|
|
|
return self::DB_EXTERNAL_PREFIX . $table;
|
|
|
|
},
|
|
|
|
self::$externalTables
|
|
|
|
);
|
|
|
|
$this->tablesUsed = array_merge( $this->tablesUsed, $prefixedTables );
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user = User::newFromName( 'FilteredUser' );
|
2018-08-25 12:44:01 +00:00
|
|
|
parent::__construct( $name, $data, $dataName );
|
|
|
|
}
|
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
/**
|
2019-08-26 13:01:09 +00:00
|
|
|
* @inheritDoc
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2019-10-09 17:58:41 +00:00
|
|
|
protected function setUp() : void {
|
2018-07-08 16:06:19 +00:00
|
|
|
parent::setUp();
|
2019-08-26 13:01:09 +00:00
|
|
|
// Ensure that our user is not blocked and is a sysop (matched filters could block or
|
|
|
|
// degroup the user)
|
2019-08-28 15:54:07 +00:00
|
|
|
$this->user->addToDatabase();
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user->addGroup( 'sysop' );
|
|
|
|
$block = DatabaseBlock::newFromTarget( $this->user );
|
2019-05-02 19:42:15 +00:00
|
|
|
if ( $block ) {
|
2018-07-08 16:06:19 +00:00
|
|
|
$block->delete();
|
|
|
|
}
|
2018-08-25 12:44:01 +00:00
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
// Make sure that the config we're using is the one we're expecting
|
|
|
|
$this->setMwGlobals( [
|
2019-08-26 13:01:09 +00:00
|
|
|
'wgUser' => $this->user,
|
2018-12-27 19:27:25 +00:00
|
|
|
// Exclude noisy creation log
|
|
|
|
'wgPageCreationLog' => false,
|
2018-07-08 16:06:19 +00:00
|
|
|
'wgAbuseFilterActions' => [
|
|
|
|
'throttle' => true,
|
|
|
|
'warn' => true,
|
|
|
|
'disallow' => true,
|
|
|
|
'blockautopromote' => true,
|
|
|
|
'block' => true,
|
|
|
|
'rangeblock' => true,
|
|
|
|
'degroup' => true,
|
|
|
|
'tag' => true
|
2018-08-25 12:44:01 +00:00
|
|
|
],
|
|
|
|
'wgAbuseFilterCentralDB' => $this->db->getDBname() . '-' . $this->dbPrefix() .
|
|
|
|
self::DB_EXTERNAL_PREFIX,
|
2019-08-26 11:26:44 +00:00
|
|
|
'wgAbuseFilterIsCentral' => false,
|
|
|
|
'wgMainCacheType' => 'hash',
|
2018-07-08 16:06:19 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2019-05-24 14:54:29 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2019-10-09 17:58:41 +00:00
|
|
|
protected function tearDown() : void {
|
2019-05-24 14:54:29 +00:00
|
|
|
// Paranoia: ensure no fake timestamp leftover
|
|
|
|
MWTimestamp::setFakeTime( false );
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
/**
|
|
|
|
* Creates new filters with the given ids, referred to self::$filters
|
|
|
|
*
|
|
|
|
* @param int[] $ids IDs of the filters to create
|
2018-08-25 12:44:01 +00:00
|
|
|
* @param bool $external Whether to create filters in the external table
|
2018-12-27 19:27:25 +00:00
|
|
|
*/
|
2019-08-26 13:01:09 +00:00
|
|
|
private function createFilters( $ids, $external = false ) {
|
2018-12-27 19:27:25 +00:00
|
|
|
global $wgAbuseFilterActions;
|
2018-08-25 12:44:01 +00:00
|
|
|
$tablePrefix = $external ? self::DB_EXTERNAL_PREFIX : '';
|
2018-12-27 19:27:25 +00:00
|
|
|
$defaultRowSection = [
|
|
|
|
'af_user_text' => 'FilterTester',
|
|
|
|
'af_user' => 0,
|
2019-08-26 13:01:09 +00:00
|
|
|
'af_timestamp' => $this->db->timestamp(),
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_group' => 'default',
|
|
|
|
'af_comments' => '',
|
|
|
|
'af_hit_count' => 0,
|
|
|
|
'af_enabled' => 1,
|
|
|
|
'af_hidden' => 0,
|
|
|
|
'af_throttled' => 0,
|
|
|
|
'af_deleted' => 0,
|
|
|
|
'af_global' => 0
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ( $ids as $id ) {
|
|
|
|
$filter = self::$filters[$id] + $defaultRowSection;
|
|
|
|
$actions = $filter['actions'];
|
|
|
|
unset( $filter['actions'] );
|
|
|
|
$filter[ 'af_actions' ] = implode( ',', array_keys( $actions ) );
|
|
|
|
$filter[ 'af_id' ] = $id;
|
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->db->insert(
|
2018-08-25 12:44:01 +00:00
|
|
|
"{$tablePrefix}abuse_filter",
|
2018-12-27 19:27:25 +00:00
|
|
|
$filter,
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
$actionsRows = [];
|
|
|
|
foreach ( array_filter( $wgAbuseFilterActions ) as $action => $_ ) {
|
|
|
|
if ( isset( $actions[$action] ) ) {
|
|
|
|
$parameters = $actions[$action];
|
|
|
|
|
|
|
|
$thisRow = [
|
|
|
|
'afa_filter' => $id,
|
|
|
|
'afa_consequence' => $action,
|
|
|
|
'afa_parameters' => implode( "\n", $parameters )
|
|
|
|
];
|
|
|
|
$actionsRows[] = $thisRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->db->insert(
|
2018-08-25 12:44:01 +00:00
|
|
|
"{$tablePrefix}abuse_filter_action",
|
2018-12-27 19:27:25 +00:00
|
|
|
$actionsRows,
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:53:55 +00:00
|
|
|
/**
|
2019-06-23 17:19:28 +00:00
|
|
|
* Stash the edit.
|
2018-08-23 18:53:55 +00:00
|
|
|
*
|
|
|
|
* @param Title $title Title of the page to edit
|
|
|
|
* @param string $text The new content of the page
|
|
|
|
* @param string $summary The summary of the edit
|
|
|
|
* @return string The status of the operation, as returned by the API.
|
|
|
|
*/
|
|
|
|
private function stashEdit( $title, $text, $summary ) {
|
2019-06-23 17:19:28 +00:00
|
|
|
$editStash = new PageEditStash(
|
|
|
|
new HashBagOStuff( [] ),
|
|
|
|
MediaWikiServices::getInstance()->getDBLoadBalancer(),
|
|
|
|
new Psr\Log\NullLogger(),
|
|
|
|
new NullStatsdDataFactory(),
|
|
|
|
PageEditStash::INITIATOR_USER
|
|
|
|
);
|
|
|
|
return $editStash->parseAndCache(
|
|
|
|
WikiPage::factory( $title ),
|
|
|
|
new WikitextContent( $text ),
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user,
|
2019-06-23 17:19:28 +00:00
|
|
|
$summary
|
|
|
|
);
|
2018-08-23 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
/**
|
|
|
|
* @param Title $title Title of the page to edit
|
|
|
|
* @param string $oldText Old content of the page
|
|
|
|
* @param string $newText The new content of the page
|
|
|
|
* @param string $summary The summary of the edit
|
2018-08-23 18:53:55 +00:00
|
|
|
* @param bool|null $fromStash Whether to stash the edit. Null means no stashing, false means
|
|
|
|
* stash the edit but don't reuse it for saving, true means stash and reuse.
|
2018-07-08 16:06:19 +00:00
|
|
|
* @return Status
|
|
|
|
*/
|
2019-08-26 13:01:09 +00:00
|
|
|
private function doEdit( Title $title, $oldText, $newText, $summary, $fromStash = null ) {
|
2018-07-08 16:06:19 +00:00
|
|
|
$page = WikiPage::factory( $title );
|
2018-12-27 19:27:25 +00:00
|
|
|
if ( !$page->exists() ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$status = $this->editPage(
|
|
|
|
$title->getText(),
|
|
|
|
$oldText,
|
|
|
|
__METHOD__ . ' page creation',
|
|
|
|
$title->getNamespace()
|
|
|
|
);
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
throw new Exception( "Could not create test page. $status" );
|
|
|
|
}
|
|
|
|
$title->resetArticleID( -1 );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-08-23 18:53:55 +00:00
|
|
|
if ( $fromStash !== null ) {
|
|
|
|
// If we want to save from stash, submit the same text
|
|
|
|
$stashText = $newText;
|
|
|
|
if ( $fromStash === false ) {
|
|
|
|
// Otherwise, stash some random text which won't match the actual edit
|
|
|
|
$stashText = md5( uniqid( rand(), true ) );
|
|
|
|
}
|
|
|
|
$stashResult = $this->stashEdit( $title, $stashText, $summary );
|
2019-06-23 17:19:28 +00:00
|
|
|
if ( $stashResult !== PageEditStash::ERROR_NONE ) {
|
|
|
|
throw new MWException( "The edit cannot be stashed, got the following error: $stashResult" );
|
2018-08-23 18:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$content = ContentHandler::makeContent( $newText, $title );
|
|
|
|
$status = Status::newGood();
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
$context->setTitle( $title );
|
|
|
|
$context->setUser( $this->user );
|
|
|
|
|
|
|
|
AbuseFilterHooks::onEditFilterMergedContent( $context, $content, $status, $summary,
|
|
|
|
$this->user, false );
|
|
|
|
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
// Edit the page in case the test will expect for it to exist
|
|
|
|
$this->editPage(
|
|
|
|
$title->getText(),
|
|
|
|
$newText,
|
|
|
|
$summary,
|
|
|
|
$title->getNamespace(),
|
|
|
|
$this->user
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes an action to filter
|
|
|
|
*
|
|
|
|
* @param array $params Parameters of the action
|
2018-12-27 19:27:25 +00:00
|
|
|
* @return Status
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2018-12-27 19:27:25 +00:00
|
|
|
private function doAction( $params ) {
|
|
|
|
$target = Title::newFromText( $params['target'] );
|
2018-07-08 16:06:19 +00:00
|
|
|
// Make sure that previous blocks don't affect the test
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user->clearInstanceCache();
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
switch ( $params['action'] ) {
|
2018-07-08 16:06:19 +00:00
|
|
|
case 'edit':
|
2018-12-27 19:27:25 +00:00
|
|
|
$status = $this->doEdit( $target, $params['oldText'], $params['newText'], $params['summary'] );
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
2018-08-23 18:53:55 +00:00
|
|
|
case 'stashedit':
|
|
|
|
$stashStatus = $params['stashType'] === 'hit';
|
|
|
|
$status = $this->doEdit(
|
|
|
|
$target,
|
|
|
|
$params['oldText'],
|
|
|
|
$params['newText'],
|
|
|
|
$params['summary'],
|
|
|
|
$stashStatus
|
|
|
|
);
|
|
|
|
break;
|
2018-07-08 16:06:19 +00:00
|
|
|
case 'move':
|
2019-08-26 13:01:09 +00:00
|
|
|
// Ensure that the page exists
|
|
|
|
$this->getExistingTestPage( $target );
|
2019-04-17 13:16:58 +00:00
|
|
|
$newTitle = isset( $params['newTitle'] )
|
|
|
|
? Title::newFromText( $params['newTitle'] )
|
|
|
|
: $this->getNonExistingTestPage()->getTitle();
|
2019-08-26 13:01:09 +00:00
|
|
|
/** @var MockObject|MovePage $mp */
|
|
|
|
$mp = $this->getMockBuilder( MovePage::class )
|
|
|
|
->setMethods( [ 'isValidMove' ] )
|
|
|
|
->setConstructorArgs( [ $target, $newTitle ] )
|
|
|
|
->getMock();
|
|
|
|
$mp->expects( $this->any() )
|
|
|
|
->method( 'isValidMove' )
|
|
|
|
->willReturn( Status::newGood() );
|
|
|
|
$status = $mp->move( $this->user, 'AbuseFilter move test', false );
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'delete':
|
2019-08-26 13:01:09 +00:00
|
|
|
$page = $this->getExistingTestPage( $target );
|
2020-03-19 00:44:26 +00:00
|
|
|
$status = $page->doDeleteArticleReal(
|
|
|
|
'Testing deletion in AbuseFilter',
|
|
|
|
$this->user
|
|
|
|
);
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'createaccount':
|
|
|
|
$user = User::newFromName( $params['username'] );
|
2019-04-05 16:35:25 +00:00
|
|
|
// A creatable username must exist to be passed to $logEntry->setPerformer(),
|
|
|
|
// so create the account.
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
2020-01-28 20:09:01 +00:00
|
|
|
$provider = new AbuseFilterPreAuthenticationProvider();
|
|
|
|
$status = $provider->testForAccountCreation( $user, $user, [] );
|
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$logEntry = new ManualLogEntry( 'newusers', 'create' );
|
2018-12-27 19:27:25 +00:00
|
|
|
$logEntry->setPerformer( $user );
|
|
|
|
$logEntry->setTarget( $user->getUserPage() );
|
|
|
|
$logid = $logEntry->insert();
|
|
|
|
$logEntry->publish( $logid );
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
2018-12-27 19:27:25 +00:00
|
|
|
default:
|
2018-08-23 18:53:55 +00:00
|
|
|
throw new UnexpectedValueException( 'Unrecognized action ' . $params['action'] );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear cache since we'll need to retrieve some fresh data about the user
|
|
|
|
// like blocks and groups later when checking expected values
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user->clearInstanceCache();
|
2018-07-08 16:06:19 +00:00
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 19:27:25 +00:00
|
|
|
* @param array[] $actionsParams Arrays of parameters for every action
|
|
|
|
* @return Status[]
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2018-12-27 19:27:25 +00:00
|
|
|
private function doActions( $actionsParams ) {
|
|
|
|
$ret = [];
|
|
|
|
foreach ( $actionsParams as $params ) {
|
|
|
|
$ret[] = $this->doAction( $params );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
2018-12-27 19:27:25 +00:00
|
|
|
return $ret;
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 19:27:25 +00:00
|
|
|
* Helper function to retrieve change tags applied to an edit or log entry
|
2018-07-08 16:06:19 +00:00
|
|
|
*
|
2018-12-27 19:27:25 +00:00
|
|
|
* @param array $actionParams As given by the data provider
|
|
|
|
* @return string[] The applied tags
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2018-12-27 19:27:25 +00:00
|
|
|
private function getActionTags( $actionParams ) {
|
2018-08-23 18:53:55 +00:00
|
|
|
if ( $actionParams['action'] === 'edit' || $actionParams['action'] === 'stashedit' ) {
|
2018-12-27 19:27:25 +00:00
|
|
|
$page = WikiPage::factory( Title::newFromText( $actionParams['target'] ) );
|
2020-02-22 13:50:43 +00:00
|
|
|
return ChangeTags::getTags( $this->db, null, $page->getLatest() );
|
2018-08-20 14:24:23 +00:00
|
|
|
}
|
2018-12-27 19:27:25 +00:00
|
|
|
|
2020-02-22 13:50:43 +00:00
|
|
|
$logType = $actionParams['action'] === 'createaccount' ? 'newusers' : $actionParams['action'];
|
|
|
|
$logAction = $logType === 'newusers' ? 'create' : $logType;
|
|
|
|
$title = Title::newFromText( $actionParams['target'] );
|
|
|
|
$id = $this->db->selectField(
|
|
|
|
'logging',
|
|
|
|
'log_id',
|
|
|
|
[
|
|
|
|
'log_title' => $title->getDBkey(),
|
|
|
|
'log_type' => $logType,
|
|
|
|
'log_action' => $logAction
|
|
|
|
],
|
|
|
|
__METHOD__,
|
|
|
|
[],
|
|
|
|
[ 'ORDER BY' => 'log_id DESC' ]
|
2018-12-27 19:27:25 +00:00
|
|
|
);
|
2020-02-22 13:50:43 +00:00
|
|
|
if ( !$id ) {
|
|
|
|
$this->fail( 'Could not find the action in the logging table.' );
|
2018-08-20 14:24:23 +00:00
|
|
|
}
|
2020-02-22 13:50:43 +00:00
|
|
|
return ChangeTags::getTags( $this->db, null, null, $id );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
/**
|
2019-02-23 09:01:57 +00:00
|
|
|
* Checks that consequences are effectively taken and builds an array of expected and actual
|
|
|
|
* consequences which can be compared.
|
2018-12-27 19:27:25 +00:00
|
|
|
*
|
|
|
|
* @param Status $result As returned by self::doAction
|
|
|
|
* @param array $actionParams As it's given by data providers
|
|
|
|
* @param array $consequences As it's given by data providers
|
|
|
|
* @return array [ expected consequences, actual consequences ]
|
|
|
|
*/
|
|
|
|
private function checkConsequences( $result, $actionParams, $consequences ) {
|
2018-07-16 12:10:36 +00:00
|
|
|
global $wgAbuseFilterRestrictions;
|
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
$expectedErrors = [];
|
|
|
|
$testErrorMessage = false;
|
|
|
|
foreach ( $consequences as $consequence => $ids ) {
|
|
|
|
foreach ( $ids as $id ) {
|
|
|
|
$params = self::$filters[$id]['actions'][$consequence];
|
|
|
|
switch ( $consequence ) {
|
|
|
|
case 'warn':
|
|
|
|
// Aborts the hook with the warning message as error.
|
2018-10-12 08:51:45 +00:00
|
|
|
$expectedErrors['warn'][] = $params[0] ?? 'abusefilter-warning';
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'disallow':
|
2018-10-12 08:51:45 +00:00
|
|
|
// Aborts the hook with the disallow message error.
|
|
|
|
$expectedErrors['disallow'][] = $params[0] ?? 'abusefilter-disallowed';
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'block':
|
|
|
|
// Aborts the hook with 'abusefilter-blocked-display' error. Should block
|
|
|
|
// the user with expected duration and options.
|
2019-08-26 13:01:09 +00:00
|
|
|
$userBlock = $this->user->getBlock( false );
|
2018-07-08 16:06:19 +00:00
|
|
|
|
|
|
|
if ( !$userBlock ) {
|
|
|
|
$testErrorMessage = "User isn't blocked.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
$shouldPreventTalkEdit = $params[0] === 'blocktalk';
|
2019-08-26 13:01:09 +00:00
|
|
|
$edittalkCheck = $userBlock->appliesToUsertalk( $this->user->getTalkPage() ) ===
|
2019-02-23 09:01:57 +00:00
|
|
|
$shouldPreventTalkEdit;
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( !$edittalkCheck ) {
|
|
|
|
$testErrorMessage = 'The expected block option "edittalk" options does not ' .
|
|
|
|
'match the actual one.';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
$expectedExpiry = SpecialBlock::parseExpiryInput( $params[2] );
|
2018-07-08 16:06:19 +00:00
|
|
|
// Get rid of non-numeric 'infinity' by setting it to 0
|
|
|
|
$actualExpiry = wfIsInfinity( $userBlock->getExpiry() ) ? 0 : $userBlock->getExpiry();
|
|
|
|
$expectedExpiry = wfIsInfinity( $expectedExpiry ) ? 0 : $expectedExpiry;
|
2018-08-21 14:02:42 +00:00
|
|
|
// We need to take into account code execution time. 10 seconds should be enough
|
|
|
|
$durationCheck = abs( strtotime( $actualExpiry ) - strtotime( $expectedExpiry ) ) < 10;
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( !$durationCheck ) {
|
2018-08-16 17:08:13 +00:00
|
|
|
$testErrorMessage = "The expected block expiry ($expectedExpiry) does not " .
|
|
|
|
"match the actual one ($actualExpiry).";
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
$expectedErrors['block'][] = 'abusefilter-blocked-display';
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'degroup':
|
|
|
|
// Aborts the hook with 'abusefilter-degrouped' error and degroups the user.
|
|
|
|
$expectedErrors['degroup'][] = 'abusefilter-degrouped';
|
2019-08-26 13:01:09 +00:00
|
|
|
$groupCheck = !in_array( 'sysop', $this->user->getEffectiveGroups() );
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( !$groupCheck ) {
|
|
|
|
$testErrorMessage = 'The user was not degrouped.';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'tag':
|
2019-08-26 13:01:09 +00:00
|
|
|
// Only adds tags, to be retrieved in change_tag table.
|
2018-12-27 19:27:25 +00:00
|
|
|
$appliedTags = $this->getActionTags( $actionParams );
|
2018-11-27 17:56:02 +00:00
|
|
|
$tagCheck = count( array_diff( $params, $appliedTags ) ) === 0;
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( !$tagCheck ) {
|
2018-12-27 19:27:25 +00:00
|
|
|
$expectedTags = implode( ', ', $params );
|
|
|
|
$actualTags = implode( ', ', $appliedTags );
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
$testErrorMessage = "Expected the action to have the following tags: $expectedTags. " .
|
2018-07-08 16:06:19 +00:00
|
|
|
"Got the following instead: $actualTags.";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'throttle':
|
2018-12-27 19:27:25 +00:00
|
|
|
throw new UnexpectedValueException( 'Use self::testThrottleConsequence to test throttling' );
|
2018-07-16 12:10:36 +00:00
|
|
|
case 'blockautopromote':
|
|
|
|
// Aborts the hook with 'abusefilter-autopromote-blocked' error and prevent promotion.
|
|
|
|
$expectedErrors['blockautopromote'][] = 'abusefilter-autopromote-blocked';
|
2019-08-26 13:01:09 +00:00
|
|
|
$value = AbuseFilter::getAutoPromoteBlockStatus( $this->user );
|
2019-07-06 23:35:03 +00:00
|
|
|
if ( !$value ) {
|
2018-07-16 12:10:36 +00:00
|
|
|
$testErrorMessage = "The key for blocking autopromotion wasn't set.";
|
|
|
|
}
|
|
|
|
break;
|
2018-12-27 19:27:25 +00:00
|
|
|
default:
|
2019-08-26 13:01:09 +00:00
|
|
|
throw new UnexpectedValueException( "Consequence not recognized: $consequence." );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $testErrorMessage ) {
|
2018-12-08 15:29:46 +00:00
|
|
|
$this->fail( $testErrorMessage );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 12:10:36 +00:00
|
|
|
if ( array_intersect_key( $expectedErrors, array_filter( $wgAbuseFilterRestrictions ) ) ) {
|
|
|
|
$filteredExpected = array_intersect_key(
|
|
|
|
$expectedErrors,
|
|
|
|
array_filter( $wgAbuseFilterRestrictions )
|
|
|
|
);
|
|
|
|
$expected = [];
|
|
|
|
foreach ( $filteredExpected as $values ) {
|
|
|
|
$expected = array_merge( $expected, $values );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$expected = $expectedErrors['warn'] ?? $expectedErrors['disallow'] ?? null;
|
|
|
|
if ( !is_array( $expected ) ) {
|
|
|
|
$expected = (array)$expected;
|
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = $result->getErrors();
|
|
|
|
|
|
|
|
$actual = [];
|
|
|
|
foreach ( $errors as $error ) {
|
Actually return errors for action=edit API
Setting 'apiHookResult' results in a "successful" response; if we want
to report an error, we need to use ApiMessage. We already were doing
this for action=upload. Now our action=edit API responses will be
consistent with MediaWiki and other extensions, and will be able to
take advantage of errorformat=html.
Since this breaks compatibility anyway, also remove some redundant
backwards-compatibility values from the output.
To avoid user interface regressions in VisualEditor, the changes
I3b9c4fef (in VE) and I106dbd3c (in MediaWiki) should be merged first.
Before:
{
"edit": {
"code": "abusefilter-disallowed",
"message": {
"key": "abusefilter-disallowed",
"params": [ ... ]
},
"abusefilter": { ... },
"info": "Hit AbuseFilter: Test filter disallow",
"warning": "This action has been automatically identified ...",
"result": "Failure"
}
}
After:
{
"errors": [
{
"code": "abusefilter-disallowed",
"data": {
"abusefilter": { ... },
},
"module": "edit",
"*": "This action has been automatically identified ..."
}
],
"*": "See http://localhost:3080/w/api.php for API usage. ..."
}
For comparison, a 'readonly' error:
{
"errors": [
{
"code": "readonly",
"data": {
"readonlyreason": "foo bar"
},
"module": "main",
"*": "The wiki is currently in read-only mode."
}
],
"*": "See http://localhost:3080/w/api.php for API usage. ..."
}
Bug: T229539
Depends-On: I106dbd3cbdbf7082b1d1f1c1106ece6b19c22a86
Depends-On: I3b9c4fefc0869ef7999c21cef754434febd852ec
Change-Id: I5424de387cbbcc9c85026b8cfeaf01635eee34a0
2019-08-01 01:48:08 +00:00
|
|
|
// We don't use any of the "API" stuff in ApiMessage here, but this is the most
|
|
|
|
// convenient way to get a Message from a StatusValue error structure.
|
|
|
|
$msg = ApiMessage::create( $error )->getKey();
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( strpos( $msg, 'abusefilter' ) !== false ) {
|
|
|
|
$actual[] = $msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 12:10:36 +00:00
|
|
|
sort( $expected );
|
|
|
|
sort( $actual );
|
2018-12-27 19:27:25 +00:00
|
|
|
return [ $expected, $actual ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new filters, execute an action and check the consequences
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideFilters
|
|
|
|
*/
|
|
|
|
public function testFilterConsequences( $createIds, $actionParams, $consequences ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-12-27 19:27:25 +00:00
|
|
|
$result = $this->doAction( $actionParams );
|
|
|
|
list( $expected, $actual ) = $this->checkConsequences( $result, $actionParams, $consequences );
|
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-07-08 16:06:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 19:27:25 +00:00
|
|
|
* Data provider for testFilterConsequences. For every test case, we pass
|
2018-08-20 14:24:23 +00:00
|
|
|
* - an array with the IDs of the filters to be created (listed in self::$filters),
|
|
|
|
* - an array with details of the action to execute in order to trigger the filters,
|
|
|
|
* - an array of expected consequences of the form
|
|
|
|
* [ 'consequence name' => [ IDs of the filter to take its parameters from ] ]
|
|
|
|
* Such IDs may be more than one if we have a warning that is shown twice.
|
2018-07-08 16:06:19 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideFilters() {
|
|
|
|
return [
|
2018-12-08 15:29:46 +00:00
|
|
|
'Basic test for "edit" action' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Test page',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'warn' => [ 1 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-08 15:29:46 +00:00
|
|
|
'Basic test for "move" action' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 2 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Test page',
|
2018-07-08 16:06:19 +00:00
|
|
|
'newTitle' => 'Another test page'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'disallow' => [ 2 ], 'block' => [ 2 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-08 15:29:46 +00:00
|
|
|
'Basic test for "delete" action' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 2, 3 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'degroup' => [ 3 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Basic test for "createaccount", no consequences.' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 1, 2, 3, 4 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
2018-07-08 16:06:19 +00:00
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Basic test for "createaccount", disallowed.' => [
|
|
|
|
[ 15 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 15 ] ]
|
|
|
|
],
|
|
|
|
'Check that all tags are applied' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 5 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'User:FilteredUser',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Hey.',
|
|
|
|
'newText' => 'I am a very nice user, really!',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'tag' => [ 5 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 6 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Help:Help',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Some help.',
|
|
|
|
'newText' => 'Some help for you',
|
|
|
|
'summary' => 'Help! I need somebody'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'disallow' => [ 6 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that degroup and block are executed together' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Link',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'degroup' => [ 7 ], 'block' => [ 8 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that the block duration is the longer one' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 8, 9 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Whatever',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Whatever is whatever',
|
|
|
|
'newText' => 'Whatever is whatever, whatever it is. BTW, here is a [[Link|]]',
|
|
|
|
'summary' => 'Whatever'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'disallow' => [ 8 ], 'block' => [ 8 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that throttled filters only execute "safe" actions' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 10 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Buffalo',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Buffalo',
|
|
|
|
'newText' => 'Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.',
|
|
|
|
'summary' => 'Buffalo!'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'tag' => [ 10 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that degroup and block are both executed and degroup warning is shown twice' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 1, 3, 7, 12 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'User:FilteredUser',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'A couple of lines about me...',
|
|
|
|
'summary' => 'My user page'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'block' => [ 12 ], 'degroup' => [ 7, 12 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that every throttled filter only executes "safe" actions' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 10, 13 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Tyger! Tyger! Burning bright',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'In the forests of the night',
|
|
|
|
'newText' => 'What immortal hand or eye',
|
|
|
|
'summary' => 'Could frame thy fearful symmetry?'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'tag' => [ 10 ] ]
|
2018-08-20 14:24:23 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that runtime exceptions (division by zero) are correctly handled' => [
|
2018-08-20 14:24:23 +00:00
|
|
|
[ 14 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => '0',
|
2018-08-20 14:24:23 +00:00
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'New text',
|
|
|
|
'summary' => 'Some summary'
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
2018-07-16 12:10:36 +00:00
|
|
|
'Test for blockautopromote action.' => [
|
|
|
|
[ 21 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Rainbow',
|
|
|
|
'oldText' => '',
|
|
|
|
'newText' => '...',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'blockautopromote' => [ 21 ] ],
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[
|
2018-08-20 14:24:23 +00:00
|
|
|
[ 8, 10 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Anything',
|
2018-08-20 14:24:23 +00:00
|
|
|
'oldText' => 'Bar',
|
|
|
|
'newText' => 'Foo',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[]
|
2018-08-20 14:24:23 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[
|
2018-08-20 14:24:23 +00:00
|
|
|
[ 7, 12 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Something',
|
2018-08-20 14:24:23 +00:00
|
|
|
'oldText' => 'Please allow me',
|
|
|
|
'newText' => 'to introduce myself',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'degroup' => [ 7 ] ]
|
2018-08-20 14:24:23 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[
|
2018-08-20 14:24:23 +00:00
|
|
|
[ 13 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'My page',
|
2018-08-20 14:24:23 +00:00
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'AbuseFilter will not block me',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that hitting the conditions limit stops the execution, and thus no actions are taken.
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
2019-01-24 10:33:01 +00:00
|
|
|
* @covers AbuseFilterParser::getCondCount
|
|
|
|
* @covers AbuseFilterParser::raiseCondCount
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @covers AbuseFilterRunner::checkAllFilters
|
2018-12-27 19:27:25 +00:00
|
|
|
* @dataProvider provideFiltersNoConsequences
|
|
|
|
*/
|
|
|
|
public function testCondsLimit( $createIds, $actionParams ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-12-27 19:27:25 +00:00
|
|
|
$this->setMwGlobals( [ 'wgAbuseFilterConditionLimit' => 0 ] );
|
2019-02-23 09:01:57 +00:00
|
|
|
$res = $this->doAction( $actionParams );
|
2018-12-27 19:27:25 +00:00
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
$res->isGood(),
|
|
|
|
"The action should succeed when testing the conds limit. Error: $res"
|
|
|
|
);
|
2018-12-27 19:27:25 +00:00
|
|
|
$appliedTags = $this->getActionTags( $actionParams );
|
|
|
|
$this->assertContains(
|
|
|
|
'abusefilter-condition-limit',
|
|
|
|
$appliedTags,
|
|
|
|
"The action wasn't tagged with 'abusefilter-condition-limit' upon hitting the limit"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that hitting the time limit is logged
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @covers AbuseFilterRunner::checkFilter
|
|
|
|
* @covers AbuseFilterRunner::recordSlowFilter
|
2018-12-27 19:27:25 +00:00
|
|
|
* @dataProvider provideFiltersNoConsequences
|
|
|
|
*/
|
|
|
|
public function testTimeLimit( $createIds, $actionParams ) {
|
|
|
|
$loggerMock = new TestLogger();
|
|
|
|
$loggerMock->setCollect( true );
|
|
|
|
$this->setLogger( 'AbuseFilter', $loggerMock );
|
|
|
|
$this->setMwGlobals( [ 'wgAbuseFilterSlowFilterRuntimeLimit' => -1 ] );
|
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-12-27 19:27:25 +00:00
|
|
|
// We don't care about consequences here
|
|
|
|
$this->doAction( $actionParams );
|
|
|
|
|
|
|
|
// Ensure slow filters are logged
|
|
|
|
$loggerBuffer = $loggerMock->getBuffer();
|
|
|
|
$found = false;
|
|
|
|
foreach ( $loggerBuffer as $entry ) {
|
|
|
|
$check = preg_match( '/Edit filter [^ ]+ on [^ ]+ is taking longer than expected/', $entry[1] );
|
|
|
|
if ( $check ) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertTrue( $found, 'The time limit hit was not logged.' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to self::provideFilters, but for tests where we don't care about consequences.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideFiltersNoConsequences() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Test page',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 5 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'User:FilteredUser',
|
|
|
|
'oldText' => 'Hey.',
|
|
|
|
'newText' => 'I am a very nice user, really!',
|
|
|
|
'summary' => ''
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Link',
|
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 8, 10 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Anything',
|
|
|
|
'oldText' => 'Bar',
|
|
|
|
'newText' => 'Foo',
|
|
|
|
'summary' => ''
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3 ],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 10, 13 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Tyger! Tyger! Burning bright',
|
|
|
|
'oldText' => 'In the forests of the night',
|
|
|
|
'newText' => 'What immortal hand or eye',
|
|
|
|
'summary' => 'Could frame thy fearful symmetry?'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 15 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that hitting the throttle effectively updates abuse_filter.af_throttled.
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @covers AbuseFilterRunner::checkEmergencyDisable
|
2018-12-27 19:27:25 +00:00
|
|
|
* @dataProvider provideThrottleLimitFilters
|
|
|
|
*/
|
|
|
|
public function testThrottleLimit( $createIds, $actionParams ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2020-01-28 20:09:01 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgAbuseFilterEmergencyDisableCount' => [ 'default' => -1 ],
|
|
|
|
'wgAbuseFilterEmergencyDisableThreshold' => [ 'default' => -1 ],
|
|
|
|
] );
|
2018-12-27 19:27:25 +00:00
|
|
|
// We don't care about consequences here
|
|
|
|
$this->doAction( $actionParams );
|
|
|
|
|
|
|
|
$throttled = [];
|
|
|
|
foreach ( $createIds as $filter ) {
|
|
|
|
$curThrottle = $this->db->selectField(
|
|
|
|
'abuse_filter',
|
|
|
|
'af_throttled',
|
|
|
|
[ 'af_id' => $filter ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
if ( $curThrottle ) {
|
|
|
|
$throttled[] = $filter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:09:01 +00:00
|
|
|
$this->assertEquals( $createIds, $throttled, 'Some filters weren\'t automatically throttled.' );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-23 09:01:57 +00:00
|
|
|
* Data provider for testThrottleLimit. Note that using filters with af_throttled = 1 in
|
|
|
|
* self::$filters makes the test case useless.
|
2018-12-27 19:27:25 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideThrottleLimitFilters() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ 1 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'newTitle' => 'Another test page'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 5 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'User:FilteredUser',
|
|
|
|
'oldText' => 'Hey.',
|
|
|
|
'newText' => 'I am a very nice user, really!',
|
|
|
|
'summary' => ''
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 7, 8 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Link',
|
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 3 ],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 15 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check an array of results from self::doAction to ensure that all but the last actions have been
|
|
|
|
* executed (i.e. no errors).
|
|
|
|
* @param Status[] $results As returned by self::doActions
|
|
|
|
* @return Status The Status of the last action, to be later checked with self::checkConsequences
|
|
|
|
*/
|
|
|
|
private function checkThrottleConsequence( $results ) {
|
|
|
|
$finalRes = array_pop( $results );
|
|
|
|
foreach ( $results as $result ) {
|
|
|
|
if ( !$result->isGood() ) {
|
2019-02-23 09:01:57 +00:00
|
|
|
$this->fail( 'Only the last actions should have triggered a filter; the other ones ' .
|
|
|
|
'should have been allowed.' );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $finalRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like self::testFilterConsequences but for throttle, which deserves a special treatment
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array[] $actionsParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideThrottleFilters
|
|
|
|
*/
|
|
|
|
public function testThrottle( $createIds, $actionsParams, $consequences ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-12-27 19:27:25 +00:00
|
|
|
$results = self::doActions( $actionsParams );
|
|
|
|
$res = $this->checkThrottleConsequence( $results );
|
|
|
|
$lastParams = array_pop( $actionsParams );
|
|
|
|
list( $expected, $actual ) = $this->checkConsequences( $res, $lastParams, $consequences );
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-12-27 19:27:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testThrottle. For every test case, we pass
|
|
|
|
* - an array with the IDs of the filters to be created (listed in self::$filters),
|
2019-02-23 09:01:57 +00:00
|
|
|
* - an array of array, where every sub-array holds the details of the action to execute in
|
|
|
|
* order to trigger the filters, each one like in self::provideFilters
|
2018-12-27 19:27:25 +00:00
|
|
|
* - an array of expected consequences for the last action (i.e. after throttling) of the form
|
|
|
|
* [ 'consequence name' => [ IDs of the filter to take its parameters from ] ]
|
|
|
|
* Such IDs may be more than one if we have a warning that is shown twice.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideThrottleFilters() {
|
|
|
|
return [
|
|
|
|
'Basic test for throttling edits' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Throttle',
|
|
|
|
'oldText' => 'What is throttle?',
|
|
|
|
'newText' => 'Throttle is something that should happen...',
|
|
|
|
'summary' => 'Throttle'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Throttle',
|
|
|
|
'oldText' => 'Throttle is something that should happen...',
|
|
|
|
'newText' => '... Right now!',
|
|
|
|
'summary' => 'Throttle'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for throttling "move"' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Throttle test',
|
|
|
|
'newTitle' => 'Another throttle test'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Another throttle test',
|
|
|
|
'newTitle' => 'Yet another throttle test'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for throttling "delete"' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for throttling "createaccount"' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:YetAnotherUser',
|
|
|
|
'username' => 'YetAnotherUser'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-12-28 16:01:19 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-26 13:01:09 +00:00
|
|
|
* Test storing and loading the var dump. See also AbuseFilterDBTest::testVarDump
|
2018-12-28 16:01:19 +00:00
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param string[] $usedVars The variables effectively computed by filters in $createIds.
|
|
|
|
* We'll search these in the stored dump.
|
|
|
|
* @covers AbuseFilter::storeVarDump
|
|
|
|
* @covers AbuseFilter::loadVarDump
|
|
|
|
* @covers AbuseFilterVariableHolder::dumpAllVars
|
|
|
|
* @dataProvider provideFiltersAndVariables
|
|
|
|
*/
|
|
|
|
public function testVarDump( $createIds, $actionParams, $usedVars ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-12-28 16:01:19 +00:00
|
|
|
// We don't care about consequences here
|
2018-08-23 18:53:55 +00:00
|
|
|
$this->doAction( $actionParams );
|
2018-12-28 16:01:19 +00:00
|
|
|
|
|
|
|
// We just take a dump from a single filters, as they're all identical for the same action
|
2019-08-26 13:01:09 +00:00
|
|
|
$dumpID = $this->db->selectField(
|
2018-12-28 16:01:19 +00:00
|
|
|
'abuse_filter_log',
|
|
|
|
'afl_var_dump',
|
|
|
|
'',
|
|
|
|
__METHOD__,
|
|
|
|
[ 'ORDER BY' => 'afl_timestamp DESC' ]
|
|
|
|
);
|
|
|
|
|
2018-12-27 17:06:56 +00:00
|
|
|
$vars = AbuseFilter::loadVarDump( $dumpID )->getVars();
|
2018-12-28 16:01:19 +00:00
|
|
|
|
|
|
|
$interestingVars = array_intersect_key( $vars, array_fill_keys( $usedVars, true ) );
|
|
|
|
|
|
|
|
sort( $usedVars );
|
|
|
|
ksort( $interestingVars );
|
|
|
|
$this->assertEquals(
|
|
|
|
$usedVars,
|
|
|
|
array_keys( $interestingVars ),
|
|
|
|
"The saved variables aren't the expected ones."
|
|
|
|
);
|
|
|
|
$this->assertContainsOnlyInstancesOf(
|
|
|
|
AFPData::class,
|
|
|
|
$interestingVars,
|
|
|
|
'Some variables have not been computed.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testVarDump
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideFiltersAndVariables() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
],
|
|
|
|
[ 'added_lines', 'action' ]
|
|
|
|
],
|
2018-08-23 18:53:55 +00:00
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.',
|
|
|
|
'stashType' => 'hit'
|
|
|
|
],
|
|
|
|
[ 'added_lines', 'action' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.',
|
|
|
|
'stashType' => 'miss'
|
|
|
|
],
|
|
|
|
[ 'added_lines', 'action' ]
|
|
|
|
],
|
2018-12-28 16:01:19 +00:00
|
|
|
[
|
|
|
|
[ 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'newTitle' => 'Another test page'
|
|
|
|
],
|
|
|
|
[ 'action', 'moved_to_title' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 5 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'User:FilteredUser',
|
|
|
|
'oldText' => 'Hey.',
|
|
|
|
'newText' => 'I am a very nice user, really!',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'user_name' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Link',
|
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining'
|
|
|
|
],
|
|
|
|
[ 'action', 'timestamp', 'added_lines_pst' ]
|
|
|
|
],
|
2018-08-23 18:53:55 +00:00
|
|
|
[
|
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Link',
|
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining',
|
|
|
|
'stashType' => 'hit'
|
|
|
|
],
|
|
|
|
[ 'action', 'timestamp', 'added_lines_pst' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Link',
|
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining',
|
|
|
|
'stashType' => 'miss'
|
|
|
|
],
|
|
|
|
[ 'action', 'timestamp', 'added_lines_pst' ]
|
|
|
|
],
|
2018-12-28 16:01:19 +00:00
|
|
|
[
|
|
|
|
[ 8, 10 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Anything',
|
|
|
|
'oldText' => 'Bar',
|
|
|
|
'newText' => 'Foo',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'added_lines_pst' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3 ],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
],
|
|
|
|
[ 'action', 'page_prefixedtitle' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 10, 13 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Tyger! Tyger! Burning bright',
|
|
|
|
'oldText' => 'In the forests of the night',
|
|
|
|
'newText' => 'What immortal hand or eye',
|
|
|
|
'summary' => 'Could frame thy fearful symmetry?'
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 15 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[ 'action' ]
|
|
|
|
],
|
2018-08-23 18:53:55 +00:00
|
|
|
[
|
|
|
|
[ 16 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Random',
|
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'Some new text which will not match',
|
|
|
|
'summary' => 'No summary'
|
|
|
|
],
|
|
|
|
[ 'edit_diff_pst', 'new_pst', 'new_html' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 16 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Random',
|
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'Some new text which will not match',
|
|
|
|
'summary' => 'No summary',
|
|
|
|
'stashType' => 'miss'
|
|
|
|
],
|
|
|
|
[ 'edit_diff_pst', 'new_pst', 'new_html' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 16 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Random',
|
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'Some new text which will not match',
|
|
|
|
'summary' => 'No summary',
|
|
|
|
'stashType' => 'hit'
|
|
|
|
],
|
|
|
|
[ 'edit_diff_pst', 'new_pst', 'new_html' ]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as testFilterConsequences but only for stashed edits
|
|
|
|
*
|
|
|
|
* @param string $type Either "hit" or "miss". The former saves the edit from stash, the second
|
|
|
|
* stashes the edit but doesn't reuse it.
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideStashedEdits
|
|
|
|
*/
|
|
|
|
public function testStashedEdit( $type, $createIds, $actionParams, $consequences ) {
|
|
|
|
if ( $type !== 'hit' && $type !== 'miss' ) {
|
|
|
|
throw new InvalidArgumentException( '$type must be either "hit" or "miss"' );
|
|
|
|
}
|
|
|
|
// Add some info in actionParams identical for all tests
|
|
|
|
$actionParams['action'] = 'stashedit';
|
|
|
|
$actionParams['stashType'] = $type;
|
|
|
|
|
|
|
|
$loggerMock = new TestLogger();
|
|
|
|
$loggerMock->setCollect( true );
|
|
|
|
$this->setLogger( 'StashEdit', $loggerMock );
|
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-08-23 18:53:55 +00:00
|
|
|
$result = $this->doAction( $actionParams );
|
|
|
|
|
|
|
|
// Check that we stored the edit and then hit/missed the cache
|
|
|
|
$foundStore = false;
|
|
|
|
$foundHitOrMiss = false;
|
|
|
|
// The conversion back and forth is needed because if the wiki language is not english
|
|
|
|
// the given namespace has been localized and thus wouldn't match.
|
2019-08-26 13:01:09 +00:00
|
|
|
$pageName = Title::newFromText( $actionParams['target'] )->getPrefixedText();
|
2018-08-23 18:53:55 +00:00
|
|
|
foreach ( $loggerMock->getBuffer() as $entry ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
if ( preg_match( "/AbuseFilterRunner::logCache: cache $type for '$pageName'/", $entry[1] ) ) {
|
2018-08-23 18:53:55 +00:00
|
|
|
$foundHitOrMiss = true;
|
|
|
|
}
|
2019-08-26 13:01:09 +00:00
|
|
|
if ( preg_match( "/AbuseFilterRunner::logCache: cache store for '$pageName'/", $entry[1] ) ) {
|
2018-08-23 18:53:55 +00:00
|
|
|
$foundStore = true;
|
|
|
|
}
|
|
|
|
if ( $foundStore && $foundHitOrMiss ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !$foundStore ) {
|
|
|
|
$this->fail( 'Did not store the edit in cache as expected for a stashed edit.' );
|
|
|
|
} elseif ( !$foundHitOrMiss ) {
|
|
|
|
$this->fail( "Did not $type the cache as expected for a stashed edit." );
|
|
|
|
}
|
|
|
|
|
|
|
|
list( $expected, $actual ) = $this->checkConsequences( $result, $actionParams, $consequences );
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-08-23 18:53:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testStashedEdit
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideStashedEdits() {
|
|
|
|
$sets = [
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
],
|
|
|
|
[ 'warn' => [ 1 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 5 ],
|
|
|
|
[
|
|
|
|
'target' => 'User:FilteredUser',
|
|
|
|
'oldText' => 'Hey.',
|
|
|
|
'newText' => 'I am a very nice user, really!',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 5 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 6 ],
|
|
|
|
[
|
|
|
|
'target' => 'Help:Help',
|
|
|
|
'oldText' => 'Some help.',
|
|
|
|
'newText' => 'Some help for you',
|
|
|
|
'summary' => 'Help! I need somebody'
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 6 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
|
|
|
'target' => 'Link',
|
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining'
|
|
|
|
],
|
|
|
|
[ 'degroup' => [ 7 ], 'block' => [ 8 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 8, 9 ],
|
|
|
|
[
|
|
|
|
'target' => 'Whatever',
|
|
|
|
'oldText' => 'Whatever is whatever',
|
|
|
|
'newText' => 'Whatever is whatever, whatever it is. BTW, here is a [[Link|]]',
|
|
|
|
'summary' => 'Whatever'
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 8 ], 'block' => [ 8 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 10 ],
|
|
|
|
[
|
|
|
|
'target' => 'Buffalo',
|
|
|
|
'oldText' => 'Buffalo',
|
|
|
|
'newText' => 'Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.',
|
|
|
|
'summary' => 'Buffalo!'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 10 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 1, 3, 7, 12 ],
|
|
|
|
[
|
|
|
|
'target' => 'User:FilteredUser',
|
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'A couple of lines about me...',
|
|
|
|
'summary' => 'My user page'
|
|
|
|
],
|
|
|
|
[ 'block' => [ 12 ], 'degroup' => [ 7, 12 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 10, 13 ],
|
|
|
|
[
|
|
|
|
'target' => 'Tyger! Tyger! Burning bright',
|
|
|
|
'oldText' => 'In the forests of the night',
|
|
|
|
'newText' => 'What immortal hand or eye',
|
|
|
|
'summary' => 'Could frame thy fearful symmetry?'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 10 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 14 ],
|
|
|
|
[
|
|
|
|
'target' => '0',
|
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'New text',
|
|
|
|
'summary' => 'Some summary'
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 8, 10 ],
|
|
|
|
[
|
|
|
|
'target' => 'Anything',
|
|
|
|
'oldText' => 'Bar',
|
|
|
|
'newText' => 'Foo',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 7, 12 ],
|
|
|
|
[
|
|
|
|
'target' => 'Something',
|
|
|
|
'oldText' => 'Please allow me',
|
|
|
|
'newText' => 'to introduce myself',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'degroup' => [ 7 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 13 ],
|
|
|
|
[
|
|
|
|
'target' => 'My page',
|
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'AbuseFilter will not block me',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
2018-12-28 16:01:19 +00:00
|
|
|
];
|
2018-08-23 18:53:55 +00:00
|
|
|
|
|
|
|
$finalSets = [];
|
|
|
|
foreach ( $sets as $set ) {
|
2019-06-14 00:08:41 +00:00
|
|
|
// Test both successfully saving a stashed edit and stashing the edit but re-executing filters
|
2018-08-23 18:53:55 +00:00
|
|
|
$finalSets[] = array_merge( [ 'miss' ], $set );
|
|
|
|
$finalSets[] = array_merge( [ 'hit' ], $set );
|
|
|
|
}
|
|
|
|
return $finalSets;
|
2018-12-28 16:01:19 +00:00
|
|
|
}
|
2018-08-24 14:22:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test filter profiling, both for total and per-filter stats. NOTE: This test performs several
|
|
|
|
* actions for every test set, and is thus HEAVY.
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $expectedGlobal Expected global stats
|
|
|
|
* @param array $expectedPerFilter Expected stats for every created filter
|
2015-04-01 04:51:16 +00:00
|
|
|
* @covers AbuseFilter::filterProfileKey
|
|
|
|
* @covers AbuseFilter::filterProfileGroupKey
|
2018-08-24 14:22:58 +00:00
|
|
|
* @covers AbuseFilter::getFilterProfile
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @covers AbuseFilterRunner::checkAllFilters
|
|
|
|
* @covers AbuseFilterRunner::recordStats
|
2018-08-24 14:22:58 +00:00
|
|
|
* @dataProvider provideProfilingFilters
|
|
|
|
*/
|
|
|
|
public function testProfiling( $createIds, $actionParams, $expectedGlobal, $expectedPerFilter ) {
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgAbuseFilterConditionLimit' => $actionParams[ 'condsLimit' ]
|
|
|
|
] );
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-08-24 14:22:58 +00:00
|
|
|
for ( $i = 1; $i <= $actionParams['repeatAction'] - 1; $i++ ) {
|
|
|
|
// First make some other actions to increase stats
|
|
|
|
// @ToDo This doesn't works well with account creations
|
|
|
|
$this->doAction( $actionParams );
|
|
|
|
$actionParams['target'] .= $i;
|
|
|
|
}
|
|
|
|
// This is the magic value used by filter 16 to change the amount of used condition
|
|
|
|
MWTimestamp::setFakeTime( self::MAGIC_TIMESTAMP );
|
|
|
|
// We don't care about consequences here
|
|
|
|
$this->doAction( $actionParams );
|
|
|
|
MWTimestamp::setFakeTime( false );
|
|
|
|
|
|
|
|
$stash = MediaWikiServices::getInstance()->getMainObjectStash();
|
|
|
|
// Global stats shown on the top of Special:AbuseFilter
|
2015-04-01 04:51:16 +00:00
|
|
|
$globalStats = $stash->get( AbuseFilter::filterProfileGroupKey( 'default' ) );
|
2018-08-24 14:22:58 +00:00
|
|
|
$actualGlobalStats = [
|
2015-04-01 04:51:16 +00:00
|
|
|
'totalMatches' => $globalStats['matches'],
|
|
|
|
'totalActions' => $globalStats['total'],
|
|
|
|
'totalOverflows' => $globalStats['overflow']
|
2018-08-24 14:22:58 +00:00
|
|
|
];
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedGlobal,
|
|
|
|
$actualGlobalStats,
|
|
|
|
'Global profiling stats are not computed correctly.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Per-filter stats shown on the top of Special:AbuseFilter/xxx
|
|
|
|
foreach ( $createIds as $id ) {
|
2019-02-10 10:57:37 +00:00
|
|
|
list( $totalActions, $matches, , $conds ) = AbuseFilter::getFilterProfile( $id );
|
2018-08-24 14:22:58 +00:00
|
|
|
$actualStats = [
|
2019-02-10 10:57:37 +00:00
|
|
|
'matches' => $matches,
|
|
|
|
'actions' => $totalActions,
|
|
|
|
'averageConditions' => $conds
|
2018-08-24 14:22:58 +00:00
|
|
|
];
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedPerFilter[ $id ],
|
|
|
|
$actualStats,
|
|
|
|
"Profiling stats are not computed correctly for filter $id."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testProfiling. We only want filters which let the edit pass, since
|
|
|
|
* we'll perform multiple edits. How this test works: we repeat the action X times. For 1 to
|
|
|
|
* X - 1, it would take 1 + 1 + 5 + 1 conditions, but it will overflow without checking filter
|
2018-08-25 12:44:01 +00:00
|
|
|
* 19 (since the conds limit is 7). Then we perform the last execution using a trick that will
|
2018-08-24 14:22:58 +00:00
|
|
|
* make filter 17 only consume 1 condition.
|
|
|
|
*
|
|
|
|
* @todo All these values should be more customizable, or just hardcoded in the test method.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideProfilingFilters() {
|
|
|
|
return [
|
|
|
|
'Basic test for statistics recording on edit.' => [
|
2018-08-25 12:44:01 +00:00
|
|
|
[ 4, 5, 17, 19 ],
|
2018-08-24 14:22:58 +00:00
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Some page',
|
|
|
|
'oldText' => 'Some old text',
|
|
|
|
'newText' => 'Some new text',
|
|
|
|
'summary' => 'Some summary',
|
|
|
|
'condsLimit' => 7,
|
2019-08-26 13:01:09 +00:00
|
|
|
'repeatAction' => 3
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
|
|
|
[
|
2019-08-26 13:01:09 +00:00
|
|
|
'totalMatches' => 3,
|
|
|
|
'totalActions' => 3,
|
|
|
|
'totalOverflows' => 2
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
4 => [
|
|
|
|
'matches' => 0,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
5 => [
|
2019-08-26 13:01:09 +00:00
|
|
|
'matches' => 3,
|
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
17 => [
|
|
|
|
'matches' => 1,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
|
|
|
'averageConditions' => 3.7
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
2018-08-25 12:44:01 +00:00
|
|
|
19 => [
|
2018-08-24 14:22:58 +00:00
|
|
|
'matches' => 1,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'Test for statistics recording on a successfully stashed edit.' => [
|
2018-08-25 12:44:01 +00:00
|
|
|
[ 4, 5, 17, 19 ],
|
2018-08-24 14:22:58 +00:00
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Some page',
|
|
|
|
'oldText' => 'Some old text',
|
|
|
|
'newText' => 'Some new text',
|
|
|
|
'summary' => 'Some summary',
|
|
|
|
'stashType' => 'hit',
|
|
|
|
'condsLimit' => 7,
|
2019-08-26 13:01:09 +00:00
|
|
|
'repeatAction' => 3
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
|
|
|
[
|
2019-08-26 13:01:09 +00:00
|
|
|
'totalMatches' => 3,
|
|
|
|
'totalActions' => 3,
|
|
|
|
'totalOverflows' => 2
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
4 => [
|
|
|
|
'matches' => 0,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
5 => [
|
2019-08-26 13:01:09 +00:00
|
|
|
'matches' => 3,
|
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
17 => [
|
|
|
|
'matches' => 1,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
|
|
|
'averageConditions' => 3.7
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
2018-08-25 12:44:01 +00:00
|
|
|
19 => [
|
2018-08-24 14:22:58 +00:00
|
|
|
'matches' => 1,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'Test for statistics recording on an unsuccessfully stashed edit.' => [
|
2018-08-25 12:44:01 +00:00
|
|
|
[ 4, 5, 17, 19 ],
|
2018-08-24 14:22:58 +00:00
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Some page',
|
|
|
|
'oldText' => 'Some old text',
|
|
|
|
'newText' => 'Some new text',
|
|
|
|
'summary' => 'Some summary',
|
|
|
|
'stashType' => 'miss',
|
|
|
|
'condsLimit' => 7,
|
2019-08-26 13:01:09 +00:00
|
|
|
'repeatAction' => 3
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
|
|
|
[
|
2019-08-26 13:01:09 +00:00
|
|
|
'totalMatches' => 3,
|
|
|
|
'totalActions' => 3,
|
|
|
|
'totalOverflows' => 2
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
4 => [
|
|
|
|
'matches' => 0,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
5 => [
|
2019-08-26 13:01:09 +00:00
|
|
|
'matches' => 3,
|
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
17 => [
|
|
|
|
'matches' => 1,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
|
|
|
'averageConditions' => 3.7
|
2018-08-24 14:22:58 +00:00
|
|
|
],
|
2018-08-25 12:44:01 +00:00
|
|
|
19 => [
|
2018-08-24 14:22:58 +00:00
|
|
|
'matches' => 1,
|
2019-08-26 13:01:09 +00:00
|
|
|
'actions' => 3,
|
2018-08-24 14:22:58 +00:00
|
|
|
'averageConditions' => 1.0
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2018-08-25 12:44:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for global filters, defined on a central wiki and executed on another (e.g. a filter
|
|
|
|
* defined on meta but triggered on another wiki using meta's global filters).
|
|
|
|
* We emulate an external database by using different tables prefixed with
|
|
|
|
* self::DB_EXTERNAL_PREFIX
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideGlobalFilters
|
|
|
|
*/
|
|
|
|
public function testGlobalFilters( $createIds, $actionParams, $consequences ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds, true );
|
2018-08-25 12:44:01 +00:00
|
|
|
|
|
|
|
$result = $this->doAction( $actionParams );
|
|
|
|
|
|
|
|
list( $expected, $actual ) = $this->checkConsequences( $result, $actionParams, $consequences );
|
|
|
|
|
|
|
|
// First check that the filter work as expected
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-08-25 12:44:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check that the hits were logged on the "external" DB
|
|
|
|
$logged = $this->db->selectFieldValues(
|
|
|
|
self::DB_EXTERNAL_PREFIX . 'abuse_filter_log',
|
|
|
|
'afl_filter',
|
|
|
|
[ 'afl_wiki IS NOT NULL' ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
// Don't use assertSame because the DB holds strings here (T42757)
|
|
|
|
$this->assertEquals(
|
|
|
|
$createIds,
|
|
|
|
$logged,
|
|
|
|
'Some filter hits were not logged in the external DB.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testGlobalFilters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideGlobalFilters() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ 18 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Global',
|
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'New text',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 18 ], 'warn' => [ 18 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 19 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'A global page',
|
|
|
|
'oldText' => 'Foo',
|
|
|
|
'newText' => 'Bar',
|
|
|
|
'summary' => 'Baz'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 19 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 19, 20 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Cellar door',
|
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'Yay, that\'s cool',
|
|
|
|
'summary' => 'Unit test'
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 20 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 18 ],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Cellar door',
|
|
|
|
'newTitle' => 'Attic door'
|
|
|
|
],
|
|
|
|
[ 'warn' => [ 18 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 19, 20 ],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Cellar door',
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 20 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 19 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Cellar door',
|
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'Too many doors',
|
|
|
|
'summary' => '',
|
|
|
|
'stashType' => 'hit'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 19 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 18 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
2019-08-26 13:01:09 +00:00
|
|
|
'target' => 'User:AbuseFilterGlobalUser',
|
|
|
|
'username' => 'AbuseFilterGlobalUser'
|
2018-08-25 12:44:01 +00:00
|
|
|
],
|
|
|
|
[ 'warn' => [ 18 ] ]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|