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

Add CIDR range functionality for SS_TRUSTED_PROXY_IPS #5737

Closed
wants to merge 1 commit into from
Closed

Add CIDR range functionality for SS_TRUSTED_PROXY_IPS #5737

wants to merge 1 commit into from

Conversation

brettt89
Copy link
Contributor

@brettt89 brettt89 commented Jun 26, 2016

To do before merge

  • Refactor logic into ip_in_any_range($ipAddress, $rangeList)
  • Add a unit test for ip_in_any_range() and/or ip_in_range()
  • Ensure exact-matches on ipv6 addresses (e.g. ::1) will work.

Nice to have

  • Parsing of IPv6 subnet ranges

@helpfulrobot
Copy link

@brettt89, thanks for your PR! By analyzing the blame information on this pull request, I identified @hafriedlander, @chillu and @halkyon to be potential reviewers

@hafriedlander
Copy link
Contributor

I think we need to reverse this and have an "is this IP in this CIDR range" function - otherwise a big enough range (like 10.0.0.0/8) would end up creating an enormous list.

@tractorcow
Copy link
Contributor

Can you please update the documentation? Please see https://github.com/silverstripe/silverstripe-framework/blob/master/docs/en/02_Developer_Guides/09_Security/04_Secure_Coding.md#request-hostname-forgery for the current docs links.

Some phpdoc on the new method would be great too thanks. :)

@tractorcow
Copy link
Contributor

Also, +1 on what @hafriedlander said; Make it a check rather than an exhaustive allowed value generation. :P

@dhensby
Copy link
Contributor

dhensby commented Jun 29, 2016

Lot's of examples online how to check IP is in a CIDR declaration (here's one: https://gist.github.com/tott/7684443)

@brettt89
Copy link
Contributor Author

Have made changes as per comments.

* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If there's no slash, can't we do a basic comparison?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @dhensby , What do you mean by Basic Comparison? If there is no slash, it adds /32 onto the end (converts it to CIDR).

Copy link
Contributor

Choose a reason for hiding this comment

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

If there's no slash, then it's not a range, it's a single IP, right? so we can do something like: $ip == $range

Copy link
Member

Choose a reason for hiding this comment

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

Doing a straight comparison will mean that ::1 can be added.

@dhensby
Copy link
Contributor

dhensby commented Jul 2, 2016

My only question now is how we deal with ipv6? Can we get some unit tests, too?

@tractorcow
Copy link
Contributor

According to googling, there is an option for ipv6.

http://stackoverflow.com/questions/7951061/matching-ipv6-address-to-a-cidr-subnet

@brettt89
Copy link
Contributor Author

Yeah, now I just need to figure out how to tie them both together.... Nicely.

@brettt89
Copy link
Contributor Author

@dhensby Where abouts would you put tests for constants?
@tractorcow I have been failing to try and combine the ipv4 and ipv6 check into a single function, do you have any suggestions as to how we could achieve this?

@dhensby
Copy link
Contributor

dhensby commented Jul 18, 2016

@brettt89 put them in seperate functions and then call the relevant one depending on whether the IP has : in it?

As for tests - I don't think we have any method for it :)

@@ -104,7 +123,12 @@ function stripslashes_recursively(&$array) {
if(SS_TRUSTED_PROXY_IPS === '*') {
$trusted = true;
} elseif(isset($_SERVER['REMOTE_ADDR'])) {
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 you should refactor this to an ip_in_any_range() function, then you can add tests of that function.

if(SS_TRUSTED_PROXY_IPS === '*') {
  $trusted = true;
} elseif(isset($_SERVER['REMOTE_ADDR'])) {
  $trusted = ip_in_any_range($_SERVER['REMOTE_ADDR'], SS_TRUSTED_PROXY_IPS);
}

Ensure there is a test for exact match on ::1. Handling of IPv6 subnets wouldn't block merge but it might be nice.

@sminnee
Copy link
Member

sminnee commented Jul 25, 2016

I've added some tasks to the top of the PR listing merge blockers and nice to haves.

@sminnee
Copy link
Member

sminnee commented Sep 23, 2016

I believe this is superceded by #6062. If @andrewandante and @dhensby can confirm that is the case, can you close this?

@dhensby dhensby closed this Sep 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants