| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Plack::Test; |
| 5 |
use HTTP::Request::Common; |
| 6 |
use HTTP::Cookies; |
| 7 |
use Path::Tiny (); |
| 8 |
|
| 9 |
{ |
| 10 |
package |
| 11 |
Dancer2::Session::SimpleHexId; |
| 12 |
use Moo; |
| 13 |
extends 'Dancer2::Session::Simple'; |
| 14 |
|
| 15 |
# Subclass that only generates and accepts 8 character hex session id's |
| 16 |
|
| 17 |
our $valid_session_ids = 0; |
| 18 |
|
| 19 |
sub generate_id { |
| 20 |
return join "", map { sprintf("%x", rand 16) } 1..8 |
| 21 |
} |
| 22 |
|
| 23 |
sub validate_id { |
| 24 |
my $ok = ( $_[1] =~ m/^[0-9a-f]{8}$/ ); |
| 25 |
$valid_session_ids++ if $ok; # this should never increment in these tests |
| 26 |
return $ok; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
{ |
| 31 |
|
| 32 |
package App; |
| 33 |
use Dancer2; |
| 34 |
|
| 35 |
set engines => { |
| 36 |
session => { |
| 37 |
Simple => {cookie_name => 'dancer2.test'}, |
| 38 |
SimpleHexId => {cookie_name => 'dancer2.test'}, |
| 39 |
YAML => {cookie_name => 'dancer2.test'} |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
set session => 'Simple'; |
| 44 |
set show_stacktrace => 1; |
| 45 |
|
| 46 |
get '/set_session/*' => sub { |
| 47 |
my ($name) = splat; |
| 48 |
session name => $name; |
| 49 |
}; |
| 50 |
} |
| 51 |
|
| 52 |
my $url = 'http://localhost'; |
| 53 |
my $test = Plack::Test->create(App->to_app); |
| 54 |
my $app = Dancer2->runner->apps->[0]; |
| 55 |
|
| 56 |
my $bad_session_id = 'abcdefghijklmnopqrstuvwxyz123456'; |
| 57 |
|
| 58 |
for my $engine (qw(YAML Simple SimpleHexId)) { |
| 59 |
|
| 60 |
# clear current session engine, and rebuild for the test |
| 61 |
# This is *really* messy, playing in object hashrefs.. |
| 62 |
delete $app->{session_engine}; |
| 63 |
$app->config->{session} = $engine; |
| 64 |
my $session_engine = $app->session_engine; # trigger a build |
| 65 |
|
| 66 |
if ($session_engine->can('session_dir')) { |
| 67 |
# make sure our test file does not exist |
| 68 |
my $path = Path::Tiny::path( |
| 69 |
$session_engine->session_dir, |
| 70 |
$bad_session_id . $session_engine->_suffix |
| 71 |
); |
| 72 |
$path->exists and $path->remove; |
| 73 |
} |
| 74 |
|
| 75 |
# run the tests for this engine |
| 76 |
|
| 77 |
my $jar = HTTP::Cookies->new; |
| 78 |
|
| 79 |
my @cookie; |
| 80 |
|
| 81 |
subtest "[$engine] set_session and extract cookie" => sub { |
| 82 |
my $res = $test->request(GET "$url/set_session/larry"); |
| 83 |
ok($res->is_success, "set_session"); |
| 84 |
|
| 85 |
$jar->extract_cookies($res); |
| 86 |
ok($jar->as_string, 'Cookie set'); |
| 87 |
|
| 88 |
$jar->scan(sub { @cookie = @_ }); |
| 89 |
}; |
| 90 |
|
| 91 |
subtest "[$engine] set_session with bad cookie value" => sub { |
| 92 |
|
| 93 |
# set session cookie value to something bad |
| 94 |
$cookie[2] = $bad_session_id; |
| 95 |
ok($jar->set_cookie(@cookie), "Set bad cookie value"); |
| 96 |
|
| 97 |
my $req = GET "$url/set_session/larry"; |
| 98 |
$jar->add_cookie_header($req); |
| 99 |
my $res = $test->request($req); |
| 100 |
ok $res->is_success, "/read_session"; |
| 101 |
|
| 102 |
$jar->clear; |
| 103 |
ok(!$jar->as_string, 'Jar cleared'); |
| 104 |
|
| 105 |
$jar->extract_cookies($res); |
| 106 |
ok($jar->as_string, 'session cookie set again'); |
| 107 |
|
| 108 |
my $sid; |
| 109 |
$jar->scan(sub { $sid = $_[2] }); |
| 110 |
isnt $sid, 'abcdefghijklmnopqrstuvwxyz123456', |
| 111 |
"Session ID has been reset"; |
| 112 |
}; |
| 113 |
} |
| 114 |
|
| 115 |
subtest "[SimpleHexId] valid session id count" => sub { |
| 116 |
is $Dancer2::Session::SimpleHexId::valid_session_ids, 0, |
| 117 |
"No valid session keys passed during test" |
| 118 |
}; |
| 119 |
|
| 120 |
done_testing; |