I was writing a simpletest test for a project in Drupal 7 that used varnish as a front end proxy. On my development environment, the entire technology stack runs in a single server. So when simpletest ran, the client request and the upstream proxy were both 127.0.0.1.
This is a problem the way that the ip_address function is written because it simple does an array_diff between the proxy IPs and the XFF headers then pops the first result off the result and returns it as the new IP address. In the scenario described above, array_diff will return an empty array so ip_address will return null.
A null ip address will also prevent session from being saved and so, my simpletests were failing.
I've attached a 7.x and 8.x patch that fixes this problem. Basically it will revert to the original IP address if no ip address can be found in the $untrusted variable.
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | ip_address-1327728-24.patch | 2.21 KB | darren oh |
Comments
Comment #1
xjmNote that the Drupal 8.x patch will need to be rerolled, because the core directory structure for Drupal 8 has now changed. (For more information, see #22336: Move all core Drupal files under a /core folder to improve usability and upgrades). When the patch has been rerolled, please set the issue back to "Needs Review."
Tagging as novice for the task of rerolling the Drupal 8.x patch.
If you need help rerolling this patch, you can come to core office hours or ask in #drupal-gitsupport on IRC.
Comment #2
josh waihi commentedDone and done :)
Comment #3
xjmThanks. :)
Comment #4
danepowell commentedI am also using Varnish on D7, and found that Simpletests were frequently and sporadically failing, mostly due to 403 errors. There seemed to be some correlation to how long Varnish had been running, indicating an issue there.
The D7 patch above seems to have solved the problem. thanks!
Comment #5
danepowell commentedNevermind... still having the same errors, I don't think they are related to this patch...
Comment #6
c960657 commentedIf $untrusted is empty, wouldn't it be better to use the first element in $forwarded rather than just $ip_address?
Assume that you have two proxies, p1 and p2, and you make a request from p1 via p2 to the web server. In this case I think that ip_address() should return the IP address of p1. With you patch it returns the IP address of p2.
Comment #7
David_Rothstein commentedIn addition to simpletests, another thing that can fail as a result of this is any runtime code that calls drupal_http_request() to make a request between two sites on the same server.
That's where I ran into it (with Deploy, where the drupal_http_request() call uses Services to log in to the site and therefore runs directly into the session saving problem described above too).
The above patch seemed to work fine, at least for a simple setup (i.e. with only one reverse proxy); for more complicated setups it seems like #6 might indeed be an issue.
Comment #8
David_Rothstein commentedAdding the backport tag.
Comment #9
cspitzlay7.x-ip_address.patch queued for re-testing.
Comment #10
cspitzlayUploaded Josh Waihi's original D7 patch with different name. I hope this matches the naming convention for D7 now ...
Comment #12
cspitzlayOh, it doesn't work that way. In any case the original D7 patch still applies cleanly for me locally.
Re-queueing the latest D8 patch then.
Sorry for the noise.
Comment #13
cspitzlay#2: 8.x-ip_address_reroll.patch queued for re-testing.
Comment #14
cspitzlayI agree with #6. This could indeed be an issue. I attached a new D8 version of a fix.
Comment #15
socialnicheguru commented#10 patch solved an issue I had in D7.
https://drupal.org/node/1985612#comment-7596197
Comment #18
mgiffordWhat happened to function ip_address()?
Is this still an issue in D8? If not back to D7.
Comment #19
sgdev commentedCame here from @SocialNicheGuru's post on another thread.
Thank you for posting the patch in #10. It solved a Drupal 7 SQL error we would occasionally get from httpBL (https://www.drupal.org/project/httpbl) when running with Varnish:
Comment #20
darren ohIn Drupal 8, this is handled correctly by Symfony. The attached patch addresses #1327728-6: ip_address() fails when client request IP and proxy IP are the same for Drupal 7.
Comment #21
sgdev commented@Darren Oh, we've added patch #20 to our code. I'll let you know if we notice any issues after it has run for a bit. Thanks.
Comment #22
ParisLiakos commented#20 works for me as well..i guess we need a test for it though
Comment #23
cmanalansan commentedMy name is Christian and I am currently checking this out.
Comment #24
darren ohAdded patch with test.
Comment #25
darren ohComment #26
ParisLiakos commentedthanks! can we have a test only patch to illustrate the failure?
Comment #27
darren ohWhy not?
Comment #28
darren ohComment #29
darren ohComment #31
ParisLiakos commentedthanks! #24 looks good to go then
Comment #32
fabianx commentedMarked for commit.
Comment #34
ParisLiakos commented#24 is the patch to commit
Comment #35
David_Rothstein commentedCommitted to 7.x - thanks!
Might be worth a followup to see if those tests should be ported to Drupal 8 also.
Comment #37
David_Rothstein commentedI created #2759995: Port tests to Drupal 8 for IP address detection when client request IP address and proxy IP address are the same as a child issue to look into porting the tests to Drupal 8.