| 1 | package YATT::Lite::WebMVC0::Partial::Session; | ||||||||||||||||||||||||||||
| 2 | sub MY () {__PACKAGE__} | ||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use YATT::Lite::Partial | ||||||||||||||||||||||||||||
| 8 | 2 | ▶ | (requires => [qw/error | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | app_path_ensure_existing | ||||||||||||||||||||||||||||
| 10 | app_path_var_tmp | ||||||||||||||||||||||||||||
| 11 | /] | ||||||||||||||||||||||||||||
| 12 | , fields => [qw/session_driver | ||||||||||||||||||||||||||||
| 13 | session_config | ||||||||||||||||||||||||||||
| 14 | session_debug | ||||||||||||||||||||||||||||
| 15 | session_path | ||||||||||||||||||||||||||||
| 16 | csrftok_name | ||||||||||||||||||||||||||||
| 17 | tmpdir | ||||||||||||||||||||||||||||
| 18 | /] | ||||||||||||||||||||||||||||
| 19 | , -Entity, -CON | ||||||||||||||||||||||||||||
| 20 | 2 | ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use YATT::Lite::Util qw/lexpand escape nonempty symtab | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | num_is_ge secure_random_hex/; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # session_config: name, expire, plus cookie attributes | ||||||||||||||||||||||||||||
| 26 | # (httponly/secure/samesite) to override the defaults in session_cookie_opts. | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use YATT::Lite::Types [Config => fields => [qw/name expire | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | httponly secure samesite/]]; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | #======================================== | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use YATT::Lite::WebMVC0::Connection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | sub Connection () {'YATT::Lite::WebMVC0::Connection'} | ||||||||||||||||||||||||||||
| 34 | sub ConnProp () {Connection} | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #======================================== | ||||||||||||||||||||||||||||
| 37 | # Session support, based on CGI::Session. | ||||||||||||||||||||||||||||
| 38 | #======================================== | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | Entity sess => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 41 | 0 | my ($this) = shift; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # This will call MY->session_resume. | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $sess = $CON->get_session | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | or return undef; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | $sess->param(@_); | |||||||||||||||||||||||||||
| 48 | 2 | }; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | # CSRF token is a random value kept *in* the session, never the session | ||||||||||||||||||||||||||||
| 52 | # id itself: printing the sid into every form would hand it to any | ||||||||||||||||||||||||||||
| 53 | # script in the page (defeating HttpOnly) and to anyone who sees the html. | ||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||
| 55 | 7 | ▶ | Entity csrf_token_input => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 56 | 7 | my ($this, $name) = @_; | |||||||||||||||||||||||||||
| 57 | 7 | my MY $self = $CON->cget('system'); | |||||||||||||||||||||||||||
| 58 | 7 | ▶ | $name ||= $self->csrftok_name; | ||||||||||||||||||||||||||
|
Condition33%1/3
$name ||= $self->csrftok_name
MC/DC0%0/2
$name ||= $self->csrftok_name
$name
$self->csrftok_name
Truth table33%1/3
A ||= B
A= $nameB= $self->csrftok_name
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 7 | ▶ | my $token = $self->session_csrf_token($CON) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->session_csrf_token($YATT::Lite::WebMVC0::Partial::Session::CON) // ""
MC/DC0%0/1
$self->session_csrf_token($YATT::Lite::WebMVC0::Partial::Session::CON) // ""
$self->session_csrf_token($YATT::Lite::WebMVC0::Partial::Session::CON)
Truth table50%1/2
A
A= $self->session_csrf_token($YATT::Lite::WebMVC0::Partial::Session::CON)
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 7 | \ sprintf <<END, $name, escape($token); | |||||||||||||||||||||||||||
| 63 | <input type="hidden" name="%s" value="%s"> | ||||||||||||||||||||||||||||
| 64 | END | ||||||||||||||||||||||||||||
| 65 | 2 | }; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | ▶ | Entity csrf_token_check => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 68 | 8 | my ($this, $name) = @_; | |||||||||||||||||||||||||||
| 69 | 8 | my MY $self = $CON->cget('system'); | |||||||||||||||||||||||||||
| 70 | 8 | ▶ | $name ||= $self->csrftok_name; | ||||||||||||||||||||||||||
|
Condition33%1/3
$name ||= $self->csrftok_name
MC/DC0%0/2
$name ||= $self->csrftok_name
$name
$self->csrftok_name
Truth table33%1/3
A ||= B
A= $nameB= $self->csrftok_name
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 8 | ▶ | nonempty(my $token = $self->session_csrf_token($CON, 0)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | or return undef; | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | nonempty(my $got = $CON->param($name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or return undef; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | $token eq $got; | |||||||||||||||||||||||||||
| 78 | 2 | }; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 2 | my $symtab = symtab(MY); | |||||||||||||||||||||||||||
| 82 | 2 | foreach my $meth (grep {/^session_/} keys %$symtab) { | |||||||||||||||||||||||||||
| 83 | 28 | my $sub = MY->can($meth); | |||||||||||||||||||||||||||
| 84 | 23 | ▶ | Entity $meth => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 23 | my $this = shift; | |||||||||||||||||||||||||||
| 86 | 23 | my ConnProp $prop = $CON->prop; | |||||||||||||||||||||||||||
| 87 | 23 | my MY $self = $prop->{system}; | |||||||||||||||||||||||||||
| 88 | 23 | $self->$meth($CON, @_); | |||||||||||||||||||||||||||
| 89 | 28 | }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | #======================================== | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Session id as claimed by the client's cookie. A value that is not a | ||||||||||||||||||||||||||||
| 96 | # plain token is treated as "no cookie" so it never reaches the driver. | ||||||||||||||||||||||||||||
| 97 | 8 | ▶ | sub session_sid { | ||||||||||||||||||||||||||
|
Subroutine
session_sid: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 8 | (my MY $self, my ($con)) = @_; | |||||||||||||||||||||||||||
| 99 | 8 | my ConnProp $prop = $con->prop; | |||||||||||||||||||||||||||
| 100 | 8 | my $sid_name = $self->session_sid_name; | |||||||||||||||||||||||||||
| 101 | 8 | ▶ | my $ck = $con->cookies_in->{$sid_name} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | or return undef; | ||||||||||||||||||||||||||||
| 103 | 8 | ▶ | my $sid = ref $ck ? $ck->value : $ck; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 8 | ▶ | return undef unless defined $sid and $sid =~ $self->session_sid_validator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sid and $sid =~ $self->session_sid_validator
MC/DC50%1/2
defined $sid and $sid =~ $self->session_sid_validator
defined $sid
$sid =~ $self->session_sid_validator
Truth table66%2/3
A and B
A= defined $sidB= $sid =~ $self->session_sid_validator
| |||||||||||||||||||||||||||||
| 105 | 3 | $sid; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 8 | ▶ | sub session_sid_validator { qr{^[A-Za-z0-9_-]{1,128}\z} } | ||||||||||||||||||||||||||
|
Subroutine
session_sid_validator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Returns the CSRF token of the current session, creating it on first use. | ||||||||||||||||||||||||||||
| 111 | # With $create false, returns undef when there is no session or no token yet. | ||||||||||||||||||||||||||||
| 112 | 30 | ▶ | sub session_csrf_token { | ||||||||||||||||||||||||||
|
Subroutine
session_csrf_token: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 30 | (my MY $self, my ($con, $create)) = @_; | |||||||||||||||||||||||||||
| 114 | 30 | ▶ | $create //= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$create //= 1
MC/DC100%1/1
$create //= 1
$create
Truth table100%2/2
A
A= $create
| |||||||||||||||||||||||||||||
| 115 | 30 | ▶ | my $sess = $con->get_session | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | or return undef; | ||||||||||||||||||||||||||||
| 117 | 20 | my $key = $self->csrftok_session_key; | |||||||||||||||||||||||||||
| 118 | 20 | my $token = $sess->param($key); | |||||||||||||||||||||||||||
| 119 | 20 | ▶ | if (not nonempty($token) and $create) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not nonempty($token) and $create
MC/DC50%1/2
not nonempty($token) and $create
not nonempty($token)
$create
Truth table66%2/3
not(A) and B
A= not nonempty($token)B= $create
| |||||||||||||||||||||||||||||
| 120 | 7 | $token = secure_random_hex(16); | |||||||||||||||||||||||||||
| 121 | 7 | $sess->param($key, $token); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 20 | $token; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Cookie attributes for the session cookie. Defaults: HttpOnly, | ||||||||||||||||||||||||||||
| 127 | # SameSite=Lax, and Secure whenever the request itself came over TLS. | ||||||||||||||||||||||||||||
| 128 | # Each can be overridden via session_config => {httponly => 0, ...}. | ||||||||||||||||||||||||||||
| 129 | 8 | ▶ | sub session_cookie_opts { | ||||||||||||||||||||||||||
|
Subroutine
session_cookie_opts: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 8 | (my MY $self, my ($con, $path)) = @_; | |||||||||||||||||||||||||||
| 131 | 8 | my Config $opts = $self->{session_config}; | |||||||||||||||||||||||||||
| 132 | 8 | ▶ | my $httponly = $opts->{httponly} // 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"httponly"} // 1
MC/DC100%1/1
$opts->{"httponly"} // 1
$opts->{"httponly"}
Truth table100%2/2
A
A= $opts->{"httponly"}
| |||||||||||||||||||||||||||||
| 133 | 8 | ▶ | my $samesite = $opts->{samesite} // 'Lax'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"samesite"} // "Lax"
MC/DC100%1/1
$opts->{"samesite"} // "Lax"
$opts->{"samesite"}
Truth table100%2/2
A
A= $opts->{"samesite"}
| |||||||||||||||||||||||||||||
| 134 | 8 | ▶ | my $secure = $opts->{secure} // $con->is_https; | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts->{"secure"} // $con->is_https
MC/DC0%0/2
$opts->{"secure"} // $con->is_https
$opts->{"secure"}
$con->is_https
Truth table66%2/3
A // B
A= $opts->{"secure"}B= $con->is_https
| |||||||||||||||||||||||||||||
| 135 | 8 | ▶ | (-path => $path | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 136 | , ($httponly ? (-httponly => 1) : ()) | ||||||||||||||||||||||||||||
| 137 | , ($secure ? (-secure => 1) : ()) | ||||||||||||||||||||||||||||
| 138 | , (nonempty($samesite) ? (-samesite => $samesite) : ())); | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub session_regenerate_id { | ||||||||||||||||||||||||||
|
Subroutine
session_regenerate_id: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 0 | (my MY $self, my ($con, @with_init)) = @_; | |||||||||||||||||||||||||||
| 143 | 0 | my ConnProp $prop = $con->prop; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (defined $prop->{_session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $self->session_delete($con); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | $self->session_start($con, @with_init); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # This will be called back from $CON->get_session. | ||||||||||||||||||||||||||||
| 153 | # usually called from before_dirhandler | ||||||||||||||||||||||||||||
| 154 | 8 | ▶ | sub session_resume { | ||||||||||||||||||||||||||
|
Subroutine
session_resume: called CC=12 SCAR=32.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 8 | (my MY $self, my ($con)) = @_; | |||||||||||||||||||||||||||
| 156 | $con->logbacktrace("session.resume") | ||||||||||||||||||||||||||||
| 157 | 8 | ▶ | if num_is_ge($self->{session_debug}, 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 8 | my ConnProp $prop = $con->prop; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 8 | ▶ | if (exists $prop->{_session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | $con->logdump("session.resume" => "session is already loaded") | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if $self->{session_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | return $prop->{_session}; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 8 | $prop->{_session} = undef; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 8 | ▶ | my $sid = $self->session_sid($con) or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | $con->logdump("session.resume" => "sid is empty") | ||||||||||||||||||||||||||||
| 170 | 5 | ▶ | if $self->{session_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 5 | return undef; | |||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 3 | ▶ | my $sess = $self->session_create_by(load => $con, $sid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | or $self->error("Can't load session for sid='%s': %s" | ||||||||||||||||||||||||||||
| 176 | , $sid, CGI::Session->errstr); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | CHK: { | ||||||||||||||||||||||||||||
| 179 | 3 | ▶ | if ($sess->is_expired) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 180 | $con->logdump("session.resume" => expired => $sid) | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if $self->{session_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | } elsif (not $sess->id) { | ||||||||||||||||||||||||||||
| 183 | $con->logdump("session.resume" => "id is empty" | ||||||||||||||||||||||||||||
| 184 | , claimed_sid => $sid, sessobj => $sess) | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if $self->{session_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||
| 187 | 3 | last CHK; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | # not ok. | ||||||||||||||||||||||||||||
| 190 | 0 | delete $prop->{_session}; # To allow calling session_start. | |||||||||||||||||||||||||||
| 191 | 0 | return undef; # XXX: Should we notify? | |||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | $con->logdump("session.resume" => OK => sid => $sid) | ||||||||||||||||||||||||||||
| 195 | 3 | ▶ | if $self->{session_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 3 | $prop->{_session} = $sess; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # This will be called back from $CON->start_session. | ||||||||||||||||||||||||||||
| 201 | 7 | ▶ | sub session_start { | ||||||||||||||||||||||||||
|
Subroutine
session_start: called CC=9 SCAR=25.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 202 | 7 | (my MY $self, my ($con, @with_init)) = @_; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 7 | ▶ | my $opts = shift @with_init if @with_init and ref $with_init[0] eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@with_init and ref $with_init[0] eq "HASH"
MC/DC0%0/2
@with_init and ref $with_init[0] eq "HASH"
@with_init
ref $with_init[0] eq "HASH"
Truth table33%1/3
A and B
A= @with_initB= ref $with_init[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 205 | 7 | ▶ | my $path = delete $opts->{path} || $self->session_path($con); | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $opts->{'path'} || $self->session_path($con)
MC/DC0%0/2
delete $opts->{'path'} || $self->session_path($con)
delete $opts->{'path'}
$self->session_path($con)
Truth table33%1/3
A || B
A= delete $opts->{'path'}B= $self->session_path($con)
| |||||||||||||||||||||||||||||
| 206 | 7 | ▶ | if (keys %$opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | $self->error("Invalid option for session_start: %s" | |||||||||||||||||||||||||||
| 208 | , join ", ", keys %$opts); | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | $con->logbacktrace("session.start") | ||||||||||||||||||||||||||||
| 212 | 7 | ▶ | if num_is_ge($self->{session_debug}, 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 7 | my ConnProp $prop = $con->prop; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 7 | ▶ | if (defined $prop->{_session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | $self->error("session is called twice! sid=%s", $prop->{_session}->id); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 7 | $prop->{_session} = undef; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 7 | ▶ | my $sess = $self->session_create_by(new => $con) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | or $self->error("Can't create new session: %s", CGI::Session->errstr); | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 7 | $con->set_cookie($self->session_sid_name | |||||||||||||||||||||||||||
| 225 | , $sess->id | ||||||||||||||||||||||||||||
| 226 | , $self->session_cookie_opts($con, $path)); | ||||||||||||||||||||||||||||
| 227 | 7 | $sess->clear; | |||||||||||||||||||||||||||
| 228 | 7 | ▶ | $self->session_init($con, $sess, @with_init) if @with_init; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 7 | $prop->{_session} = $sess; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 10 | ▶ | sub session_create_by { | ||||||||||||||||||||||||||
|
Subroutine
session_create_by: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 234 | 10 | (my MY $self, my ($method, $con, $sid)) = @_; | |||||||||||||||||||||||||||
| 235 | 10 | require CGI::Session; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 10 | my ($type, %driver_opts) = $self->session_driver; | |||||||||||||||||||||||||||
| 238 | 10 | my Config $opts = $self->{session_config}; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # Unless the DSN names an id generator, use ours (OS CSPRNG, 160 bits) | ||||||||||||||||||||||||||||
| 241 | # instead of CGI::Session's default id:md5 (md5 of pid, time and rand()). | ||||||||||||||||||||||||||||
| 242 | 10 | ▶ | unless ($type =~ /(?:^|;)\s*id\s*:/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 10 | $self->ensure_secure_id_generator; | |||||||||||||||||||||||||||
| 244 | 10 | $type .= ";id:" . $self->secure_id_generator_name; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 10 | ▶ | my $expire = delete($opts->{expire}) // $self->default_session_expire; | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $opts->{"expire"} // $self->default_session_expire
MC/DC0%0/2
delete $opts->{"expire"} // $self->default_session_expire
delete $opts->{"expire"}
$self->default_session_expire
Truth table33%1/3
A // B
A= delete $opts->{"expire"}B= $self->default_session_expire
| |||||||||||||||||||||||||||||
| 248 | my %cgi_sess_opts = map { | ||||||||||||||||||||||||||||
| 249 | # cookie attributes are ours, not CGI::Session's. | ||||||||||||||||||||||||||||
| 250 | 10 | ▶ | /^(?:httponly|secure|samesite)$/ ? () : ($_ => $opts->{$_}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | } keys %$opts; | ||||||||||||||||||||||||||||
| 252 | 10 | ▶ | if (my $sess = CGI::Session->$method($type, $sid, \%driver_opts, \%cgi_sess_opts)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 10 | ▶ | unless ($sess->is_expired) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 10 | $sess->expire($expire); | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | 10 | return $sess; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 0 | ▶ | sub session_init { | ||||||||||||||||||||||||||
|
Subroutine
session_init: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 261 | 0 | my MY $self = shift; | |||||||||||||||||||||||||||
| 262 | 0 | my ConnProp $prop = (my $con = shift)->prop; | |||||||||||||||||||||||||||
| 263 | 0 | my ($sess, @with_init) = @_; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | foreach my $spec (@with_init) { | |||||||||||||||||||||||||||
| 266 | 0 | ▶ | unless (defined $spec) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 267 | 0 | $self->error("Undefined session initializer"); | |||||||||||||||||||||||||||
| 268 | } elsif (ref $spec eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 269 | 0 | my ($name, @value) = @$spec; | |||||||||||||||||||||||||||
| 270 | 0 | $sess->param($name, @value); | |||||||||||||||||||||||||||
| 271 | ▶ | } elsif (not ref $spec or ref $spec eq 'Regexp') { | |||||||||||||||||||||||||||
|
Condition0%0/3
not ref $spec or ref $spec eq "Regexp"
MC/DC0%0/2
not ref $spec or ref $spec eq "Regexp"
not ref $spec
ref $spec eq "Regexp"
Truth table0%0/3
not(A) or B
A= not ref $specB= ref $spec eq "Regexp"
| |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | $spec = qr{^\Q$spec} unless ref $spec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | foreach my $name ($con->param) { | |||||||||||||||||||||||||||
| 274 | 0 | ▶ | next unless $name =~ $spec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 0 | my (@value) = $con->param($name); | |||||||||||||||||||||||||||
| 276 | 0 | $sess->param($name, @value); | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | } else { | ||||||||||||||||||||||||||||
| 279 | 0 | $self->error("Invalid session initializer: %s" | |||||||||||||||||||||||||||
| 280 | , terse_dump($spec)); | ||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 10 | ▶ | sub session_driver { | ||||||||||||||||||||||||||
|
Subroutine
session_driver: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 286 | 10 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 287 | $self->{session_driver} | ||||||||||||||||||||||||||||
| 288 | ? lexpand($self->{session_driver}) | ||||||||||||||||||||||||||||
| 289 | 10 | ▶ | : $self->default_session_driver; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 1 | ▶ | sub session_delete { | ||||||||||||||||||||||||||
|
Subroutine
session_delete: called CC=7 SCAR=24.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 293 | 1 | my MY $self = shift; | |||||||||||||||||||||||||||
| 294 | 1 | my ConnProp $prop = (my $con = shift)->prop; | |||||||||||||||||||||||||||
| 295 | 1 | my $opts = shift; | |||||||||||||||||||||||||||
| 296 | 1 | ▶ | my $path = delete $opts->{path} || $self->session_path($con); | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $opts->{'path'} || $self->session_path($con)
MC/DC0%0/2
delete $opts->{'path'} || $self->session_path($con)
delete $opts->{'path'}
$self->session_path($con)
Truth table33%1/3
A || B
A= delete $opts->{'path'}B= $self->session_path($con)
| |||||||||||||||||||||||||||||
| 297 | 1 | ▶ | if (keys %$opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | $self->error("Invalid option for session_delete: %s" | |||||||||||||||||||||||||||
| 299 | , join ", ", keys %$opts); | ||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | $con->logbacktrace("session.delete") | ||||||||||||||||||||||||||||
| 303 | 1 | ▶ | if num_is_ge($self->{session_debug}, 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 1 | ▶ | if (my $sess = delete $prop->{_session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | my $sid = $sess->id; | |||||||||||||||||||||||||||
| 307 | 0 | $sess->delete; | |||||||||||||||||||||||||||
| 308 | 0 | $sess->flush; | |||||||||||||||||||||||||||
| 309 | $con->logdump("session.delete" => OK => sid => $sid) | ||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if $self->{session_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | } else { | ||||||||||||||||||||||||||||
| 312 | $con->logdump("session.delete" => 'NOP') | ||||||||||||||||||||||||||||
| 313 | 1 | ▶ | if $self->{session_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | 1 | my $name = $self->session_sid_name; | |||||||||||||||||||||||||||
| 316 | 1 | my @rm = ($name, '', -expires => '-10y' | |||||||||||||||||||||||||||
| 317 | , $self->session_cookie_opts($con, $path)); | ||||||||||||||||||||||||||||
| 318 | 1 | $con->set_cookie(@rm); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 10 | ▶ | sub session_flush { | ||||||||||||||||||||||||||
|
Subroutine
session_flush: called CC=4 SCAR=15.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 322 | 10 | my MY $self = shift; | |||||||||||||||||||||||||||
| 323 | 10 | my ConnProp $prop = (my $glob = shift)->prop; | |||||||||||||||||||||||||||
| 324 | my $sess = $prop->{_session} | ||||||||||||||||||||||||||||
| 325 | 10 | ▶ | or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 10 | ▶ | return if $sess->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 10 | $sess->flush; | |||||||||||||||||||||||||||
| 328 | 10 | ▶ | if (my $err = $sess->errstr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 0 | local $prop->{_session}; | |||||||||||||||||||||||||||
| 330 | 0 | $self->error("Can't flush session: %s", $err); | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 5 | ▶ | sub configure_use_session { | ||||||||||||||||||||||||||
|
Subroutine
configure_use_session: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 335 | 5 | (my MY $self, my $value) = @_; | |||||||||||||||||||||||||||
| 336 | 5 | ▶ | if ($value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | $self->{session_config} | ||||||||||||||||||||||||||||
| 338 | 5 | ▶ | //= ref $value ? +{lexpand($value)} : +{$self->default_session_config}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->{"session_config"} //= ref $value ? {lexpand($value)} : {$self->default_session_config}
MC/DC100%1/1
$self->{"session_config"} //= ref $value ? {lexpand($value)} : {$self->default_session_config}
$self->{"session_config"}
Truth table100%2/2
A
A= $self->{"session_config"}
| |||||||||||||||||||||||||||||
| 339 | 5 | ▶ | $self->{session_driver} //= [$self->default_session_driver]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"session_driver"} //= [$self->default_session_driver]
MC/DC0%0/1
$self->{"session_driver"} //= [$self->default_session_driver]
$self->{"session_driver"}
Truth table50%1/2
A
A= $self->{"session_driver"}
| |||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 8 | ▶ | sub session_path { | ||||||||||||||||||||||||||
|
Subroutine
session_path: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 344 | 8 | (my MY $self, my ($con)) = @_; | |||||||||||||||||||||||||||
| 345 | 8 | ▶ | $self->{session_path} || $con->site_location; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 16 | ▶ | sub session_sid_name { | ||||||||||||||||||||||||||
|
Subroutine
session_sid_name: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 349 | 16 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 350 | 16 | my Config $opts = $self->{session_config}; | |||||||||||||||||||||||||||
| 351 | 16 | ▶ | $opts->{name} || $self->default_session_sid_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 10 | ▶ | sub default_session_expire { '1d' } | ||||||||||||||||||||||||||
|
Subroutine
default_session_expire: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 355 | 15 | ▶ | sub default_session_sid_name { 'SID' } | ||||||||||||||||||||||||||
|
Subroutine
default_session_sid_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 356 | 4 | ▶ | sub default_session_config {} | ||||||||||||||||||||||||||
|
Subroutine
default_session_config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 5 | ▶ | sub default_session_driver { | ||||||||||||||||||||||||||
|
Subroutine
default_session_driver: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 359 | 5 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 360 | 5 | ▶ | my $tmpdir = $self->{tmpdir} //= $self->app_path_var_tmp('sess'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"tmpdir"} //= $self->app_path_var_tmp("sess")
MC/DC0%0/2
$self->{"tmpdir"} //= $self->app_path_var_tmp("sess")
$self->{"tmpdir"}
$self->app_path_var_tmp("sess")
Truth table33%1/3
A //= B
A= $self->{"tmpdir"}B= $self->app_path_var_tmp("sess")
| |||||||||||||||||||||||||||||
| 361 | 5 | ("driver:file" | |||||||||||||||||||||||||||
| 362 | , Directory => $tmpdir | ||||||||||||||||||||||||||||
| 363 | ) | ||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | ▶ | sub cmd_session_list { | ||||||||||||||||||||||||||
|
Subroutine
cmd_session_list: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 367 | 0 | (my MY $self, my @param) = @_; | |||||||||||||||||||||||||||
| 368 | 0 | print join("\t", qw(id created accessed), @param), "\n"; | |||||||||||||||||||||||||||
| 369 | 0 | require CGI::Session; | |||||||||||||||||||||||||||
| 370 | 0 | my ($type, %driver_opts) = $self->session_driver; | |||||||||||||||||||||||||||
| 371 | 0 | ▶ | CGI::Session->find($type, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 372 | 0 | my ($sess) = @_; | |||||||||||||||||||||||||||
| 373 | 0 | ▶ | print join("\t", map {defined $_ ? $_ : "(undef)"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | $sess->id, $sess->ctime, $sess->atime | ||||||||||||||||||||||||||||
| 375 | 0 | , map {$sess->param($_)} @param), "\n"; | |||||||||||||||||||||||||||
| 376 | 0 | }, \%driver_opts); | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 15 | ▶ | sub csrftok_name { | ||||||||||||||||||||||||||
|
Subroutine
csrftok_name: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 381 | 15 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 382 | 15 | ▶ | $self->{csrftok_name} || $self->default_csrftok_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 15 | ▶ | sub default_csrftok_name { '--csrftok' } | ||||||||||||||||||||||||||
|
Subroutine
default_csrftok_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | # Session parameter name under which the CSRF token is stored. | ||||||||||||||||||||||||||||
| 388 | 20 | ▶ | sub csrftok_session_key { '_yatt_csrf_token' } | ||||||||||||||||||||||||||
|
Subroutine
csrftok_session_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | #======================================== | ||||||||||||||||||||||||||||
| 391 | # Session id generator plugged into CGI::Session (as "id:yatt_secure"). | ||||||||||||||||||||||||||||
| 392 | # | ||||||||||||||||||||||||||||
| 393 | # CGI::Session only accepts generators named CGI::Session::ID::<word> | ||||||||||||||||||||||||||||
| 394 | # and loads them with `require`, so the package is defined here and | ||||||||||||||||||||||||||||
| 395 | # registered in %INC once, right before CGI::Session needs it. | ||||||||||||||||||||||||||||
| 396 | #======================================== | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 20 | ▶ | sub secure_id_generator_name { 'yatt_secure' } | ||||||||||||||||||||||||||
|
Subroutine
secure_id_generator_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 10 | ▶ | sub ensure_secure_id_generator { | ||||||||||||||||||||||||||
|
Subroutine
ensure_secure_id_generator: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 401 | 10 | (my MY $self) = @_; | |||||||||||||||||||||||||||
| 402 | 10 | my $name = $self->secure_id_generator_name; | |||||||||||||||||||||||||||
| 403 | 10 | my $pm = "CGI/Session/ID/$name.pm"; | |||||||||||||||||||||||||||
| 404 | 10 | ▶ | return if $INC{$pm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 1 | require CGI::Session::ErrorHandler; | |||||||||||||||||||||||||||
| 406 | 1 | my $pkg = "CGI::Session::ID::$name"; | |||||||||||||||||||||||||||
| 407 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 408 | 1 | @{"${pkg}::ISA"} = ('CGI::Session::ErrorHandler'); | |||||||||||||||||||||||||||
| 409 | 1 | ▶ | *{"${pkg}::generate_id"} = sub { secure_random_hex(20) }; # 40 hex chars | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 410 | 1 | *{"${pkg}::generate"} = \&{"${pkg}::generate_id"}; | |||||||||||||||||||||||||||
| 411 | 1 | $INC{$pm} = __FILE__; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 2 | 1; | |||||||||||||||||||||||||||