r17865 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17864‎ | r17865 | r17866 >
Date:23:21, 22 November 2006
Author:simetrical
Status:old
Tags:
Comment:
* Moved Werdna's new IP functions to the IP class
* Handle bad input to new IP::isAddressInRange gracefully (return false)
* Block::doAutoblock always returns a bool now, as the docs say
* Split off Block::isWhitelistedIp from Block::doAutoblock
* Put AOL proxy IPs in whitelist, and also one from Singapore that was troublesome on enwiki (more should be added, probably?)
* Improve some docs
* Fix a bug: check if the passed IP is in the whitelist, not if the request IP is in the whitelist
Modified paths:
  • /trunk/phase3/includes/Block.php (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/IP.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -2037,37 +2037,6 @@
20382038 }
20392039 }
20402040
2041 -/**
2042 - * Get the start and end of a range.
2043 - * @param $range The range to get the start and end for.
2044 - * @return array An array with the first element as the start of the range, as a long, and the second element as the end of the range, also as a long.
2045 - *
2046 - */
2047 -function wfRangeStartEnd( $range ) {
2048 - list( $network, $bits ) = wfParseCIDR( $range );
2049 - if ( $network !== false ) {
2050 - $start = sprintf( '%08X', $network );
2051 - $end = sprintf( '%08X', $network + (1 << (32 - $bits)) - 1 );
2052 - return array($start, $end);
2053 - }
2054 - return false;
2055 -}
2056 -
2057 -/**
2058 - * Determine if a given integer IPv4 address is in a given CIDR network
2059 - * @param $addr The address to check against the given range.
2060 - * @param $range The range to check the given address against.
2061 - * @return bool Whether or not the given address is in the given range.
2062 - */
2063 -function wfIsAddressInRange( $addr, $range ) {
2064 - $unsignedIP = IP::toUnsigned($addr);
2065 - $startend = wfRangeStartEnd($range);
2066 - $start = $startend[0];
2067 - $end = $startend[1];
2068 -
2069 - return (($unsignedIP >= $start) && ($unsignedip <= $end));
2070 -}
2071 -
20722041 /*
20732042 * Get a Database object
20742043 * @param integer $db Index of the connection to get. May be DB_MASTER for the
Index: trunk/phase3/includes/Block.php
@@ -241,10 +241,10 @@
242242
243243 /**
244244 * Determine if a given integer IPv4 address is in a given CIDR network
245 - * @deprecated Use wfIsAddressInRange
 245+ * @deprecated Use IP::isAddressInRange
246246 */
247247 function isAddressInRange( $addr, $range ) {
248 - return wfIsAddressInRange( $addr, $range );
 248+ return IP::isAddressInRange( $addr, $range );
249249 }
250250
251251 function initFromRow( $row )
@@ -275,9 +275,7 @@
276276 $this->mRangeEnd = '';
277277
278278 if ( $this->mUser == 0 ) {
279 - $startend = wfRangeStartEnd($this->mAddress);
280 - $this->mRangeStart = $startend[0];
281 - $this->mRangeEnd = $startend[1];
 279+ list($this->mRangeStart, $this->mRangeEnd) = IP::parseRange$this->mAddress);
