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.
| Comment | File | Size | Author |
|---|---|---|---|
| #29 | drupal-2466417-29.patch | 8.67 KB | mustanggb |
Comments
Comment #1
cafuego commentedComment #2
cafuego commentedHere'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.
Comment #4
cafuego commentedCheck 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.
Comment #6
cafuego commentedUpdated call to use explode() and not preg_split(). Do not try to access item[1] even in empty(), but do a count() first.
Comment #8
cafuego commentedFound a different helper that copes with both ipv4 and ipv6, and handles short notation IPv6 addresses.
Comment #9
cafuego commented... and remove the cruft I'd left in there :-/
Comment #10
cafuego commentedI decided that I like the look of the Symfony code from D8 better, so I rewrote the patch to use its helper functions instead.
Comment #11
yesct commentedComment #12
hanoiiComment #13
hanoiiI 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.
Comment #14
hanoiiThe re-test didn't work, simply re-rolled it so that new tests are run.
Comment #16
hanoiiComment #17
hanoiiFWIW, I just tested this and seems to be working awesomely, is it OK TO RTBC?
Comment #18
yesct commented@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.
Comment #19
hanoiiok, 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.
Comment #20
cafuego commentedI'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.
Comment #23
mustanggb commentedSupport for netmasks in reverse_proxy_address would be fantastic, just need to fix those errors.
Comment #24
cafuego commentedYeah, I think the tests are wrong... the patch works fine in production :-)
Comment #25
cafuego commentedUpdated 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.
Comment #28
mustanggb commented@cafuego
I found the typo in your patch causing the tests to fail.
Comment #29
mustanggb commentedComment #30
cafuego commented@MustangGB 👍🏻
Comment #31
wesleymusgrove commented@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!
Comment #32
mustanggb commented@wesleymusgrove It needs reviewing, see #18 and https://www.drupal.org/patch/review
Comment #33
mustanggb commentedJust used this again today, still works like a charm.
Comment #34
anybodyDrupal 8 / 9 issue: #3017957: Improve documentation for (unused) trusted proxy headers