SDK/Signature.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 67 / 67 CC 5 SCAR 6.2
CC
5
Cov
100%
_timing_safe_eq
3.0
_timing_safe_eq_bytes
3.0
BEGIN
1.0
CRAP 1.9
« SDK/Error.pm ↑ Summary SDK/Resource/Destinations.pm »
1 package Apertur::SDK::Signature;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Digest::SHA qw(hmac_sha256 hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our @EXPORT_OK = qw(
11 verify_webhook_signature
12 verify_event_signature
13 verify_svix_signature
14 );
15  
16 2 ▶sub verify_webhook_signature {
Subroutine verify_webhook_signature: called CC=1 SCAR=0.0
Pod documented
17 2 my ($body, $signature, $secret) = @_;
18  
19 2 my $expected = hmac_sha256_hex($body, $secret);
20 2 my $sig = $signature;
21 2 $sig =~ s/^sha256=//;
22  
23 2 return _timing_safe_eq($expected, $sig);
24 }
25  
26 2 ▶sub verify_event_signature {
Subroutine verify_event_signature: called CC=1 SCAR=0.0
Pod documented
27 2 my ($body, $timestamp, $signature, $secret) = @_;
28  
29 2 my $signature_base = "${timestamp}.${body}";
30 2 my $expected = hmac_sha256_hex($signature_base, $secret);
31 2 my $sig = $signature;
32 2 $sig =~ s/^sha256=//;
33  
34 2 return _timing_safe_eq($expected, $sig);
35 }
36  
37 2 ▶sub verify_svix_signature {
Subroutine verify_svix_signature: called CC=1 SCAR=0.0
Pod documented
38 2 my ($body, $svix_id, $timestamp, $signature, $secret) = @_;
39  
40 2 my $signature_base = "${svix_id}.${timestamp}.${body}";
41 2 my $secret_bytes = pack('H*', $secret);
42 2 my $expected_bytes = hmac_sha256($signature_base, $secret_bytes);
43 2 my $expected = encode_base64($expected_bytes, '');
44  
45 2 my $sig = $signature;
46 2 $sig =~ s/^v1,//;
47  
48 2 my $sig_bytes = decode_base64($sig);
49 2 my $exp_bytes = decode_base64($expected);
50  
51 2 return _timing_safe_eq_bytes($exp_bytes, $sig_bytes);
52 }
53  
54 # Constant-time string comparison to prevent timing attacks.
55 4 ▶sub _timing_safe_eq {
Subroutine _timing_safe_eq: called CC=3 SCAR=11.0
56 4 my ($a, $b) = @_;
57 4 ▶ return 0 if length($a) != length($b);
Branch: 1 branch
BranchTrueFalseTotal
if length $a != length $b 2 2 4
58 2 my $result = 0;
59 2 for my $i (0 .. length($a) - 1) {
60 128 $result |= ord(substr($a, $i, 1)) ^ ord(substr($b, $i, 1));
61 }
62 2 return $result == 0;
63 }
64  
65 # Constant-time byte string comparison.
66 2 ▶sub _timing_safe_eq_bytes {
Subroutine _timing_safe_eq_bytes: called CC=3 SCAR=11.0
67 2 my ($a, $b) = @_;
68 2 ▶ return 0 if length($a) != length($b);
Branch: 1 branch
BranchTrueFalseTotal
if length $a != length $b 1 1 2
69 1 my $result = 0;
70 1 for my $i (0 .. length($a) - 1) {
71 32 $result |= ord(substr($a, $i, 1)) ^ ord(substr($b, $i, 1));
72 }
73 1 return $result == 0;
74 }
75  
76 1 1;
77  
78 __END__
« SDK/Error.pm ↑ Summary SDK/Resource/Destinations.pm »