since php5 there are two new settings:

session.hash_function integer
session.hash_function allows you to specify the hash algorithm used to generate the session IDs. '0' means MD5 (128 bits) and '1' means SHA-1 (160 bits).

Note: This was introduced in PHP 5.

session.hash_bits_per_character integer
session.hash_bits_per_character allows you to define how many bits are stored in each character when converting the binary hash data to something readable. The possible values are '4' (0-9, a-f), '5' (0-9, a-v), and '6' (0-9, a-z, A-Z, "-", ",").

Note: This was introduced in PHP 5.

however, if one sets the hash functions to SHA-1 the session id is 40 characters long. the database field for the sid is only 32 characters long, so the sid is choped and users get duplicate session id errors every page hit and aren't able to login. So drupal becomes completely unusable!

session.hash_bits_per_character can't be set lower than 4. In combination with SHA-1 hashing the sid is as long as possible: 40 characters.

so I propose to change the database field to VARCHAR(40)

I'm not attaching a patch, because I'm not so familiar with postgreSQL, however for mysql this statement worked for me:

ALTER TABLE `sessions` CHANGE `sid` `sid` VARCHAR( 40 )

note: this issue affects all drupal versions!

Comments

beginner’s picture

Version: 4.7.3 » x.y.z
Category: bug » feature
Priority: Critical » Normal
fago’s picture

Category: feature » bug
Priority: Normal » Critical

imho that's a bug report!

AjK’s picture

Status: Active » Needs review
StatusFileSize
new579 bytes

The other solution to this is to set :-

ini_set('session.hash_bits_per_character', 5);

in the settings.php file. If SHA-1 is used the 160bits are then fitted into 32characters. I tested this and it works fine on PHP5.1.4 and doesn't require a database change.

regards,
--AjK

killes@www.drop.org’s picture

Maybe we should add a comment?

AjK’s picture

StatusFileSize
new1.04 KB

We could add a comment to make it more clear. The main problem is that PHP's default bits per char is 4. Might have made more sense to make it 5 but hey, what more can you say.

I've attached a system.install patch here also which changes the VARCHAR from 32 to 40 (as noted above) if this is the prefered method/solution.

I wasn't sure if this alteration required an update_ as it seems to me that if the problem exists then an old site just won't be working anyway! So, only possible future versions really need 40 chars.

But a retrospective change by a site admin would render the login system dead so adding a note is a good idea.

regards,
--AjK

webchick’s picture

Status: Needs review » Needs work

There should still be an update for this. I might have a 4.7 site and not use this functionality yet, but I may in the future when I upgrade to 5.0, for example.

AjK’s picture

having spoke with killes on IRC it seems (so far) the prefered fix is to set the hash_bits_per_character correctly as per my first patch (and with some added comments in there regarding this issue?)

I guuess this one is "for the powers that be" to decide on the proper end solution.

regards,
--AjK

webchick’s picture

Ok, in that case, patch still needs comments saying why you're setting the bits to 5. :)

chx’s picture

How reasonable would be to document session.hash_bits_per_character 5 necessity if sha1 is used and not bother with code? What happens if you set an ini setting which does not exist in the relevant php version?

AjK’s picture

Status: Needs work » Needs review
StatusFileSize
new581 bytes

Sorry, yet another patch to apply the "fix" in a more suitable way.

This patch adds the two PHP INI settings to the .htaccess file as there exists in there a way to set these depending upon the PHP version (the PHP version test already exists in .htaccess).

There's little in the way of comments in .htaccess so if this needs documenting then I'd be happy to add a doc page in the handbook that explains what these two settings do and implications of altering them (and how to alter them safely).

regards,
--AjK

fago’s picture

I don't like using mod-php to set php-values. What is with users using php-cgi, php-fcgi or su-php?
In my opinion everything that could be done with ini_set() should be done with ini_set().

@chx: I think it will return FALSE. So there should be no problem.

dries’s picture

Status: Needs review » Needs work

