2023-05-24 20:09:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
|
|
|
use ApiRawMessage;
|
|
|
|
use BagOStuff;
|
|
|
|
use DBAccessObjectUtils;
|
|
|
|
use IDBAccessObject;
|
|
|
|
use MediaWiki\CommentStore\CommentStoreComment;
|
2024-06-12 18:01:35 +00:00
|
|
|
use MediaWiki\Content\JsonContent;
|
|
|
|
use MediaWiki\Json\FormatJson;
|
|
|
|
use MediaWiki\Message\Message;
|
2023-05-24 20:09:59 +00:00
|
|
|
use MediaWiki\Page\WikiPageFactory;
|
2024-10-05 21:52:24 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2023-05-24 20:09:59 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2023-05-24 20:09:59 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2024-10-05 21:52:24 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2023-05-24 20:09:59 +00:00
|
|
|
use MediaWiki\Utils\UrlUtils;
|
|
|
|
use RecentChange;
|
|
|
|
use StatusValue;
|
|
|
|
|
|
|
|
/**
|
2023-06-14 19:55:30 +00:00
|
|
|
* Hold and update information about blocked external domains
|
|
|
|
*
|
|
|
|
* @ingroup SpecialPage
|
2023-05-24 20:09:59 +00:00
|
|
|
*/
|
|
|
|
class BlockedDomainStorage implements IDBAccessObject {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterBlockedDomainStorage';
|
2023-06-01 13:01:14 +00:00
|
|
|
public const TARGET_PAGE = 'BlockedExternalDomains.json';
|
|
|
|
|
2023-05-24 20:09:59 +00:00
|
|
|
private RevisionLookup $revisionLookup;
|
|
|
|
private BagOStuff $cache;
|
|
|
|
private UserFactory $userFactory;
|
|
|
|
private WikiPageFactory $wikiPageFactory;
|
|
|
|
private UrlUtils $urlUtils;
|
|
|
|
|
|
|
|
/**
|
2023-06-14 19:55:30 +00:00
|
|
|
* @param BagOStuff $cache Local-server caching
|
2023-05-24 20:09:59 +00:00
|
|
|
* @param RevisionLookup $revisionLookup
|
|
|
|
* @param UserFactory $userFactory
|
|
|
|
* @param WikiPageFactory $wikiPageFactory
|
|
|
|
* @param UrlUtils $urlUtils
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
BagOStuff $cache,
|
|
|
|
RevisionLookup $revisionLookup,
|
|
|
|
UserFactory $userFactory,
|
|
|
|
WikiPageFactory $wikiPageFactory,
|
|
|
|
UrlUtils $urlUtils
|
|
|
|
) {
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->revisionLookup = $revisionLookup;
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
$this->wikiPageFactory = $wikiPageFactory;
|
|
|
|
$this->urlUtils = $urlUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function makeCacheKey() {
|
2023-06-11 00:05:19 +00:00
|
|
|
return $this->cache->makeKey( 'abusefilter-blocked-domains' );
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-14 19:55:30 +00:00
|
|
|
* Load the configuration page, with optional local-server caching.
|
|
|
|
*
|
2024-01-26 13:56:58 +00:00
|
|
|
* @param int $flags bit field, see IDBAccessObject::READ_XXX
|
2023-05-24 20:09:59 +00:00
|
|
|
* @return StatusValue The content of the configuration page (as JSON
|
|
|
|
* data in PHP-native format), or a StatusValue on error.
|
|
|
|
*/
|
2023-06-12 14:24:40 +00:00
|
|
|
public function loadConfig( int $flags = 0 ): StatusValue {
|
2024-01-26 13:56:58 +00:00
|
|
|
if ( DBAccessObjectUtils::hasFlags( $flags, IDBAccessObject::READ_LATEST ) ) {
|
2023-05-24 20:09:59 +00:00
|
|
|
return $this->fetchConfig( $flags );
|
|
|
|
}
|
|
|
|
|
2023-06-14 19:55:30 +00:00
|
|
|
// Load configuration from APCU
|
|
|
|
return $this->cache->getWithSetCallback(
|
|
|
|
$this->makeCacheKey(),
|
|
|
|
BagOStuff::TTL_MINUTE * 5,
|
|
|
|
function ( &$ttl ) use ( $flags ) {
|
|
|
|
$result = $this->fetchConfig( $flags );
|
|
|
|
if ( !$result->isGood() ) {
|
|
|
|
// error should not be cached
|
|
|
|
$ttl = BagOStuff::TTL_UNCACHEABLE;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
);
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 19:55:30 +00:00
|
|
|
/**
|
2024-10-05 21:52:24 +00:00
|
|
|
* Load the computed domain blocklist
|
2023-06-14 19:55:30 +00:00
|
|
|
*
|
2023-06-12 14:24:40 +00:00
|
|
|
* @return array<string,true> Flipped for performance reasons
|
2023-06-14 19:55:30 +00:00
|
|
|
*/
|
2023-06-12 14:24:40 +00:00
|
|
|
public function loadComputed(): array {
|
2023-05-24 20:09:59 +00:00
|
|
|
return $this->cache->getWithSetCallback(
|
2023-06-11 00:05:19 +00:00
|
|
|
$this->cache->makeKey( 'abusefilter-blocked-domains-computed' ),
|
2023-06-14 19:55:30 +00:00
|
|
|
BagOStuff::TTL_MINUTE * 5,
|
2023-06-12 14:24:40 +00:00
|
|
|
function () {
|
|
|
|
$status = $this->loadConfig();
|
|
|
|
if ( !$status->isGood() ) {
|
2023-06-14 19:55:30 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$computedDomains = [];
|
2023-06-12 14:24:40 +00:00
|
|
|
foreach ( $status->getValue() as $domain ) {
|
|
|
|
if ( !( $domain['domain'] ?? null ) ) {
|
2023-06-14 19:55:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$validatedDomain = $this->validateDomain( $domain['domain'] );
|
2023-06-12 14:24:40 +00:00
|
|
|
if ( $validatedDomain ) {
|
|
|
|
// It should be a map, benchmark at https://phabricator.wikimedia.org/P48956
|
|
|
|
$computedDomains[$validatedDomain] = true;
|
2023-06-14 19:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $computedDomains;
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-15 21:17:18 +00:00
|
|
|
* Validate an input domain
|
2023-05-24 20:09:59 +00:00
|
|
|
*
|
2023-06-12 14:24:40 +00:00
|
|
|
* @param string|null $domain Domain such as foo.wikipedia.org
|
2023-06-15 21:17:18 +00:00
|
|
|
* @return string|false Parsed domain, or false otherwise
|
2023-05-24 20:09:59 +00:00
|
|
|
*/
|
2023-06-15 21:17:18 +00:00
|
|
|
public function validateDomain( $domain ) {
|
2023-06-12 14:24:40 +00:00
|
|
|
if ( !$domain ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$domain = trim( $domain );
|
|
|
|
if ( !str_contains( $domain, '//' ) ) {
|
2023-05-24 20:09:59 +00:00
|
|
|
$domain = 'https://' . $domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedUrl = $this->urlUtils->parse( $domain );
|
|
|
|
// Parse url returns a valid URL for "foo"
|
2023-06-12 14:24:40 +00:00
|
|
|
if ( !$parsedUrl || !str_contains( $parsedUrl['host'], '.' ) ) {
|
2023-05-24 20:09:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $parsedUrl['host'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the contents of the configuration page, without caching.
|
|
|
|
*
|
2024-10-05 21:52:24 +00:00
|
|
|
* The result is not validated with a config validator.
|
2023-05-24 20:09:59 +00:00
|
|
|
*
|
|
|
|
* @param int $flags bit field, see IDBAccessObject::READ_XXX; do NOT pass READ_UNCACHED
|
|
|
|
* @return StatusValue Status object, with the configuration (as JSON data) on success.
|
|
|
|
*/
|
2023-06-12 14:24:40 +00:00
|
|
|
private function fetchConfig( int $flags ): StatusValue {
|
2023-05-24 20:09:59 +00:00
|
|
|
$revision = $this->revisionLookup->getRevisionByTitle( $this->getBlockedDomainPage(), 0, $flags );
|
|
|
|
if ( !$revision ) {
|
|
|
|
// The configuration page does not exist. Pretend it does not configure anything
|
2024-10-05 21:52:24 +00:00
|
|
|
// specific (failure mode and empty-page behaviors are equal).
|
2023-05-24 20:09:59 +00:00
|
|
|
return StatusValue::newGood( [] );
|
|
|
|
}
|
|
|
|
$content = $revision->getContent( SlotRecord::MAIN );
|
|
|
|
if ( !$content instanceof JsonContent ) {
|
|
|
|
return StatusValue::newFatal( new ApiRawMessage(
|
|
|
|
'The configuration title has no content or is not JSON content.',
|
|
|
|
'newcomer-tasks-configuration-loader-content-error' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return FormatJson::parse( $content->getText(), FormatJson::FORCE_ASSOC );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This doesn't do validation.
|
|
|
|
*
|
|
|
|
* @param string $domain domain to be blocked
|
|
|
|
* @param string $notes User provided notes
|
2024-10-05 21:52:24 +00:00
|
|
|
* @param Authority|UserIdentity $user Performer
|
|
|
|
*
|
2023-06-22 09:54:53 +00:00
|
|
|
* @return RevisionRecord|null Null on failure
|
2023-05-24 20:09:59 +00:00
|
|
|
*/
|
2023-06-12 14:54:13 +00:00
|
|
|
public function addDomain( string $domain, string $notes, $user ): ?RevisionRecord {
|
2023-06-14 19:55:30 +00:00
|
|
|
$content = $this->fetchLatestConfig();
|
2023-06-22 08:32:54 +00:00
|
|
|
if ( $content === null ) {
|
2023-06-12 14:54:13 +00:00
|
|
|
return null;
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
2023-12-25 20:07:38 +00:00
|
|
|
$content[] = [ 'domain' => $domain, 'notes' => $notes, 'addedBy' => $user->getName() ];
|
2023-06-08 19:47:37 +00:00
|
|
|
$comment = Message::newFromSpecifier( 'abusefilter-blocked-domains-domain-added-comment' )
|
|
|
|
->params( $domain, $notes )
|
|
|
|
->plain();
|
2023-05-24 20:09:59 +00:00
|
|
|
return $this->saveContent( $content, $user, $comment );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This doesn't do validation
|
|
|
|
*
|
|
|
|
* @param string $domain domain to be removed from the blocked list
|
|
|
|
* @param string $notes User provided notes
|
2024-10-05 21:52:24 +00:00
|
|
|
* @param Authority|UserIdentity $user Performer
|
|
|
|
*
|
2023-06-22 09:54:53 +00:00
|
|
|
* @return RevisionRecord|null Null on failure
|
2023-05-24 20:09:59 +00:00
|
|
|
*/
|
2023-06-12 14:54:13 +00:00
|
|
|
public function removeDomain( string $domain, string $notes, $user ): ?RevisionRecord {
|
2023-06-14 19:55:30 +00:00
|
|
|
$content = $this->fetchLatestConfig();
|
2023-06-22 09:54:53 +00:00
|
|
|
if ( $content === null ) {
|
2023-06-12 14:54:13 +00:00
|
|
|
return null;
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
foreach ( $content as $key => $value ) {
|
|
|
|
if ( ( $value['domain'] ?? '' ) == $domain ) {
|
|
|
|
unset( $content[$key] );
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 19:47:37 +00:00
|
|
|
$comment = Message::newFromSpecifier( 'abusefilter-blocked-domains-domain-removed-comment' )
|
|
|
|
->params( $domain, $notes )
|
|
|
|
->plain();
|
2023-06-17 22:35:21 +00:00
|
|
|
return $this->saveContent( array_values( $content ), $user, $comment );
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 14:54:13 +00:00
|
|
|
/**
|
2023-06-22 09:54:53 +00:00
|
|
|
* @return array[]|null Empty array when the page doesn't exist, null on failure
|
2023-06-12 14:54:13 +00:00
|
|
|
*/
|
2023-06-14 19:55:30 +00:00
|
|
|
private function fetchLatestConfig(): ?array {
|
2023-05-24 20:09:59 +00:00
|
|
|
$configPage = $this->getBlockedDomainPage();
|
2024-01-26 13:56:58 +00:00
|
|
|
$revision = $this->revisionLookup->getRevisionByTitle( $configPage, 0, IDBAccessObject::READ_LATEST );
|
2023-05-24 20:09:59 +00:00
|
|
|
if ( !$revision ) {
|
2023-06-12 14:24:40 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$revContent = $revision->getContent( SlotRecord::MAIN );
|
|
|
|
if ( $revContent instanceof JsonContent ) {
|
2023-05-24 20:09:59 +00:00
|
|
|
$status = FormatJson::parse( $revContent->getText(), FormatJson::FORCE_ASSOC );
|
2023-06-12 14:24:40 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
return $status->getValue();
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-12 14:24:40 +00:00
|
|
|
|
|
|
|
return null;
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the provided content into the page
|
|
|
|
*
|
2023-06-22 09:54:53 +00:00
|
|
|
* @param array[] $content To be turned into JSON
|
2024-10-05 21:52:24 +00:00
|
|
|
* @param Authority|UserIdentity $user Performer
|
2023-05-24 20:09:59 +00:00
|
|
|
* @param string $comment Save comment
|
2024-10-05 21:52:24 +00:00
|
|
|
*
|
2023-06-12 14:54:13 +00:00
|
|
|
* @return RevisionRecord|null
|
2023-05-24 20:09:59 +00:00
|
|
|
*/
|
2024-09-23 13:25:50 +00:00
|
|
|
private function saveContent( array $content, $user, string $comment ): ?RevisionRecord {
|
2023-05-24 20:09:59 +00:00
|
|
|
$configPage = $this->getBlockedDomainPage();
|
|
|
|
$page = $this->wikiPageFactory->newFromLinkTarget( $configPage );
|
|
|
|
$updater = $page->newPageUpdater( $user );
|
|
|
|
$updater->setContent( SlotRecord::MAIN, new JsonContent( FormatJson::encode( $content ) ) );
|
|
|
|
|
|
|
|
if ( $this->userFactory->newFromUserIdentity( $user )->isAllowed( 'autopatrol' ) ) {
|
|
|
|
$updater->setRcPatrolStatus( RecentChange::PRC_AUTOPATROLLED );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $updater->saveRevision(
|
|
|
|
CommentStoreComment::newUnsavedComment( $comment )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-05 21:52:24 +00:00
|
|
|
* @return TitleValue TitleValue of the config JSON page
|
2023-05-24 20:09:59 +00:00
|
|
|
*/
|
2024-09-23 13:25:50 +00:00
|
|
|
private function getBlockedDomainPage(): TitleValue {
|
2023-06-01 13:01:14 +00:00
|
|
|
return new TitleValue( NS_MEDIAWIKI, self::TARGET_PAGE );
|
2023-05-24 20:09:59 +00:00
|
|
|
}
|
|
|
|
}
|