[Catalyst-commits] r8425 - in trunk/Catalyst-Plugin-Session-Store-DBIC: . lib/Catalyst/Plugin/Session/Store t t/lib/TestApp/Model/DBIC t/lib/TestApp/Schema

jhannah at dev.catalyst.perl.org jhannah at dev.catalyst.perl.org
Wed Sep 17 01:22:59 BST 2008


Author: jhannah
Date: 2008-09-17 01:22:59 +0100 (Wed, 17 Sep 2008)
New Revision: 8425

Modified:
   trunk/Catalyst-Plugin-Session-Store-DBIC/Changes
   trunk/Catalyst-Plugin-Session-Store-DBIC/lib/Catalyst/Plugin/Session/Store/DBIC.pm
   trunk/Catalyst-Plugin-Session-Store-DBIC/t/04dbic.t
   trunk/Catalyst-Plugin-Session-Store-DBIC/t/05dbic-schema.t
   trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Model/DBIC/Session.pm
   trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Schema/Session.pm
Log:
Code was silently truncating storage to MySQL, rendering the session
unreadable. Patched to check DBIx::Class size from column_info (if
available).


Modified: trunk/Catalyst-Plugin-Session-Store-DBIC/Changes
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBIC/Changes	2008-09-16 20:08:39 UTC (rev 8424)
+++ trunk/Catalyst-Plugin-Session-Store-DBIC/Changes	2008-09-17 00:22:59 UTC (rev 8425)
@@ -1,5 +1,10 @@
 Revision history for Catalyst-Plugin-Session-Store-DBIC
 
+0.07  Tue Sep 16 19:14:52 CDT 2008
+        - Code was silently truncating storage to MySQL, rendering the 
+          session unreadable. Patched to check DBIx::Class size from 
+          column_info (if available).
+
 0.06  Sat Sep 16 15:42:50 EDT 2006
         - Convert to Catalyst::Plugin::Session::Store::Delegate (with
           help from nothingmuch)

Modified: trunk/Catalyst-Plugin-Session-Store-DBIC/lib/Catalyst/Plugin/Session/Store/DBIC.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBIC/lib/Catalyst/Plugin/Session/Store/DBIC.pm	2008-09-16 20:08:39 UTC (rev 8424)
+++ trunk/Catalyst-Plugin-Session-Store-DBIC/lib/Catalyst/Plugin/Session/Store/DBIC.pm	2008-09-17 00:22:59 UTC (rev 8425)
@@ -9,7 +9,7 @@
 use NEXT;
 use Storable ();
 
-our $VERSION = '0.06';
+our $VERSION = '0.07';
 
 =head1 NAME
 
@@ -179,13 +179,36 @@
 
     my $accessor = sub { shift->$type($key)->$field(@_) };
 