The best solution is to grow the size of the database field. .htaccess solutions are limited to Apache and not portable to other web servers. We should have a good index/key on the field so it shouldn't really matter for performance (I think).

dopry’s picture

Status: Needs work » Needs review
StatusFileSize
new1.49 KB

Here is a patch which updates system_install and adds system_update_1013. I tested the query for the mysql update manually. I tested a clean install of HEAD on mysql 4.1. The pgsql should be examined by someone who can vouch for it. I can still login and my sessions seems to work fine.

Kjartan’s picture

Why not just set the field to VARCHAR(255) so we don't need to update again should an even longer session id pop up. Storage wise it really doesn't matter in the MySQL world at least.

http://dev.mysql.com/doc/refman/5.0/en/char.html#char

Also since the sid key is the primary key I think it defaults to using the first 96 chars for the index. My bet is that the index file will be just as large, if not bigger, as the data file with the current indexes.

beginner’s picture

Status: Needs review » Reviewed & tested by the community

The patch above is exactly was was originally asked for.
I tested on mysql 4 and it works as expected.
postgresql untested.

If it is felt that the field should be increased to 255 chars, the patch can easily be updated (even manually) to reflect this.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

beginner’s picture

FYI: patch committed has varchar(64)
http://drupal.org/cvs?commit=41667

fago’s picture

Version: x.y.z » 4.7.3
Status: Fixed » Reviewed & tested by the community

great. It would be nice to have this fixed for 4.7 too.

killes@www.drop.org’s picture

Status: Reviewed & tested by the community » Needs work

To add this to 4.7 I'd need a patch that adds the correct db update number. However, I am not really in favour of adding db updates to maintenance releases and I dont consider this to be really critical.

chx’s picture

Status: Needs work » Closed (won't fix)

if you are savvy enough to set sha1 then you are savvy enoguh to raise session.hash_bits_per_character integer too. While it's OK in HEAD because no pain (and imo no gain but no lose either) it's not important to disturb the peace of 4.7.

fago’s picture

yeah, I am. But what if some hosters decide to set sha1 session ids as default setting? then all drupal 4.7 installations are going to die...

chx’s picture

No, not all. Only at that host. You are free to complain at the host and if there is no act, vote with your feet.

fago’s picture

yeah, of course I meant "only" the installations on these hosts..

Zen’s picture

Status: Closed (won't fix) » Needs work

The HEAD patch misses the sid field in the access_log table [statistics module].

IMO, omitting this patch for 4.7 is very negligent. I will try to submit a patch for it soon.

Cheers,
-K

Zen’s picture

Version: 4.7.3 » x.y.z

Correction, the committed patch:

  1. does not address the sid in the accesslog table.
  2. removes the NOT NULL and DEFAULT attributes of these fields.
  3. does not use db_change_column for the pgSQL updates. [Unsure if this has changed in HEAD].

I will submit a backport for 4.7 once this has been sorted. Changing version to CVS.

Thanks,
-K

webchick’s picture

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

Try this...

Note: I did not add another update to correct the mistake in the previous update because we do not support HEAD -> HEAD updates.

webchick’s picture

StatusFileSize
new2.87 KB

That capitalized VARCHAR was annoying me. ;)

dries’s picture

Status: Needs review » Fixed

Committed to CVS HEAD. Thanks.

Zen’s picture

Title: drupal won't work with sha1 session ids » Drupal won't work with sha1 session ids
Version: x.y.z » 4.7.4
Priority: Critical » Normal
Status: Fixed » Active

What update number should I use for a backport?

Thanks
-K

webchick’s picture

Looks like 183.

webchick’s picture

Version: 4.7.4 » 5.x-dev
Status: Active » Fixed

Actually, for 4.7.x there is http://drupal.org/node/64049 which was posted before this one. So that's a better place for discussion. Marking this bug as 5.x-dev and fixed.

Anonymous’s picture

Status: Fixed » Closed (fixed)