Hi,

it is me again. I've thought that with beta6 we've resolved the issue, introduced through beta5 (I can not confirm that 100%, maybe it was the same also before, but working for me by chance, as described below - in any case, that's when I've noticed it for the first time...).

What I can confirm now for -beta6 and for the last -dev version (2009-May-02) is, that there is definitely still a problem with persistent logins in my case - I have 2 web sites: www.domain.ext and new.domain.ext (tested with Firefox 3.0.10).

If I clean all persistent logins and erase all cookies for both sites (Firefox CookieSafe extension shows cookies for new.domain.ext and for domain.ext, by the way - maybe it matters somehow...), there is no problem to login first into new.domain.ext with "remember me". I can safely close the tab or Firefox, and when I am back, I am properly recognized. I can login after that also into www.domain.ext, and it is properly remembered well (if "remember me" is checked, of course).

But, as soon there is some cookie for domain.ext present (it seems those are saved by www.domain.ext site...), it messes up everything. I can login into new.domain.ext also with "remember me", no problem. I can also close the tab, as long as I don't close the browser, I am recognized when reopen new.domain.ext properly. But, as soon as I close Firefox, I am not remembered for new.domain.com anymore. I see the record in the database, I see a "permanent login" cookie for new.domain.ext, but alltogether somehow simply doesn't work...

It may be also Firefox (or some of its extensions), or even Drupal itself - I would definitely prefer to see cookies for www.domain.ext and for new.domain.ext, which would probably work better, but for the moment there are only cookies for domain.ext and new.domain.ext (and, it seems in some circumstances cookies for domain.ext are considered instead of new.domain.ext...).

In any case, it is weird that if I permanently login for new.domain.ext first, it seems to work well after, so probably something could be done within premanent login, even if the source of the issue is elsewhere (always properly distinguishing domain.ext and somethingelse.domain.ext cookies...).

If it matters, I have 2 separate Drupal installations, one for www.domain.ext and another one for new.domain.ext (2 separate folders and 2 separate databases, but at the same Apache / MySQL server; that enables me to test everything and simply copy it to a production site after, almost without a risk to crash the whole production site...).

And yes, I have a setting in Drupal for www.domain.ext to use the whole site address for a cookie (exactly the same setting that for new.domain.ext site produces a complete new.doamin.ext cookie...). I don't know what is causing a "www" part to be skipped, but "new" to be kept (used), but I guess this is where to start digging...

Comments

markus_petrux’s picture

hmm... the 'www.' part of the cookie domain seems to be dropped in Drupal bootstrap.inc

  // Strip leading periods, www., and port numbers from cookie domain.
  $cookie_domain = ltrim($cookie_domain, '.');
  if (strpos($cookie_domain, 'www.') === 0) {
    $cookie_domain = substr($cookie_domain, 4);
  }

I didn't noticed that before. Look like a bug, or in need of more documentation because it says what it does but not why.

So... what can we do? ...hmm... what if the PL cookie name is derived from the Drupal session name? Then it would be different for each site, so there would be no conflicts of this kind?

PS: Before the changes I introduced to PL affecting all this stuff was because the domain cookie was not specified, and then the browser takes the domain in the URL, and that makes impossible to share PL cookies, and that needed a solution. Maybe little by little we can find one that works in all situations.

markus_petrux’s picture

Title: Persistant logins not working for different sites within the same domain » Problems when cookie domains are www.example.com and subdomain.example.com (drupal removes www.)
markus_petrux’s picture

Status: Active » Needs review

How about using a name for the PL cookie that's derived from the session cookie?

Could you please try the following change to persisten_login.module, function _persistent_login_get_cookie_name()?

-    $cookie_name = 'PERSISTENT_LOGIN_'. md5($GLOBALS['base_path']);
+    $cookie_name = 'PERSISTENT_LOGIN_'. substr(session_name(), 4);

This approach should ensure a different cookie name for PL is used for each different Drupal session.

markus_petrux’s picture

For reference, I created a bug report to the Drupal issues queue: #458704: Don't automatically remove "www." from admin-set cookie domains

markus_petrux’s picture

Unless Drupal defines a different cookie_domain for www.example.com and example.com. I think the mini-patch will still not work, since the same PL cookie name will be generated.

Here' another approach, but it will fail as well, because the path to settings.php is probably the same for www.example.com and example.com. :(

-    $cookie_name = 'PERSISTENT_LOGIN_'. md5($GLOBALS['base_path']);
+    $cookie_name = 'PERSISTENT_LOGIN_'. md5(conf_path());

So... how can we generate a different cookie name for each separate site? I think it should be based on something unique to each site that Drupal has already elaborated, which is sanitized, etc. That's the cookie_domain, but Drupal strips "www." from there, so... what else?

