| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $blob and length $blob
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $packed and length $packed > 28
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 163 | 8 | my $bytes; | |||||||||||||||||||||||||||
| 164 | 8 | ▶ | read($fh, $bytes, $n) == $n or die "Short read from /dev/urandom"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||