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

IP and WAF: Add CIDR support #39425

Merged
merged 10 commits into from
Sep 20, 2024
Merged

IP and WAF: Add CIDR support #39425

merged 10 commits into from
Sep 20, 2024

Conversation

nateweller
Copy link
Contributor

@nateweller nateweller commented Sep 17, 2024

Proposed changes:

  • Adds support to the ip package for handling CIDR ranges in all utility functions.
  • Adds support to the waf package for handling CIDR ranges in the block and allow lists.

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

https://github.com/Automattic/jetpack-scan-team/issues/1419

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

No

Testing instructions:

  • Review and run unit tests added in this PR.
  • Test the firewall and brute force protection features:
    • Test using both IPv4 and IPv6 addresses, with ranges using both hyphens and CIDR notation. See unit test for examples.
    • Test the firewall's block list.
    • Test that your IP address can be blocked by brute force protection.
    • Test the firewall's allow list for both the automatic firewall rules and brute force protection.

Easily testing CIDR notation: If your IP address is 127.0.0.1, use 127.0.0.0/24 as a range that translates to 127.0.0.*.

@nateweller nateweller requested a review from a team September 17, 2024 23:51
@nateweller nateweller self-assigned this Sep 17, 2024
Copy link
Contributor

github-actions bot commented Sep 17, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the add/ip/cidr branch.

    • For jetpack-mu-wpcom changes, also add define( 'JETPACK_MU_WPCOM_LOAD_VIA_BETA_PLUGIN', true ); to your wp-config.php file.
  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack add/ip/cidr
    
    bin/jetpack-downloader test jetpack-mu-wpcom-plugin add/ip/cidr
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

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.
  • ✅ 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.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!

@nateweller nateweller force-pushed the add/ip/cidr branch 2 times, most recently from 492f8a0 to 53c39c6 Compare September 18, 2024 20:42
@nateweller nateweller marked this pull request as ready for review September 18, 2024 21:28
@nateweller nateweller changed the title IP: Add CIDR support IP and WAF: Add CIDR support Sep 18, 2024
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.

Other than that, as you know, I am not a big fan of having static calls everywhere due to the non-mockability, but it sure works.

Comment on lines 236 to 246
// Validate prefix length.
if ( $ip_version === 'ipv4' ) {
// For IPv4, the prefix length must be an integer between 0 and 32.
if ( ! ctype_digit( $prefix ) || $prefix < 0 || $prefix > 32 ) {
return false;
}
} elseif ( $ip_version === 'ipv6' ) {
// For IPv6, the prefix length must be an integer between 0 and 128.
if ( ! ctype_digit( $prefix ) || $prefix < 0 || $prefix > 128 ) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be replaced with a call to self::validate_netmask() and then return false if it comes back as false? This would also enable us to put the ctype_digit() check into that function and not have to check it individually beforehand (if we like to)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've gone ahead and used self::validate_netmask() here to avoid the code duplication! 👍

I do kind of like enforcing the numeric netmask up front in the parsing step, so that the parsing function deals with extracting it from the string and ensuring it is cast to the correct type right away, and the validation function only needs to validate the value of the netmask (no passing in strings to validate, that we will always want to use as integers anyways, if the validation passes).

No opinions strongly held though!

@nateweller nateweller force-pushed the add/ip/cidr branch 2 times, most recently from e718dae to ebbe743 Compare September 20, 2024 17:54
@nateweller
Copy link
Contributor Author

Thank you @ArSn for the time reviewing and testing this PR!

Other than that, as you know, I am not a big fan of having static calls everywhere due to the non-mockability, but it sure works.

I don't disagree, although I like the convenience of providing static methods from a utility-type library like this, so that consumers can essentially just opt-in to whatever methods they want to use like quickly getting or validating an IP address.

Though it would be cool to interface with the package via classes that represent the IP lists, or individual IP addresses, i.e. new IP_List( array( ... ) ) or new IP_Address( ... ) and then work with methods off of the class instances.

@nateweller nateweller merged commit 836a715 into trunk Sep 20, 2024
73 checks passed
@nateweller nateweller deleted the add/ip/cidr branch September 20, 2024 20:31
gogdzl pushed a commit that referenced this pull request Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants