-
Notifications
You must be signed in to change notification settings - Fork 808
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
WAF: Add support for handling IP ranges in allow/block lists #29131
Changes from 8 commits
9cff414
2ef1be9
baf29be
b26e38c
f7ce0d0
0fe6efd
1c9f3fb
892c987
c23e71d
677b08d
f4c2215
3edf570
218b293
8a62784
28ff072
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Added a utility function to extract an array of IP addresses from a given string. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,4 +305,45 @@ public function test_ip_address_is_in_range() { | |
$this->assertFalse( Utils::ip_address_is_in_range( $out_range_ip, $range_low, $range_high ) ); | ||
} | ||
|
||
/** | ||
* Test `get_ip_addresses_from_string`. | ||
* Covers IPv4 and IPv6 addresses, including ranges, concatenated with various delimiters. | ||
* | ||
* @covers ::get_ip_addresses_from_string | ||
*/ | ||
public function test_get_ip_addresses_from_string() { | ||
$string = ''; | ||
|
||
$delimiters = array( "\n", ',', ';', ' ' ); | ||
$delimiters_index = 0; | ||
|
||
$ips = array( | ||
// IPv4 | ||
'1.1.1.1', | ||
'2.2.2.2', | ||
'3.3.3.3', | ||
'4.4.4.4', | ||
'5.5.5.5-6.6.6.6', | ||
// IPv6 | ||
'2001:db8::1', | ||
'2001:db8::2', | ||
'2001:db8::3', | ||
'2001:db8::4', | ||
'2001:db8::5-2001:db8::6', | ||
); | ||
|
||
// Generate a string that includes all IPs and delimiters. | ||
foreach ( $ips as $ips_index => $ip ) { | ||
$string .= $ip; | ||
|
||
$is_last_loop = $ips_index === count( $ips ) - 1; | ||
if ( ! $is_last_loop ) { | ||
$string .= $delimiters[ $delimiters_index ]; | ||
$delimiters_index = count( $delimiters ) === $delimiters_index + 1 ? 0 : $delimiters_index + 1; | ||
} | ||
} | ||
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. I'd certainly prefer if our expectation was hardcoded here, because this makes it pretty hard to understand what exactly we are expecting. 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. Thanks for checking this out Kolja! Good point - it is much more understandable by hardcoding it: 677b08d |
||
|
||
$this->assertEquals( $ips, Utils::get_ip_addresses_from_string( $string ) ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Added support for IP ranges in allow and block lists. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Significance: patch | ||
Type: other | ||
Comment: Updated composer.lock. | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Significance: patch | ||
Type: changed | ||
Comment: Updated composer.lock. | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I think here we'll want
&&
over||
to determine that both the starting and ending addresses are valid. Otherwise, I believe things like12.12.12.1-not.a.valid.ip
andnot.a.valid.ip-12.12.12.5
will pass through the filter.I also wonder, during this filtering process, if is there anything else we need to check for as far as inaccuracies go before passing the range as valid. For example, is
$range[0]
in fact lower than, and not equal to$range[1]
?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.
Nice catch, thanks Dean - I've added some additional validation which should cover everything you've mentioned: f4c2215 👍