-    if ($field eq $c->session_store_dbic_data_field) {
-        @args = map { MIME::Base64::encode(Storable::nfreeze($_ || '')) } @args;
+    my $data_field = $c->session_store_dbic_data_field;
+    if ($field eq $data_field) {
+        my @new_args;
+        my $total_size = 0;
+        foreach my $arg (@args) {
+            my $value = MIME::Base64::encode(Storable::nfreeze($arg || ''));
+            $total_size += length($value);
+            push @new_args, $value;
+        }
+
+        $DB::single = 1;
+        my $size;
+        if ($c->session_store_model->can('column_info')) {
+            # A DBIx::Class object.
+            $size = $c->session_store_model->column_info($data_field)->{size};
+        } elsif ($c->session_store_model->can('result_source')) {
+            # A DBIx::Class::ResultSet object.
+            $size = $c->session_store_model->result_source->column_info($data_field)->{size};
+        }
+        if ($size && $total_size > $size) {
+           warn "This session requires $total_size bytes of storage, but your database column '$data_field' can only store $size bytes. Cannot store session";
+           @new_args = ();
+        }
+
         $accessor = sub {
             my $value = shift->$type($key)->$field(@_);
             return unless defined $value;
             return Storable::thaw(MIME::Base64::decode($value));
         };
+        @args = @new_args;
     }
 
     return ($accessor, @args);
@@ -293,6 +316,10 @@
 The C<session_data> column should be a long text field.  Session data
 is encoded using L<MIME::Base64> before being stored in the database.
 
+Note that MySQL TEXT fields only store 64KB, so if your session data 
+will exceed that size you'll want to move to MEDIUMTEXT, MEDIUMBLOB, 
+or larger.
+
 The C<expires> column stores the future expiration time of the
 session.  This may be null for per-user and flash sessions.
 
@@ -316,11 +343,14 @@
 =item * Yuval Kogman, for assistance in converting to
         L<Catalyst::Plugin::Session::Store::Delegate>
 
+=item * Jay Hannah, for tests and warning when session size 
+        exceeds DBIx::Class storage size.
+
 =back
 
 =head1 COPYRIGHT
 
-Copyright 2006 Daniel Westermann-Clark, all rights reserved.
+Copyright 2006,2008 Daniel Westermann-Clark, all rights reserved.
 
 This program is free software; you can redistribute it and/or modify it
 under the same terms as Perl itself.

Modified: trunk/Catalyst-Plugin-Session-Store-DBIC/t/04dbic.t
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBIC/t/04dbic.t	2008-09-16 20:08:39 UTC (rev 8424)
+++ trunk/Catalyst-Plugin-Session-Store-DBIC/t/04dbic.t	2008-09-17 00:22:59 UTC (rev 8425)
@@ -17,7 +17,7 @@
     eval { require Test::WWW::Mechanize::Catalyst }
         or plan skip_all => "Test::WWW::Mechanize::Catalyst is required for this test";
 
-    plan tests => 12;
+    plan tests => 15;
 
     $ENV{TESTAPP_DB_FILE} = "$FindBin::Bin/session.db";
 
@@ -63,6 +63,12 @@
 $mech->get_ok("http://localhost/session/output?key=$key", 'request to get session value');
 $mech->content_is($value, 'got session value back');
 
+# Exceed our session storage capactity
+$value = "blah" x 200;
+$mech->get_ok("http://localhost/session/setup?key=$key&value=$value", 'exceeding storage capacity');
+$mech->get_ok("http://localhost/session/output?key=$key", 'request to get session value');
+$mech->content_lacks($value, 'value is not set');
+
 # Delete session
 $mech->get_ok('http://localhost/session/delete', 'request to delete session');
 $mech->content_is('ok', 'deleted session');

Modified: trunk/Catalyst-Plugin-Session-Store-DBIC/t/05dbic-schema.t
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBIC/t/05dbic-schema.t	2008-09-16 20:08:39 UTC (rev 8424)
+++ trunk/Catalyst-Plugin-Session-Store-DBIC/t/05dbic-schema.t	2008-09-17 00:22:59 UTC (rev 8425)
@@ -20,7 +20,7 @@
     eval { require Catalyst::Model::DBIC::Schema }
         or plan skip_all => "Catalyst::Model::DBIC::Schema is required for this test";
 
-    plan tests => 12;
+    plan tests => 15;
 
     $ENV{TESTAPP_DB_FILE} = "$FindBin::Bin/session.db";
 
@@ -63,6 +63,12 @@
 $mech->get_ok("http://localhost/session/output?key=$key", 'request to get session value');
 $mech->content_is($value, 'got session value back');
 
+# Exceed our session storage capactity
+$value = "blah" x 200;
+$mech->get_ok("http://localhost/session/setup?key=$key&value=$value", 'exceeding storage capacity');
+$mech->get_ok("http://localhost/session/output?key=$key", 'request to get session value');
+$mech->content_lacks($value, 'value is not set');
+
 # Delete session
 $mech->get_ok('http://localhost/session/delete', 'request to delete session');
 $mech->content_is('ok', 'deleted session');

Modified: trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Model/DBIC/Session.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Model/DBIC/Session.pm	2008-09-16 20:08:39 UTC (rev 8424)
+++ trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Model/DBIC/Session.pm	2008-09-17 00:22:59 UTC (rev 8425)
@@ -7,7 +7,7 @@
 use warnings;
 
 __PACKAGE__->table('sessions');
-__PACKAGE__->add_columns(qw/id data expires/);
+__PACKAGE__->add_columns('id', 'data', { size => 200 }, 'expires');
 __PACKAGE__->set_primary_key('id');
 
 1;

Modified: trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Schema/Session.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Schema/Session.pm	2008-09-16 20:08:39 UTC (rev 8424)
+++ trunk/Catalyst-Plugin-Session-Store-DBIC/t/lib/TestApp/Schema/Session.pm	2008-09-17 00:22:59 UTC (rev 8425)
@@ -7,7 +7,7 @@
 __PACKAGE__->load_components(qw/Core/);
 
 __PACKAGE__->table('sessions');
-__PACKAGE__->add_columns(qw/id data expires/);
+__PACKAGE__->add_columns('id', { }, 'data', { size => 200 }, 'expires', { });
 __PACKAGE__->set_primary_key('id');
 
 1;




More information about the Catalyst-commits mailing list