Skip to content

Commit 7b8c9ab

Browse files
committed
Use Crypt::SysRandom to generate session ids
This replaces convoluted code for generating session ids with a simple call to retrieve random bytes from the system source of randomness.
1 parent eae4a54 commit 7b8c9ab

File tree

1 file changed

+12
-72
lines changed

1 file changed

+12
-72
lines changed

lib/Catalyst/Plugin/Session.pm

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Moose;
44
with 'MooseX::Emulate::Class::Accessor::Fast';
55
use MRO::Compat;
66
use Catalyst::Exception ();
7-
use Digest ();
7+
use Crypt::SysRandom ();
88
use overload ();
99
use Object::Signature ();
1010
use HTML::Entities ();
@@ -598,11 +598,7 @@ sub initialize_session_data {
598598
}
599599

600600
sub generate_session_id {
601-
my $c = shift;
602-
603-
my $digest = $c->_find_digest();
604-
$digest->add( $c->session_hash_seed() );
605-
return $digest->hexdigest;
601+
return unpack( "H*", Crypt::SysRandom::random_bytes(16) );
606602
}
607603

608604
sub create_session_id_if_needed {
@@ -624,33 +620,10 @@ sub create_session_id {
624620
return $sid;
625621
}
626622

627-
my $counter;
628-
629-
sub session_hash_seed {
630-
my $c = shift;
631-
632-
return join( "", ++$counter, time, rand, $$, {}, overload::StrVal($c), );
633-
}
623+
sub session_hash_seed { }
634624

635625
my $usable;
636626

637-
sub _find_digest () {
638-
unless ($usable) {
639-
foreach my $alg (qw/SHA-1 SHA-256 MD5/) {
640-
if ( eval { Digest->new($alg) } ) {
641-
$usable = $alg;
642-
last;
643-
}
644-
}
645-
Catalyst::Exception->throw(
646-
"Could not find a suitable Digest module. Please install "
647-
. "Digest::SHA1, Digest::SHA, or Digest::MD5" )
648-
unless $usable;
649-
}
650-
651-
return Digest->new($usable);
652-
}
653-
654627
sub dump_these {
655628
my $c = shift;
656629

@@ -973,53 +946,22 @@ insensitive hexadecimal characters.
973946
974947
=item generate_session_id
975948
976-
This method will return a string that can be used as a session ID. It is
977-
supposed to be a reasonably random string with enough bits to prevent
978-
collision. It basically takes C<session_hash_seed> and hashes it using SHA-1,
979-
MD5 or SHA-256, depending on the availability of these modules.
980-
981-
=item session_hash_seed
982-
983-
This method is actually rather internal to generate_session_id, but should be
984-
overridable in case you want to provide more random data.
949+
This method will return a string that can be used as a session ID.
985950
986-
Currently it returns a concatenated string which contains:
951+
The string is simply 32 hex digits from the system randomness source using L<Crypt::SysRandom>.
987952
988-
=over 4
989-
990-
=item * A counter
991-
992-
=item * The current time
993-
994-
=item * One value from C<rand>.
995-
996-
=item * The stringified value of a newly allocated hash reference
997-
998-
=item * The stringified value of the Catalyst context object
999-
1000-
=back
953+
You can override this if you prefer to use a different source of randomness or different format of session ids:
1001954
1002-
in the hopes that those combined values are entropic enough for most uses. If
1003-
this is not the case you can replace C<session_hash_seed> with e.g.
1004-
1005-
sub session_hash_seed {
1006-
open my $fh, "<", "/dev/random";
1007-
read $fh, my $bytes, 20;
1008-
close $fh;
1009-
return $bytes;
1010-
}
1011-
1012-
Or even more directly, replace C<generate_session_id>:
955+
use Crypt::URandom::Token ();
1013956
1014957
sub generate_session_id {
1015-
open my $fh, "<", "/dev/random";
1016-
read $fh, my $bytes, 20;
1017-
close $fh;
1018-
return unpack("H*", $bytes);
958+
state $tok = Crypt::URandom::Token->new();
959+
return $tok->get;
1019960
}
1020961
1021-
Also have a look at L<Crypt::Random> and the various openssl bindings - these
1022-
modules provide APIs for cryptographically secure random data.
962+
=item session_hash_seed
963+
964+
This method is no longer used.
1023965
1024966
=item finalize_session
1025967
@@ -1249,5 +1191,3 @@ Robert Rothenberg <[email protected]> (on behalf of Foxtons Ltd.)
12491191
it and/or modify it under the same terms as Perl itself.
12501192
12511193
=cut
1252-
1253-

0 commit comments

Comments
 (0)