Merge verify_user_agent branch
Tomas Doran [Tue, 16 Jun 2009 19:34:51 +0000 (19:34 +0000)]
Changes
lib/Catalyst/Plugin/Session.pm
t/01_setup.t
t/lib/SessionTestApp.pm

diff --git a/Changes b/Changes
index 891b2c8..d69c5c7 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,8 +1,9 @@
 Revision history for Perl extension Catalyst::Plugin::Session
 
-        - Add a test case to prove that logging in with a session cookie still causes
-          a new cookie to be issued for you, proving that the code is not vulnerable
-          to a session fixation attack.
+        - Add the verify_user_agent config parameter (kmx)
+        - Add a test case to prove that logging in with a session cookie still 
+          causes a new cookie to be issued for you, proving that the code is
+          not vulnerable to a session fixation attack. (t0m)
 
 0.22 2009-05-13
         - INSANE HACK to ensure B::Hooks::EndOfScope inlines us a new method right now
index b038778..4fdb4f2 100644 (file)
@@ -70,6 +70,7 @@ sub setup_session {
     %$cfg = (
         expires        => 7200,
         verify_address => 0,
+        verify_user_agent => 0,
         %$cfg,
     );
 
@@ -225,6 +226,17 @@ sub _load_session {
                 $c->delete_session("address mismatch");
                 return;
             }
+            if (   $c->config->{session}{verify_user_agent}
+                && $session_data->{__user_agent} ne $c->request->user_agent )
+            {
+                $c->log->warn(
+                        "Deleting session $sid due to user agent mismatch ("
+                      . $session_data->{__user_agent} . " != "
+                      . $c->request->user_agent . ")"
+                );
+                $c->delete_session("user agent mismatch");
+                return;
+            }
 
             $c->log->debug(qq/Restored session "$sid"/) if $c->debug;
             $c->_session_data_sig( Object::Signature::signature($session_data) ) if $session_data;
@@ -454,6 +466,11 @@ sub initialize_session_data {
                 ? ( __address => $c->request->address )
                 : ()
             ),
+            (
+                $c->config->{session}{verify_user_agent}
+                ? ( __user_agent => $c->request->user_agent )
+                : ()
+            ),
         }
     );
 }
@@ -915,6 +932,14 @@ not the same as the address that initiated the session, the session is deleted.
 
 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 
+deleted.
+
+Defaults to false.
+
 =item flash_to_stash
 
 This option makes it easier to have actions behave the same whether they were
@@ -947,6 +972,11 @@ The time when the session was first created.
 The value of C<< $c->request->address >> at the time the session was created.
 This value is only populated if C<verify_address> is true in the configuration.
 
+=item __user_agent
+
+The value of C<< $c->request->user_agent>> at the time the session was created.
+This value is only populated if C<verify_user_agent> is true in the configuration.
+
 =back
 
 =head1 CAVEATS
index f23ffa5..7116db2 100644 (file)
@@ -63,7 +63,7 @@ ok( !$log->called("fatal"), "no fatal error logged either" );
 
 cmp_deeply(
     [ keys %{ $config{session} } ],
-    bag(qw/expires verify_address/),
+    bag(qw/expires verify_address verify_user_agent/),
     "default values for config were populated in successful setup",
 );
 
index cdd181a..c7258d9 100644 (file)
@@ -6,6 +6,11 @@ use Catalyst qw/Session Session::Store::Dummy Session::State::Cookie/;
 use strict;
 use warnings;
 
+__PACKAGE__->config->{session} = {
+    # needed for live_verify_user_agent.t; should be harmless for other tests 
+    verify_user_agent => 1,  
+};
+
 sub login : Global {
     my ( $self, $c ) = @_;
     $c->session;
@@ -30,6 +35,11 @@ sub page : Global {
     }
 }
 
+sub user_agent : Global {
+    my ( $self, $c ) = @_;
+    $c->res->output('UA=' . $c->req->user_agent);
+}
+
 __PACKAGE__->setup;
 
 __PACKAGE__;