blib/lib/WebDyne/Session.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 149 CC 5 SCAR 24.3
CC
5
Cov
0%
handler
30.0
import
2.0
session_id
2.0
CRAP 11.3
1 #
2 # This file is part of WebDyne.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13 package WebDyne::Session;
14  
15  
16 # Compiler Pragma
17 #
18 use strict qw(vars);
19 use vars qw(
20 $VERSION
21 $WEBDYNE_SESSION_COOKIE_HTTPONLY
22 $WEBDYNE_SESSION_COOKIE_PATH
23 $WEBDYNE_SESSION_COOKIE_SAMESITE
24 $WEBDYNE_SESSION_COOKIE_SECURE
25 $WEBDYNE_SESSION_ID_COOKIE_NAME
26 );
27 use warnings;
28 no warnings qw(uninitialized);
29  
30  
31 # WebDyne Modules.
32 #
33 use WebDyne::Session::Constant;
34 use WebDyne::Util;
35  
36  
37 # External modules
38 #
39 #use Digest::MD5 qw(md5_hex);
40 use Crypt::URandom qw( urandom );
41  
42  
43 # Version information
44 #
45 $VERSION='3.029';
46  
47  
48 # Shortcut error handler.
49 #
50 require WebDyne::Err;
51 *err_html=\&WebDyne::Err::err_html || *err_html;
52  
53  
54 # Debug
55 #
56 0 && debug("%s loaded, version $VERSION", __PACKAGE__);
57  
58  
59 # And done
60 #
61 1;
62  
63  
64 #------------------------------------------------------------------------------
65  
66  
67 sub import {
68  
69  
70 # Will only work if called from within a __PERL__ block in WebDyne
71 #
72 my $class=shift();
73 my $self_cr=UNIVERSAL::can(scalar caller, 'self') || return;
74 my $self=$self_cr->() || return;
75 $self->set_handler('WebDyne::Chain');
76 my $meta_hr=$self->meta();
77 push @{$meta_hr->{'webdynechain'}}, $class;
78  
79  
80 }
81  
82  
83 sub handler : method {
84  
85  
86 # Get class, request object
87 #
88 my ($self, $r, $param_hr)=@_;
89  
90  
91 # Debug
92 #
93 0 && debug("in %s handler, self $self, r $r, param_hr $param_hr", __PACKAGE__);
94  
95  
96 # Get CGI object ref
97 #
98 my $cgi_or=$self->CGI() ||
99 return err('no CGI object availble');
100
101  
102  
103 # Get cookie name we are looking for
104 #
105 my $cookie_name=$WEBDYNE_SESSION_ID_COOKIE_NAME;
106 0 && debug("using cookie_name: $cookie_name");
107
108
109 # Get current value
110 #
111 my $session_id=$cgi_or->cookie($cookie_name);
112 0 && debug("found session_id: $session_id");
113  
114  
115 # Get or set the cookie id
116 #
117 unless($session_id) {
118  
119  
120 # Debug
121 #
122 0 && debug('session cookie not found, generating new session_id');
123  
124  
125 # Generate a new session id based on an MD5 checksum. UPDATE deprectaed, CVE-2026-5084
126 #
127 #$session_id=&Digest::MD5::md5_hex(rand($$ . time() . ($self =~ /(\d+)/)[0]));
128
129 # Use urandom for session now
130 $session_id = unpack("H*", urandom(16));
131 0 && debug("generated new session_id $session_id");
132  
133  
134 # If no session id now, something has gone horribly wrong
135 #
136 $session_id || return $self->err_html(
137 'unable to create unique session id'
138 );
139  
140  
141 # Debug
142 #
143 0 && debug("session_id generation success, generated id $session_id");
144  
145  
146 # Create a cookie with our session id
147 #
148 my @cookie=(
149 -name => $cookie_name,
150 -value => $session_id,
151 -path => $WEBDYNE_SESSION_COOKIE_PATH
152 );
153 push @cookie, (-secure => 1)
154 if $WEBDYNE_SESSION_COOKIE_SECURE;
155 push @cookie, (-httponly => 1)
156 if $WEBDYNE_SESSION_COOKIE_HTTPONLY;
157 if (defined($WEBDYNE_SESSION_COOKIE_SAMESITE) && length($WEBDYNE_SESSION_COOKIE_SAMESITE)) {
158 ($WEBDYNE_SESSION_COOKIE_SAMESITE =~ /^(?:Lax|Strict|None)$/) ||
159 return $self->err_html("invalid SameSite value for session cookie: $WEBDYNE_SESSION_COOKIE_SAMESITE");
160 push @cookie, (-samesite => $WEBDYNE_SESSION_COOKIE_SAMESITE);
161 }
162 my $cookie=$cgi_or->cookie(@cookie) ||
163 return $self->err_html("unable to generate sid: $session_id cookie");
164  
165  
166 # Get our header hash ref
167 #
168 my $header_hr=$r->headers_out() ||
169 return $self->err_html('unable to get outbound headers');
170  
171  
172 # Reinstall the new cookie into the params that will be passed
173 # to our base header function
174 #
175 $header_hr->{'Set-cookie'}=$cookie;
176  
177  
178 }
179  
180  
181 # Set in class _self area so will be propogated to next blessed self ref
182 #
183 $self->{'_session_id'}=$session_id;
184  
185  
186 # All done, chain to next handler
187 #
188 $self->SUPER::handler($r, @_[2..$#_]);
189  
190  
191 }
192  
193  
194 sub session_id {
195  
196  
197 # Accessor for session_id var, set in handler above
198 #
199 my $self=shift();
200 return $self->{'_session_id'};
201  
202  
203 }
204  
205 __END__