blib/lib/Discord/Client/Interaction/Verifier.pm

filter: Statementstmt 100% 34 / 34 Branchbran 71.4% 10 / 14 Conditioncond 61.3% 27 / 44 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 68.5% 85 / 124 CC 23 SCAR 25.2
CC
23
Cov
77%
new
17.2
verify
14.6
__ANON__
1.0
CRAP 12.4
« blib/lib/Discord/Client/Cache.pm ↑ Summary blib/lib/Discord/Client/Interactions.pm »
1 package Discord::Client::Interaction::Verifier;
2  
3 4 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Crypt::PK::Ed25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.003';
11  
12 19 ▶sub new {
Subroutine new: called CC=11 SCAR=28.4
Pod documented
13 19 my ( $class, %args ) = @_;
14  
15 # Import only an exact Ed25519 public key, then configure the local freshness policy.
16 croak 'public_key must be a 32-byte hexadecimal Ed25519 public key'
17 19 ▶ unless defined( $args{public_key} ) && !ref( $args{public_key} ) && $args{public_key} =~ /\A[0-9a-fA-F]{64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"public_key"} and not ref $args{"public_key"} and $args{"public_key"} =~ /\A[0-9a-fA-F]{64}\z/u 0 19 19
Condition33%2/6
defined $args{"public_key"} and not ref $args{"public_key"} and $args{"public_key"} =~ /\A[0-9a-fA-F]{64}\z/u
!ll&&!rl&&r
0019
defined $args{"public_key"} and not ref $args{"public_key"}
!ll&&!rl&&r
0019
MC/DC0%0/3
defined $args{"public_key"} and not ref $args{"public_key"} and $args{"public_key"} =~ /\A[0-9a-fA-F]{64}\z/u
defined $args{"public_key"} not ref $args{"public_key"} $args{"public_key"} =~ /\A[0-9a-fA-F]{64}\z/u
Truth table25%1/4
A and not(B) and C
A= defined $args{"public_key"}B= not ref $args{"public_key"}C= $args{"public_key"} =~ /\A[0-9a-fA-F]{64}\z/u
ABCresult
0XX0
10X0
1100
1111
18 19 my $key = eval { Crypt::PK::Ed25519->new->import_key_raw( pack( 'H*', $args{public_key} ), 'public' ) };
19 19 ▶ croak 'Invalid Ed25519 public key' unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 19 19
20 19 ▶ my $age = $args{max_age} // 300;
Condition50%1/2
$args{"max_age"} // 300
l!l
019
MC/DC0%0/1
$args{"max_age"} // 300
$args{"max_age"}
Truth table50%1/2
A
A= $args{"max_age"}
Aresult
11
00
21 19 ▶ croak 'max_age must be a nonnegative integer' unless !ref($age) && $age =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $age and $age =~ /\A\d+\z/u 0 19 19
Condition33%1/3
not ref $age and $age =~ /\A\d+\z/u
!ll&&!rl&&r
0019
MC/DC0%0/2
not ref $age and $age =~ /\A\d+\z/u
not ref $age $age =~ /\A\d+\z/u
Truth table33%1/3
not(A) and B
A= not ref $ageB= $age =~ /\A\d+\z/u
ABresult
0X0
100
111
22 19 ▶ croak 'wall_clock must be a code reference' if exists( $args{wall_clock} ) && ref( $args{wall_clock} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"wall_clock"} and ref $args{"wall_clock"} ne "CODE" 0 19 19
Condition66%2/3
exists $args{"wall_clock"} and ref $args{"wall_clock"} ne "CODE"
!ll&&!rl&&r
1180
MC/DC0%0/2
exists $args{"wall_clock"} and ref $args{"wall_clock"} ne "CODE"
exists $args{"wall_clock"} ref $args{"wall_clock"} ne "CODE"
Truth table66%2/3
A and B
A= exists $args{"wall_clock"}B= ref $args{"wall_clock"} ne "CODE"
ABresult
0X0
100
111
23 19 ▶ return bless { key => $key, max_age => $age, wall_clock => $args{wall_clock} // sub { time() } }, $class;
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{"wall_clock"} // sub { &Discord::Client::Interaction::Verifier::time(); }
l!l&&r!l&&!r
1810
MC/DC0%0/2
$args{"wall_clock"} // sub { &Discord::Client::Interaction::Verifier::time(); }
$args{"wall_clock"} sub { &Discord::Client::Interaction::Verifier::time(); }
Truth table66%2/3
A // B
A= $args{"wall_clock"}B= sub { &Discord::Client::Interaction::Verifier::time(); }
ABresult
1X1
011
000
24 }
25  
26 1077 ▶sub verify {
Subroutine verify: called CC=13 SCAR=26.8
Pod documented
27 1077 my ( $self, $signature, $timestamp, $body ) = @_;
28  
29 # Reject malformed signatures, timestamps and decoded text before cryptographic work.
30 1077 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp and not ref $timestamp and $timestamp =~ /\A\d{1,20}\z/u and defined $body and not ref $body and not utf8::is_utf8($body) 2 1075 1077
Condition70%17/24
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp and not ref $timestamp and $timestamp =~ /\A\d{1,20}\z/u and defined $body and not ref $body and not utf8::is_utf8($body)
!ll&&!rl&&r
201075
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp and not ref $timestamp and $timestamp =~ /\A\d{1,20}\z/u and defined $body and not ref $body
!ll&&!rl&&r
201075
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp and not ref $timestamp and $timestamp =~ /\A\d{1,20}\z/u and defined $body
!ll&&!rl&&r
201075
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp and not ref $timestamp and $timestamp =~ /\A\d{1,20}\z/u
!ll&&!rl&&r
201075
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp and not ref $timestamp
!ll&&!rl&&r
201075
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp
!ll&&!rl&&r
201075
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u
!ll&&!rl&&r
111075
defined $signature and not ref $signature
!ll&&!rl&&r
101076
MC/DC22%2/9
defined $signature and not ref $signature and $signature =~ /\A[0-9a-fA-F]{128}\z/u and defined $timestamp and not ref $timestamp and $timestamp =~ /\A\d{1,20}\z/u and defined $body and not ref $body and not utf8::is_utf8($body)
defined $signature not ref $signature $signature =~ /\A[0-9a-fA-F]{128}\z/u defined $timestamp not ref $timestamp $timestamp =~ /\A\d{1,20}\z/u defined $body not ref $body not utf8::is_utf8($body)
Truth table30%3/10
A and not(B) and C and D and not(E) and F and G and not(H) and not(I)
A= defined $signatureB= not ref $signatureC= $signature =~ /\A[0-9a-fA-F]{128}\z/uD= defined $timestampE= not ref $timestampF= $timestamp =~ /\A\d{1,20}\z/uG= defined $bodyH= not ref $bodyI= not utf8::is_utf8($body)
ABCDEFGHIresult
0XXXXXXXX0
10XXXXXXX0
110XXXXXX0
1110XXXXX0
11110XXXX0
111110XXX0
1111110XX0
11111110X0
1111111100
1111111111
31 unless defined($signature)
32 && !ref($signature)
33 && $signature =~ /\A[0-9a-fA-F]{128}\z/
34 && defined($timestamp)
35 && !ref($timestamp)
36 && $timestamp =~ /\A\d{1,20}\z/
37 && defined($body)
38 && !ref($body)
39 && !utf8::is_utf8($body);
40  
41 # Timestamp tolerance is a local replay policy, independent of signature validity.
42 1075 ▶ return 0 if $self->{max_age} && abs( $self->{wall_clock}->() - $timestamp ) > $self->{max_age};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_age"} and abs $self->{"wall_clock"}->() - $timestamp > $self->{"max_age"} 3 1072 1075
Condition66%2/3
$self->{"max_age"} and abs $self->{"wall_clock"}->() - $timestamp > $self->{"max_age"}
!ll&&!rl&&r
010723
MC/DC50%1/2
$self->{"max_age"} and abs $self->{"wall_clock"}->() - $timestamp > $self->{"max_age"}
$self->{"max_age"} abs $self->{"wall_clock"}->() - $timestamp > $self->{"max_age"}
Truth table66%2/3
A and B
A= $self->{"max_age"}B= abs $self->{"wall_clock"}->() - $timestamp > $self->{"max_age"}
ABresult
0X0
100
111
43  
44 # Verify the exact timestamp and body octets before JSON parsing.
45 1072 ▶ return eval { $self->{key}->verify_message( pack( 'H*', $signature ), $timestamp . $body ) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{'key'}->verify_message(pack('H*', $signature), $timestamp . $body) } } ? : 1067 5 1072
46 }
47  
48 4 1;
49  
50 __END__
« blib/lib/Discord/Client/Cache.pm ↑ Summary blib/lib/Discord/Client/Interactions.pm »