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.

Comments

xjm’s picture

Status: Needs review » Needs work
Issue tags: +Novice

Note 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.

josh waihi’s picture

Status: Needs work » Needs review
StatusFileSize
new515 bytes

Done and done :)

xjm’s picture

Issue tags: -Novice

Thanks. :)

danepowell’s picture

I 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!

danepowell’s picture

Nevermind... still having the same errors, I don't think they are related to this patch...

c960657’s picture

If $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.

David_Rothstein’s picture

In 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.

David_Rothstein’s picture

Issue tags: +Needs backport to D7

Adding the backport tag.

cspitzlay’s picture

7.x-ip_address.patch queued for re-testing.

cspitzlay’s picture

StatusFileSize
new673 bytes

Uploaded Josh Waihi's original D7 patch with different name. I hope this matches the naming convention for D7 now ...

Status: Needs review » Needs work

The last submitted patch, ip_address-1327728-D7.patch, failed testing.

cspitzlay’s picture

Oh, 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.

cspitzlay’s picture

Status: Needs work » Needs review

#2: 8.x-ip_address_reroll.patch queued for re-testing.

cspitzlay’s picture

I agree with #6. This could indeed be an issue. I attached a new D8 version of a fix.

socialnicheguru’s picture

#10 patch solved an issue I had in D7.
https://drupal.org/node/1985612#comment-7596197

Status: Needs review » Needs work

The last submitted patch, 14: always-return-ip-address-1327728-14.patch, failed testing.

mgifford’s picture

Issue summary: View changes

What happened to function ip_address()?

Is this still an issue in D8? If not back to D7.

sgdev’s picture

Came 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:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'hostname' cannot be null: INSERT INTO {httpbl} (hostname, status, expire) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => 0 [:db_insert_placeholder_2] => 1435337201 ) in _httpbl_cache_set() (line 607 of /sites/all/modules/httpbl/httpbl.module).
darren oh’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.19 KB

In 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.

sgdev’s picture

@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.

ParisLiakos’s picture

Status: Needs review » Needs work
Issue tags: -Needs backport to D7 +Needs tests

#20 works for me as well..i guess we need a test for it though

cmanalansan’s picture

Issue tags: +neworleans2016

My name is Christian and I am currently checking this out.

darren oh’s picture

Status: Needs work » Needs review
StatusFileSize
new2.21 KB

Added patch with test.

darren oh’s picture

Issue tags: -Needs tests
ParisLiakos’s picture

thanks! can we have a test only patch to illustrate the failure?

darren oh’s picture

StatusFileSize
new976 bytes

Why not?

darren oh’s picture

darren oh’s picture

Status: Needs review » Needs work

The last submitted patch, 27: 1327728-ip_address-test.patch, failed testing.

ParisLiakos’s picture

Status: Needs work » Reviewed & tested by the community

thanks! #24 looks good to go then

fabianx’s picture

Issue tags: +Pending Drupal 7 commit

Marked for commit.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 27: 1327728-ip_address-test.patch, failed testing.

ParisLiakos’s picture

Status: Needs work » Reviewed & tested by the community

#24 is the patch to commit

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Pending Drupal 7 commit +7.50 release notes

Committed to 7.x - thanks!

Might be worth a followup to see if those tests should be ported to Drupal 8 also.

  • David_Rothstein committed bc48446 on 7.x
    Issue #1327728 by Darren Oh, Josh Waihi, cspitzlay, c960657: ip_address...
David_Rothstein’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.