blib/lib/Dancer/Session/Abstract.pm

filter: Statementstmt 92.5% 62 / 67 Branchbran 60.0% 12 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.2% 20 / 21 Podpod 85.7% 12 / 14 totaltotal 86.8% 106 / 122 SCAR 26.1
CC
13
Cov
85%
write_session_id
9.1
read_session_id
3.1
build_id
3.0
CRAP 13.6
« blib/lib/Dancer/Serializer/Mutable.pm ↑ Summary blib/lib/Dancer/Response.pm »
1 package Dancer::Session::Abstract;
2 97 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: abstract class for session engine
4 97 $Dancer::Session::Abstract::VERSION = '1.3520';
5 97 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 97 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 97 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 97 ▶use base 'Dancer::Engine';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 97 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 97 ▶use Dancer::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 97 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 97 __PACKAGE__->attributes('id');
16  
17 # args: ($class, $id)
18 # receives a session id and should return a session object if found, or undef
19 # otherwise.
20 sub retrieve {
21 1 ▶ confess "retrieve not implemented";
Subroutine retrieve: called CC=1 SCAR=0.0
Pod documented
22 }
23  
24 # args: ($class)
25 # create a new empty session, flush it and return it.
26 sub create {
27 1 ▶ confess "create not implemented";
Subroutine create: called CC=1 SCAR=0.0
Pod documented
28 }
29  
30 # args: ($self)
31 # write the (serialized) current session to the session storage
32 sub flush {
33 1 ▶ confess "flush not implemented";
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
34 }
35  
36 # args: ($self)
37 # remove the session from the session storage
38 sub destroy {
39 1 ▶ confess "destroy not implemented";
Subroutine destroy: called CC=1 SCAR=0.0
Pod documented
40 }
41  
42 # does nothing in most cases (exception is YAML)
43 sub reset {
44 0 ▶ return;
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
45 }
46  
47 # if subclass overrides to true, flush will not be called after write
48 # and subclass or application must call flush (perhaps in an after hook)
49 8 ▶sub is_lazy { 0 };
Subroutine is_lazy: called CC=1 SCAR=0.0
Pod documented
50  
51 # This is the default constructor for the session object, the only mandatory
52 # attribute is 'id'. The whole object should be serialized by the session
53 # engine.
54 # If you override this constructor, remember to call $self->SUPER::init() so
55 # that the session ID is still generated.
56 sub init {
57 134 ▶ my ($self) = @_;
Subroutine init: called CC=2 SCAR=7.1
Pod documented
58 134 ▶ if (!$self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->id) 134 0 134
59 134 $self->id(build_id());
60 }
61 }
62  
63 # this method can be overwritten in any Dancer::Session::* module
64 sub session_name {
65 469 ▶ setting('session_name') || 'dancer.session';
Subroutine session_name: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
setting("session_name") or "dancer.session" 461 8 469
66 }
67  
68 # May be overriden if session key value pairs aren't stored in the
69 # session object's hash
70 sub get_value {
71 8 ▶ my ( $self, $key ) = @_;
Subroutine get_value: called CC=1 SCAR=0.0
Pod documented
72 8 return $self->{$key};
73 }
74  
75 # May be overriden if session key value pairs aren't stored in the
76 # session object's hash
77 sub set_value {
78 8 ▶ my ( $self, $key, $value ) = @_;
Subroutine set_value: called CC=1 SCAR=0.0
Pod documented
79 8 $self->{$key} = $value;
80 }
81  
82  
83 # Methods below this line should not be overloaded.
84  
85 # we try to make the best random number
86 # with native Perl 5 code.
87 # to rebuild a session id, an attacker should know:
88 # - the running PID of the server
89 # - the current timestamp of the time it was built
90 # - the path of the installation directory
91 # - guess the correct number between 0 and 1000000000
92 # - should be able to reproduce that 3 times
93 sub build_id {
94 134 ▶ my $session_id = "";
Subroutine build_id: called CC=3 SCAR=11.0
Pod documented
95 134 foreach my $seed (rand(1000), rand(1000), rand(1000)) {
96 402 my $c = 0;
97 402 $c += ord($_) for (split //, File::Spec->rel2abs(File::Spec->curdir));
98 402 my $current = int($seed * 1000000000) + time + $$ + $c;
99 402 $session_id .= $current;
100 }
101 134 return $session_id;
102 }
103  
104 sub read_session_id {
105 177 ▶ my ($class) = @_;
Subroutine read_session_id: called CC=3 SCAR=11.4
Pod documented
106  
107 177 my $name = $class->session_name();
108 177 my $c = Dancer::Cookies->cookies->{$name};
109 177 ▶ return unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 34 143 177
110 143 ▶ if ($class->validate_session_id($c->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->validate_session_id($c->value)) { } 143 0 143
111 143 return $c->value;
112 } else {
113 0 warn "Rejecting invalid session ID ". $c->value;
114 0 return;
115 }
116 }
117  
118 # Validate session ID (base64 chars plus hyphen by default) to avoid potential
119 # issues, e.g. if the ID is used insecurely elsewhere. If a session provider
120 # expects more unusual IDs, it can override this class method with one that
121 # validates according to that provider's expectation of how a session ID should
122 # look.
123 sub validate_session_id {
124 143 ▶ my ($class, $id) = @_;
Subroutine validate_session_id: called CC=1 SCAR=0.0
Pod undocumented
125 143 return $id =~ m{^[A-Za-z0-9_\-~]+$};
126 }
127  
128 sub write_session_id {
129 97 ▶ my ($class, $id) = @_;
Subroutine write_session_id: called CC=7 SCAR=22.0
Pod documented
130  
131 97 my $name = $class->session_name();
132  
133 # If we've already pushed the appropriate cookie to the response, then we
134 # don't need to do any more
135 97 ▶ if (my $cookie = Dancer::Cookies->cookie($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cookie = "Dancer::Cookies"->cookie($name)) 63 34 97
136 63 ▶ if ($cookie eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie eq $id) 63 0 63
137 63 return;
138 }
139 }
140  
141 34 ▶ my %cookie = (
Branch: 2 branches
BranchTrueFalseTotal
defined setting('session_is_http_only') ? : 0 34 34
defined setting('session_same_site') ? : 0 34 34
142 name => $name,
143 value => $id,
144 domain => setting('session_domain'),
145 secure => setting('session_secure'),
146 http_only => defined(setting("session_is_http_only")) ?
147 setting("session_is_http_only") : 1,
148 same_site => defined(setting("session_same_site")) ?
149 setting("session_same_site") : 'None',
150 );
151 34 ▶ if (my $expires = setting('session_expires')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires = setting("session_expires")) 0 34 34
152 # It's # of seconds from the current time
153 # Otherwise just feed it through.
154 0 ▶ $expires = Dancer::Cookie::_epoch_to_gmtstring(time + $expires) if $expires =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $expires =~ /^\d+$/ 0 0 0
155 0 $cookie{expires} = $expires;
156 }
157  
158 34 my $c = Dancer::Cookie->new(%cookie);
159 34 Dancer::Cookies->set_cookie_object($name => $c);
160 }
161  
162 97 1;
163  
164 __END__
« blib/lib/Dancer/Serializer/Mutable.pm ↑ Summary blib/lib/Dancer/Response.pm »