File: BlockedDomainStorage.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (288 lines) | stat: -rw-r--r-- 9,061 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?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 MediaWiki\Api\ApiRawMessage;
use MediaWiki\CommentStore\CommentStoreComment;
use MediaWiki\Content\JsonContent;
use MediaWiki\Json\FormatJson;
use MediaWiki\Message\Message;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Permissions\Authority;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Title\TitleValue;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserIdentity;
use MediaWiki\Utils\UrlUtils;
use RecentChange;
use StatusValue;
use Wikimedia\ObjectCache\BagOStuff;
use Wikimedia\Rdbms\DBAccessObjectUtils;
use Wikimedia\Rdbms\IDBAccessObject;

/**
 * Hold and update information about blocked external domains
 *
 * @ingroup SpecialPage
 */
class BlockedDomainStorage implements IDBAccessObject {
	public const SERVICE_NAME = 'AbuseFilterBlockedDomainStorage';
	public const TARGET_PAGE = 'BlockedExternalDomains.json';

	private RevisionLookup $revisionLookup;
	private BagOStuff $cache;
	private UserFactory $userFactory;
	private WikiPageFactory $wikiPageFactory;
	private UrlUtils $urlUtils;

	/**
	 * @param BagOStuff $cache Local-server caching
	 * @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() {
		return $this->cache->makeKey( 'abusefilter-blocked-domains' );
	}

	/**
	 * Load the configuration page, with optional local-server caching.
	 *
	 * @param int $flags bit field, see IDBAccessObject::READ_XXX
	 * @return StatusValue The content of the configuration page (as JSON
	 *   data in PHP-native format), or a StatusValue on error.
	 */
	public function loadConfig( int $flags = 0 ): StatusValue {
		if ( DBAccessObjectUtils::hasFlags( $flags, IDBAccessObject::READ_LATEST ) ) {
			return $this->fetchConfig( $flags );
		}

		// 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;
			}
		);
	}

	/**
	 * Load the computed domain blocklist
	 *
	 * @return array<string,true> Flipped for performance reasons
	 */
	public function loadComputed(): array {
		return $this->cache->getWithSetCallback(
			$this->cache->makeKey( 'abusefilter-blocked-domains-computed' ),
			BagOStuff::TTL_MINUTE * 5,
			function () {
				$status = $this->loadConfig();
				if ( !$status->isGood() ) {
					return [];
				}
				$computedDomains = [];
				foreach ( $status->getValue() as $domain ) {
					if ( !( $domain['domain'] ?? null ) ) {
						continue;
					}
					$validatedDomain = $this->validateDomain( $domain['domain'] );
					if ( $validatedDomain ) {
						// It should be a map, benchmark at https://phabricator.wikimedia.org/P48956
						$computedDomains[$validatedDomain] = true;
					}
				}
				return $computedDomains;
			}
		);
	}

	/**
	 * Validate an input domain
	 *
	 * @param string|null $domain Domain such as foo.wikipedia.org
	 * @return string|false Parsed domain, or false otherwise
	 */
	public function validateDomain( $domain ) {
		if ( !$domain ) {
			return false;
		}

		$domain = trim( $domain );
		if ( !str_contains( $domain, '//' ) ) {
			$domain = 'https://' . $domain;
		}

		$parsedUrl = $this->urlUtils->parse( $domain );
		// Parse url returns a valid URL for "foo"
		if ( !$parsedUrl || !str_contains( $parsedUrl['host'], '.' ) ) {
			return false;
		}
		return $parsedUrl['host'];
	}

	/**
	 * Fetch the contents of the configuration page, without caching.
	 *
	 * The result is not validated with a config validator.
	 *
	 * @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.
	 */
	private function fetchConfig( int $flags ): StatusValue {
		$revision = $this->revisionLookup->getRevisionByTitle( $this->getBlockedDomainPage(), 0, $flags );
		if ( !$revision ) {
			// The configuration page does not exist. Pretend it does not configure anything
			// specific (failure mode and empty-page behaviors are equal).
			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
	 * @param Authority|UserIdentity $user Performer
	 *
	 * @return RevisionRecord|null Null on failure
	 */
	public function addDomain( string $domain, string $notes, $user ): ?RevisionRecord {
		$content = $this->fetchLatestConfig();
		if ( $content === null ) {
			return null;
		}
		$content[] = [ 'domain' => $domain, 'notes' => $notes, 'addedBy' => $user->getName() ];
		$comment = Message::newFromSpecifier( 'abusefilter-blocked-domains-domain-added-comment' )
			->params( $domain, $notes )
			->plain();
		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
	 * @param Authority|UserIdentity $user Performer
	 *
	 * @return RevisionRecord|null Null on failure
	 */
	public function removeDomain( string $domain, string $notes, $user ): ?RevisionRecord {
		$content = $this->fetchLatestConfig();
		if ( $content === null ) {
			return null;
		}
		foreach ( $content as $key => $value ) {
			if ( ( $value['domain'] ?? '' ) == $domain ) {
				unset( $content[$key] );
			}
		}
		$comment = Message::newFromSpecifier( 'abusefilter-blocked-domains-domain-removed-comment' )
			->params( $domain, $notes )
			->plain();
		return $this->saveContent( array_values( $content ), $user, $comment );
	}

	/**
	 * @return array[]|null Empty array when the page doesn't exist, null on failure
	 */
	private function fetchLatestConfig(): ?array {
		$configPage = $this->getBlockedDomainPage();
		$revision = $this->revisionLookup->getRevisionByTitle( $configPage, 0, IDBAccessObject::READ_LATEST );
		if ( !$revision ) {
			return [];
		}

		$revContent = $revision->getContent( SlotRecord::MAIN );
		if ( $revContent instanceof JsonContent ) {
			$status = FormatJson::parse( $revContent->getText(), FormatJson::FORCE_ASSOC );
			if ( $status->isOK() ) {
				return $status->getValue();
			}
		}

		return null;
	}

	/**
	 * Save the provided content into the page
	 *
	 * @param array[] $content To be turned into JSON
	 * @param Authority|UserIdentity $user Performer
	 * @param string $comment Save comment
	 *
	 * @return RevisionRecord|null
	 */
	private function saveContent( array $content, $user, string $comment ): ?RevisionRecord {
		$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 )
		);
	}

	/**
	 * @return TitleValue TitleValue of the config JSON page
	 */
	private function getBlockedDomainPage(): TitleValue {
		return new TitleValue( NS_MEDIAWIKI, self::TARGET_PAGE );
	}
}