Skip to content

Commit

Permalink
Multiple reverse proxy IPs in $_SERVER['HTTP_X_FORWARDED_FOR'] (#4007)
Browse files Browse the repository at this point in the history
* ignore intellij file

* Added a check for multiple IPs stored in HTTP_X_FORWARDED_FOR header
  • Loading branch information
geoffdutton authored and cklosowski committed Jun 22, 2017
1 parent 4fd8c4c commit 27b6ca4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -17,6 +17,7 @@ $RECYCLE.BIN/

# PhpStorm
.idea
Easy-Digital-Downloads.iml

# Eclipse
*.pydevproject
Expand Down
4 changes: 3 additions & 1 deletion includes/misc-functions.php
Expand Up @@ -169,7 +169,9 @@ function edd_get_ip() {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
// can include more than 1 ip, first is the public one
$ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = trim($ip[0]);
} elseif( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
$ip = $_SERVER['REMOTE_ADDR'];
}
Expand Down
13 changes: 13 additions & 0 deletions tests/tests-misc.php
Expand Up @@ -57,6 +57,19 @@ public function test_get_ip() {
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
}

public function test_get_ip_reverse_proxies() {
$_SERVER['HTTP_X_FORWARDED_FOR'] = '123.123.123.123, 10.0.0.2';
$this->assertEquals( '123.123.123.123', edd_get_ip() );
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
}

public function test_get_ip_reverse_proxy() {
$_SERVER['HTTP_X_FORWARDED_FOR'] = '123.123.123.123';
$this->assertEquals( '123.123.123.123', edd_get_ip() );
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
}


public function test_get_currencies() {
$expected = array(
'USD' => __( 'US Dollars ($)', 'easy-digital-downloads' ),
Expand Down

0 comments on commit 27b6ca4

Please sign in to comment.