luti’s picture

Can we simply redeclare a function messing with $cookie_domain? I believe Drupal supports some custom function declarations (the same name, just with module name included in the function name or so...), but don't know much about that.

Or, can we simply set a $cookie_domain variable so that it is different from URL / path and it doesn't begin with "www."? Would it work if I set it for example to "default.example.com" (or whatever that doesn't exist as another site)? I can not just try, as it is a production site, and I really wouldn't like to mess too much...

markus_petrux’s picture

Status: Needs review » Needs work

Nope. We cannot use anything for the cookie domain, as it is subject to security issues. We can only use the host name in the URL or a domain which is part of that.

Before the change I made to PL, the cookie domain was not explicitly specified when creating the PL cookie, and then it defaults to the whole host name in the URL. But, that has the problem that a site cannot share the PL cookie. And I wanted to resolve this.

What I did was using the same exact cookie domain as the one used for the Drupal session, which is one that a site can customize from settings.php. And it can be set to share the cookies between several subdomains or not. You have choices.

The problem is that Drupal blindly removes the "www." prefix for a cookie domain. A fact that I missed. :( And that's the cause of your problem. Hence I opened that bug to the Drupal issues queue. I'm afraid, though, that this issue will end up lost in the queue, because the current Drupal behavior does not affect the typical usage, and it's a complex thing to change.

We cannot alter the cookie domain used by Drupal. There's no hook that allows us to undo the "www." removal. So now I think the only way for sites with a use case like yours, is to have the ability to define the PL cookies as it was before, with no explicit domain. If it's an option, then we have a choice again.

Now, if I implement this as an option (Something like "Domain for PL cookie: a) Same as session cookies, b) Use host name in URL"), then it should be done in a way that the cookie name itself is affected by this option. Otherwise, if the cookie name does not change when this option is changed, it could cause problems due to visibility of the cookies and the fact that PHP cannot see the same cookie for different scopes ($_COOKIE array keys are just cookie names).

Makes sense?

luti’s picture

As much as I am able to understand your feedback, it should be OK (for my case). But, I am not very familiar with this subject, so maybe I am not the most appropriate person to provide the evaluation. In any case, if you can implement both options, it would be a step ahead, as we can at least test it.

markus_petrux’s picture

@anyone having this issue: Could you please test the change in #3, and try the patch in the Drupal issue mentioned in #4?

luti’s picture

I've applied both patches, #3 (to the latest 6.x-1.x-dev version from 2009-Jul-20) and #4. As much as I've tested up to now, everything seems to work fine.

markus_petrux’s picture

Status: Needs work » Fixed

Thanks for the feedback. I have committed the patch in #3 to CVS.

@all: Please, try out the patch to Drupal core and give feedback to that issue. Hopefully this encourages core maintainers validate the patch. See: #458704: Don't automatically remove "www." from admin-set cookie domains

Status: Fixed » Closed (fixed)

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

luti’s picture

Status: Closed (fixed) » Active

Sorry, but I have to reopen this issue - I don't know if it has any influence to the behavior, but I am noticing occasional losses of my saved premanent login sessions (it happened those days again).

For example, on Thursday afternoon I've successfully logged in from home (Firefox 3) and on Friday morning from work (Firefox 3.5). Friday evening, I've tried from home, without success. I've manually logged in (with checkbox "Remember me"), and checked the number of remembered sessions - it was only 1 (the one I've just created). So, both previous sessions were obviously lost in between. In the past, it happened that only one was lost, or both (I see no pattern in this behavior).

Is anyone else noticing the (partial or general) loss of remembered sessions, or is it just me?

markus_petrux’s picture

Have you applied the patch to Drupal after upgrading Drupal core?

For reference: #458704: Don't automatically remove "www." from admin-set cookie domains

