r20375 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r20374‎ | r20375 | r20376 >
Date:21:37, 12 March 2007
Author:aaron
Status:old
Tags:
Comment:
*IPv6 sanitizing, avoid use of native hex functions, enlarge ip box for blockip a bit
Modified paths:
  • /trunk/phase3/includes/Block.php (modified) (history)
  • /trunk/phase3/includes/IP.php (modified) (history)
  • /trunk/phase3/includes/SpecialBlockip.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialBlockip.php
@@ -113,7 +113,7 @@
114114 <tr>
115115 <td align=\"right\">{$mIpaddress}:</td>
116116 <td align=\"left\">
117 - " . Xml::input( 'wpBlockAddress', 40, $this->BlockAddress,
 117+ " . Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
118118 array(
119119 'tabindex' => '1',
120120 'id' => 'mw-bi-target',
@@ -136,14 +136,14 @@
137137 <tr id='wpBlockOther'>
138138 <td align=\"right\">{$mIpbother}:</td>
139139 <td align=\"left\">
140 - " . Xml::input( 'wpBlockOther', 40, $this->BlockOther,
 140+ " . Xml::input( 'wpBlockOther', 45, $this->BlockOther,
141141 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
142142 </td>
143143 </tr>
144144 <tr>
145145 <td align=\"right\">{$mIpbreason}:</td>
146146 <td align=\"left\">
147 - " . Xml::input( 'wpBlockReason', 40, $this->BlockReason,
 147+ " . Xml::input( 'wpBlockReason', 45, $this->BlockReason,
148148 array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
149149 </td>
150150 </tr>
@@ -200,7 +200,7 @@
201201 $userId = 0;
202202 $this->BlockAddress = trim( $this->BlockAddress );
203203 # Expand valid IPv6 addresses, usernames are left as is
204 - $this->BlockAddress = IP::expandIP( $this->BlockAddress );
 204+ $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
205205 # isIPv4() and IPv6() are used for final validation
206206 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
207207 $rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';
Index: trunk/phase3/includes/Block.php
@@ -27,7 +27,7 @@
2828 {
2929 $this->mId = 0;
3030 # Expand valid IPv6 addresses
31 - $address = IP::expandIP( $address );
 31+ $address = IP::sanitizeIP( $address );
3232 $this->mAddress = $address;
3333 $this->mUser = $user;
3434 $this->mBy = $by;
@@ -176,7 +176,8 @@
177177 /**
178178 * Fill in member variables from a result wrapper
179179 */
180 - function loadFromResult( ResultWrapper $res, $killExpired = true ) {
 180+ function loadFromResult( ResultWrapper $res, $killExpired = true )
 181+ {
181182 $ret = false;
182183 if ( 0 != $res->numRows() ) {
183184 # Get first block
Index: trunk/phase3/includes/IP.php
@@ -82,7 +82,7 @@
8383 */
8484 public function toUnsigned6( $ip ) {
8585 if ( !$ip ) return null;
86 - $ip = explode(':', self::expandIP( $ip ) );
 86+ $ip = explode(':', self::sanitizeIP( $ip ) );
8787 $r_ip = '';
8888 foreach ($ip as $v) {
8989 $r_ip .= wfBaseConvert( $v, 16, 2, 16);
@@ -95,10 +95,12 @@
9696 * @param $ip octet ipv6 IP address.
9797 * @return string
9898 */
99 - public function expandIP( $ip ) {
 99+ public function sanitizeIP( $ip ) {
100100 if ( !$ip ) return null;
101101 // Only IPv6 addresses can be expanded
102102 if ( !self::isIPv6( $ip ) ) return $ip;
 103+ // Convert to upper case
 104+ $ip = strtoupper( $ip );
103105 // Expand zero abbreviations
104106 if ( substr_count($ip, '::') ) {
105107 $ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip);
@@ -112,14 +114,13 @@
113115 * @return string
114116 */
115117 public function toOctet( $ip_int ) {
116 - $ip_int = strval($ip_int);
117118 // Convert integer to binary
118119 $ip_int = wfBaseConvert($ip_int, 10, 2, 128);
119120 // Seperate into 8 octets
120 - $ip_oct = base_convert( substr( $ip_int, 0, 16 ), 2, 16 );
 121+ $ip_oct = wfBaseConvert( substr( $ip_int, 0, 16 ), 2, 16, 1, false );
121122 for ($n=1; $n < 8; $n++) {
122123 // Convert to hex, and add ":" marks
123 - $ip_oct .= ':' . base_convert( substr($ip_int, 16*$n, 16), 2, 16 );
 124+ $ip_oct .= ':' . wfBaseConvert( substr($ip_int, 16*$n, 16), 2, 16, 1, false );
124125 }
125126 return $ip_oct;
126127 }
@@ -129,7 +130,8 @@
130131 * @return array(string, int)
131132 */
132133 public static function parseCIDR6( $range ) {
133 - $parts = explode( '/', $range, 2 );
 134+ # Expand any IPv6 IP
 135+ $parts = explode( '/', IP::sanitizeIP( $range ), 2 );
134136 if ( count( $parts ) != 2 ) {
135137 return array( false, false );
136138 }
@@ -166,14 +168,21 @@
167169 * @return array(string, int)
168170 */
169171 public static function parseRange6( $range ) {
 172+ # Expand any IPv6 IP
 173+ $range = IP::sanitizeIP( $range );
170174 if ( strpos( $range, '/' ) !== false ) {
171175 # CIDR
172176 list( $network, $bits ) = self::parseCIDR6( $range );
173177 if ( $network === false ) {
174178 $start = $end = false;
175179 } else {
176 - $start = sprintf( '%08X', $network );
177 - $end = sprintf( '%08X', $network + pow( 2, (128 - $bits) ) - 1 );
 180+ $start = wfBaseConvert( $network, 10, 16, 1, false );
 181+ # Turn network to binary (again)
 182+ $end = wfBaseConvert( $network, 10, 2, 128 );
 183+ # Truncate the last (128-$bits) bits and replace them with ones
 184+ $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
 185+ # Convert to hex
 186+ $end = wfBaseConvert( $end, 2, 16, 1, false );
178187 }
179188 } elseif ( strpos( $range, '-' ) !== false ) {
180189 # Explicit range
@@ -182,8 +191,8 @@
183192 if ( $start > $end ) {
184193 $start = $end = false;
185194 } else {
186 - $start = sprintf( '%08X', $start );
187 - $end = sprintf( '%08X', $end );
 195+ $start = wfBaseConvert( $start, 10, 16, 1, false );
 196+ $end = wfBaseConvert( $end, 10, 16, 1, false );
188197 }
189198 } else {
190199 # Single IP
@@ -191,7 +200,7 @@
192201 }
193202 if ( $start === false || $end === false ) {
194203 return array( false, false );
195 - } else {
 204+ } else {
196205 return array( $start, $end );
197206 }
198207 }
@@ -283,7 +292,7 @@
284293 // Use IPv6 functions if needed
285294 $n = ( self::isIPv6($ip) ) ? self::toUnsigned6( $ip ) : self::toUnsigned( $ip );
286295 if ( $n !== false ) {
287 - $n = sprintf( '%08X', $n );
 296+ $n = wfBaseConvert( $n, 10, 16, 1, false );
288297 }
289298 return $n;
290299 }