blib/lib/HTTP/Message/Signature.pm

filter: Statementstmt 74.0% 191 / 258 Branchbran 42.1% 43 / 102 Conditioncond 18.0% 11 / 61 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 71.4% 40 / 56 Podpod 80.0% 4 / 5 totaltotal 55.4% 289 / 521 CC 90 SCAR 34.5
CC
90
Cov
58%
check_signature
93.2
__ANON__
72.0
_build_public_keys_json
72.0
CRAP 31.4
↑ Summary
1 package HTTP::Message::Signature;
2  
3 # ABSTRACT: Verify HTTP Message Signatures (RFC 9421)
4  
5 4 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Crypt::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Crypt::PK::Ed25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Crypt::PK::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Hash::Ordered;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use HTTP::Message 6.41;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use HTTP::StructuredField v0.0.5;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use JSON qw( decode_json ); # used by CryptX
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use List::Util 1.29 qw( any first max pairmap );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use PerlX::Maybe qw( maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Ref::Util qw( is_blessed_ref is_plain_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use String::Util qw( trim unquote );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Types::Common qw( Bool HashRef InstanceOf NonEmptyStr PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Types::URI qw( Uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # RECOMMEND PREREQ: Ref::Util::XS
29 # RECOMMEND PREREQ: Type::Tiny::XS
30  
31 4 ▶use experimental qw( lexical_subs signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 4 our $VERSION = 'v0.2.0';
36  
37  
38  
39 4 has message => (
40 is => 'ro',
41 isa => InstanceOf [qw/ HTTP::Message Plack::Request /],
42 required => 1,
43 handles => [qw/ as_string headers /],
44 );
45  
46  
47 has is_request => (
48 is => 'lazy',
49 isa => Bool,
50 0 ▶ builder => sub($self) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Subroutine _build_is_request: not called CC=2 SCAR=17.9
51 0 my $message = $self->message;
52 0 ▶ return $message->isa("HTTP::Request") || $message->isa("Plack::Request");
Condition0%0/6
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
l!l&&r!l&&!r
000
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
l!l&&r!l&&!r
000
MC/DC0%0/2
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
$message->isa('HTTP::Request') $message->isa('Plack::Request')
MC/DC0%0/2
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
$message->isa('HTTP::Request') $message->isa('Plack::Request')
Truth table0%0/3
A || B
A= $message->isa('HTTP::Request')B= $message->isa('Plack::Request')
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $message->isa('HTTP::Request')B= $message->isa('Plack::Request')
ABresult
1X1
011
000
53 },
54 4 init_arg => undef,
55 );
56  
57  
58 has is_response => (
59 is => 'lazy',
60 isa => Bool,
61 9 ▶ builder => sub($self) { $self->message->isa("HTTP::Response") },
Subroutine _build_is_response: called CC=1 SCAR=0.0
62 4 init_arg => undef,
63 );
64  
65  
66 4 has request => (
67 is => 'ro',
68 isa => InstanceOf [qw/ HTTP::Request Plack::Request /],
69 predicate => 1,
70 );
71  
72  
73 has user_agent => (
74 is => 'lazy',
75 isa => InstanceOf ['LWP::UserAgent'],
76 0 ▶ builder => sub($self) {
Subroutine _build_user_agent: not called CC=1 SCAR=6.9
77 0 return LWP::UserAgent->new( max_size => 8192, timeout => 10 );
78 },
79 4 );
80  
81  
82 has signature_agent => (
83 is => 'lazy',
84 isa => NonEmptyStr,
85 0 ▶ coerce => sub($uri) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
86 0 ▶ if (is_blessed_ref($uri) && $uri->isa("URI")) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")) 0 0 0
Condition0%0/3
Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")
Ref::Util::XS::is_blessed_ref($uri) $uri->isa("URI")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($uri)B= $uri->isa("URI")
ABresult
0X0
100
111
87 0 return $uri->scheme . "://" . $uri->authority;
88 }
89 0 return "$uri";
90 },
91 0 ▶ builder => sub($self) {
Subroutine _build_signature_agent: not called CC=6 SCAR=37.4
92 0 ▶ my $agent = join(",", $self->headers->header("signature-agent") // "" );
Condition0%0/2
$self->headers->header("signature-agent") // ""
l!l
00
MC/DC0%0/1
$self->headers->header("signature-agent") // ""
$self->headers->header("signature-agent")
Truth table0%0/2
A
A= $self->headers->header("signature-agent")
Aresult
11
00
93 0 ▶ if ( !$agent && $self->is_response ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $agent and $self->is_response) 0 0 0
Condition0%0/3
not $agent and $self->is_response
!ll&&!rl&&r
000
MC/DC0%0/2
not $agent and $self->is_response
not $agent $self->is_response
Truth table0%0/3
not(A) and B
A= not $agentB= $self->is_response
ABresult
0X0
100
111
94 0 return $self->request->uri;
95 }
96 0 ▶ if ( $agent =~ /\A [a-z\*][a-z0-1_\-\.\*]* = /ax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent =~ /\A [a-z\*][a-z0-1_\-\.\*]* = /ax) 0 0 0
97 0 ▶ my ($key) = $self->headers->header("signature-agent-key") || $self->signature_labels;
Condition0%0/3
$self->headers->header('signature-agent-key') || $self->signature_labels
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->headers->header('signature-agent-key') || $self->signature_labels
$self->headers->header('signature-agent-key') $self->signature_labels
Truth table0%0/3
A || B
A= $self->headers->header('signature-agent-key')B= $self->signature_labels
ABresult
1X1
011
000
98 0 my $agents = HTTP::StructuredField->parse_dictionary( $agent );
99 0 $agent = $agents->get( unquote($key) )->value;
100 }
101 0 return $agent;
102 }
103 4 );
104  
105  
106 has signature_agent_uri => (
107 is => 'lazy',
108 isa => Uri,
109 coerce => 1,
110 0 ▶ builder => sub($self) {
Subroutine _build_signature_agent_uri: not called CC=3 SCAR=24.8
111 0 my $uri = URI->new( unquote( $self->signature_agent ) );
112 0 ▶ carp "signature-agent header contains a full URL" if $uri->path && $uri->path ne "/";
Branch: 2 branches
BranchTrueFalseTotal
if $uri->path and $uri->path ne "/" 0 0 0
if $uri->path and $uri->path ne "/" 0 0 0
Condition0%0/6
$uri->path and $uri->path ne "/"
!ll&&!rl&&r
000
$uri->path and $uri->path ne "/"
!ll&&!rl&&r
000
MC/DC0%0/2
$uri->path and $uri->path ne "/"
$uri->path $uri->path ne "/"
MC/DC0%0/2
$uri->path and $uri->path ne "/"
$uri->path $uri->path ne "/"
Truth table0%0/3
A and B
A= $uri->pathB= $uri->path ne "/"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $uri->pathB= $uri->path ne "/"
ABresult
0X0
100
111
113 0 $uri->path("/.well-known/http-message-signatures-directory");
114 0 return $uri;
115 },
116 4 );
117  
118  
119 has signature_input => (
120 is => 'lazy',
121 isa => InstanceOf ['Hash::Ordered'],
122 11 ▶ builder => sub($self) {
Subroutine _build_signature_input: called CC=1 SCAR=0.4
123 0 my @input = $self->headers->header("signature-input");
124 11 return HTTP::StructuredField->parse_dictionary( join( ',', @input ) );
125 },
126 4 handles => {
127 signature_labels => 'keys',
128 },
129 );
130  
131  
132 has signature_header => (
133 is => 'lazy',
134 isa => InstanceOf ['Hash::Ordered'],
135 8 ▶ builder => sub($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 8 my @input = $self->headers->header("signature");
137 8 return HTTP::StructuredField->parse_dictionary( join( ',', @input ) );
138 },
139 4 );
140  
141  
142 has public_keys_json => (
143 is => 'lazy',
144 isa => NonEmptyStr,
145 0 ▶ builder => sub($self) {
Subroutine __ANON__: not called CC=8 SCAR=42.8
146  
147 0 state $types = { map { $_ => 1 } qw[ application/http-message-signatures-directory+json application/json ] };
148  
149 0 my $uri = $self->signature_agent_uri;
150 0 ▶ croak "agent_uri should be an https URL" unless $uri->scheme eq "https";
Branch: 2 branches
BranchTrueFalseTotal
unless $uri->scheme eq "https" 0 0 0
unless $uri->scheme eq "https" 0 0 0
151  
152 0 my $req = HTTP::Request->new(
153 GET => $uri,
154 [
155 'Accept' => join( ",", keys $types->%* ),
156 'User-Agent' => sprintf( 'Perl-%s-%s', __PACKAGE__, $VERSION ),
157 'Want-Content-Digest' => 'sha-512=10, sha-256=9',
158 ]
159 );
160  
161 0 my $res = $self->user_agent->request($req);
162  
163 0 ▶ if ( $res->is_success && $types->{ $res->content_type } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success and $$types{$res->content_type}) 0 0 0
Condition0%0/6
$res->is_success and $$types{$res->content_type}
!ll&&!rl&&r
000
$res->is_success and $$types{$res->content_type}
!ll&&!rl&&r
000
MC/DC0%0/2
$res->is_success and $$types{$res->content_type}
$res->is_success $$types{$res->content_type}
MC/DC0%0/2
$res->is_success and $$types{$res->content_type}
$res->is_success $$types{$res->content_type}
Truth table0%0/3
A and B
A= $res->is_successB= $$types{$res->content_type}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $res->is_successB= $$types{$res->content_type}
ABresult
0X0
100
111
164  
165 0 $res->max_body_size( $self->max_public_keys_json_size );
166  
167 0 ▶ if ( $self->verify_public_keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify_public_keys) 0 0 0
168  
169 0 ▶ if ( $res->header("Signature-Input") && $res->header("Signature") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header('Signature-Input') and $res->header('Signature')) { } 0 0 0
Condition0%0/3
$res->header("Signature-Input") and $res->header("Signature")
!ll&&!rl&&r
000
MC/DC0%0/2
$res->header("Signature-Input") and $res->header("Signature")
$res->header("Signature-Input") $res->header("Signature")
Truth table0%0/3
A and B
A= $res->header("Signature-Input")B= $res->header("Signature")
ABresult
0X0
100
111
170  
171 0 my $check = __PACKAGE__->new(
172 request => $req,
173 response => $res,
174 require_tag => "http-message-signatures-directory",
175 public_keys_json => $res->decoded_content,
176 );
177  
178 0 ▶ $check->verify( { check_digest => 1 } ) or croak "signatures-directory was not signed";
Branch: 1 branch
BranchTrueFalseTotal
$check->verify({"check_digest", 1}) or croak("signatures-directory was not signed") 0 0 0
179  
180 }
181 else {
182 0 carp "signatures-directory response was unsigned";
183 }
184  
185 }
186  
187 0 return $res->decoded_content;
188 }
189  
190 0 croak "error retrieving ${uri}";
191 }
192 4 );
193  
194  
195 4 has max_public_keys_json_size => (
196 is => 'ro',
197 isa => PositiveInt,
198 default => 16384,
199 );
200  
201  
202 4 has verify_public_keys => (
203 is => 'ro',
204 isa => Bool,
205 default => 0,
206 );
207  
208  
209 has public_keys => (
210 is => 'lazy',
211 isa => HashRef, # TODO make strict validation optional
212 2 ▶ builder => sub($self) {
Subroutine _build_public_keys: called CC=2 SCAR=6.9
213 2 my $pks = decode_json( $self->public_keys_json );
214 2 my $max = $self->max_keys;
215 2 ▶ if ( defined $pks->{keys}[$max] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pks->{"keys"}[$max]) 1 1 2
216 1 splice( $pks->{keys}->@*, $max );
217 1 carp "public_keys truncated to the first ${max} keys";
218 }
219  
220 # TODO cache keys here, by signature_agent_uri->authority and key id
221  
222 2 return $pks;
223 },
224 4 );
225  
226  
227  
228 4 has max_keys => (
229 is => 'ro',
230 isa => PositiveInt,
231 default => 100,
232 );
233  
234  
235 4 has require_tag => (
236 is => 'ro',
237 isa => NonEmptyStr,
238 predicate => 1,
239 );
240  
241  
242 8 ▶sub has_signature($self) {
Subroutine has_signature: called CC=2 SCAR=8.1
Pod documented
243  
244 8 my $headers = $self->headers;
245 8 ▶ return $headers->header('signature-input') # check for this first as per RFC
Condition33%1/3
$headers->header('signature-input') && $headers->header('signature')
!ll&&!rl&&r
008
MC/DC0%0/2
$headers->header('signature-input') && $headers->header('signature')
$headers->header('signature-input') $headers->header('signature')
Truth table33%1/3
A && B
A= $headers->header('signature-input')B= $headers->header('signature')
ABresult
0X0
100
111
246 && $headers->header('signature');
247  
248 }
249  
250  
251 12 ▶sub is_expired( $self, $label = undef ) {
Subroutine is_expired: called CC=4 SCAR=15.2
Pod documented
252 12 ▶ my $input = $self->_get_input_from_label( $label // $self->_get_default_label );
Condition33%1/3
$label // $self->_get_default_label
l!l&&r!l&&!r
0120
MC/DC0%0/2
$label // $self->_get_default_label
$label $self->_get_default_label
Truth table33%1/3
A // B
A= $labelB= $self->_get_default_label
ABresult
1X1
011
000
253 12 ▶ my $expires = $input->params->get("expires") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $expires = $input->params->get("expires") or return undef 12 0 12
254 12 return time > $expires->value;
255 }
256  
257  
258 8 ▶sub verify( $self, $options = undef ) {
Subroutine verify: called CC=3 SCAR=13.1
Pod documented
259  
260 8 ▶ if ( my $err = $self->check_signature($options) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->check_signature($options)) 0 8 8
261 0 carp $err;
262 0 return 0;
263 }
264  
265 8 return 1;
266 }
267  
268 8 ▶sub check_signature( $self, $options = undef ) {
Subroutine check_signature: called CC=29 SCAR=45.3
Pod documented
269  
270 8 ▶ return "no signature" unless $self->has_signature;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_signature 0 8 8
271  
272 8 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
80
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
273 8 ▶ $options = { label => $options } unless is_plain_hashref($options);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($options) 0 8 8
274  
275 8 ▶ my $label = $options->{label} //= $self->_get_default_label;
Condition33%1/3
$options->{"label"} //= $self->_get_default_label
l!l&&r!l&&!r
080
MC/DC0%0/2
$options->{"label"} //= $self->_get_default_label
$options->{"label"} $self->_get_default_label
Truth table33%1/3
A //= B
A= $options->{"label"}B= $self->_get_default_label
ABresult
1X1
011
000
276 8 my $input = $self->_get_input_from_label($label);
277  
278 8 ▶ if ( $options->{check_expires} //= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"check_expires"} //= 1) 4 4 8
Condition50%1/2
$options->{"check_expires"} //= 1
l!l
80
MC/DC0%0/1
$options->{"check_expires"} //= 1
$options->{"check_expires"}
Truth table50%1/2
A
A= $options->{"check_expires"}
Aresult
11
00
279 4 ▶ return "signature is expired" if $self->is_expired;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_expired 0 4 4
280 }
281  
282 8 ▶ if ( $options->{check_created} //= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"check_created"} //= 1) 8 0 8
Condition50%1/2
$options->{"check_created"} //= 1
l!l
08
MC/DC0%0/1
$options->{"check_created"} //= 1
$options->{"check_created"}
Truth table50%1/2
A
A= $options->{"check_created"}
Aresult
11
00
283 8 my $created = $input->params->get("created");
284 8 ▶ return "signature is postdated" if $created && $created->value > time;
Branch: 1 branch
BranchTrueFalseTotal
if $created and $created->value > time 0 8 8
Condition33%1/3
$created and $created->value > time
!ll&&!rl&&r
080
MC/DC0%0/2
$created and $created->value > time
$created $created->value > time
Truth table33%1/3
A and B
A= $createdB= $created->value > time
ABresult
0X0
100
111
285 }
286  
287 8 ▶ if ( $self->has_require_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_require_tag) 8 0 8
288 8 my $check = $input->params->get("tag");
289 8 my $tag = $self->require_tag;
290 8 ▶ return "tag is not '${tag}'" unless $check && $check->value eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $check and $check->value eq $tag 0 8 8
Condition33%1/3
$check and $check->value eq $tag
!ll&&!rl&&r
008
MC/DC0%0/2
$check and $check->value eq $tag
$check $check->value eq $tag
Truth table33%1/3
A and B
A= $checkB= $check->value eq $tag
ABresult
0X0
100
111
291 }
292  
293 8 state $algs = {
294 "ed25519" => { class => "Crypt::PK::Ed25519", key => { kty => "OKP", crv => "Ed25519" } },
295 "rsa-pss-sha512" => { class => "Crypt::PK::RSA", key => { kty => "RSA", alg => "PS512" }, hash => [qw/ SHA512 pss 64 /] },
296 };
297  
298 8 ▶ my $alg = eval { $input->params->get("alg")->value } or croak "missing alg parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $alg = eval { do { $input->params->get("alg")->value } } or croak("missing alg parameter") 8 0 8
299 8 ▶ my $meta = $algs->{ $alg } or croak "unsupported algorithm";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $algs->{$alg} or croak("unsupported algorithm") 8 0 8
300  
301 8 ▶ my $keyid = $input->params->get("keyid") or croak "no keyid was found in the signature-input";
Branch: 1 branch
BranchTrueFalseTotal
my $keyid = $input->params->get("keyid") or croak("no keyid was found in the signature-input") 8 0 8
302 8 my $kid = unquote( $keyid->value );
303  
304 8 ▶ my $key = $self->_get_key($kid)
Branch: 1 branch
BranchTrueFalseTotal
my $key = $self->_get_key($kid) or return "keyid '${kid}' was not found in the public keys" 8 0 8
305 or return "keyid '${kid}' was not found in the public keys";
306  
307 # TODO: add support for looking up keys from a cache based on agent URL and key id, and alg
308  
309 8 for my $field ( keys $meta->{key}->%* ) {
310 16 ▶ $key->{$field} eq $meta->{key}{$field} or croak "keyid ${kid} is incompatible with ${alg}";
Branch: 1 branch
BranchTrueFalseTotal
$key->{$field} eq $meta->{"key"}{$field} or croak("keyid $kid is incompatible with $alg") 16 0 16
311 }
312  
313 8 my $class = $meta->{class};
314  
315 8 ▶ my @params = $meta->{hash} ? $meta->{hash}->@* : ();
Branch: 1 branch
BranchTrueFalseTotal
$meta->{'hash'} ? : 4 4 8
316  
317 8 my $pk = $class->new($key);
318  
319 8 my $sig = $self->signature_header->get($label)->value;
320  
321 8 my $base = $self->_generate_signature_base($options);
322  
323 8 ▶ $pk->verify_message( $sig, $base, @params )
Branch: 1 branch
BranchTrueFalseTotal
$pk->verify_message($sig, $base, @params) or return "signature failed" 8 0 8
324 or return "signature failed";
325  
326 8 ▶ if ( $self->is_response && $options->{check_digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_response and $options->{"check_digest"}) 0 8 8
Condition33%1/3
$self->is_response and $options->{"check_digest"}
!ll&&!rl&&r
800
MC/DC0%0/2
$self->is_response and $options->{"check_digest"}
$self->is_response $options->{"check_digest"}
Truth table33%1/3
A and B
A= $self->is_responseB= $options->{"check_digest"}
ABresult
0X0
100
111
327  
328 0 ▶ if (my @digests = $self->headers->header("content-digest") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@digests) = $self->headers->header("content-digest")) 0 0 0
329  
330 0 ▶ unless ( any { $_->value eq "content-digest" } $input->value->@* ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_->value eq "content-digest"; } , @{$input->value})) 0 0 0
331 0 carp "content-digest is not part of the signature-input";
332 }
333  
334 0 my $digests = HTTP::StructuredField->parse_dictionary( join(",", @digests ) );
335  
336 0 ▶ my $alg = first { $digests->exists($_) } qw/ sha-512 sha-256 /;
Subroutine __ANON__: not called CC=1 SCAR=6.9
337  
338 0 my $d = Crypt::Digest->new( uc $alg =~ s/\-//g );
339 # Note: RFC 9530 the Content-Digest is of the encoded content
340 0 $d->add( $self->message->content );
341 0 ▶ unless ( $d->digest eq $digests->get($alg)->value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d->digest eq $digests->get($alg)->value) 0 0 0
342 0 return "content-digest ${alg} does not match";
343 }
344  
345 }
346 }
347  
348 8 return undef;
349 }
350  
351 8 ▶sub _get_key( $self, $kid ) {
Subroutine _get_key: called CC=1 SCAR=0.0
352  
353 8 ▶ return first { $_->{kid} eq $kid } $self->public_keys->{keys}->@*;
Subroutine __ANON__: called CC=1 SCAR=0.0
354  
355 }
356  
357 20 ▶sub _get_default_label($self) {
Subroutine _get_default_label: called CC=2 SCAR=7.1
358 20 my ($label) = $self->signature_labels;
359 20 ▶ croak "no labels found" unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 20 20
360 20 return $label;
361 }
362  
363 31 ▶sub _get_input_from_label( $self, $label ) {
Subroutine _get_input_from_label: called CC=2 SCAR=7.2
364 31 ▶ my $input = $self->signature_input->get($label) or croak "signature-input label ${label} was not found";
Branch: 1 branch
BranchTrueFalseTotal
my $input = $self->signature_input->get($label) or croak("signature-input label $label was not found") 31 0 31
365 31 return $input;
366 }
367  
368 11 ▶sub _generate_signature_base( $self, $options ) {
Subroutine _generate_signature_base: called CC=6 SCAR=18.0
369  
370 11 my $label = $options->{label};
371  
372 11 my @buffer;
373  
374 11 my $seen = Hash::Ordered->new;
375  
376 28 ▶ my sub _process_header_field( $field, $value ) {
Subroutine _process_header_field: called CC=3 SCAR=11.4
377  
378 # TODO serialize line as per "component-identifier ABNF rule"
379 # TODO canonicalize the field value
380 28 ▶ my $line = sprintf( '%s: %s', lc($field), $value // "" );
Condition50%1/2
$value // ""
l!l
280
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
381 28 ▶ croak "component has already been added to the sginature base" if $seen->exists( lc $field );
Branch: 1 branch
BranchTrueFalseTotal
if $seen->exists(lc $field) 0 28 28
382 28 $seen->set( lc $field => 1 );
383 28 push @buffer, $line;
384 }
385  
386 11 my $input = $self->_get_input_from_label($label);
387 11 ▶ croak "label ${label} is not a list" unless $input->is_inner_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $input->is_inner_list 0 11 11
388  
389 11 for my $field ( $input->value->@* ) {
390  
391 17 my $name = unquote($field->value, braces => 1 );
392 17 ▶ if ( substr($name, 0, 1) eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, 0, 1) eq '@') { } 11 6 17
393 11 _process_header_field( $self->_get_derived_field( $field, $options ) );
394 }
395 else {
396  
397 6 my $header = $self->headers->header($name);
398 6 ▶ if ($field->has_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->has_params) { } 2 4 6
399  
400 2 my ($key) = $field->params->get("key")->value;
401 2 my $dict = HTTP::StructuredField->parse_dictionary($header);
402 2 my $param = $dict->get($key);
403  
404 # TODO: improve quoting
405 2 _process_header_field( "\"${name}\";key=\"${key}\"" => sprintf('"%s"', $param->value ) );
406  
407 }
408 else {
409 4 _process_header_field( "\"${name}\"" => $header );
410 }
411 }
412  
413 }
414  
415 11 my $header = $self->headers->header("signature-input");
416 11 ▶ croak "unexpected signature-input" unless substr( $header, 0, length($label) + 1 ) eq "${label}=";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($header, 0, length($label) + 1) eq "$label=" 0 11 11
417 11 my $params = substr( $header, length($label) + 1 );
418 11 _process_header_field( '"@signature-params"' => $params );
419  
420 11 return join( "\n", @buffer );
421 }
422  
423 11 ▶sub _get_derived_field( $self, $field, $options ) {
Subroutine _get_derived_field: called CC=5 SCAR=22.1
424  
425 11 my $name = $field->value;
426  
427 # Note: HTTP::Message has uri_canonical but Plack::Request does not.
428  
429 state $handlers = {
430 0 ▶ '@status' => sub( $message, $, $ ) { $message->code },
Subroutine __ANON__: not called CC=1 SCAR=6.9
431 0 ▶ '@method' => sub( $message, $, $ ) { $message->method },
Subroutine __ANON__: not called CC=1 SCAR=6.9
432 0 ▶ '@target-uri' => sub( $message, $, $ ) { $message->uri },
Subroutine __ANON__: not called CC=1 SCAR=6.9
433 11 ▶ '@authority' => sub( $message, $, $ ) { $message->uri->authority },
Subroutine __ANON__: called CC=1 SCAR=0.0
434 0 ▶ '@scheme' => sub( $message, $, $ ) { $message->uri->scheme },
Subroutine __ANON__: not called CC=1 SCAR=6.9
435 0 ▶ '@path' => sub( $message, $, $ ) { $message->uri->path },
Subroutine __ANON__: not called CC=1 SCAR=6.9
436 0 ▶ '@query' => sub( $message, $, $ ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
437 0 my $query = $message->uri->query;
438 0 ▶ if ( defined $query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query) 0 0 0
439 0 return '?' . $query;
440 }
441 0 return undef;
442 },
443 0 ▶ '@query-param' => sub( $message, $, $params ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
444 0 ▶ if ( my ($key) = $params->keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($key) = $params->keys) 0 0 0
445 # Note: RFC 9421 Sec 2.2.8 says named parameters with multiple values must not be included
446 0 my @values = $message->uri->query_param($key);
447 0 ▶ croak "multipe query-param values for '${key}'" if @values > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @values > 1 0 0 0
448 0 return $values[0];
449 }
450 }
451 11 };
452  
453 11 ▶ croak "unsupported field ${name}" unless exists $handlers->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $handlers->{$name} 0 11 11
454  
455 11 my $quoted = "\"${name}\"";
456 11 my $message = $self->message;
457  
458 11 my ($param, @extra) = $field->params->keys;
459 11 ▶ if ( defined $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param) 1 10 11
460 1 ▶ croak "extra parameters in field ${name}" if @extra;
Branch: 1 branch
BranchTrueFalseTotal
if @extra 0 1 1
461 1 $quoted .= ";" . unquote($param);
462 1 ▶ if ( $param eq "req" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "req") 1 0 1
463 1 $message = $self->request;
464 }
465 }
466  
467 11 return ( maybe $quoted => $handlers->{$name}->( $message, $options, $field->params ) );
468 }
469  
470  
471 13 ▶sub BUILDARGS( $, @args ) {
Subroutine BUILDARGS: called CC=6 SCAR=18.1
Pod undocumented
472  
473 13 ▶ my %args = @args == 1 && is_plain_hashref( $args[0] ) ? $args[0]->%* : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && Ref::Util::XS::is_plain_hashref($args[0]) ? : 0 13 13
Condition33%1/3
@args == 1 and Ref::Util::XS::is_plain_hashref($args[0])
!ll&&!rl&&r
1300
MC/DC0%0/2
@args == 1 and Ref::Util::XS::is_plain_hashref($args[0])
@args == 1 Ref::Util::XS::is_plain_hashref($args[0])
Truth table33%1/3
A and B
A= @args == 1B= Ref::Util::XS::is_plain_hashref($args[0])
ABresult
0X0
100
111
474  
475 13 ▶ if (my $res = $args{response}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $res = $args{'response'}) { } 1 12 13
elsif (my $req = $args{'request'}) { } 8 4 12
476 1 $args{message} = $res;
477 }
478 elsif (my $req = $args{request}) {
479 8 $args{message} = $req;
480 }
481  
482 13 my $message = $args{message};
483 13 ▶ if ( is_plain_hashref($message) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_hashref($message)) 1 12 13
484 1 require Plack::Request;
485 1 $args{message} = Plack::Request->new($message);
486  
487 }
488  
489 13 return \%args;
490 }
491  
492  
493 4 1;
494  
495 __END__
↑ Summary