blib/lib/PAGI/Middleware/Session/Store/Cookie.pm

filter: Statementstmt 95.5% 65 / 68 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 91.6% 99 / 108 CC 9 SCAR 8.1
CC
9
Cov
92%
_decrypt
4.0
_random_bytes
3.5
get
3.0
CRAP 2.3
↑ Summary
1 package PAGI::Middleware::Session::Store::Cookie;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.001003';
7  
8 1 ▶use parent 'PAGI::Middleware::Session::Store';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::SHA qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 PAGI::Middleware::Session::Store::Cookie - Encrypted client-side session store
17  
18 =head1 SYNOPSIS
19  
20 use PAGI::Middleware::Session::Store::Cookie;
21  
22 my $store = PAGI::Middleware::Session::Store::Cookie->new(
23 secret => 'at-least-32-bytes-of-secret-key!',
24 );
25  
26 =head1 DESCRIPTION
27  
28 Stores session data encrypted in the client cookie itself using AES-256-GCM
29 (authenticated encryption). No server-side storage is needed.
30  
31 The C<set()> method returns the encrypted blob (not the session ID).
32 The C<get()> method accepts the encrypted blob and returns the decoded
33 session data, or undef if decryption/verification fails.
34  
35 B<Limitations:> Cookie size is limited to ~4KB. Large sessions will fail.
36 Session revocation requires server-side state (e.g., a blocklist).
37  
38 B<Note:> This module will be extracted to a separate CPAN distribution
39 in a future release.
40  
41 =cut
42  
43 sub new {
44 12 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
45 12 ▶ die "Store::Cookie requires 'secret'" unless $args{secret};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"secret"} 1 11 12
46  
47 11 my $self = $class->SUPER::new(%args);
48  
49 # Derive a 32-byte key from the secret via SHA-256
50 11 $self->{_key} = sha256($self->{secret});
51  
52 11 return $self;
53 }
54  
55 =head1 METHODS
56  
57 =head2 new
58  
59 my $store = PAGI::Middleware::Session::Store::Cookie->new(
60 secret => 'at-least-32-bytes-of-secret-key!',
61 );
62  
63 Creates a new cookie session store. The C<secret> parameter is required
64 and is used to derive the AES-256 encryption key via SHA-256.
65  
66 =head2 get
67  
68 my $data = await $store->get($encrypted_blob);
69  
70 Decrypts and decodes the blob. Returns a Future resolving to the session
71 hashref, or undef if the blob is invalid, tampered, or cannot be decoded.
72  
73 =cut
74  
75 sub get {
76 7 ▶ my ($self, $blob) = @_;
Subroutine get: called CC=3 SCAR=11.0
Pod documented
77  
78 7 ▶ return Future->done(undef) unless defined $blob && length $blob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $blob and length $blob 1 6 7
Condition66%2/3
defined $blob and length $blob
!ll&&!rl&&r
106
MC/DC50%1/2
defined $blob and length $blob
defined $blob length $blob
Truth table66%2/3
A and B
A= defined $blobB= length $blob
ABresult
0X0
100
111
79  
80 6 my $data = eval { $self->_decrypt($blob) };
81 6 return Future->done($data);
82 }
83  
84 =head2 set
85  
86 my $transport_value = await $store->set($id, $data);
87  
88 Encrypts the session data and returns a Future resolving to the encrypted
89 blob. This blob is what gets passed to C<State::inject()> for storage
90 in the response cookie. Nothing is stored server-side.
91  
92 =cut
93  
94 sub set {
95 8 ▶ my ($self, $id, $data) = @_;
Subroutine set: called CC=1 SCAR=0.0
Pod documented
96  
97 8 my $blob = $self->_encrypt($data);
98 8 return Future->done($blob);
99 }
100  
101 =head2 delete
102  
103 await $store->delete($id);
104  
105 No-op for cookie stores (client manages cookie lifetime).
106 Returns a Future resolving to 1.
107  
108 =cut
109  
110 sub delete {
111 1 ▶ my ($self, $id) = @_;
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
112 1 return Future->done(1);
113 }
114  
115 sub _encrypt {
116 8 ▶ my ($self, $data) = @_;
Subroutine _encrypt: called CC=1 SCAR=0.0
117  
118 8 require Crypt::AuthEnc::GCM;
119  
120 8 my $json = encode_json($data);
121  
122 # Generate random 12-byte IV (standard for AES-GCM)
123 8 my $iv = _random_bytes(12);
124  
125 8 my $gcm = Crypt::AuthEnc::GCM->new('AES', $self->{_key}, $iv);
126 8 my $ciphertext = $gcm->encrypt_add($json);
127 8 my $tag = $gcm->encrypt_done;
128  
129 # Pack: iv (12) + tag (16) + ciphertext
130 8 my $packed = $iv . $tag . $ciphertext;
131 8 return encode_base64($packed, '');
132 }
133  
134 sub _decrypt {
135 6 ▶ my ($self, $blob) = @_;
Subroutine _decrypt: called CC=4 SCAR=13.9
136  
137 6 require Crypt::AuthEnc::GCM;
138  
139 6 my $packed = decode_base64($blob);
140 6 ▶ return undef unless defined $packed && length($packed) > 28;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packed and length $packed > 28 1 5 6
Condition66%2/3
defined $packed and length $packed > 28
!ll&&!rl&&r
015
MC/DC50%1/2
defined $packed and length $packed > 28
defined $packed length $packed > 28
Truth table66%2/3
A and B
A= defined $packedB= length $packed > 28
ABresult
0X0
100
111
141  
142 # Unpack: iv (12) + tag (16) + ciphertext
143 5 my $iv = substr($packed, 0, 12);
144 5 my $tag = substr($packed, 12, 16);
145 5 my $ciphertext = substr($packed, 28);
146  
147 5 my $gcm = Crypt::AuthEnc::GCM->new('AES', $self->{_key}, $iv);
148 5 my $json = $gcm->decrypt_add($ciphertext);
149 5 my $valid = $gcm->decrypt_done($tag);
150  
151 5 ▶ return undef unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 2 3 5
152  
153 3 return decode_json($json);
154 }
155  
156 sub _random_bytes {
157 8 ▶ my ($n) = @_;
Subroutine _random_bytes: called CC=3 SCAR=12.6
158  
159 # Use /dev/urandom for cryptographically secure random bytes.
160 # Falls back to Perl's rand() if /dev/urandom is unavailable
161 # (e.g., on some non-Unix systems). See SECURITY section for details.
162 8 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 8 0 8
163 8 my $bytes;
164 8 ▶ read($fh, $bytes, $n) == $n or die "Short read from /dev/urandom";
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $bytes, $n) == $n or die "Short read from /dev/urandom" 0 8 8
165 8 close $fh;
166 8 return $bytes;
167 }
168  
169 0 warn "PAGI::Middleware::Session::Store::Cookie: /dev/urandom not available, "
170 . "falling back to Perl's rand() for IV generation. "
171 . "Install Crypt::URandom for secure random bytes on this platform.\n";
172 0 return join('', map { chr(int(rand(256))) } 1 .. $n);
173 }
174  
175 1 1;
176  
177 __END__
↑ Summary