Skip to content
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

Merged
merged 15 commits into from
Feb 27, 2023

Conversation

nateweller
Copy link
Contributor

@nateweller nateweller commented Feb 23, 2023

This PR introduces support for IP ranges in the firewall allow and block lists.

Ranges can be denoted by a dash - between two IP addresses. For example, 1.2.3.4-5.6.7.8 or 2001:0db8:85a3:0000:0000:0000:0000:0001-2001:0db8:85a3:0000:0000:0000:0000:9999

Proposed changes:

  • Remove Waf_Runner::ip_option_to_array() in favor of a new IP\Utils::get_ip_addresses_from_string() method, which takes the existing ip_option_to_array() code and adds support for IP ranges (i.e. "1.1.1.1-2.2.2.2").
  • Adds new IP\Utils::validate_ip_range() function to ensure the given IP addresses for a range are valid and ordered correctly.
  • Update Waf_Runtime::is_ip_in_array() to support IP ranges.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

#28633 (comment)

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  • Add an IP range to your WAF IP block list, which includes your IP address i.e. "1.1.1.1-255.255.255.255".
  • Validate that the rules file at wp-content/jetpack-waf/rules/block-ip.php now includes the range in $waf_block_list.
  • Validate that your IP address is blocked from accessing the site.

@nateweller nateweller requested a review from a team February 23, 2023 18:13
@nateweller nateweller self-assigned this Feb 23, 2023
@nateweller nateweller added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] In Progress [Package] WAF [Package] IP labels Feb 23, 2023
@nateweller nateweller added this to the protect/1.3.0 milestone Feb 23, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Feb 23, 2023

Are you an Automattician? You can now test your Pull Request on WordPress.com. On your sandbox, run

bin/jetpack-downloader test jetpack add/waf-ip-ranges

to get started. More details: p9dueE-5Nn-p2

@github-actions
Copy link
Contributor

github-actions bot commented Feb 23, 2023

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Jetpack plugin:

  • Next scheduled release: March 7, 2023.
  • Scheduled code freeze: February 28, 2023.

Protect plugin:

  • Next scheduled release: March 7, 2023.
  • Scheduled code freeze: February 27, 2023.

@github-actions github-actions bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. labels Feb 24, 2023
Copy link
Member

@ArSn ArSn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only flew over this and have this one comment. Someone should certainly do a deeper review.

Comment on lines 336 to 344
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;
}
}
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

// Validate both IP values from the range
$range = explode( '-', $ip );
if ( count( $range ) === 2 ) {
if ( filter_var( $range[0], FILTER_VALIDATE_IP ) !== false || filter_var( $range[1], FILTER_VALIDATE_IP ) !== false ) {
Copy link
Contributor

@dkmyta dkmyta Feb 24, 2023

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 like 12.12.12.1-not.a.valid.ip and not.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]?

Copy link
Contributor Author

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 👍

@nateweller nateweller marked this pull request as ready for review February 24, 2023 18:54
@nateweller nateweller requested a review from dkmyta February 24, 2023 18:54
Copy link
Contributor

@dkmyta dkmyta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, and works great! Covers all the bases - nice work! 👍🏻

@nateweller nateweller added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Team Review labels Feb 24, 2023
@nateweller
Copy link
Contributor Author

Looks great, and works great! Covers all the bases - nice work! 👍🏻

Thanks Dean! I'm now just trying to figure out why the continuous-integration/a8c-teamcity build is failing. It looks like the failures in D102785-code seem unrelated...

Copy link
Contributor

@sdixon194 sdixon194 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for me 👍 The wpcom teamcity failures are unrelated. More discussion here.

@nateweller nateweller merged commit 8fa35bc into trunk Feb 27, 2023
@nateweller nateweller deleted the add/waf-ip-ranges branch February 27, 2023 22:12
@nateweller
Copy link
Contributor Author

Thanks all!

@github-actions github-actions bot removed the [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. label Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] IP [Package] WAF [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Status] Needs Test Review [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants