From 96a988f7ec9c652cfa2fbf26da17fd7a9bf75a1f Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sat, 7 Jan 2006 21:44:10 +0000 Subject: [PATCH] * Protect against spoofing of X-Forwarded-For header --- RELEASE-NOTES | 1 + includes/ProxyTools.php | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 1b385fb6dc..9245c39ac3 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -412,6 +412,7 @@ fully support the editing toolbar, but was found to be too confusing. http://en.wikipedia.org/wiki/Windows_Metafile_vulnerability * (bug 4507) Adjust FULLPAGENAMEE escaping to standard form * Blocked users can no longer roll back, change the protection of, or delete/undelete pages +* Protect against spoofing of X-Forwarded-For header === Caveats === diff --git a/includes/ProxyTools.php b/includes/ProxyTools.php index 42e39a8de7..5818309bfb 100644 --- a/includes/ProxyTools.php +++ b/includes/ProxyTools.php @@ -6,6 +6,23 @@ if ( ! defined( 'MEDIAWIKI' ) ) * @package MediaWiki */ +function wfGetForwardedFor() { + if( function_exists( 'apache_request_headers' ) ) { + // More reliable than $_SERVER due to case and -/_ folding + $set = apache_request_headers(); + $index = 'X-Forwarded-For'; + } else { + // Subject to spoofing with headers like X_Forwarded_For + $set = $_SERVER; + $index = 'HTTP_X_FORWARDED_FOR'; + } + if( isset( $set[$index] ) ) { + return $set[$index]; + } else { + return null; + } +} + /** Work out the IP address based on various globals */ function wfGetIP() { global $wgSquidServers, $wgSquidServersNoPurge, $wgIP; @@ -30,8 +47,9 @@ function wfGetIP() { $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) ); if ( count( $trustedProxies ) ) { # Append XFF on to $ipchain - if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { - $xff = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) ); + $forwardedFor = wfGetForwardedFor(); + if ( isset( $forwardedFor ) ) { + $xff = array_map( 'trim', explode( ',', $forwardedFor ) ); $xff = array_reverse( $xff ); $ipchain = array_merge( $ipchain, $xff ); } -- 2.20.1