Problem/Motivation

On systems where Drupal sits behind a reverse proxy you need to specify the proxy IP address (or addresses) in $conf['reverse_proxy_address'] in settings.php, so Drupal can exclude the proxy IPs when trying to return the clients IP address in the ip_address() function.

It is possible to be in a situation where there may be many reverse proxies, or the reverse proxy address is assigned dynamically from a pool, which means you need to specify a large number of IPs in settings.php. For a /16 network range for the proxy, that adds 2.1MB to settings.php.

Drupal 8 solves this by using a symfony component that allows you to specify network ranges, but Drupal 7 does not have support for this. A workaround is to add $_SERVER['REMOTE_ADDR'] to the array of reverse proxies, but it would be nice to explicitly specify a whole network range.

Proposed resolution

Modify the ip_address() function to also handle network ranges for $conf['reverse_proxy_address'].

David Rothstein said he does in principle not oppose this change, as long as it is backwards compatible.

Remaining tasks

User interface changes

None.

API changes

None.

Comments

cafuego’s picture

Issue summary: View changes
cafuego’s picture

Status: Active » Needs review
StatusFileSize
new4.14 KB

Here's the first go at a patch. I've used helper from a module I wrote a few years back and those I took from the comments on the PHP docs for ip2long().

Not sure if I should instead dig out the innards of Symfony and rewrite what they use.

Status: Needs review » Needs work

The last submitted patch, 2: 2466417-reverse-proxy-range-2.patch, failed testing.

cafuego’s picture

Status: Needs work » Needs review
StatusFileSize
new5.26 KB

Check the output of preg_split() to avoid a warning in case there is no /NETMASK part of a proxy entry.

Add two simple network ranges to the BootstrapIPAddressTestCase proxy list.

Status: Needs review » Needs work

The last submitted patch, 4: 2466417-reverse-proxy-range-4.patch, failed testing.

cafuego’s picture

Status: Needs work » Needs review
StatusFileSize
new5.26 KB

Updated call to use explode() and not preg_split(). Do not try to access item[1] even in empty(), but do a count() first.

Status: Needs review » Needs work

The last submitted patch, 6: 2466417-reverse-proxy-range-6.patch, failed testing.

cafuego’s picture

Status: Needs work » Needs review
StatusFileSize
new5.69 KB

Found a different helper that copes with both ipv4 and ipv6, and handles short notation IPv6 addresses.

cafuego’s picture

StatusFileSize
new5.22 KB

... and remove the cruft I'd left in there :-/

cafuego’s picture

StatusFileSize
new5.78 KB

I decided that I like the look of the Symfony code from D8 better, so I rewrote the patch to use its helper functions instead.

yesct’s picture

Issue tags: +Security improvements
hanoii’s picture

Status: Needs review » Needs work
hanoii’s picture

Status: Needs work » Needs review

I think this is very useful and I am about to use it, I just wanted to retry the tests, can't do it from within the test link as those are old. Not sure if this is the way to trigger another test.

hanoii’s picture

StatusFileSize
new5.77 KB

The re-test didn't work, simply re-rolled it so that new tests are run.

Status: Needs review » Needs work

The last submitted patch, 14: 2466417-reverse-proxy-range-14.patch, failed testing.

hanoii’s picture

Status: Needs work » Needs review
hanoii’s picture

Status: Needs review » Reviewed & tested by the community

FWIW, I just tested this and seems to be working awesomely, is it OK TO RTBC?

yesct’s picture

Status: Reviewed & tested by the community » Needs review

@hanoii Thanks for rerolling the patch.

You say you tested it, and wondered about rtbc'ing it. It will help committers and other reviewers if you are specific and write down exactly what you did (and if you did the same on head without the patch, and what differences you noticed)

I think it needs a code review still,
and since there are changes to tests, usually a tests only patch is nice to have on the issue, so we can document the fails and see if they make sense.

hanoii’s picture

ok, in my case I added the patch in order to work with fastly and its IP public list

https://api.fastly.com/public-ip-list

that comes right in CIDR format. with that, drupal was able to consider fastly ip address as trusted properly.

without the patch, there's not much to test as I would have needed to specify IP address individually, so, it will not work unless I added that specific IP.

cafuego’s picture

StatusFileSize
new7.8 KB
new3.09 KB

I've added four test cases to check that the proxy network ranges are used to correctly detect proxies in those ranges for both ipv4 and ipv6.

The last submitted patch, 20: 2466417-reverse-proxy-range-20.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 20: 2466417-reverse-proxy-range-20-testonly-mustfail.patch, failed testing.

mustanggb’s picture

Support for netmasks in reverse_proxy_address would be fantastic, just need to fix those errors.

cafuego’s picture

Yeah, I think the tests are wrong... the patch works fine in production :-)

cafuego’s picture

Status: Needs work » Needs review
StatusFileSize
new8.7 KB
new3.1 KB

Updated the original work, pulled in fixes from the Symfony checkIP4 function and added extra checks to ensure that the ipv4 and ipv6 helpers only check both v4 and v6 IPs and networks respectively.

The last submitted patch, 25: 2466417-reverse-proxy-range-25.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 25: 2466417-reverse-proxy-range-25-testonly-mustfail.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mustanggb’s picture

@cafuego
I found the typo in your patch causing the tests to fail.

  function drupal_check_ip6($ip, $network) {
    ...
-   if (FALSE !== strpos($ip, '/')) {
+   if (FALSE !== strpos($network, '/')) {
      ...
    }
    ...
  }
mustanggb’s picture

Status: Needs work » Needs review
StatusFileSize
new8.67 KB
cafuego’s picture

@MustangGB 👍🏻

wesleymusgrove’s picture

@cafuego @hanoii,

I also have a need to specify Fastly's IPs (https://api.fastly.com/public-ip-list) in the `reverse_proxy_addresses`. It's sounds like this worked well for you?

Is there any known reason why this wasn't merged into core? Is this the approved way of accomplishing this for D7?

Thanks!

mustanggb’s picture

@wesleymusgrove It needs reviewing, see #18 and https://www.drupal.org/patch/review

mustanggb’s picture

Just used this again today, still works like a charm.

anybody’s picture

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.