If domain access and Drupal core's page caching are both turned on, then pages that have been precached by anonymous users will be displayed to logged in users, giving the appearance of being logged out on some pages.

Steps to reproduce:

1. Enable domain access and anonymous page caching.
2. Add the domain bootstrap to the settings.php file.
3. 'Warm' the page cache by visiting a page as a logged out, anonymous user.
4. In another browser, already logged in, visit the same page.
5. Observe that you now appear logged out.

This is the output of curl. The first shows the headers as an anonymous user, and the second shows the output the page as a logged in user immediately after. The ETag is different, which suggests this hasn't been pulled from the page cache and as well the X-Drupal-Cache header is missing, but the Cache-Control headers show drupal thinks this page is public and cacheable (which it should never do if a user is logged in).

Torrances-Macbook:~ torrance$ curl -I sub.server.com/content/resourcesHTTP/1.1 200 OK
Date: Thu, 16 Jun 2011 00:50:07 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.6-6~dotdeb.1
X-Drupal-Cache: HIT
Etag: "1308185230-0"
Link: </node/1210>; rel="shortlink",</content/resources>; rel="canonical"
X-Generator: Drupal 7 (http://drupal.org)
Cache-Control: public, max-age=0
Last-Modified: Thu, 16 Jun 2011 00:47:10 +0000
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Vary: Cookie,Accept-Encoding
Content-Type: text/html; charset=utf-8
Via: 1.1 bc3
Connection: Keep-Alive

Torrances-Macbook:~ torrance$ curl -I -b SESS3f0fd158b45beefaee9114d4bfc01f8c=Psg6tQM8tXOuhE_smtinI3amoRYoG_pOLsvBuSUIebg sub.server.com/content/resources
HTTP/1.1 200 OK
Date: Thu, 16 Jun 2011 00:50:22 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.6-6~dotdeb.1
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Thu, 16 Jun 2011 00:50:22 +0000
Cache-Control: public, max-age=0
ETag: "1308185422-0"
Link: </node/1210>; rel="shortlink",</content/resources>; rel="canonical"
X-Generator: Drupal 7 (http://drupal.org)
Vary: Cookie,Accept-Encoding
Content-Type: text/html; charset=utf-8
Via: 1.1 bc3
Connection: Keep-Alive

Removing the bootstrap includes line in settings.php stops this from happening.

Comments

torrance123’s picture

It may be necessary to log out and log back in once both domain access and page caching have been enabled, before you can reproduce the problem.

torrance123’s picture

This is the output of curl, first as an anonymous user and secondly as a logged in user, with the bootstrap file not included in settings.php. The cache-control header is the noticeable difference:


Torrances-Macbook:~ torrance$ curl -I sub.server.com/
HTTP/1.1 200 OK
Date: Thu, 16 Jun 2011 01:29:10 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.6-6~dotdeb.1
X-Drupal-Cache: HIT
Etag: "1308185878-0"
X-Generator: Drupal 7 (http://drupal.org)
Cache-Control: public, max-age=0
Last-Modified: Thu, 16 Jun 2011 00:57:58 +0000
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Vary: Cookie,Accept-Encoding
Content-Type: text/html; charset=utf-8
Via: 1.1 bc3
Connection: Keep-Alive

Torrances-Macbook:~ torrance$ curl -I -b SESS3f0fd158b45beefaee9114d4bfc01f8c=hEXj3pG4m51eJMCDMez1-N-20veUqnj1E_Nm_ZYzUoI sub.server.com/
HTTP/1.1 200 OK
Date: Thu, 16 Jun 2011 01:29:16 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.6-6~dotdeb.1
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Thu, 16 Jun 2011 01:29:16 +0000
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
ETag: "1308187756"
X-Generator: Drupal 7 (http://drupal.org)
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
Via: 1.1 bc3
Connection: Keep-Alive
andros’s picture

I can confirm that

agentrickard’s picture

Status: Active » Postponed (maintainer needs more info)

Which version of Drupal? If 7.2, I'm going to scream, since this has worked fine since Drupal 5.

Are you using core caching or an external mode (like varnish)?

What is in {cache_page}, especially, what cache_ids?

agentrickard’s picture

This came up before -- see #1046844: domain_bootstrap breaks page cache -- and so if that behavior has changed...

agentrickard’s picture

I cannot replicate this report in the browser.

agentrickard’s picture

@andros

Did you confirm this error when running curl?

If not, what browsers? I'm testing on FireFox 4 and Chrome 12 (Max OS 10.6) with no issues.

agentrickard’s picture

As logged in (user 1), with a valid matching page in cache.

Firefox: user 1

Cache-Control:no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:text/html; charset=utf-8
Date:Fri, 17 Jun 2011 19:15:11 GMT
Etag:"1308338111"
Expires:Sun, 19 Nov 1978 05:00:00 GMT
Keep-Alive:timeout=15, max=99
Last-Modified:Fri, 17 Jun 2011 19:15:11 +0000
Server:Apache/2.0.63 (Unix) PHP/5.3.2 DAV/2
Transfer-Encoding:chunked
X-Generator:Drupal 7 (http://drupal.org)
X-Powered-By:PHP/5.3.2


Chrome: user 2
Cache-Control:no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:text/html; charset=utf-8
Date:Fri, 17 Jun 2011 19:17:10 GMT
ETag:"1308338230"
Expires:Sun, 19 Nov 1978 05:00:00 GMT
Keep-Alive:timeout=15, max=100
Last-Modified:Fri, 17 Jun 2011 19:17:10 +0000
Server:Apache/2.0.63 (Unix) PHP/5.3.2 DAV/2
Transfer-Encoding:chunked
X-Drupal-Cache:MISS
X-Generator:Drupal 7 (http://drupal.org)
X-Powered-By:PHP/5.3.2

Chrome: logged out

Cache-Control:public, max-age=0
Connection:Keep-Alive
Date:Fri, 17 Jun 2011 19:18:17 GMT
Etag:"1308337690-1"
Keep-Alive:timeout=15, max=99
Server:Apache/2.0.63 (Unix) PHP/5.3.2 DAV/2

torrance123’s picture

It's drupal 7.2 with core caching. We had varnish in front of it, but when we encountered these logouts, that was the first thing to go. The problem still persists with a bare apache+mod_php installation.

However, this bug may be really hard to track down as it's not easily reproducible. I haven't been able to reliably set up one page to be logged out through warming the cache - the page on which one is logged out is quite random. But once you come across one, you can navigate away from it and back, and you'll still be logged out *just* on that page.

I ran tcpdump to collect the response headers whilst using Chrome logged in. The first set of headers below show Chrome loading a page properly - displaying me as logged in, and the second you can see I've been returned a 304 based on Chrome including an If-None-Match header and the page is displaying me as logged out.

HTTP/1.1.200.OK
Date:.Sat,.18.Jun.2011.13:26:28.GMT
Server:.Apache/2.2.16.(Debian)..X-Powered-By:.PHP/5.3.6-6~dotdeb.1..
X-Drupal-Cache:.MISS
Expires:.Sun,.19.Nov.1978.05:00:00.GMT..
Last-Modified:.Sat,.18.Jun.2011.13:26:28.+0000..
Cache-Control:.no-cache,.must-revalidate,.post-check=0,.pre-check=0..
ETag:."1308403588"..
Link:.</node/1212>;.rel="shortlink",</about-us>;.rel="canonical"..
X-Generator:.Drupal.7.(http://drupal.org)..
Vary:.Accept-Encoding..
Content-Encoding:.gzip..
Content-Length:.7934..
Keep-Alive:.timeout=30,.max=17..
Connection:.Keep-Alive..
Content-Type:.text/html;.charset=utf-8

HTTP/1.1.304.
Not.Modified..
Date:.Sat,.18.Jun.2011.13:28:34.GMT..
Server:.Apache/2.2.16.(Debian)..
Connection:.Keep-Alive..
Keep-Alive:.timeout=30,.max=18..
Etag:."1308401399-0"..
Cache-Control:.public,.max-age=0..
Vary:.Accept-Encoding.
torrance123’s picture

Oh! Right, so I've got this slightly wrong. Steps to reproduce:

1. Visit site with domain and page caching enabled. Click through a few links and prewarm your *browser's* cache.
2. Login.
3. Re-visit those pages that you previously visited and you'll appear logged out.
4. Visit other pages (eg. /user ) that you didn't visit before you logged in and you'll appear logged in.

I've tested this on both Chrome and Safari. So it seems to be that the browser is correctly caching the pages when logged out, but that once logged in drupal is simply returning 304s in response to these browsers sending If-None-Match headers and browser is therefore hitting its own cache.

agentrickard’s picture

Status: Postponed (maintainer needs more info) » Active

OK, just replicated.

/me curses Drupal 7.2

agentrickard’s picture

Priority: Major » Critical

Bumping.

The steps in #10 work in Chrome to replicate the bug.

agentrickard’s picture

Title: Domain breaks core anonymous page caching » Login rejected on cached pages

More accurate title.

agentrickard’s picture

Note that this does not appear to happen to user 1.

EDIT: This is actually browser-specific. FireFox 4 doesn't have this problem.

agentrickard’s picture

AND it doesn't happen in FireFox. But it does in Safari and Chrome.

agentrickard’s picture

Commenting out drupal_settings_initialize() in domain.bootstrap.inc solves this problem, but probably causes others.

At least that points us to the right place.

agentrickard’s picture

Status: Active » Needs review
StatusFileSize
new2.5 KB

Patch for 7.x.2.

This does what I tried to avoid in the first place -- replicates core logic to ensure that $base_url is set properly. Seems to work. Porting up to 7.x.3 for testing.

agentrickard’s picture

StatusFileSize
new2.49 KB

Patch for 7.x.3, and tests still pass.

torrance123’s picture

Status: Needs review » Needs work

Hi there - it's great that you've been able to reproduce the bug!

However, I'm still able to reliably reproduce this bug with the patch applied, even after clearing both drupal's and the browser's cache.

andros’s picture

@agentrickard
I can confirm the behavior for my pages. user1 and some other rolls like editors have this problem all the time. mainly we work with Firefox 4, and it happens to me all the time with Firefox 4 too. I can login and see the admin menu but as i visit some other page it seems the i am not login anymore. when i clear the complete drupal cache all went fine and i am logged in on all pages. but when someone visit a page anonymously it happens again to me on this page.

agentrickard’s picture

Odd. I would swear it worked.

agentrickard’s picture

Status: Needs work » Needs review
StatusFileSize
new4.71 KB

OK, another patch. Essentially, we have two choices:

-- Duplicate most of the logic from drupal_initialize_settings().

-- Use a cache handler insert instead.

I don't like either option, but the first requires less work by the site, and doesn't require docs changes.

agentrickard’s picture

StatusFileSize
new4.71 KB

Patch for 7.x.3.

Note that if you manually set $cookie_domain in settings.php, that line MUST go above the include.

agentrickard’s picture

StatusFileSize
new8.05 KB

And a 7.x.3 version with a test!

torrance123’s picture

Status: Needs review » Needs work

Thanks agentrickard for your perseverance in this issue, but I am the deliverer of bad news once again: the patch in #22 on drupal 7.2 still doesn't work, and in Chrome, Safari and Firefox included. I can still reliably reproduce the bug.

agentrickard’s picture

Status: Needs work » Postponed (maintainer needs more info)

FFS.

Then you need to do some work and tell me what's going on. I have WORKING TESTS in the 7.x.3 branch.

agentrickard’s picture

Works fine for me on all browsers. Logged in users stay logged in. Anon users are served cached pages.

What does your settings.php look like?

What URLs are you hitting?

What $cookie_domain (if any) are you setting?

Are you setting $base_url manually? (Don't)

agentrickard’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new8.6 KB

And an even better test patch for 7.x.3, which passes.

agentrickard’s picture

StatusFileSize
new7.55 KB

LiveHTTP Headers (FireFox) output for test user. Works as designed.

If you say it doesn't, I need lots of detail. (See #27)

torrance123’s picture

StatusFileSize
new18.41 KB
new2.23 MB

See attached for a screencast documenting the issue as well as our settings.php file (for the $cookie_domain and $base_url settings).

torrance123’s picture

And just to be perfectly clear, by commenting out the bootstrap include line in the settings.php file I am unable to reproduce this bug.

torrance123’s picture

StatusFileSize
new9.37 KB

This is liveheaders output on Firefox.

This involved: front page -> front page (again) -> /user/login -> (login redirect) -> /user/torrance -> front page (appear logged out) -> /news (appear logged in).

I cleared both my browser cache and the drupal cache before running this.

torrance123’s picture

Fuck, should've really cleaned those liveheaders. *goes away to change passwords...*

(Why can't I delete attachments?)

agentrickard’s picture

That's really odd and exactly the reverse of what my test shows.

The patch applied cleanly?

Are you doing anything special with www.* handling at the Domain configuration or Apache / mod_rewrite / .htaccess level?

agentrickard’s picture

OK, the only difference is that I have these lines above the include in settings.php. If I remove them, logins break:

// Set cookie domain.
if (substr_count($_SERVER['HTTP_HOST'], 'myexample') < 1) {
  $cookie_domain = '.example.com';
}

So we need to check $cookie_domain handling.

agentrickard’s picture

StatusFileSize
new5.06 KB

Looks like a session name mismatch caused by the logic loop. This patch should work whether $cookie_domain is set manually or not.

See the notes at line 360:

  // We have set $cookie_domain, so we must match $session_name to it, since
  // that's what will happen inside drupal_settings_initialize() after we run.
  // Essentially, we short-circuit the IF routine when this copied code runs
  // after our routine.
  $session_name = $cookie_domain;

Porting up to 7.x.3 for testing.

agentrickard’s picture

StatusFileSize
new5.04 KB

Ooops. left in a stray debug statement.

torrance123’s picture

Status: Needs review » Reviewed & tested by the community

Wicked, patch #37 works like a charm. Thanks very much agentrickard!

agentrickard’s picture

The problem was that you aren't setting $cookie_domain to allow shared login -- I always do that first, and I suspect most people do. That explains why this issue wasn't discovered earlier.

agentrickard’s picture

Note that this issue does not affect Drupal 6.

agentrickard’s picture

StatusFileSize
new10.32 KB

And we made the cache tests work, too.

agentrickard’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks, @torrance123!

andros’s picture

when will this get in some stable state, or is there a release coming in version 2.x?

mefisto75’s picture

subscribing to the question above

agentrickard’s picture

It's ready for inclusion in 7.x.2 and 7.x.3, pending some reviews of outstanding issues.

http://drupal.org/project/issues/domain?text=&status=8&priorities=All&ca...

Status: Fixed » Closed (fixed)

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

camsoft2000’s picture

We are having the same issue with 7.x-2.13 (stable) and tried applying the patch but it failed to patch. We tried to update our site to 7.x-3.x-dev but it completely broke the site and failed to upgrade the database. We've managed to restore the site and reverted to 7.x-2.13

Any chance of this patch being ported to 7.x-2.13, I would be forever in your debt?

At the moment we either have to disable caching on the sites which slows the sites down big time and has been causing us server loads problems or the editors can't edit the site properly as the edit options are gone due to cached pages being loaded.

EDIT: Just realised that there was a 7.x-2.x patch in the above comments. My bad. Sorry.

pieterdt’s picture

Hi, i hope somebody still reads this, certainly because i'm probably not in the right place:

I have the exact same behaviour as mentioned in this ticket, although I'm not using the domain access module.
We have configured caching with Varnish, but this results also in browser caching.

The browser cache shows pages from before the login when they are revisited after the login.

Has someone an idea where i need to start looking?

ktrev’s picture

Version: 7.x-2.x-dev » 7.x-3.10
Category: bug » support
Priority: Critical » Normal
Status: Closed (fixed) » Active

I am using 7.x3.10 version and i still have this problem.
My settings.php contains the line
$cookie_domain = '.mysite.in';
I think I am missing something.This patch is already added in this version.I am using drupal 7.22 version.I am using Boost to cache anonymous user page
Please help

agentrickard’s picture

Well, that sounds like a Boost problem, frankly.

agentrickard’s picture

You may also be having a cross-domain login issue. See https://drupal.org/node/1197684 for background.

ktrev’s picture

Yes ,That is a boost problem.I disabled boost for now.. Will post this issue in boost module

ktrev’s picture

Issue summary: View changes
Status: Active » Fixed

Status: Fixed » Closed (fixed)

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