r96368 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96367‎ | r96368 | r96369 >
Date:20:33, 6 September 2011
Author:ialex
Status:ok
Tags:
Comment:
* Call $wgRequest->getHeader( 'X-Forwarded-For' ) instead of wfGetForwardedFor(), the latter is deprecated
* Merged CheckUserHooks::XFFChainIsSquid() into CheckUserHooks::getClientIPfromXFF() and changed the second item to be the value returned by XFFChainIsSquid() since $trusted was not used anywhere
Modified paths:
  • /trunk/extensions/CheckUser/CheckUser.hooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CheckUser/CheckUser.hooks.php
@@ -12,10 +12,8 @@
1313 // Get IP
1414 $ip = wfGetIP();
1515 // Get XFF header
16 - $xff = wfGetForwardedFor();
17 - list( $xff_ip, $trusted ) = self::getClientIPfromXFF( $xff );
18 - // Our squid XFFs can flood this up sometimes
19 - $isSquidOnly = self::XFFChainIsSquid( $xff );
 16+ $xff = $wgRequest->getHeader( 'X-Forwarded-For' );
 17+ list( $xff_ip, $isSquidOnly ) = self::getClientIPfromXFF( $xff );
2018 // Get agent
2119 $agent = $wgRequest->getHeader( 'User-Agent' );
2220 // Store the log action text for log events
@@ -78,10 +76,8 @@
7977 global $wgRequest;
8078
8179 // Get XFF header
82 - $xff = wfGetForwardedFor();
83 - list( $xff_ip, $trusted ) = self::getClientIPfromXFF( $xff );
84 - // Our squid XFFs can flood this up sometimes
85 - $isSquidOnly = self::XFFChainIsSquid( $xff );
 80+ $xff = $wgRequest->getHeader( 'X-Forwarded-For' );
 81+ list( $xff_ip, $isSquidOnly ) = self::getClientIPfromXFF( $xff );
8682 // Get agent
8783 $agent = $wgRequest->getHeader( 'User-Agent' );
8884 $dbw = wfGetDB( DB_MASTER );
@@ -125,10 +121,8 @@
126122 // Get IP
127123 $ip = wfGetIP();
128124 // Get XFF header
129 - $xff = wfGetForwardedFor();
130 - list( $xff_ip, $trusted ) = self::getClientIPfromXFF( $xff );
131 - // Our squid XFFs can flood this up sometimes
132 - $isSquidOnly = self::XFFChainIsSquid( $xff );
 125+ $xff = $wgRequest->getHeader( 'X-Forwarded-For' );
 126+ list( $xff_ip, $isSquidOnly ) = self::getClientIPfromXFF( $xff );
133127 // Get agent
134128 $agent = $wgRequest->getHeader( 'User-Agent' );
135129 $dbw = wfGetDB( DB_MASTER );
@@ -180,10 +174,8 @@
181175 // Get IP
182176 $ip = wfGetIP();
183177 // Get XFF header
184 - $xff = wfGetForwardedFor();
185 - list( $xff_ip, $trusted ) = self::getClientIPfromXFF( $xff );
186 - // Our squid XFFs can flood this up sometimes
187 - $isSquidOnly = self::XFFChainIsSquid( $xff );
 178+ $xff = $wgRequest->getHeader( 'X-Forwarded-For' );
 179+ list( $xff_ip, $isSquidOnly ) = self::getClientIPfromXFF( $xff );
188180 // Get agent
189181 $agent = $wgRequest->getHeader( 'User-Agent' );
190182 $dbw = wfGetDB( DB_MASTER );
@@ -228,16 +220,19 @@
229221 /**
230222 * Locates the client IP within a given XFF string
231223 * @param string $xff
232 - * @param string $address, the ip that sent this header (optional)
233224 * @return array( string, bool )
234225 */
235 - public static function getClientIPfromXFF( $xff, $address = null ) {
 226+ public static function getClientIPfromXFF( $xff ) {
 227+ global $wgSquidServers, $wgSquidServersNoPurge;
 228+
236229 if ( !$xff ) {
237230 return array( null, false );
238231 }
 232+
239233 // Avoid annoyingly long xff hacks
240234 $xff = trim( substr( $xff, 0, 255 ) );
241235 $client = null;
 236+ $isSquidOnly = true;
242237 $trusted = true;
243238 // Check each IP, assuming they are separated by commas
244239 $ips = explode( ',', $xff );
@@ -251,51 +246,18 @@
252247 if ( IP::isPublic( $ip ) ) {
253248 $client = $ip;
254249 }
255 - # Check that all servers are trusted
256 - } elseif ( !wfIsTrustedProxy( $ip ) ) {
257 - $trusted = false;
258 - break;
259 - }
260 - }
261 - }
262 - // We still have to test if the IP that sent
263 - // this header is trusted to confirm results
264 - if ( $client != $address && ( !$address || !wfIsTrustedProxy( $address ) ) ) {
265 - $trusted = false;
266 - }
267 -
268 - return array( $client, $trusted );
269 - }
270 -
271 - public static function XFFChainIsSquid( $xff ) {
272 - global $wgSquidServers, $wgSquidServersNoPurge;
273 -
274 - if ( !$xff ) {
275 - false;
276 - }
277 - // Avoid annoyingly long xff hacks
278 - $xff = trim( substr( $xff, 0, 255 ) );
279 - $squidOnly = true;
280 - // Check each IP, assuming they are separated by commas
281 - $ips = explode( ',', $xff );
282 - foreach ( $ips as $n => $ip ) {
283 - $ip = trim( $ip );
284 - // If it is a valid IP, not a hash or such
285 - if ( IP::isIPAddress( $ip ) ) {
286 - if ( $n == 0 ) {
287 - // The first IP should be the client...
288250 } elseif ( !in_array( $ip, $wgSquidServers )
289251 && !in_array( $ip, $wgSquidServersNoPurge ) )
290252 {
291 - $squidOnly = false;
 253+ $isSquidOnly = false;
292254 break;
293255 }
294256 }
295257 }
296 -
297 - return $squidOnly;
 258+
 259+ return array( $client, $isSquidOnly );
298260 }
299 -
 261+
300262 public static function checkUserSchemaUpdates( DatabaseUpdater $updater ) {
301263 $base = dirname( __FILE__ );
302264

Status & tagging log