If that patch works for you, then please bump that issue in the Drupal queue. No one else seems to care about this. :(

markus_petrux’s picture

Status: Active » Postponed (maintainer needs more info)

If it is not missing that patch, then I don't know where else to look. This is working ok for us.

luti’s picture

It is working for me as well (most of the time). But, as said, occasionally 1 or both saved sessions (I have 2 sessions saved, at work and at home...) are lost, without any action or pattern I am able to recognize... It is interesting that it is mainly happening with www. site and not with the other (test) site (I am almost sure it never happened with that one, at least up to now). And, I run cron jobs on the the test site manually, while at www. site they are automatically performed each hour. Could it be that something within cron jobs is messing with sessions so that saved sessions are somehow discarded (or lost) after?

Could this information be of any help?

markus_petrux’s picture

Re: "!Could it be that something within cron jobs is messing with sessions so that saved sessions are somehow discarded (or lost) after?"

I guess it depends on what these cron jobs are doing. Try scanning your modules for implementations of hook_cron(), any function like MODULE_cron(), and see what they're doing.

Re: "Could this information be of any help?"

Not too much. We need to identify where are the cookies being removed, then see why and what we can do to prevent it.

luti’s picture

Huh,

the problem has happened 2 or 3 times now in about half a year (or even more...). When it happens it is already too late, before everything is OK.

I'll try to see if I can find something interesting somewhere...

luti’s picture

I've found an option to enable persistent_login_history data to be saved. I've enabled it on Tuesday, December 15th (15:40 local time) and I have the following data logged since then:

uid series        token        expires at                                              why
persistent_login_history:
1    <s_str_1>   <t_str_1>   0          15.12.2009 (Tuesday) 19:05:46       used
1    <s_str_1>   <t_str_2>   0          15.12.2009 (Tuesday) 23:42:47       used
1    <s_str_1>   <t_str_3>   0          16.12.2009 (Wednesday) 23:10:00   used
1    <s_str_2>   <t_str_4>   0          21.12.2009 (Monday) 07:48:56        used
1    <s_str_2>   <t_str_5>   0          21.12.2009 (Monday) 07:48:57        stolen
1    <s_str_3>   <t_str_6>   0          21.12.2009 (Monday) 07:48:57        stolen
1    <s_str_4>   <t_str_7>   0          24.12.2009 (Thursday) 15:58:31      used
1    <s_str_4>   <t_str_8>   0          26.12.2009 (Saturday) 11:38:14      used

persistent_login:
1    <s_str_4>   <t_str_9>   0

I've replaced strings with str_n to indicate, which ones are the same... I've also replaced date/time values with actual local dates and times. Three entries (07:48) are from work PC, while all others are from my home PC (I have 2 permanent logins saved generally). Right now, there is only 1 permanent login saved (home PC), as I haven't been at work since the last "crash" yet. All connections are made from my PCs, Firefox 3.5, from home mainly as a new browser session, at work as a saved session (windows) as Firefox enables if more than 1 tab is used...

I was connected to my site every working day (Monday - Friday) in working hours, so it is very interesting that there are days with and days without entries. From home, it is almost the same (can not tell precisely, but there are maybe a couple of days I haven't connect from home at all only...). It seems it is definitely not related to cron jobs though.

I've been kicked out at "stolen", of course (both persistent logins erased...).

Can those data help anyhow?

I have to point out also that none of the strings saved at the moment (<s_str_4> or <t_str_9>) match current strings within cookie names for my site (PERSISTENT_LOGIN_<cur_str> and SES<cur_str>) as I can see them in Firefox...

markus_petrux’s picture

Nope, these are different strings. It's ok that they don't match with what you see in the browser cookie names.

Still, no idea what might be causing this. PL is working like a charm here. I think browsers may loose cookies sometimes. cookie storage is not 100% reliable, so maybe your issue is related to that. Without a method to reproduce the issue, I'm short on ideas.

luti’s picture

What exactly means a record in persistent_login_history, marked as used?

It seems strange to me that they are added without any common logic. I am accessing my site every working day from work (in the morning, immediately at 8:00 to see if everything is OK) and often in the afternoon (or, evening) from home. Since my previous post, there are just 4 new records (all with and marked as "used"). I can not recall anything different for those dates (comparing to the others, when nothing is recorded...).

Maybe it would help to introduce some additional debug logging, to get more information about what is going on? If you know what exactly you'd need to get details about, can you suggest the code (I can patch my module for this purpose...)?

markus_petrux’s picture

"used" means PL has logged you in using the PL cookie, which is the first time after a while you visit the site and see the "Welcome back" message, if enabled.
"stolen" means PL has detected an invalid PL cookie.

hmm... I found (yesterday) that the part of the code in PL that detects a stolen cookie had a bug, making it impossible to log an additional watchdog() error, which is something that could help analyze what happens when "stolen" PL cookie is detected. I would suggest trying out the patch here: #621916: PHP notice before security alert.

luti’s picture

I've installed 6.x-1.4, and as I see this patch is already included there.

I've just enabled logging (debugging), so let's see what will be recorded in next days.

Thank you for all your efforts, markus.

markus_petrux’s picture

Well, the patch for Drupal core was applied to the D7 branch. It is now waiting to be backported/applied to Drupal 6 as well. If anyone is interested in this, please provide feedback on the following issue: #458704: Don't automatically remove "www." from admin-set cookie domains Thanks!

luti’s picture

Done

gapple’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

It sounds like this issue should have been resolved with patches to core, and a correct cookie domain configuration.