blib/lib/VAPID.pm

filter: Statementstmt 84.7% 161 / 190 Branchbran 67.1% 47 / 70 Conditioncond 54.5% 18 / 33 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 96.1% 25 / 26 Podpod 100% 11 / 11 totaltotal 76.2% 267 / 350 CC 48 SCAR 18.5
CC
48
Cov
77%
validate_subscription
11.6
build_push_request
10.2
validate_subject
9.3
CRAP 6.4
↑ Summary
1 package VAPID;
2 3 ▶use 5.006; use strict; use warnings; our $VERSION = '2.00';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Crypt::JWT qw(encode_jwt); use Crypt::PK::ECC; use URI; use MIME::Base64 qw/encode_base64url decode_base64url/;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate); use Crypt::KeyDerivation qw(hkdf); use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use HTTP::Request; use LWP::UserAgent; use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base 'Import::Export';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our (%EX, $DEFAULT_SECONDS, $MAX_DEFAULT_SECONDS);
9  
10 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 $DEFAULT_SECONDS = 12 * 60 * 60; # 12 hours
12 3 $MAX_DEFAULT_SECONDS = 24 * 60 * 60; # 24 hours
13 3 %EX = (
14 generate_vapid_keys => [qw/all generate/],
15 generate_future_expiration_timestamp => [qw/all generate/],
16 generate_vapid_header => [qw/all generate/],
17 validate_subject => [qw/all validate/],
18 validate_public_key => [qw/all validate/],
19 validate_private_key => [qw/all validate/],
20 validate_expiration => [qw/all validate/],
21 validate_subscription => [qw/all validate/],
22 encrypt_payload => [qw/all encrypt/],
23 build_push_request => [qw/all push/],
24 send_push_notification => [qw/all push/],
25 );
26 }
27  
28 4 ▶sub generate_vapid_keys {
Subroutine generate_vapid_keys: called CC=3 SCAR=12.7
Pod documented
29 4 my $curve = Crypt::PK::ECC->new();
30 4 $curve->generate_key('prime256v1');
31 4 my $priv = $curve->export_key_raw('private');
32 4 my $pub = $curve->export_key_raw('public');
33
34 4 ▶ if (length($priv) < 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $priv < 32) 0 4 4
35 0 my $padding = 32 - length $priv;
36 0 $priv = ("\0" x $padding) . $priv;
37 }
38  
39 4 ▶ if (length($pub) < 65) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pub < 65) 0 4 4
40 0 my $padding = 65 - length $pub;
41 0 $pub = ("\0" x $padding) . $pub;
42 }
43  
44 return (
45 4 encode_base64url($pub),
46 encode_base64url($priv)
47 );
48 }
49  
50 7 ▶sub generate_vapid_header {
Subroutine generate_vapid_header: called CC=6 SCAR=18.5
Pod documented
51 7 my ($aud, $subject, $pub, $priv, $expiration, $enc) = @_;
52  
53 7 ▶ if (!$aud) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aud) 0 7 7
54 0 die "No audience could be generated for VAPID.";
55 }
56  
57 7 ▶ if (ref $aud) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $aud) 0 7 7
58 0 die "The audience value must be a string containing the origin of a push service";
59 }
60  
61 7 my $aud_uri = URI->new($aud);
62  
63 7 ▶ if (!$aud_uri->host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aud_uri->host) 0 7 7
64 0 die "VAPID audience is not a url.";
65 }
66  
67 7 validate_subject($subject);
68 7 validate_public_key($pub);
69 7 $priv = validate_private_key($priv);
70  
71 7 ▶ if ($expiration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expiration) { } 3 4 7
72 3 validate_expiration($expiration);
73 } else {
74 4 $expiration = generate_future_expiration_timestamp();
75 }
76  
77 7 my $payload = {
78 aud => $aud,
79 exp => $expiration,
80 sub => $subject
81 };
82  
83 7 my $key = Crypt::PK::ECC->new
84 ->import_key_raw($priv, 'prime256v1')
85 ->export_key_pem('private');
86  
87  
88 7 my $jwt_token = encode_jwt(
89 payload=>$payload,
90 extra_headers => { typ => 'JWT' },
91 alg=>'ES256',
92 key => \$key
93 );
94  
95 7 ▶ return $enc
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 6 1 7
96 ? {
97 Authorization => "vapid t=${jwt_token}, k=${pub}"
98 }
99 : {
100 Authorization => 'WebPush ' . $jwt_token,
101 'Crypto-Key' => 'p256ecdsa=' . $pub
102 };
103 }
104  
105 8 ▶sub generate_future_expiration_timestamp {
Subroutine generate_future_expiration_timestamp: called CC=2 SCAR=7.1
Pod documented
106 8 my ($add) = shift;
107 8 ▶ return time + ($add || $DEFAULT_SECONDS);
Condition66%2/3
$add || $VAPID::DEFAULT_SECONDS
l!l&&r!l&&!r
440
MC/DC0%0/2
$add || $VAPID::DEFAULT_SECONDS
$add $VAPID::DEFAULT_SECONDS
Truth table66%2/3
A || B
A= $addB= $VAPID::DEFAULT_SECONDS
ABresult
1X1
011
000
108 }
109  
110 8 ▶sub validate_subject {
Subroutine validate_subject: called CC=5 SCAR=22.3
Pod documented
111 8 my ($subject) = shift;
112
113 8 ▶ if (!$subject) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subject) 0 8 8
114 0 die "No subject passed to validate_subject";
115 }
116  
117 8 ▶ if (ref $subject) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subject) 0 8 8
118 0 die "The subject value must be a string containing a URL or 'mailto: address.'";
119 }
120  
121 8 ▶ unless ($subject =~ m/^mailto\:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subject =~ /^mailto\:/) 0 8 8
122 0 my $uri = URI->new($subject);
123 0 ▶ if (!$uri->host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->host) 0 0 0
124 0 die "VAPID subject is not a url or mailto: address";
125 }
126 }
127  
128 8 return $subject;
129 }
130  
131 8 ▶sub validate_public_key {
Subroutine validate_public_key: called CC=4 SCAR=16.1
Pod documented
132 8 my ($pub) = shift;
133  
134 8 ▶ if (!$pub) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pub) 0 8 8
135 0 die "No public key passed to validate_public_key";
136 }
137  
138 8 ▶ if (ref $pub) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pub) 0 8 8
139 0 die "Vapid public key is must be a URL safe Base 64 encoded string";
140 }
141  
142 8 $pub = decode_base64url($pub);
143  
144 8 ▶ if (length $pub != 65) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pub != 65) 0 8 8
145 0 die "VAPID public key should be 65 bytes long when decoded.";
146 }
147
148 8 return $pub;
149 }
150  
151 8 ▶sub validate_private_key {
Subroutine validate_private_key: called CC=4 SCAR=16.1
Pod documented
152 8 my ($priv) = shift;
153  
154 8 ▶ if (!$priv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($priv) 0 8 8
155 0 die "No private key passed to validate_private_key";
156 }
157  
158 8 ▶ if (ref $priv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $priv) 0 8 8
159 0 die "VAPID private key is must be a URL safe Base 64 encoded string";
160 }
161  
162 8 $priv = decode_base64url($priv);
163
164 8 ▶ if (length $priv != 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $priv != 32) 0 8 8
165 0 die "VAPID private key should be 32 bytes long when decoded.";
166 }
167  
168 8 return $priv;
169 }
170  
171 4 ▶sub validate_expiration {
Subroutine validate_expiration: called CC=4 SCAR=16.6
Pod documented
172 4 my $expiration = shift;
173  
174 4 ▶ if (!$expiration || $expiration !~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expiration and $expiration =~ /^\d+$/) 0 4 4
Condition33%1/3
$expiration and $expiration =~ /^\d+$/
!ll&&!rl&&r
004
MC/DC0%0/2
$expiration and $expiration =~ /^\d+$/
$expiration $expiration =~ /^\d+$/
Truth table33%1/3
A and B
A= $expirationB= $expiration =~ /^\d+$/
ABresult
0X0
100
111
175 0 die "expiration value must be a number";
176 }
177  
178 4 my $max = generate_future_expiration_timestamp($MAX_DEFAULT_SECONDS);
179  
180 4 ▶ if ($expiration >= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expiration >= $max) 0 4 4
181 0 die "expiration value is greater than maximum of 24 hours";
182 }
183
184 4 return $expiration;
185 }
186  
187 18 ▶sub validate_subscription {
Subroutine validate_subscription: called CC=11 SCAR=24.5
Pod documented
188 18 my ($subscription) = @_;
189  
190 18 ▶ if (!$subscription) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription) 1 17 18
191 1 die "No subscription passed to validate_subscription";
192 }
193  
194 17 ▶ if (!ref $subscription || ref $subscription ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $subscription or ref $subscription ne "HASH") 1 16 17
Condition66%2/3
not ref $subscription or ref $subscription ne "HASH"
l!l&&r!l&&!r
1016
MC/DC50%1/2
not ref $subscription or ref $subscription ne "HASH"
not ref $subscription ref $subscription ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $subscriptionB= ref $subscription ne "HASH"
ABresult
1X1
011
000
195 1 die "Subscription must be a hash reference";
196 }
197  
198 16 ▶ if (!$subscription->{endpoint}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"endpoint"}) 1 15 16
199 1 die "Subscription must have an endpoint";
200 }
201  
202 15 my $uri = URI->new($subscription->{endpoint});
203 15 ▶ if (!$uri->host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->host) 0 15 15
204 0 die "Subscription endpoint is not a valid URL";
205 }
206  
207 15 ▶ if (!$subscription->{keys}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"keys"}) 1 14 15
208 1 die "Subscription must have keys";
209 }
210  
211 14 ▶ if (!$subscription->{keys}{p256dh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"keys"}{"p256dh"}) 1 13 14
212 1 die "Subscription must have a p256dh key";
213 }
214  
215 13 ▶ if (!$subscription->{keys}{auth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription->{"keys"}{"auth"}) 1 12 13
216 1 die "Subscription must have an auth key";
217 }
218  
219 12 ▶ if (length(decode_base64url($subscription->{keys}{p256dh})) != 65) {
Branch: 1 branch
BranchTrueFalseTotal
if (length decode_base64url($subscription->{"keys"}{"p256dh"}) != 65) 0 12 12
220 0 die "Subscription p256dh should be 65 bytes long when decoded";
221 }
222  
223 12 ▶ if (length(decode_base64url($subscription->{keys}{auth})) != 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length decode_base64url($subscription->{"keys"}{"auth"}) != 16) 0 12 12
224 0 die "Subscription auth should be 16 bytes long when decoded";
225 }
226  
227 12 return $subscription;
228 }
229  
230 8 ▶sub encrypt_payload {
Subroutine encrypt_payload: called CC=7 SCAR=19.6
Pod documented
231 8 my ($payload, $subscription, %opts) = @_;
232  
233 8 ▶ if (!defined $payload) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $payload) 1 7 8
234 1 die "No payload passed to encrypt_payload";
235 }
236  
237 7 validate_subscription($subscription);
238  
239 7 my $ua_public = decode_base64url($subscription->{keys}{p256dh});
240 7 my $auth_secret = decode_base64url($subscription->{keys}{auth});
241  
242 7 ▶ my $salt = $opts{salt} || random_bytes(16);
Condition66%2/3
$opts{'salt'} || &random_bytes(16)
l!l&&r!l&&!r
160
MC/DC0%0/2
$opts{'salt'} || &random_bytes(16)
$opts{'salt'} &random_bytes(16)
Truth table66%2/3
A || B
A= $opts{'salt'}B= &random_bytes(16)
ABresult
1X1
011
000
243 7 ▶ if (length $salt != 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $salt != 16) 0 7 7
244 0 die "salt must be 16 bytes";
245 }
246  
247 7 my $local_key = $opts{local_key};
248 7 ▶ if (!$local_key) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $local_key) { } 6 1 7
elsif (not ref $local_key) { } 1 0 1
249 6 $local_key = Crypt::PK::ECC->new();
250 6 $local_key->generate_key('prime256v1');
251 } elsif (!ref $local_key) {
252 1 $local_key = Crypt::PK::ECC->new->import_key_raw($local_key, 'prime256v1');
253 }
254 7 my $as_public = $local_key->export_key_raw('public');
255  
256 7 my $ua_key = Crypt::PK::ECC->new->import_key_raw($ua_public, 'prime256v1');
257 7 my $ecdh_secret = $local_key->shared_secret($ua_key);
258  
259 7 my $key_info = "WebPush: info\x00" . $ua_public . $as_public;
260 7 my $ikm = hkdf($ecdh_secret, $auth_secret, 'SHA256', 32, $key_info);
261  
262 7 my $cek = hkdf($ikm, $salt, 'SHA256', 16, "Content-Encoding: aes128gcm\x00");
263 7 my $nonce = hkdf($ikm, $salt, 'SHA256', 12, "Content-Encoding: nonce\x00");
264  
265 7 ▶ my $rs = $opts{record_size} || 4096;
Condition50%1/2
$opts{'record_size'} || 4096
l!l
07
MC/DC0%0/1
$opts{'record_size'} || 4096
$opts{'record_size'}
Truth table50%1/2
A
A= $opts{'record_size'}
Aresult
11
00
266  
267 7 my ($ciphertext, $tag) = gcm_encrypt_authenticate(
268 'AES', $cek, $nonce, '', $payload . "\x02"
269 );
270  
271 7 my $header = $salt
272 . pack('N', $rs)
273 . pack('C', length $as_public)
274 . $as_public;
275  
276 7 return $header . $ciphertext . $tag;
277 }
278  
279 5 ▶sub build_push_request {
Subroutine build_push_request: called CC=10 SCAR=23.2
Pod documented
280 5 my (%args) = @_;
281  
282 5 my $subscription = $args{subscription};
283 5 my $payload = $args{payload};
284 5 my $vapid_public = $args{vapid_public};
285 5 my $vapid_private = $args{vapid_private};
286 5 my $subject = $args{subject};
287 5 ▶ my $ttl = $args{ttl} // 60;
Condition100%2/2
$args{"ttl"} // 60
l!l
14
MC/DC100%1/1
$args{"ttl"} // 60
$args{"ttl"}
Truth table100%2/2
A
A= $args{"ttl"}
Aresult
11
00
288 5 my $expiration = $args{expiration};
289  
290 5 ▶ if (defined $args{encoding} && $args{encoding} ne 'aes128gcm') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"encoding"} and $args{"encoding"} ne "aes128gcm") 1 4 5
Condition66%2/3
defined $args{"encoding"} and $args{"encoding"} ne "aes128gcm"
!ll&&!rl&&r
401
MC/DC50%1/2
defined $args{"encoding"} and $args{"encoding"} ne "aes128gcm"
defined $args{"encoding"} $args{"encoding"} ne "aes128gcm"
Truth table66%2/3
A and B
A= defined $args{"encoding"}B= $args{"encoding"} ne "aes128gcm"
ABresult
0X0
100
111
291 1 die "Unsupported encoding '$args{encoding}': VAPID 2.00 sends "
292 . "RFC 8291 aes128gcm only";
293 }
294  
295 4 validate_subscription($subscription);
296  
297 4 my $endpoint = $subscription->{endpoint};
298 4 my $uri = URI->new($endpoint);
299 4 my $audience = $uri->scheme . '://' . $uri->host;
300 4 ▶ if (defined $uri->port && defined $uri->default_port
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri->port and defined $uri->default_port and $uri->port != $uri->default_port) 1 3 4
Condition50%3/6
defined $uri->port and defined $uri->default_port and $uri->port != $uri->default_port
!ll&&!rl&&r
031
defined $uri->port and defined $uri->default_port
!ll&&!rl&&r
004
MC/DC33%1/3
defined $uri->port and defined $uri->default_port and $uri->port != $uri->default_port
defined $uri->port defined $uri->default_port $uri->port != $uri->default_port
Truth table50%2/4
A and B and C
A= defined $uri->portB= defined $uri->default_portC= $uri->port != $uri->default_port
ABCresult
0XX0
10X0
1100
1111
301 && $uri->port != $uri->default_port) {
302 1 $audience .= ':' . $uri->port;
303 }
304  
305 4 my $vapid_headers = generate_vapid_header(
306 $audience,
307 $subject,
308 $vapid_public,
309 $vapid_private,
310 $expiration,
311 1
312 );
313  
314 4 my $req = HTTP::Request->new(POST => $endpoint);
315 4 $req->header(TTL => $ttl);
316 4 $req->header(Authorization => $vapid_headers->{Authorization});
317  
318 4 ▶ if (defined $payload && length $payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload and length $payload) 3 1 4
Condition66%2/3
defined $payload and length $payload
!ll&&!rl&&r
103
MC/DC50%1/2
defined $payload and length $payload
defined $payload length $payload
Truth table66%2/3
A and B
A= defined $payloadB= length $payload
ABresult
0X0
100
111
319 3 $req->header('Content-Encoding' => 'aes128gcm');
320 3 $req->header('Content-Type' => 'application/octet-stream');
321 3 $req->content(encrypt_payload($payload, $subscription));
322 }
323  
324 4 ▶ $req->header('Content-Length' => length($req->content // ''));
Condition50%1/2
$req->content // ""
l!l
40
MC/DC0%0/1
$req->content // ""
$req->content
Truth table50%1/2
A
A= $req->content
Aresult
11
00
325  
326 4 return $req;
327 }
328  
329 0 ▶sub send_push_notification {
Subroutine send_push_notification: not called CC=2 SCAR=17.9
Pod documented
330 0 my (%args) = @_;
331  
332 0 ▶ my $ua = $args{ua} // LWP::UserAgent->new(timeout => 30);
Condition0%0/3
$args{"ua"} // "LWP::UserAgent"->new("timeout", 30)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"ua"} // "LWP::UserAgent"->new("timeout", 30)
$args{"ua"} "LWP::UserAgent"->new("timeout", 30)
Truth table0%0/3
A // B
A= $args{"ua"}B= "LWP::UserAgent"->new("timeout", 30)
ABresult
1X1
011
000
333  
334 0 my $req = build_push_request(%args);
335 0 my $resp = $ua->request($req);
336  
337 return {
338 0 success => $resp->is_success,
339 status => $resp->code,
340 message => $resp->message,
341 response => $resp
342 };
343 }
344  
345 3 1;
346  
347 __END__
↑ Summary