Debian Codegraph @

includes/block/BlockUtils.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 namespace MediaWiki\Block;
23
24 use MediaWiki\Config\ServiceOptions;
25 use MediaWiki\MainConfigNames;
26 use MediaWiki\Status\Status;
27 use MediaWiki\User\UserIdentity;
28 use MediaWiki\User\UserIdentityLookup;
29 use MediaWiki\User\UserIdentityValue;
30 use MediaWiki\User\UserNameUtils;
31 use Wikimedia\IPUtils;
32
33 /**
34 * Backend class for blocking utils
35 *
36 * This service should contain any methods that are useful
37 * to more than one blocking-related class and don't fit any
38 * other service.
39 *
40 * For now, this includes only
41 * - block target parsing
42 * - block target validation
43 * - parsing the target and type of a block in the database
44 *
45 * @since 1.36
46 */
47 class BlockUtils {
48 private ServiceOptions $options;
49 private UserIdentityLookup $userIdentityLookup;
50 private UserNameUtils $userNameUtils;
51
52 /** @var string|false */
53 private $wikiId;
54
55 /**
56 * @internal Only for use by ServiceWiring
57 */
58 public const CONSTRUCTOR_OPTIONS = [
59 MainConfigNames::BlockCIDRLimit,
60 ];
61
62 public function __construct(
63 ServiceOptions $options,
64 UserIdentityLookup $userIdentityLookup,
65 UserNameUtils $userNameUtils,
66 /* string|false */ $wikiId = Block::LOCAL
67 ) {
68 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
69 $this->options = $options;
70 $this->userIdentityLookup = $userIdentityLookup;
71 $this->userNameUtils = $userNameUtils;
72 $this->wikiId = $wikiId;
73 }
74
75 /**
76 * From string specification or UserIdentity, get the block target and the
77 * type of target.
78 *
79 * Note that, except for null, it is always safe to treat the target
80 * as a string; for UserIdentityValue objects this will return
81 * UserIdentityValue::__toString() which in turn gives
82 * UserIdentityValue::getName().
83 *
84 * If the type is not null, it will be an AbstractBlock::TYPE_ constant.
85 *
86 * Since 1.42, it is no longer safe to pass a value from the database field
87 * ipb_address/bt_address to this method, since the username is normalized.
88 * Use parseBlockTargetRow() instead. (T346683)
89 *
90 * @param string|UserIdentity|null $target
91 * @return array [ UserIdentity|String|null, int|null ]
92 */
93 public function parseBlockTarget( $target ): array {
94 // We may have been through this before
95 if ( $target instanceof UserIdentity ) {
96 if ( IPUtils::isValid( $target->getName() ) ) {
97 return [ $target, AbstractBlock::TYPE_IP ];
98 } else {
99 return [ $target, AbstractBlock::TYPE_USER ];
100 }
101 } elseif ( $target === null ) {
102 return [ null, null ];
103 }
104
105 $target = trim( $target );
106
107 if ( IPUtils::isValid( $target ) ) {
108 return [
109 UserIdentityValue::newAnonymous( IPUtils::sanitizeIP( $target ), $this->wikiId ),
110 AbstractBlock::TYPE_IP
111 ];
112
113 } elseif ( IPUtils::isValidRange( $target ) ) {
114 // Can't create a UserIdentity from an IP range
115 return [ IPUtils::sanitizeRange( $target ), AbstractBlock::TYPE_RANGE ];
116 }
117
118 if ( preg_match( '/^#\d+$/', $target ) ) {
119 // Autoblock reference in the form "#12345"
120 return [ substr( $target, 1 ), AbstractBlock::TYPE_AUTO ];
121 }
122
123 $userFromDB = $this->userIdentityLookup->getUserIdentityByName( $target );
124 if ( $userFromDB instanceof UserIdentity ) {
125 // Note that since numbers are valid usernames, a $target of "12345" will be
126 // considered a UserIdentity. If you want to pass a block ID, prepend a hash "#12345",
127 // since hash characters are not valid in usernames or titles generally.
128 return [ $userFromDB, AbstractBlock::TYPE_USER ];
129 }
130
131 // Wrap the invalid user in a UserIdentityValue.
132 // This allows validateTarget() to return a "nosuchusershort" message,
133 // which is needed for Special:Block.
134 $canonicalName = $this->userNameUtils->getCanonical( $target );
135 if ( $canonicalName !== false ) {
136 return [
137 new UserIdentityValue( 0, $canonicalName ),
138 AbstractBlock::TYPE_USER
139 ];
140 }
141
142 return [ null, null ];
143 }
144
145 /**
146 * From a row which must contain bt_auto, bt_user, bt_address and bl_id,
147 * and optionally bt_user_text, determine the block target and type.
148 *
149 * @since 1.42
150 * @param \stdClass $row
151 * @return array [ UserIdentity|String|null, int|null ]
152 */
153 public function parseBlockTargetRow( $row ) {
154 if ( $row->bt_auto ) {
155 return [ $row->bl_id, AbstractBlock::TYPE_AUTO ];
156 } elseif ( isset( $row->bt_user ) ) {
157 if ( isset( $row->bt_user_text ) ) {
158 $user = new UserIdentityValue( $row->bt_user, $row->bt_user_text, $this->wikiId );
159 } else {
160 $user = $this->userIdentityLookup->getUserIdentityByUserId( $row->bt_user );
161 }
162 return [ $user, AbstractBlock::TYPE_USER ];
163 } elseif ( $row->bt_address === null ) {
164 return [ null, null ];
165 } elseif ( IPUtils::isValid( $row->bt_address ) ) {
166 return [
167 UserIdentityValue::newAnonymous( IPUtils::sanitizeIP( $row->bt_address ), $this->wikiId ),
168 AbstractBlock::TYPE_IP
169 ];
170 } elseif ( IPUtils::isValidRange( $row->bt_address ) ) {
171 // Can't create a UserIdentity from an IP range
172 return [ IPUtils::sanitizeRange( $row->bt_address ), AbstractBlock::TYPE_RANGE ];
173 } else {
174 return [ null, null ];
175 }
176 }
177
178 /**
179 * Validate block target
180 *
181 * @param string|UserIdentity $value
182 *
183 * @return Status
184 */
185 public function validateTarget( $value ): Status {
186 [ $target, $type ] = $this->parseBlockTarget( $value );
187
188 $status = Status::newGood( $target );
189
190 switch ( $type ) {
191 case AbstractBlock::TYPE_USER:
192 if ( !$target->isRegistered() ) {
193 $status->fatal(
194 'nosuchusershort',
195 wfEscapeWikiText( $target->getName() )
196 );
197 }
198 break;
199
200 case AbstractBlock::TYPE_RANGE:
201 [ $ip, $range ] = explode( '/', $target, 2 );
202
203 if ( IPUtils::isIPv4( $ip ) ) {
204 $status->merge( $this->validateIPv4Range( (int)$range ) );
205 } elseif ( IPUtils::isIPv6( $ip ) ) {
206 $status->merge( $this->validateIPv6Range( (int)$range ) );
207 } else {
208 // Something is FUBAR
209 $status->fatal( 'badipaddress' );
210 }
211 break;
212
213 case AbstractBlock::TYPE_IP:
214 // All is well
215 break;
216
217 default:
218 $status->fatal( 'badipaddress' );
219 break;
220 }
221
222 return $status;
223 }
224
225 /**
226 * Validate an IPv4 range
227 *
228 * @param int $range
229 *
230 * @return Status
231 */
232 private function validateIPv4Range( int $range ): Status {
233 $status = Status::newGood();
234 $blockCIDRLimit = $this->options->get( MainConfigNames::BlockCIDRLimit );
235
236 if ( $blockCIDRLimit['IPv4'] == 32 ) {
237 // Range block effectively disabled
238 $status->fatal( 'range_block_disabled' );
239 } elseif ( $range > 32 ) {
240 // Such a range cannot exist
241 $status->fatal( 'ip_range_invalid' );
242 } elseif ( $range < $blockCIDRLimit['IPv4'] ) {
243 $status->fatal( 'ip_range_toolarge', $blockCIDRLimit['IPv4'] );
244 }
245
246 return $status;
247 }
248
249 /**
250 * Validate an IPv6 range
251 *
252 * @param int $range
253 *
254 * @return Status
255 */
256 private function validateIPv6Range( int $range ): Status {
257 $status = Status::newGood();
258 $blockCIDRLimit = $this->options->get( MainConfigNames::BlockCIDRLimit );
259
260 if ( $blockCIDRLimit['IPv6'] == 128 ) {
261 // Range block effectively disabled
262 $status->fatal( 'range_block_disabled' );
263 } elseif ( $range > 128 ) {
264 // Dodgy range - such a range cannot exist
265 $status->fatal( 'ip_range_invalid' );
266 } elseif ( $range < $blockCIDRLimit['IPv6'] ) {
267 $status->fatal( 'ip_range_toolarge', $blockCIDRLimit['IPv6'] );
268 }
269
270 return $status;
271 }
272 }