blib/lib/SignalWire/Agents/Security/SessionManager.pm

filter: Statementstmt 97.9% 97 / 99 Branchbran 68.4% 26 / 38 Conditioncond 46.1% 12 / 26 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 10 totaltotal 75.4% 160 / 212 CC 30 SCAR 22.4
CC
30
Cov
83%
debug_token
20.7
validate_token
13.6
_random_hex
4.6
CRAP 9.4
« blib/lib/SignalWire/Agents/Contexts.pm ↑ Summary blib/lib/SignalWire/Agents/Prefabs/Concierge.pm »
1 package SignalWire::Agents::Security::SessionManager;
2 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 36 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 36 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶use Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 36 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has 'token_expiry_secs' => (
11 is => 'ro',
12 7 ▶ default => sub { 900 }, # 15 minutes
Subroutine __ANON__: called CC=1 SCAR=0.0
13 36 );
14  
15 has 'secret_key' => (
16 is => 'ro',
17 365 ▶ default => sub { _random_hex(32) },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 36 );
19  
20 has '_debug_mode' => (
21 is => 'rw',
22 371 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 36 );
24  
25 371 ▶sub _random_hex {
Subroutine _random_hex: called CC=4 SCAR=15.2
26 371 my ($len) = @_;
27 # Use /dev/urandom for cryptographically secure random bytes.
28 # Die on failure rather than falling back to weak randomness.
29 371 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 371 0 371
30 371 my $bytes;
31 371 my $read = read($fh, $bytes, $len);
32 371 close $fh;
33 371 ▶ if (defined $read && $read == $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read and $read == $len) 371 0 371
Condition33%1/3
defined $read and $read == $len
!ll&&!rl&&r
00371
MC/DC0%0/2
defined $read and $read == $len
defined $read $read == $len
Truth table33%1/3
A and B
A= defined $readB= $read == $len
ABresult
0X0
100
111
34 371 return unpack('H*', $bytes);
35 }
36 }
37 0 die "FATAL: Cannot generate secure random bytes - /dev/urandom unavailable. "
38 . "This is required for session security.\n";
39 }
40  
41 1 ▶sub _random_urlsafe {
Subroutine _random_urlsafe: called CC=2 SCAR=6.9
42 1 my ($len) = @_;
43 1 my $bytes = '';
44 1 for (1 .. $len) {
45 16 $bytes .= chr(int(rand(256)));
46 }
47 1 return MIME::Base64::encode_base64url($bytes, '');
48 }
49  
50 2 ▶sub create_session {
Subroutine create_session: called CC=2 SCAR=7.0
Pod undocumented
51 2 my ($self, $call_id) = @_;
52 2 ▶ $call_id //= _random_urlsafe(16);
Condition66%2/3
$call_id //= _random_urlsafe(16)
l!l&&r!l&&!r
110
MC/DC0%0/2
$call_id //= _random_urlsafe(16)
$call_id _random_urlsafe(16)
Truth table66%2/3
A //= B
A= $call_idB= _random_urlsafe(16)
ABresult
1X1
011
000
53 2 return $call_id;
54 }
55  
56 6 ▶sub generate_token {
Subroutine generate_token: called CC=1 SCAR=0.0
Pod undocumented
57 6 my ($self, $function_name, $call_id) = @_;
58 6 my $expiry = int(time()) + $self->token_expiry_secs;
59 6 my $nonce = _random_hex(8);
60  
61 6 my $message = "$call_id:$function_name:$expiry:$nonce";
62 6 my $signature = hmac_sha256_hex($message, $self->secret_key);
63  
64 6 my $token = "$call_id.$function_name.$expiry.$nonce.$signature";
65 6 return MIME::Base64::encode_base64url($token, '');
66 }
67  
68 # Alias
69 1 ▶sub create_tool_token {
Subroutine create_tool_token: called CC=1 SCAR=0.0
Pod undocumented
70 1 my ($self, $function_name, $call_id) = @_;
71 1 return $self->generate_token($function_name, $call_id);
72 }
73  
74 23 ▶sub _timing_safe_compare {
Subroutine _timing_safe_compare: called CC=1 SCAR=0.0
75 23 my ($a, $b) = @_;
76 # Compare HMAC of both values for constant-time comparison
77 23 my $key = 'timing-safe-token-comparison';
78 23 my $hmac_a = hmac_sha256_hex($a, $key);
79 23 my $hmac_b = hmac_sha256_hex($b, $key);
80 23 return $hmac_a eq $hmac_b;
81 }
82  
83 12 ▶sub validate_token {
Subroutine validate_token: called CC=13 SCAR=26.1
Pod undocumented
84 12 my ($self, $call_id, $function_name, $token) = @_;
85  
86 12 ▶ return 0 unless $call_id && $function_name && $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $call_id and $function_name and $token 3 9 12
Condition83%5/6
$call_id and $function_name and $token
!ll&&!rl&&r
129
$call_id and $function_name
!ll&&!rl&&r
1011
MC/DC66%2/3
$call_id and $function_name and $token
$call_id $function_name $token
Truth table75%3/4
A and B and C
A= $call_idB= $function_nameC= $token
ABCresult
0XX0
10X0
1100
1111
87  
88 9 my $decoded;
89 9 eval {
90 9 $decoded = MIME::Base64::decode_base64url($token);
91 };
92 9 ▶ return 0 if $@ || !$decoded;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $decoded 0 9 9
Condition33%1/3
$@ or not $decoded
l!l&&r!l&&!r
009
MC/DC0%0/2
$@ or not $decoded
$@ not $decoded
Truth table33%1/3
A or not(B)
A= $@B= not $decoded
ABresult
1X1
011
000
93  
94 9 my @parts = split(/\./, $decoded);
95 9 ▶ return 0 unless @parts == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 5 1 8 9
96  
97 8 my ($token_call_id, $token_function, $token_expiry, $token_nonce, $token_signature) = @parts;
98  
99 # Verify function matches
100 8 ▶ return 0 unless _timing_safe_compare($token_function, $function_name);
Branch: 1 branch
BranchTrueFalseTotal
unless _timing_safe_compare($token_function, $function_name) 1 7 8
101  
102 # Check expiry
103 7 my $expiry = eval { int($token_expiry) };
104 7 ▶ return 0 if $@ || !defined $expiry;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $expiry 0 7 7
Condition33%1/3
$@ or not defined $expiry
l!l&&r!l&&!r
007
MC/DC0%0/2
$@ or not defined $expiry
$@ not defined $expiry
Truth table33%1/3
A or not(B)
A= $@B= not defined $expiry
ABresult
1X1
011
000
105 7 ▶ return 0 if $expiry < time();
Branch: 1 branch
BranchTrueFalseTotal
if $expiry < time 1 6 7
106  
107 # Recreate and verify signature
108 6 my $message = "$token_call_id:$token_function:$token_expiry:$token_nonce";
109 6 my $expected_signature = hmac_sha256_hex($message, $self->secret_key);
110 6 ▶ return 0 unless _timing_safe_compare($token_signature, $expected_signature);
Branch: 1 branch
BranchTrueFalseTotal
unless _timing_safe_compare($token_signature, $expected_signature) 1 5 6
111  
112 # Verify call_id
113 5 ▶ return 0 unless _timing_safe_compare($token_call_id, $call_id);
Branch: 1 branch
BranchTrueFalseTotal
unless _timing_safe_compare($token_call_id, $call_id) 1 4 5
114  
115 4 return 1;
116 }
117  
118 # Alias with different parameter order for backward compat
119 1 ▶sub validate_tool_token {
Subroutine validate_tool_token: called CC=1 SCAR=0.0
Pod undocumented
120 1 my ($self, $function_name, $token, $call_id) = @_;
121 1 return $self->validate_token($call_id, $function_name, $token);
122 }
123  
124 # Legacy methods - no-ops for API compat
125 1 ▶sub activate_session { return 1 }
Subroutine activate_session: called CC=1 SCAR=0.0
Pod undocumented
126 1 ▶sub end_session { return 1 }
Subroutine end_session: called CC=1 SCAR=0.0
Pod undocumented
127 1 ▶sub get_session_metadata { return {} }
Subroutine get_session_metadata: called CC=1 SCAR=0.0
Pod undocumented
128 1 ▶sub set_session_metadata { return 1 }
Subroutine set_session_metadata: called CC=1 SCAR=0.0
Pod undocumented
129  
130 3 ▶sub debug_token {
Subroutine debug_token: called CC=13 SCAR=30.3
Pod undocumented
131 3 my ($self, $token) = @_;
132 3 ▶ return { error => 'debug mode not enabled' } unless $self->_debug_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_debug_mode 1 2 3
133  
134 2 my $decoded;
135 2 eval { $decoded = MIME::Base64::decode_base64url($token) };
136 2 ▶ if ($@ || !$decoded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $decoded) 0 2 2
Condition33%1/3
$@ or not $decoded
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ or not $decoded
$@ not $decoded
Truth table33%1/3
A or not(B)
A= $@B= not $decoded
ABresult
1X1
011
000
137 return {
138 0 ▶ valid_format => JSON::false,
Branch: 1 branch
BranchTrueFalseTotal
defined $token ? : 0 0 0
Condition0%0/2
$@ // "decode failed"
l!l
00
MC/DC0%0/1
$@ // "decode failed"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
139 error => $@ // 'decode failed',
140 token_length => defined $token ? length($token) : 0,
141 };
142 }
143  
144 2 my @parts = split(/\./, $decoded);
145 2 ▶ if (@parts != 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts != 5) 1 1 2
146 return {
147 1 valid_format => JSON::false,
148 parts_count => scalar @parts,
149 token_length => length($token),
150 };
151 }
152  
153 1 my ($tc, $tf, $te, $tn, $ts) = @parts;
154 1 my $current = int(time());
155 1 my $expiry = eval { int($te) };
156 1 ▶ my $expired = defined $expiry ? ($expiry < $current ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $expiry ? : 1 0 1
$expiry < $current ? : 0 1 1
157  
158 return {
159 1 ▶ valid_format => JSON::true,
Branch: 3 branches
BranchTrueFalseTotal
length $tc > 8 ? : 0 1 1
length $ts > 8 ? : 1 0 1
defined $expiry && !$expired ? : 1 0 1
Condition33%1/3
defined $expiry and not $expired
!ll&&!rl&&r
001
MC/DC0%0/2
defined $expiry and not $expired
defined $expiry not $expired
Truth table33%1/3
A and not(B)
A= defined $expiryB= not $expired
ABresult
0X0
100
111
160 components => {
161 call_id => (length($tc) > 8 ? substr($tc, 0, 8) . '...' : $tc),
162 function => $tf,
163 expiry => $te,
164 nonce => $tn,
165 signature => (length($ts) > 8 ? substr($ts, 0, 8) . '...' : $ts),
166 },
167 status => {
168 current_time => $current,
169 is_expired => $expired,
170 expires_in_seconds => (defined $expiry && !$expired ? $expiry - $current : 0),
171 },
172 };
173 }
174  
175 36 1;
« blib/lib/SignalWire/Agents/Contexts.pm ↑ Summary blib/lib/SignalWire/Agents/Prefabs/Concierge.pm »