Twilio/Security/RequestValidator.pm

filter: Statementstmt 99.0% 105 / 106 Branchbran 100% 32 / 32 Conditioncond 88.3% 38 / 43 MC/DCmcdc 66.6% 14 / 21 Subroutinesub 100% 23 / 23 Podpod 100% 10 / 10 totaltotal 94.4% 222 / 235 CC 40 SCAR 15.4
CC
40
Cov
97%
compare
8.0
compute_signature
7.0
validate
7.0
CRAP 4.7
« Twilio/Jwt/JWT.pm ↑ Summary Twilio/Http/ValidationClient.pm »
1 package Twilio::Security::RequestValidator;
2  
3 7 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Digest::SHA qw(hmac_sha1 hmac_sha256 sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Twilio::Exceptions::TwilioException;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 our $VERSION = '2.001';
13  
14 7 my $DEFAULT_ALGORITHM = 'HMAC_SHA1';
15 7 my %HMAC = (HMAC_SHA1 => \&hmac_sha1, HMAC_SHA256 => \&hmac_sha256);
16  
17 7 my %DEFAULT_PORT = (https => 443, http => 80);
18  
19 # scheme, user information, host, port, and everything after them
20 7 my $HOST = qr/\[[^\]]*\]|[^\/?\#:]*/;
21 7 my $URL_PARTS = qr{\A([^:/?\#]+)://(?:([^/?\#@]*)@)?($HOST)(?::(\d*))?(.*)\z}s;
22  
23 1 ▶sub SIGNATURE_HEADER { return 'X-Twilio-Signature' }
Subroutine SIGNATURE_HEADER: called CC=1 SCAR=0.0
Pod documented
24 1 ▶sub SIGNATURE_SETTINGS_HEADER { return 'X-Twilio-Webhooks-Signature-Settings' }
Subroutine SIGNATURE_SETTINGS_HEADER: called CC=1 SCAR=0.0
Pod documented
25 1 ▶sub HMAC_SHA1 { return 'HMAC_SHA1' }
Subroutine HMAC_SHA1: called CC=1 SCAR=0.0
Pod documented
26 1 ▶sub HMAC_SHA256 { return 'HMAC_SHA256' }
Subroutine HMAC_SHA256: called CC=1 SCAR=0.0
Pod documented
27  
28 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
29 3 my ($class, $auth_token) = @_;
30 3 ▶ return bless { auth_token => _bytes($auth_token // q{}) }, $class;
Condition50%1/2
$auth_token // ""
l!l
30
MC/DC0%0/1
$auth_token // ""
$auth_token
Truth table50%1/2
A
A= $auth_token
Aresult
11
00
31 }
32  
33 8 ▶sub get_signature_algorithm {
Subroutine get_signature_algorithm: called CC=6 SCAR=17.9
Pod documented
34 8 my ($class, $settings_header) = @_;
35  
36 8 ▶ for my $part (split /,/, $settings_header // q{}) {
Condition100%2/2
$settings_header // ""
l!l
71
MC/DC100%1/1
$settings_header // ""
$settings_header
Truth table100%2/2
A
A= $settings_header
Aresult
11
00
37 8 my ($name, $value) = split /=/, $part, 2;
38 8 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 7 8
39  
40 7 $name =~ s/\A\s+|\s+\z//g;
41 7 $value =~ s/\A\s+|\s+\z//g;
42 7 ▶ return $value if $name eq 'algorithm' && length $value;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "algorithm" and length $value 3 4 7
Condition100%3/3
$name eq "algorithm" and length $value
!ll&&!rl&&r
313
MC/DC100%2/2
$name eq "algorithm" and length $value
$name eq "algorithm" length $value
Truth table100%3/3
A and B
A= $name eq "algorithm"B= length $value
ABresult
0X0
100
111
43 }
44 5 return $DEFAULT_ALGORITHM;
45 }
46  
47 96 ▶sub compute_signature {
Subroutine compute_signature: called CC=7 SCAR=19.5
Pod documented
48 96 my ($self, $url, $data, $algorithm) = @_;
49  
50 96 my $hmac = _hmac_for($algorithm);
51 96 ▶ if (!$hmac) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hmac) 1 95 96
52 1 Twilio::Exceptions::TwilioException->throw(
53 "Unsupported webhook signature algorithm: $algorithm");
54 }
55  
56 95 my $text = _bytes($url);
57 95 ▶ my %values = %{ $data // {} };
Condition100%2/2
$data // {}
l!l
6728
MC/DC100%1/1
$data // {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
58 95 for my $key (sort keys %values) {
59 25 my $value = $values{$key};
60 25 ▶ my @list = ref $value eq 'ARRAY' ? @{$value} : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 2 23 25
61 25 my %seen;
62 25 ▶ my @unique = grep { !$seen{$_}++ } map { _bytes($_ // q{}) } @list;
Condition100%2/2
$_ // ""
l!l
271
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
63 25 $text .= _bytes($key) . $_ for sort @unique;
64 }
65  
66 95 return MIME::Base64::encode_base64($hmac->($text, $self->{auth_token}), q{});
67 }
68  
69 14 ▶sub compute_body_hash {
Subroutine compute_body_hash: called CC=2 SCAR=7.2
Pod documented
70 14 my ($class, $body) = @_;
71 14 ▶ return sha256_hex(_bytes($body // q{}));
Condition50%1/2
$body // ""
l!l
140
MC/DC0%0/1
$body // ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
72 }
73  
74 36 ▶sub validate {
Subroutine validate: called CC=7 SCAR=19.5
Pod documented
75 36 my ($self, $signature, $url, $data, $algorithm) = @_;
76  
77 36 ▶ if (!_hmac_for($algorithm)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_hmac_for($algorithm)) 1 35 36
78 1 carp "Unsupported webhook signature algorithm '$algorithm'";
79 1 return 0;
80 }
81  
82 # May not receive a body, so that part passes unless one is given.
83 35 my $valid_body = 1;
84 35 ▶ if (defined $data && ref $data ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and ref $data ne "HASH") 7 28 35
Condition100%3/3
defined $data and ref $data ne "HASH"
!ll&&!rl&&r
8207
MC/DC100%2/2
defined $data and ref $data ne "HASH"
defined $data ref $data ne "HASH"
Truth table100%3/3
A and B
A= defined $dataB= ref $data ne "HASH"
ABresult
0X0
100
111
85 7 $valid_body = $self->compare($self->compute_body_hash($data), _query_value($url, 'bodySHA256'));
86 7 $data = {};
87 }
88  
89 # Twilio is not consistent about the port in what it signs, so both forms are accepted.
90 35 my $with_port = $self->compute_signature(_url_with_port($url), $data, $algorithm);
91 35 my $without_port = $self->compute_signature(_url_without_port($url), $data, $algorithm);
92  
93 # Both comparisons run every time, so the time taken says nothing about which one matched.
94 35 my $matches_with = $self->compare($signature, $with_port);
95 35 my $matches_without = $self->compare($signature, $without_port);
96 35 ▶ return $valid_body && ($matches_with || $matches_without) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$valid_body && ($matches_with || $matches_without) ? : 20 15 35
Condition100%6/6
$valid_body and $matches_with || $matches_without
!ll&&!rl&&r
41120
$matches_with or $matches_without
l!l&&r!l&&!r
101011
MC/DC100%3/3
$valid_body and $matches_with || $matches_without
$valid_body $matches_with $matches_without
Truth table100%4/4
A and B or C
A= $valid_bodyB= $matches_withC= $matches_without
ABCresult
0XX0
1000
11X1
1011
97 }
98  
99 # The time taken depends on the length, not on where the two strings differ.
100 145 ▶sub compare {
Subroutine compare: called CC=8 SCAR=20.8
Pod documented
101 145 my ($class, $one, $other) = @_;
102  
103 145 ▶ return 0 if !length($one // q{}) || !length($other // q{});
Branch: 1 branch
BranchTrueFalseTotal
unless length($one // "") and length($other // "") 11 134 145
Condition100%7/7
length($one // "") and length($other // "")
!ll&&!rl&&r
83134
$one // ""
l!l
1414
$other // ""
l!l
1343
MC/DC100%2/2
length($one // "") and length($other // "")
$one $other
Truth table100%3/3
A and B
A= $oneB= $other
ABresult
0X0
100
111
104  
105 134 my $one_bytes = _bytes($one);
106 134 my $other_bytes = _bytes($other);
107 134 ▶ return 0 if length $one_bytes != length $other_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if length $one_bytes != length $other_bytes 10 124 134
108  
109 124 my $difference = 0;
110 124 for my $position (0 .. length($one_bytes) - 1) {
111 3849 $difference |= ord(substr $one_bytes, $position, 1) ^ ord(substr $other_bytes, $position, 1);
112 }
113 124 ▶ return $difference == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$difference == 0 ? : 84 40 124
114 }
115  
116 132 ▶sub _hmac_for {
Subroutine _hmac_for: called CC=3 SCAR=11.1
117 132 my ($algorithm) = @_;
118 132 ▶ return $HMAC{ uc($algorithm // q{}) || $DEFAULT_ALGORITHM };
Condition80%4/5
uc($algorithm // '') || $DEFAULT_ALGORITHM
l!l&&r!l&&!r
101220
$algorithm // ""
l!l
10122
MC/DC0%0/2
uc($algorithm // '') || $DEFAULT_ALGORITHM
$algorithm $DEFAULT_ALGORITHM
Truth table66%2/3
A || B
A= $algorithmB= $DEFAULT_ALGORITHM
ABresult
1X1
011
000
119 }
120  
121 435 ▶sub _bytes {
Subroutine _bytes: called CC=2 SCAR=6.9
122 435 my ($text) = @_;
123 435 my $bytes = "$text";
124 435 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 6 429 435
125 435 return $bytes;
126 }
127  
128 35 ▶sub _url_without_port {
Subroutine _url_without_port: called CC=2 SCAR=6.9
129 35 my ($url) = @_;
130  
131 35 ▶ my ($scheme, $user, $host, undef, $rest) = $url =~ $URL_PARTS or return $url;
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ /$URL_PARTS/u or return $url 34 1 35
132 34 return _url($scheme, $user, $host, $rest);
133 }
134  
135 35 ▶sub _url_with_port {
Subroutine _url_with_port: called CC=5 SCAR=16.1
136 35 my ($url) = @_;
137  
138 35 ▶ my ($scheme, $user, $host, $port, $rest) = $url =~ $URL_PARTS or return $url;
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ /$URL_PARTS/u or return $url 34 1 35
139 34 ▶ my $number = length($port // q{}) ? $port : $DEFAULT_PORT{ lc $scheme } // $DEFAULT_PORT{http};
Branch: 1 branch
BranchTrueFalseTotal
length($port // '') ? : 4 30 34
Condition80%4/5
$port // ""
l!l
529
$DEFAULT_PORT{lc $scheme} // $DEFAULT_PORT{"http"}
l!l&&r!l&&!r
2910
MC/DC100%1/1
$port // ""
$port
MC/DC0%0/2
$DEFAULT_PORT{lc $scheme} // $DEFAULT_PORT{"http"}
$DEFAULT_PORT{lc $scheme} $DEFAULT_PORT{"http"}
Truth table100%2/2
A
A= $port
Aresult
11
00
Truth table66%2/3
A // B
A= $DEFAULT_PORT{lc $scheme}B= $DEFAULT_PORT{"http"}
ABresult
1X1
011
000
140 34 return _url($scheme, $user, "$host:$number", $rest);
141 }
142  
143 68 ▶sub _url {
Subroutine _url: called CC=2 SCAR=6.9
144 68 my ($scheme, $user, $host, $rest) = @_;
145 68 ▶ return "$scheme://" . (defined $user ? "$user\@" : q{}) . $host . $rest;
Branch: 1 branch
BranchTrueFalseTotal
defined $user ? : 6 62 68
146 }
147  
148 7 ▶sub _query_value {
Subroutine _query_value: called CC=5 SCAR=16.1
149 7 my ($url, $name) = @_;
150  
151 7 my ($query) = $url =~ /[?]([^#]*)/;
152 7 my $found;
153 7 ▶ for my $pair (split /&/, $query // q{}) {
Condition100%2/2
$query // ""
l!l
61
MC/DC100%1/1
$query // ""
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
154 10 my ($key, $value) = split /=/, $pair, 2;
155 10 ▶ $found = _decoded($value // q{}) if _decoded($key) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if _decoded($key) eq $name 7 3 10
Condition50%1/2
$value // ""
l!l
70
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
156 }
157 7 return $found;
158 }
159  
160 17 ▶sub _decoded {
Subroutine _decoded: called CC=1 SCAR=0.1
161 17 my ($text) = @_;
162 17 $text =~ tr/+/ /;
163 0 $text =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge;
164 17 return $text;
165 }
166  
167 7 1;
168  
169 __END__
« Twilio/Jwt/JWT.pm ↑ Summary Twilio/Http/ValidationClient.pm »