Fix session fixation tests, kentnl++
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session.pm
index f69b179..d1ab3ac 100644 (file)
@@ -13,7 +13,8 @@ use Carp;
 
 use namespace::clean -except => 'meta';
 
-our $VERSION = '0.24';
+our $VERSION = '0.28';
+$VERSION = eval $VERSION;
 
 my @session_data_accessors; # used in delete_session
 
@@ -35,6 +36,14 @@ __PACKAGE__->mk_accessors(
           /
 );
 
+sub _session_plugin_config {
+    my $c = shift;
+    # FIXME - Start warning once all the state/store modules have also been updated.
+    #$c->log->warn("Deprecated 'session' config key used, please use the key 'Plugin::Session' instead")
+    #    if exists $c->config->{session}
+    #$c->config->{'Plugin::Session'} ||= delete($c->config->{session}) || {};
+    $c->config->{'Plugin::Session'} ||= $c->config->{session} || {};
+}
 
 sub setup {
     my $c = shift;
@@ -65,7 +74,7 @@ sub check_session_plugin_requirements {
 sub setup_session {
     my $c = shift;
 
-    my $cfg = ( $c->config->{session} ||= {} );
+    my $cfg = $c->_session_plugin_config;
 
     %$cfg = (
         expires        => 7200,
@@ -80,14 +89,14 @@ sub setup_session {
 sub prepare_action {
     my $c = shift;
 
-    if (    $c->config->{session}{flash_to_stash}
+    $c->maybe::next::method(@_);
+
+    if (    $c->_session_plugin_config->{flash_to_stash}
         and $c->sessionid
         and my $flash_data = $c->flash )
     {
         @{ $c->stash }{ keys %$flash_data } = values %$flash_data;
     }
-
-    $c->maybe::next::method(@_);
 }
 
 sub finalize_headers {
@@ -168,7 +177,7 @@ sub _save_flash {
                 delete $flash_data->{$key};
             }
         }
-        
+
         my $sid = $c->sessionid;
 
         my $session_data = $c->_session;
@@ -215,7 +224,7 @@ sub _load_session {
             $c->_session($session_data);
 
             no warnings 'uninitialized';    # ne __address
-            if (   $c->config->{session}{verify_address}
+            if (   $c->_session_plugin_config->{verify_address}
                 && $session_data->{__address} ne $c->request->address )
             {
                 $c->log->warn(
@@ -226,7 +235,7 @@ sub _load_session {
                 $c->delete_session("address mismatch");
                 return;
             }
-            if (   $c->config->{session}{verify_user_agent}
+            if (   $c->_session_plugin_config->{verify_user_agent}
                 && $session_data->{__user_agent} ne $c->request->user_agent )
             {
                 $c->log->warn(
@@ -262,7 +271,7 @@ sub _load_flash {
         if ( my $flash_data = $c->_flash )
         {
             $c->_flash_key_hashes({ map { $_ => Object::Signature::signature( \$flash_data->{$_} ) } keys %$flash_data });
-            
+
             return $flash_data;
         }
     }
@@ -294,7 +303,7 @@ sub change_session_id {
     my $sessiondata = $c->session;
     my $oldsid = $c->sessionid;
     my $newsid = $c->create_session_id;
-               
+
     if ($oldsid) {
         $c->log->debug(qq/change_sessid: deleting session data from "$oldsid"/) if $c->debug;
         $c->delete_session_data("${_}:${oldsid}") for qw/session expires flash/;
@@ -303,7 +312,7 @@ sub change_session_id {
     $c->log->debug(qq/change_sessid: storing session data to "$newsid"/) if $c->debug;
     $c->store_session_data( "session:$newsid" => $sessiondata );
 
-    return $newsid; 
+    return $newsid;
 }
 
 sub delete_session {
@@ -353,7 +362,7 @@ sub extend_session_expires {
 
 sub calculate_initial_session_expires {
     my $c = shift;
-    return ( time() + $c->config->{session}{expires} );
+    return ( time() + $c->_session_plugin_config->{expires} );
 }
 
 sub calculate_extended_session_expires {
@@ -363,7 +372,7 @@ sub calculate_extended_session_expires {
 
 sub reset_session_expires {
     my ( $c, $sid ) = @_;
-    
+
     my $exp = $c->calculate_initial_session_expires;
     $c->_session_expires( $exp );
     $c->_extended_session_expires( $exp );
@@ -372,7 +381,7 @@ sub reset_session_expires {
 
 sub sessionid {
     my $c = shift;
-    
+
     return $c->_sessionid || $c->_load_sessionid;
 }
 
@@ -428,7 +437,7 @@ sub keep_flash {
     (@{$href}{@keys}) = ((undef) x @keys);
 }
 
-sub _flash_data { 
+sub _flash_data {
     my $c = shift;
     $c->_flash || $c->_load_flash || do {
         $c->create_session_id_if_needed;
@@ -454,7 +463,7 @@ sub flash {
 
 sub clear_flash {
     my $c = shift;
-    
+
     #$c->delete_session_data("flash:" . $c->sessionid); # should this be in here? or delayed till finalization?
     $c->_flash_key_hashes({});
     $c->_flash_keep_keys({});
@@ -480,12 +489,12 @@ sub initialize_session_data {
             __updated => $now,
 
             (
-                $c->config->{session}{verify_address}
+                $c->_session_plugin_config->{verify_address}
                 ? ( __address => $c->request->address||'' )
                 : ()
             ),
             (
-                $c->config->{session}{verify_user_agent}
+                $c->_session_plugin_config->{verify_user_agent}
                 ? ( __user_agent => $c->request->user_agent||'' )
                 : ()
             ),
@@ -508,7 +517,7 @@ sub create_session_id_if_needed {
 
 sub create_session_id {
     my $c = shift;
-    
+
     my $sid = $c->generate_session_id;
 
     $c->log->debug(qq/Created session "$sid"/) if $c->debug;
@@ -712,7 +721,7 @@ of every request.
         my ( $self, $c ) = @_;
 
         if ( exists $c->flash->{beans} ) { # false
-        
+
         }
     }
 
@@ -756,7 +765,7 @@ expiry time for the whole session).
 
 For example:
 
-    __PACKAGE__->config->{session}{expires} = 1000000000000; # forever
+    __PACKAGE__->config('Plugin::Session' => { expires => 1000000000000 }); # forever
 
     # later
 
@@ -776,7 +785,7 @@ advanced variations of session fixation attack.
 If you want to prevent this session fixation scenario:
 
     0) let us have WebApp with anonymous and authenticated parts
-    1) a hacker goes to vulnerable WebApp and gets a real sessionid, 
+    1) a hacker goes to vulnerable WebApp and gets a real sessionid,
        just by browsing anonymous part of WebApp
     2) the hacker inserts (somehow) this values into a cookie in victim's browser
     3) after the victim logs into WebApp the hacker can enter his/her session
@@ -810,7 +819,7 @@ application.
 
 =item setup_session
 
-This method populates C<< $c->config->{session} >> with the default values
+This method populates C<< $c->config('Plugin::Session') >> with the default values
 listed in L</CONFIGURATION>.
 
 =item prepare_action
@@ -954,12 +963,12 @@ the store.
 
 =head1 CONFIGURATION
 
-    $c->config->{session} = {
+    $c->config('Plugin::Session' => {
         expires => 1234,
-    };
+    });
 
 All configuation parameters are provided in a hash reference under the
-C<session> key in the configuration hash.
+C<Plugin::Session> key in the configuration hash.
 
 =over 4
 
@@ -978,7 +987,7 @@ Defaults to false.
 =item verify_user_agent
 
 When true, C<<$c->request->user_agent>> will be checked at prepare time. If it
-is not the same as the user agent that initiated the session, the session is 
+is not the same as the user agent that initiated the session, the session is
 deleted.
 
 Defaults to false.
@@ -1093,6 +1102,12 @@ Tomas Doran (t0m) C<bobtfish@bobtfish.net> (current maintainer)
 
 Sergio Salvi
 
+kmx C<kmx@volny.cz>
+
+Florian Ragwitz (rafl) C<rafl@debian.org>
+
+Kent Fredric (kentnl)
+
 And countless other contributers from #catalyst. Thanks guys!
 
 =head1 COPYRIGHT & LICENSE