2017-05-01 22:33:21 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2024-06-10 18:42:06 +00:00
|
|
|
|
use MediaWiki\Context\RequestContext;
|
2023-05-06 21:20:28 +00:00
|
|
|
|
use MediaWiki\EditPage\EditPage;
|
2022-04-08 13:05:02 +00:00
|
|
|
|
use MediaWiki\Extension\SpamBlacklist\BaseBlacklist;
|
|
|
|
|
use MediaWiki\Extension\SpamBlacklist\SpamBlacklist;
|
2020-03-14 13:08:25 +00:00
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-01-05 18:28:12 +00:00
|
|
|
|
use MediaWiki\Request\FauxRequest;
|
2024-03-05 10:37:19 +00:00
|
|
|
|
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
|
2023-08-19 04:19:23 +00:00
|
|
|
|
use MediaWiki\Title\Title;
|
2020-03-14 13:08:25 +00:00
|
|
|
|
|
2017-05-01 22:33:21 +00:00
|
|
|
|
/**
|
2017-07-06 00:58:33 +00:00
|
|
|
|
* @group SpamBlacklist
|
2018-09-14 19:56:24 +00:00
|
|
|
|
* @group Database
|
2022-04-08 13:05:02 +00:00
|
|
|
|
* @covers \MediaWiki\Extension\SpamBlacklist\SpamBlacklist
|
2017-05-01 22:33:21 +00:00
|
|
|
|
*/
|
2021-10-11 23:04:44 +00:00
|
|
|
|
class SpamBlacklistTest extends MediaWikiIntegrationTestCase {
|
2024-03-05 10:37:19 +00:00
|
|
|
|
|
|
|
|
|
use TempUserTestTrait;
|
|
|
|
|
|
2017-05-01 22:33:21 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var SpamBlacklist
|
|
|
|
|
*/
|
|
|
|
|
protected $spamFilter;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-06-29 17:39:54 +00:00
|
|
|
|
* Spam blacklist regexes. Examples taken from:
|
2017-05-01 22:33:21 +00:00
|
|
|
|
*
|
2019-06-01 10:20:17 +00:00
|
|
|
|
* @see https://meta.wikimedia.org/wiki/Spam_blacklist
|
|
|
|
|
* @see https://en.wikipedia.org/wiki/MediaWiki:Spam-blacklist
|
2017-06-29 17:39:54 +00:00
|
|
|
|
*
|
|
|
|
|
* via Flow extension
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $blacklist = [ '\b01bags\.com\b', 'sytes\.net' ];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spam whitelist regexes. Examples taken from:
|
|
|
|
|
*
|
2019-06-01 10:20:17 +00:00
|
|
|
|
* @see https://en.wikipedia.org/wiki/MediaWiki:Spam-whitelist
|
2017-05-01 22:33:21 +00:00
|
|
|
|
*
|
|
|
|
|
* via Flow extension
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2017-06-29 17:39:54 +00:00
|
|
|
|
protected $whitelist = [ 'a5b\.sytes\.net' ];
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2023-05-20 12:09:37 +00:00
|
|
|
|
public static function spamProvider() {
|
2017-06-06 16:29:27 +00:00
|
|
|
|
return [
|
|
|
|
|
'no spam' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'https://example.com' ],
|
|
|
|
|
false,
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
|
|
|
|
'revision with spam, with additional non-spam' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'https://foo.com', 'http://01bags.com', 'http://bar.com' ],
|
|
|
|
|
[ '01bags.com' ],
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
|
'revision with spam using full width stop normalization' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'http://01bags.com' ],
|
|
|
|
|
[ '01bags.com' ],
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
|
'revision with domain blacklisted as spam, but subdomain whitelisted' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'http://a5b.sytes.net' ],
|
|
|
|
|
false,
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
|
|
|
|
];
|
2017-05-01 22:33:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider spamProvider
|
|
|
|
|
*/
|
2024-03-05 10:37:19 +00:00
|
|
|
|
public function testSpamTempAccounts( $links, $expected ) {
|
|
|
|
|
$this->enableAutoCreateTempUser();
|
|
|
|
|
$this->prepareGlobals();
|
|
|
|
|
$tempUserCreator = MediaWikiServices::getInstance()->getTempUserCreator();
|
2024-03-28 12:44:32 +00:00
|
|
|
|
$user = $tempUserCreator->create(
|
|
|
|
|
null,
|
|
|
|
|
new FauxRequest()
|
|
|
|
|
)->getUser();
|
2024-03-05 10:37:19 +00:00
|
|
|
|
$returnValue = $this->spamFilter->filter(
|
|
|
|
|
$links,
|
|
|
|
|
Title::newMainPage(),
|
|
|
|
|
$user
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals( $expected, $returnValue );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider spamProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testSpamAnonEditing( $links, $expected ) {
|
|
|
|
|
$this->disableAutoCreateTempUser();
|
|
|
|
|
$this->prepareGlobals();
|
|
|
|
|
|
|
|
|
|
$userFactory = $this->getServiceContainer()->getUserFactory();
|
|
|
|
|
$user = $userFactory->newAnonymous();
|
2020-01-04 11:16:36 +00:00
|
|
|
|
$returnValue = $this->spamFilter->filter(
|
|
|
|
|
$links,
|
|
|
|
|
Title::newMainPage(),
|
2024-03-05 10:37:19 +00:00
|
|
|
|
$user
|
2020-01-04 11:16:36 +00:00
|
|
|
|
);
|
2017-05-01 22:33:21 +00:00
|
|
|
|
$this->assertEquals( $expected, $returnValue );
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 12:09:37 +00:00
|
|
|
|
public static function spamEditProvider() {
|
2021-07-23 13:13:34 +00:00
|
|
|
|
return [
|
|
|
|
|
'no spam' => [
|
|
|
|
|
'https://example.com',
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
'revision with spam, with additional non-spam' => [
|
|
|
|
|
"https://foo.com\nhttp://01bags.com\nhttp://bar.com'",
|
|
|
|
|
false,
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
'revision with domain blacklisted as spam, but subdomain whitelisted' => [
|
|
|
|
|
'http://a5b.sytes.net',
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider spamEditProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testSpamEdit( $text, $ok ) {
|
2024-03-05 10:37:19 +00:00
|
|
|
|
$this->prepareGlobals();
|
2021-07-23 13:13:34 +00:00
|
|
|
|
$fields = [
|
|
|
|
|
'wpTextbox1' => $text,
|
|
|
|
|
'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
|
|
|
|
|
'wpRecreate' => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$req = new FauxRequest( $fields, true );
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$title = $page->getTitle();
|
|
|
|
|
|
|
|
|
|
$articleContext = new RequestContext;
|
|
|
|
|
$articleContext->setRequest( $req );
|
|
|
|
|
$articleContext->setWikiPage( $page );
|
|
|
|
|
$articleContext->setUser( $this->getTestUser()->getUser() );
|
|
|
|
|
|
|
|
|
|
$article = new Article( $title );
|
|
|
|
|
$ep = new EditPage( $article );
|
|
|
|
|
$ep->setContextTitle( $title );
|
2024-05-04 14:21:57 +00:00
|
|
|
|
$ep->getContext()->setUser( $articleContext->getUser() );
|
2021-07-23 13:13:34 +00:00
|
|
|
|
|
|
|
|
|
$ep->importFormData( $req );
|
|
|
|
|
|
|
|
|
|
$status = $ep->attemptSave( $result );
|
|
|
|
|
|
2023-10-26 21:32:48 +00:00
|
|
|
|
if ( $ok ) {
|
|
|
|
|
$this->assertStatusOK( $status );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertStatusError( 'spam-blacklisted-link', $status );
|
2021-07-23 13:13:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 10:37:19 +00:00
|
|
|
|
private function prepareGlobals(): void {
|
2024-08-22 20:01:29 +00:00
|
|
|
|
$this->overrideConfigValue( 'BlacklistSettings', [
|
2017-06-06 16:29:27 +00:00
|
|
|
|
'files' => [],
|
|
|
|
|
] );
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2020-09-29 16:53:36 +00:00
|
|
|
|
BaseBlacklist::clearInstanceCache();
|
|
|
|
|
|
|
|
|
|
// create spam filter
|
|
|
|
|
$this->spamFilter = new SpamBlacklist;
|
|
|
|
|
|
2020-03-14 13:08:25 +00:00
|
|
|
|
MediaWikiServices::getInstance()->getMessageCache()->enable();
|
2017-05-01 22:33:21 +00:00
|
|
|
|
$this->insertPage( 'MediaWiki:Spam-blacklist', implode( "\n", $this->blacklist ) );
|
|
|
|
|
$this->insertPage( 'MediaWiki:Spam-whitelist', implode( "\n", $this->whitelist ) );
|
|
|
|
|
|
|
|
|
|
// That only works if the spam blacklist is really reset
|
|
|
|
|
$instance = BaseBlacklist::getInstance( 'spam' );
|
2022-04-08 13:05:02 +00:00
|
|
|
|
$reflProp = new ReflectionProperty( $instance, 'regexes' );
|
2017-05-01 22:33:21 +00:00
|
|
|
|
$reflProp->setAccessible( true );
|
|
|
|
|
$reflProp->setValue( $instance, false );
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 22:37:22 +00:00
|
|
|
|
protected function tearDown(): void {
|
2020-03-14 13:08:25 +00:00
|
|
|
|
MediaWikiServices::getInstance()->getMessageCache()->disable();
|
2017-05-01 22:33:21 +00:00
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
}
|