blib/lib/SignalWire/Agents/SWML/Service.pm

filter: Statementstmt 88.6% 117 / 132 Branchbran 63.8% 23 / 36 Conditioncond 52.1% 24 / 46 MC/DCmcdc 18.5% 5 / 27 Subroutinesub 87.5% 28 / 32 Podpod 0.0% 0 / 3 totaltotal 71.3% 197 / 276 CC 37 SCAR 18.0
CC
37
Cov
77%
__ANON__
15.6
_read_body
12.0
AUTOLOAD
7.1
CRAP 6.0
« blib/lib/SignalWire/Agents/Relay/Message.pm ↑ Summary blib/lib/SignalWire/Agents/SWML/Schema.pm »
1 package SignalWire::Agents::SWML::Service;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use SignalWire::Agents::SWML::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use SignalWire::Agents::SWML::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use SignalWire::Agents::Logging;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has 'route' => (
13 is => 'rw',
14 3 ▶ default => sub { '/' },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 );
16  
17 has 'host' => (
18 is => 'rw',
19 4 ▶ default => sub { $ENV{SWML_HOST} // '0.0.0.0' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$ENV{"SWML_HOST"} // "0.0.0.0"
l!l
04
MC/DC0%0/1
$ENV{"SWML_HOST"} // "0.0.0.0"
$ENV{"SWML_HOST"}
Truth table50%1/2
A
A= $ENV{"SWML_HOST"}
Aresult
11
00
20 1 );
21  
22 has 'port' => (
23 is => 'rw',
24 4 ▶ default => sub { $ENV{SWML_PORT} // 3000 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$ENV{"SWML_PORT"} // 3000
l!l
04
MC/DC0%0/1
$ENV{"SWML_PORT"} // 3000
$ENV{"SWML_PORT"}
Truth table50%1/2
A
A= $ENV{"SWML_PORT"}
Aresult
11
00
25 1 );
26  
27 has 'basic_auth_user' => (
28 is => 'rw',
29 2 ▶ default => sub { $ENV{SWML_BASIC_AUTH_USER} // _random_hex(16) },
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
$ENV{"SWML_BASIC_AUTH_USER"} // _random_hex(16)
l!l&&r!l&&!r
020
MC/DC0%0/2
$ENV{"SWML_BASIC_AUTH_USER"} // _random_hex(16)
$ENV{"SWML_BASIC_AUTH_USER"} _random_hex(16)
Truth table33%1/3
A // B
A= $ENV{"SWML_BASIC_AUTH_USER"}B= _random_hex(16)
ABresult
1X1
011
000
30 1 );
31  
32 has 'basic_auth_password' => (
33 is => 'rw',
34 2 ▶ default => sub { $ENV{SWML_BASIC_AUTH_PASSWORD} // _random_hex(32) },
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
$ENV{"SWML_BASIC_AUTH_PASSWORD"} // _random_hex(32)
l!l&&r!l&&!r
020
MC/DC0%0/2
$ENV{"SWML_BASIC_AUTH_PASSWORD"} // _random_hex(32)
$ENV{"SWML_BASIC_AUTH_PASSWORD"} _random_hex(32)
Truth table33%1/3
A // B
A= $ENV{"SWML_BASIC_AUTH_PASSWORD"}B= _random_hex(32)
ABresult
1X1
011
000
35 1 );
36  
37 has 'document' => (
38 is => 'rw',
39 4 ▶ default => sub { SignalWire::Agents::SWML::Document->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 );
41  
42 has '_logger' => (
43 is => 'ro',
44 4 ▶ default => sub { SignalWire::Agents::Logging->get_logger('signalwire.swml_service') },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 );
46  
47 # Schema-driven verb auto-vivification via AUTOLOAD
48 1 our $AUTOLOAD;
49 1 my $_schema;
50  
51 6 ▶sub _get_schema {
Subroutine _get_schema: called CC=2 SCAR=7.1
52 6 ▶ $_schema //= SignalWire::Agents::SWML::Schema->instance();
Condition66%2/3
$_schema //= "SignalWire::Agents::SWML::Schema"->instance
l!l&&r!l&&!r
510
MC/DC0%0/2
$_schema //= "SignalWire::Agents::SWML::Schema"->instance
$_schema "SignalWire::Agents::SWML::Schema"->instance
Truth table66%2/3
A //= B
A= $_schemaB= "SignalWire::Agents::SWML::Schema"->instance
ABresult
1X1
011
000
53 6 return $_schema;
54 }
55  
56 8 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=7 SCAR=19.6
57 8 my $self = shift;
58 8 my $method = $AUTOLOAD;
59 8 $method =~ s/.*:://; # strip package name
60  
61 8 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 4 4 8
62  
63 4 my $schema = _get_schema();
64 4 ▶ if ($schema->has_verb($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->has_verb($method)) 3 1 4
65 # For 'sleep' verb: takes an integer (milliseconds), not a hashref
66 3 ▶ my $section = shift // 'main';
Condition50%1/2
shift() // "main"
l!l
30
MC/DC0%0/1
shift() // "main"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
67 3 my $data;
68 3 ▶ if ($method eq 'sleep') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'sleep') { } 1 2 3
69 1 ▶ $data = shift // 0;
Condition50%1/2
shift() // 0
l!l
10
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
70 # Ensure it is a numeric value
71 1 $data = int($data);
72 } else {
73 2 ▶ $data = shift // {};
Condition50%1/2
shift() // {}
l!l
20
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
74 }
75 3 $self->document->add_verb($section, $method, $data);
76 3 return $self;
77 }
78  
79 1 die "Can't locate method \"$method\" via package \"" . ref($self) . "\"";
80 }
81  
82 # Provide can() that knows about schema verbs
83 10 ▶sub can {
Subroutine can: called CC=4 SCAR=15.2
Pod undocumented
84 10 my ($self, $method) = @_;
85 # Check if it is a regular method first
86 10 my $code = $self->SUPER::can($method);
87 10 ▶ return $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 8 2 10
88 # Check schema verbs
89 2 my $schema = _get_schema();
90 2 ▶ if ($schema && $schema->has_verb($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema and $schema->has_verb($method)) 0 2 2
Condition33%1/3
$schema and $schema->has_verb($method)
!ll&&!rl&&r
020
MC/DC0%0/2
$schema and $schema->has_verb($method)
$schema $schema->has_verb($method)
Truth table33%1/3
A and B
A= $schemaB= $schema->has_verb($method)
ABresult
0X0
100
111
91 0 ▶ return sub { $self->$method(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 }
93 2 return undef;
94 }
95  
96 4 ▶sub _random_hex {
Subroutine _random_hex: called CC=4 SCAR=15.2
97 4 my ($len) = @_;
98 # Use /dev/urandom for cryptographically secure random bytes.
99 # Die on failure rather than falling back to weak randomness.
100 4 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 4 0 4
101 4 my $bytes;
102 4 my $read = read($fh, $bytes, $len);
103 4 close $fh;
104 4 ▶ if (defined $read && $read == $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read and $read == $len) 4 0 4
Condition33%1/3
defined $read and $read == $len
!ll&&!rl&&r
004
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
105 4 return unpack('H*', $bytes);
106 }
107 }
108 0 die "FATAL: Cannot generate secure random bytes - /dev/urandom unavailable or read failed. "
109 . "Set SWML_BASIC_AUTH_USER and SWML_BASIC_AUTH_PASSWORD environment variables instead.\n";
110 }
111  
112 8 ▶sub _timing_safe_compare {
Subroutine _timing_safe_compare: called CC=1 SCAR=0.0
113 8 my ($a, $b) = @_;
114 # Compare HMAC of both values with a fixed key for constant-time comparison
115 8 my $key = 'timing-safe-comparison-key';
116 8 my $hmac_a = hmac_sha256_hex($a, $key);
117 8 my $hmac_b = hmac_sha256_hex($b, $key);
118 8 return $hmac_a eq $hmac_b;
119 }
120  
121 3 ▶sub _check_basic_auth {
Subroutine _check_basic_auth: called CC=6 SCAR=18.5
122 3 my ($self, $env) = @_;
123 3 ▶ my $auth = $env->{HTTP_AUTHORIZATION} // '';
Condition100%2/2
$env->{"HTTP_AUTHORIZATION"} // ""
l!l
21
MC/DC100%1/1
$env->{"HTTP_AUTHORIZATION"} // ""
$env->{"HTTP_AUTHORIZATION"}
Truth table100%2/2
A
A= $env->{"HTTP_AUTHORIZATION"}
Aresult
11
00
124 3 ▶ return 0 unless $auth =~ /^Basic\s+(.+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth =~ /^Basic\s+(.+)$/i 1 2 3
125 2 my $decoded = MIME::Base64::decode_base64($1);
126 2 my ($user, $pass) = split(/:/, $decoded, 2);
127 2 ▶ return 0 unless defined $user && defined $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user and defined $pass 0 2 2
Condition33%1/3
defined $user and defined $pass
!ll&&!rl&&r
002
MC/DC0%0/2
defined $user and defined $pass
defined $user defined $pass
Truth table33%1/3
A and B
A= defined $userB= defined $pass
ABresult
0X0
100
111
128 2 ▶ return _timing_safe_compare($user, $self->basic_auth_user)
Condition66%2/3
_timing_safe_compare($user, $self->basic_auth_user) && _timing_safe_compare($pass, $self->basic_auth_password)
!ll&&!rl&&r
011
MC/DC50%1/2
_timing_safe_compare($user, $self->basic_auth_user) && _timing_safe_compare($pass, $self->basic_auth_password)
_timing_safe_compare($user, $self->basic_auth_user) _timing_safe_compare($pass, $self->basic_auth_password)
Truth table66%2/3
A && B
A= _timing_safe_compare($user, $self->basic_auth_user)B= _timing_safe_compare($pass, $self->basic_auth_password)
ABresult
0X0
100
111
129 && _timing_safe_compare($pass, $self->basic_auth_password);
130 }
131  
132 4 ▶sub _security_headers {
Subroutine _security_headers: called CC=1 SCAR=0.0
133 return (
134 4 'X-Content-Type-Options' => 'nosniff',
135 'X-Frame-Options' => 'DENY',
136 'X-XSS-Protection' => '1; mode=block',
137 'Cache-Control' => 'no-store, no-cache, must-revalidate',
138 'Pragma' => 'no-cache',
139 'Content-Type' => 'application/json',
140 );
141 }
142  
143 4 ▶sub _json_response {
Subroutine _json_response: called CC=1 SCAR=0.0
144 4 my ($status, $data) = @_;
145 4 my @headers = _security_headers();
146 4 my $body = JSON::encode_json($data);
147 4 return [$status, \@headers, [$body]];
148 }
149  
150 0 ▶sub _read_body {
Subroutine _read_body: not called CC=3 SCAR=24.8
151 0 my ($env) = @_;
152 0 my $input = $env->{'psgi.input'};
153 0 ▶ return '' unless $input;
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 0 0
154 0 local $/;
155 0 my $body = <$input>;
156 0 ▶ return $body // '';
Condition0%0/2
$body // ""
l!l
00
MC/DC0%0/1
$body // ""
$body
Truth table0%0/2
A
A= $body
Aresult
11
00
157 }
158  
159 1 ▶sub to_psgi_app {
Subroutine to_psgi_app: called CC=1 SCAR=0.1
Pod undocumented
160 1 my ($self) = @_;
161  
162 6 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=27.5
163 6 my ($env) = @_;
164 6 my $method = $env->{REQUEST_METHOD};
165 6 ▶ my $path = $env->{PATH_INFO} // '/';
Condition50%1/2
$env->{"PATH_INFO"} // "/"
l!l
60
MC/DC0%0/1
$env->{"PATH_INFO"} // "/"
$env->{"PATH_INFO"}
Truth table50%1/2
A
A= $env->{"PATH_INFO"}
Aresult
11
00
166  
167 # Health/ready endpoints (no auth)
168 6 ▶ if ($path eq '/health' || $path eq '/ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/health" or $path eq "/ready") 2 4 6
Condition100%3/3
$path eq "/health" or $path eq "/ready"
l!l&&r!l&&!r
114
MC/DC100%2/2
$path eq "/health" or $path eq "/ready"
$path eq "/health" $path eq "/ready"
Truth table100%3/3
A or B
A= $path eq "/health"B= $path eq "/ready"
ABresult
1X1
011
000
169 2 return _json_response(200, { status => 'ok' });
170 }
171  
172 # Normalize route for matching
173 4 my $route = $self->route;
174 4 $route =~ s{/$}{}; # strip trailing slash
175 4 $path =~ s{/$}{}; # strip trailing slash
176 4 ▶ $route = '' if $route eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $route eq "/" 0 4 4
177 4 ▶ $path = '' if $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/" 0 4 4
178  
179 # Check if this request matches our routes
180 4 my $is_swml_route = ($path eq $route);
181 4 my $is_swaig_route = ($path eq "$route/swaig");
182 4 my $is_post_prompt = ($path eq "$route/post_prompt");
183  
184 4 ▶ if ($is_swml_route || $is_swaig_route || $is_post_prompt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_swml_route or $is_swaig_route or $is_post_prompt) 3 1 4
Condition66%4/6
$is_swml_route or $is_swaig_route or $is_post_prompt
l!l&&r!l&&!r
301
$is_swml_route or $is_swaig_route
l!l&&r!l&&!r
301
MC/DC33%1/3
$is_swml_route or $is_swaig_route or $is_post_prompt
$is_swml_route $is_swaig_route $is_post_prompt
Truth table50%2/4
A or B or C
A= $is_swml_routeB= $is_swaig_routeC= $is_post_prompt
ABCresult
1XX1
01X1
0011
0000
185 # Require basic auth for protected routes
186 3 ▶ unless ($self->_check_basic_auth($env)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_basic_auth($env)) 2 1 3
187 return [
188 2 401,
189 ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="SignalWire"'],
190 ['Authentication required'],
191 ];
192 }
193  
194 1 ▶ if ($is_swml_route) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_swml_route) { } 1 0 1
elsif ($is_swaig_route) { } 0 0 0
elsif ($is_post_prompt) { } 0 0 0
195 1 return $self->_handle_swml_request($env);
196 } elsif ($is_swaig_route) {
197 0 return $self->_handle_swaig_request($env);
198 } elsif ($is_post_prompt) {
199 0 return $self->_handle_post_prompt($env);
200 }
201 }
202  
203 1 return _json_response(404, { error => 'Not found' });
204 1 };
205 }
206  
207 1 ▶sub _handle_swml_request {
Subroutine _handle_swml_request: called CC=1 SCAR=0.0
208 1 my ($self, $env) = @_;
209 1 my $doc = $self->render_swml($env);
210 1 return _json_response(200, $doc);
211 }
212  
213 1 ▶sub render_swml {
Subroutine render_swml: called CC=1 SCAR=0.0
Pod undocumented
214 1 my ($self, $env) = @_;
215 1 return $self->document->to_hash;
216 }
217  
218 0 ▶sub _handle_swaig_request {
Subroutine _handle_swaig_request: not called CC=1 SCAR=6.9
219 0 my ($self, $env) = @_;
220 0 return _json_response(200, { response => 'SWAIG endpoint' });
221 }
222  
223 0 ▶sub _handle_post_prompt {
Subroutine _handle_post_prompt: not called CC=1 SCAR=6.9
224 0 my ($self, $env) = @_;
225 0 return _json_response(200, { response => 'Post prompt endpoint' });
226 }
227  
228 1 1;
« blib/lib/SignalWire/Agents/Relay/Message.pm ↑ Summary blib/lib/SignalWire/Agents/SWML/Schema.pm »