-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Whitelist Auth0 IPs by default and show in wp-admin #596
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
<?php | ||
/** | ||
* Contains class WP_Auth0_Ip_Check. | ||
* | ||
* @package WP-Auth0 | ||
* | ||
* @since 1.2.1 | ||
*/ | ||
|
||
/** | ||
* Class WP_Auth0_Ip_Check. | ||
* Used for checking IP addresses against whitelists and default Auth0 IPs. | ||
*/ | ||
class WP_Auth0_Ip_Check { | ||
|
||
const IP_STRING_GLUE = ','; | ||
|
||
/** | ||
* IP addresses for inbound connections per region. | ||
* The list of IP addresses may be found at the footer section of the Custom Database Editor and the header for | ||
|
@@ -73,7 +87,7 @@ class WP_Auth0_Ip_Check { | |
/** | ||
* WP_Auth0_Ip_Check constructor. | ||
* | ||
* @param WP_Auth0_Options|null $a0_options | ||
* @param WP_Auth0_Options|null $a0_options WP_Auth0_Options instance. | ||
*/ | ||
public function __construct( WP_Auth0_Options $a0_options = null ) { | ||
$this->a0_options = $a0_options; | ||
|
@@ -83,40 +97,56 @@ public function __construct( WP_Auth0_Options $a0_options = null ) { | |
* Get regional inbound IP addresses based on a domain. | ||
* | ||
* @param string $domain - Tenant domain. | ||
* @param string $glue - String used to implode arrays. | ||
* | ||
* @return string | ||
*/ | ||
public function get_ips_by_domain( $domain ) { | ||
return $this->get_ip_by_region( WP_Auth0::get_tenant_region( $domain ) ); | ||
public function get_ips_by_domain( $domain = null, $glue = self::IP_STRING_GLUE ) { | ||
if ( empty( $domain ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default to current domain if none passed in. |
||
$domain = $this->a0_options->get( 'domain' ); | ||
} | ||
$region = WP_Auth0::get_tenant_region( $domain ); | ||
return $this->get_ip_by_region( $region, $glue ); | ||
} | ||
|
||
/** | ||
* Get regional inbound IP addresses based on a region. | ||
* | ||
* @param string $region - Tenant region. | ||
* @param string $glue - String used to implode arrays. | ||
* | ||
* @return string | ||
*/ | ||
public function get_ip_by_region( $region ) { | ||
return implode( ',', $this->valid_webtask_ips[ $region ] ); | ||
public function get_ip_by_region( $region, $glue = self::IP_STRING_GLUE ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allow the glue to be customized. |
||
return implode( $glue, $this->valid_webtask_ips[ $region ] ); | ||
} | ||
|
||
/** | ||
* Get the IP address of the incoming connection. | ||
* | ||
* @return string | ||
*/ | ||
protected function get_request_ip() { | ||
$valid_proxy_ip = $this->a0_options->get( 'valid_proxy_ip' ); | ||
|
||
if ( $valid_proxy_ip ) { | ||
if ( $_SERVER['REMOTE_ADDR'] == $valid_proxy_ip ) { | ||
return $_SERVER['HTTP_X_FORWARDED_FOR']; | ||
} | ||
} else { | ||
return $_SERVER['REMOTE_ADDR']; | ||
} | ||
|
||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unreachable return line. |
||
return $_SERVER['REMOTE_ADDR']; | ||
} | ||
|
||
/** | ||
* Process an array or concatenated string of IP addresses into ranges. | ||
* | ||
* @param array|string $ip_list - IP list to process. | ||
* | ||
* @return array | ||
*/ | ||
protected function process_ip_list( $ip_list ) { | ||
$raw = explode( ',', $ip_list ); | ||
$raw = is_array( $ip_list ) ? $ip_list : explode( self::IP_STRING_GLUE, $ip_list ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allow array and string |
||
|
||
$ranges = array(); | ||
foreach ( $raw as $r ) { | ||
|
@@ -137,21 +167,51 @@ protected function process_ip_list( $ip_list ) { | |
return $ranges; | ||
} | ||
|
||
public function connection_is_valid( $valid_ips ) { | ||
$ip = $this->get_request_ip(); | ||
$valid_ip_ranges = $this->process_ip_list( $valid_ips ); | ||
/** | ||
* Check incoming IP address against default Auth0 and custom ones. | ||
* | ||
* @param string $valid_ips - String of comma-separated IP addresses to allow. | ||
* | ||
* @return bool | ||
*/ | ||
public function connection_is_valid( $valid_ips = '' ) { | ||
$valid_ips = explode( self::IP_STRING_GLUE, $valid_ips ); | ||
$default_ips = explode( self::IP_STRING_GLUE, $this->get_ips_by_domain() ); | ||
$allowed_ips = array_merge( $valid_ips, $default_ips ); | ||
$allowed_ips = array_unique( $allowed_ips ); | ||
|
||
foreach ( $valid_ip_ranges as $range ) { | ||
$in_range = $this->in_range( $ip, $range ); | ||
if ( $in_range ) { | ||
foreach ( $this->process_ip_list( $allowed_ips ) as $range ) { | ||
if ( $this->in_range( $this->get_request_ip(), $range ) ) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only moved and docblocks added, no functionality change. |
||
* Check if an IP address is within a range. | ||
* | ||
* @param string $ip - IP address to check. | ||
* @param array $range - IP range to use. | ||
* | ||
* @return bool | ||
*/ | ||
private function in_range( $ip, array $range ) { | ||
$from = ip2long( $range['from'] ); | ||
$to = ip2long( $range['to'] ); | ||
$ip = ip2long( $ip ); | ||
|
||
return $ip >= $from && $ip <= $to; | ||
} | ||
|
||
// phpcs:disable | ||
|
||
/** | ||
* TODO: Deprecate, not used. Also remove related setting. | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
public function init() { | ||
if ( ! WP_Auth0_Options::Instance()->get( 'ip_range_check' ) || is_admin() ) { | ||
return; | ||
|
@@ -160,6 +220,11 @@ public function init() { | |
add_filter( 'wp_auth0_get_option', array( $this, 'check_activate' ), 10, 2 ); | ||
} | ||
|
||
/** | ||
* TODO: Deprecate, not used. | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
public function check_activate( $val, $key ) { | ||
if ( 'active' !== $key ) { | ||
return $val; | ||
|
@@ -168,6 +233,11 @@ public function check_activate( $val, $key ) { | |
return $is_active; | ||
} | ||
|
||
/** | ||
* TODO: Deprecate, not used. | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
private function validate_ip() { | ||
$ranges = $this->get_ranges(); | ||
$ip = $_SERVER['REMOTE_ADDR']; | ||
|
@@ -182,14 +252,11 @@ private function validate_ip() { | |
return false; | ||
} | ||
|
||
private function in_range( $ip, $range ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved ☝️ |
||
$from = ip2long( $range['from'] ); | ||
$to = ip2long( $range['to'] ); | ||
$ip = ip2long( $ip ); | ||
|
||
return $ip >= $from && $ip <= $to; | ||
} | ||
|
||
/** | ||
* TODO: Deprecate, not used. Also remove related setting. | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
private function get_ranges() { | ||
$data = WP_Auth0_Options::Instance()->get( 'ip_ranges' ); | ||
$data = str_replace( "\r\n", "\n", $data ); | ||
|
@@ -215,4 +282,6 @@ private function get_ranges() { | |
|
||
return $ranges; | ||
} | ||
|
||
// phpcs:enable | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,7 @@ public function init() { | |
'function' => 'render_migration_ws_ips_filter', | ||
), | ||
array( | ||
'name' => __( 'IP Addresses', 'wp-auth0' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary label for this field (see screenshot) |
||
'name' => '', | ||
'opt' => 'migration_ips', | ||
'id' => 'wpa0_migration_ws_ips', | ||
'function' => 'render_migration_ws_ips', | ||
|
@@ -419,10 +419,13 @@ public function render_migration_ws_ips_filter( $args = array() ) { | |
* @see add_settings_field() | ||
*/ | ||
public function render_migration_ws_ips( $args = array() ) { | ||
$ip_check = new WP_Auth0_Ip_Check( WP_Auth0_Options::Instance() ); | ||
$this->render_textarea_field( $args['label_for'], $args['opt_name'] ); | ||
$this->render_field_description( | ||
__( 'Only requests from these IPs will be allowed to access the migration webservice. ', 'wp-auth0' ) . | ||
__( 'Separate multiple IPs with commas', 'wp-auth0' ) | ||
__( 'Only requests from these IPs will be allowed to access the migration endpoints. ', 'wp-auth0' ) . | ||
__( 'Separate multiple IPs with commas. ', 'wp-auth0' ) . | ||
__( 'The following Auth0 IPs are automatically whitelisted: ', 'wp-auth0' ) . | ||
'<br><br><code>' . $ip_check->get_ips_by_domain( null, '</code> <code>' ) . '</code>' | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,7 +231,8 @@ protected function render_radio_buttons( array $buttons, $id, $input_name, $curr | |
* @param string $text - description text to display | ||
*/ | ||
protected function render_field_description( $text ) { | ||
printf( '<div class="subelement"><span class="description">%s.</span></div>', $text ); | ||
$period = ! in_array( $text[ strlen( $text ) - 1 ], array( '.', ':', '>' ) ) ? '.' : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not add a period if the final char is a period, colon, or open angle bracket |
||
printf( '<div class="subelement"><span class="description">%s%s</span></div>', $text, $period ); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This migration is handled in
v20
added below.