282280 }
283281 }
284282
@@ -430,47 +428,19 @@
431429
432430 /**
433431 * Autoblocks the given IP, referring to this Block.
434 - * @param $autoblockip The IP to autoblock.
435 - * @return bool Whether or not an autoblock was inserted.
 432+ * @param string $autoblockip The IP to autoblock, dotted-quad.
 433+ * @return bool True if an autoblock was inserted OR redundant to preexisting block.
436434 */
437 - function doAutoblock( $autoblockip ) {
 435+ public function doAutoblock( $autoblockip ) {
438436 # Check if this IP address is already blocked
439437 $dbw =& wfGetDb( DB_MASTER );
440438 $dbw->begin();
441439
442 - # If autoblocks are disabled, go away.
443 - if ( !$this->mEnableAutoblock ) {
444 - return;
 440+ # If autoblocks are disabled, or if this IP is whitelisted, go away.
 441+ if ( !$this->mEnableAutoblock || self::isWhitelistedIp( $autoblockip ) ) {
 442+ return false;
445443 }
446444
447 - # Check for presence on the autoblock whitelist
448 - # TODO cache this?
449 - $lines = explode( "\n", wfMsgForContentNoTrans( 'autoblock_whitelist' ) );
450 -
451 - $ip = wfGetIp();
452 -
453 - wfDebug("Checking the autoblock whitelist..\n");
454 -
455 - foreach( $lines as $line ) {
456 - # List items only
457 - if ( substr( $line, 0, 1 ) !== '*' ) {
458 - continue;
459 - }
460 -
461 - $wlEntry = substr($line, 1);
462 - $wlEntry = trim($wlEntry);
463 -
464 - wfDebug("Checking $wlEntry\n");
465 -
466 - # Is the IP in this range?
467 - if (wfIsAddressInRange( $ip, $wlEntry )) {
468 - wfDebug("IP $ip matches $wlEntry, not autoblocking\n");
469 - #$autoblockip = null; # Don't autoblock a whitelisted IP.
470 - return; #This /SHOULD/ introduce a dummy block - but
471 - # I don't know a safe way to do so. -werdna
472 - }
473 - }
474 -
475445 # It's okay to autoblock. Go ahead and create/insert the block.
476446
477447 $ipblock = Block::newFromDB( $autoblockip );
@@ -480,11 +450,11 @@
481451 # prolong block time
482452 if ($this->mExpiry &&
483453 ($this->mExpiry < Block::getAutoblockExpiry($ipblock->mTimestamp))) {
484 - return;
 454+ return true;
485455 }
486456 # Just update the timestamp
487457 $ipblock->updateTimestamp();
488 - return;
 458+ return true;
489459 } else {
490460 $ipblock = new Block;
491461 }
@@ -510,6 +480,38 @@
511481 return $ipblock->insert();
512482 }
513483
 484+ /**
 485+ * Checks whether an IP is whitelisted in the autoblock_whitelist message.
 486+ * @todo Cache this?
 487+ *
 488+ * @param string $ip Dotted quad
 489+ * @return bool
 490+ */
 491+ private static function isWhitelistedIp( $ip ) {
 492+ $lines = explode( "\n", wfMsgForContentNoTrans( 'autoblock_whitelist' ) );
 493+
 494+ wfDebug("Checking the autoblock whitelist..\n");
 495+
 496+ foreach( $lines as $line ) {
 497+ # Parse list items only
 498+ if ( substr( $line, 0, 1 ) !== '*' ) {
 499+ continue;
 500+ }
 501+
 502+ $wlEntry = substr($line, 1);
 503+ $wlEntry = trim($wlEntry);
 504+
 505+ wfDebug("Checking $wlEntry\n");
 506+
 507+ # Is the IP in this range?
 508+ if (IP::isAddressInRange( $autoblockip, $wlEntry )) {
 509+ wfDebug("IP $autoblockip matches $wlEntry, not autoblocking\n");
 510+ return true; #This /SHOULD/ introduce a dummy block - but
 511+ # I don't know a safe way to do so. -werdna
 512+ }
 513+ }
 514+ return false;
 515+ }
514516 function deleteIfExpired()
515517 {
516518 $fname = 'Block::deleteIfExpired';
Index: trunk/phase3/includes/IP.php
@@ -20,6 +20,7 @@
2121
2222 /**
2323 * Validate an IP address.
 24+ * @param string $ipblock Dotted quad
2425 * @return boolean True if it is valid.
2526 */
2627 public static function isValid( $ip ) {
@@ -28,6 +29,7 @@
2930
3031 /**
3132 * Validate an IP Block.
 33+ * @param string $ipblock Dotted quad
3234 * @return boolean True if it is valid.
3335 */
3436 public static function isValidBlock( $ipblock ) {
@@ -38,6 +40,9 @@
3941 * Determine if an IP address really is an IP address, and if it is public,
4042 * i.e. not RFC 1918 or similar
4143 * Comes from ProxyTools.php
 44+ *
 45+ * @param string $ip Dotted quad
 46+ * @return bool
4247 */
4348 public static function isPublic( $ip ) {
4449 $n = IP::toUnsigned( $ip );
@@ -74,10 +79,10 @@
7580
7681 /**
7782 * Split out an IP block as an array of 4 bytes and a mask,
78 - * return false if it cant be determined
 83+ * return false if it can't be determined
7984 *
80 - * @parameter $ip string A quad dotted IP address
81 - * @return array
 85+ * @param string $ipblock A quad dotted IP address
 86+ * @return mixed Array or false
8287 */
8388 public static function toArray( $ipblock ) {
8489 if(! preg_match( '/^' . RE_IP_ADD . '(?:\/(?:'.RE_IP_PREFIX.'))?' . '$/', $ipblock, $matches ) ) {
@@ -95,7 +100,8 @@
96101 * function for an IPv6 address will be prefixed with "v6-", a non-
97102 * hexadecimal string which sorts after the IPv4 addresses.
98103 *
99 - * @param $ip Quad dotted IP address.
 104+ * @param string $ip Quad dotted IP address.
 105+ * @return mixed String or false
100106 */
101107 public static function toHex( $ip ) {
102108 $n = self::toUnsigned( $ip );
@@ -110,6 +116,7 @@
111117 * Like ip2long() except that it actually works and has a consistent error return value.
112118 * Comes from ProxyTools.php
113119 * @param $ip Quad dotted IP address.
 120+ * @return mixed Int or false
114121 */
115122 public static function toUnsigned( $ip ) {
116123 if ( $ip == '255.255.255.255' ) {
@@ -177,6 +184,8 @@
178185 * 1.2.3.4/24 CIDR
179186 * 1.2.3.4 - 1.2.3.5 Explicit range
180187 * 1.2.3.4 Single IP
 188+ * @param string $range
 189+ * @return array (hex string start, hex string end), or (false, false) if an error occurred
181190 */
182191 public static function parseRange( $range ) {
183192 if ( strpos( $range, '/' ) !== false ) {
@@ -207,5 +216,21 @@
208217 return array( $start, $end );
209218 }
210219 }
 220+
 221+ /**
 222+ * Determine if a given integer IPv4 address is in a given range
 223+ * @param int $addr
 224+ * @param string $range (CIDR, hyphenated dotted-quad, or single dotted-quad)
 225+ * @return bool Whether or not the given address is in the given range. Returns false on error.
 226+ */
 227+ public static function isAddressInRange( $addr, $range ) {
 228+ $unsignedIP = IP::toUnsigned($addr);
 229+ list($start, $end) = IP::parseRange($range);
 230+
 231+ if ($start == false || $end == false)
 232+ return false;
 233+ else
 234+ return (($unsignedIP >= $start) && ($unsignedip <= $end)); // string comparison
 235+ }
211236 }
212237 ?>
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -2616,7 +2616,30 @@
26172617 'autosumm-shortnew' => 'New page: $1',
26182618
26192619 # Autoblock whitelist
2620 -'autoblock_whitelist' => '',
 2620+'autoblock_whitelist' => "AOL http://webmaster.info.aol.com/proxyinfo.html
 2621+*64.12.96.0/19
 2622+*149.174.160.0/20
 2623+*152.163.240.0/21
 2624+*152.163.248.0/22
 2625+*152.163.252.0/23
 2626+*152.163.96.0/22
 2627+*152.163.100.0/23
 2628+*195.93.32.0/22
 2629+*195.93.48.0/22
 2630+*195.93.64.0/19
 2631+*195.93.96.0/19
 2632+*195.93.16.0/20
 2633+*198.81.0.0/22
 2634+*198.81.16.0/20
 2635+*198.81.8.0/23
 2636+*202.67.64.128/25
 2637+*205.188.192.0/20
 2638+*205.188.208.0/23
 2639+*205.188.112.0/20
 2640+*205.188.146.144/30
 2641+*207.200.112.0/21
 2642+StarHub
 2643+*202.156.6.54/32",
26212644
26222645 );
26232646