lib/Mail/DKIM2/RecipeJSON.pm

filter: Statementstmt 57.5% 91 / 158 Branchbran 45.0% 45 / 100 Conditioncond 41.6% 5 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 70.0% 14 / 20 Podpod 100% 6 / 6 totaltotal 53.9% 164 / 304 CC 55 SCAR 32.9
CC
55
Cov
52%
_json_lexical_error
54.2
_json_lexical_error
54.2
_json_octets
10.5
CRAP 26.8
« lib/Mail/DKIM2/BodyPlan.pm ↑ Summary lib/Mail/DKIM2/Gate.pm »
1 package Mail::DKIM2::RecipeJSON;
2 207 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 207 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 207 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 207 our $VERSION = '0.33';
7  
8 207 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 207 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 207 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 207 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 207 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 207 our @EXPORT_OK = qw(
15 encode_tag_json
16 decode_tag_json
17 is_utf8_octets
18 );
19  
20 # Encode data as base64 JSON for a tag value
21 291 ▶sub encode_tag_json {
Subroutine encode_tag_json: called CC=1 SCAR=1.2
Pod documented
22 0 ▶ my ($data) = @_;
Subroutine encode_tag_json: not called CC=1 SCAR=1.2
Pod documented
23 0 return encode_base64(JSON->new->canonical(1)->encode($data), '');
24 }
25  
26 # Decode a base64 JSON tag value. The JSON text must be UTF-8 (RFC 8259
27 # section 8.1; -09 section 5): anything else dies. Every string in the
28 # result, object keys included, is UTF-8 octets, as the message's own
29 # header values and body lines are, so a literal compares and hashes
30 # byte for byte; a string holding a lone surrogate (a "\ud800" escape) is
31 # not UTF-8 and dies too.
32 2295 ▶sub decode_tag_json {
Subroutine decode_tag_json: called CC=4 SCAR=18.6
Pod documented
33 0 ▶ my ($b64) = @_;
Subroutine decode_tag_json: not called CC=4 SCAR=18.6
Pod documented
34 0 my $octets = decode_base64($b64);
35 0 my $text = eval { Encode::decode('UTF-8', $octets, Encode::FB_CROAK() | Encode::LEAVE_SRC()) };
36 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 2295 2295
if ref $@ 0 0 0
37 0 ▶ die "JSON text is not UTF-8\n" unless defined $text;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $text 1 2294 2295
unless defined $text 0 0 0
38 0 my $data = JSON->new->decode($text);
39 # A key given twice in one object is invalid here: JSON parsers disagree
40 # on which value wins (first or last), so {"b":[...],"b":null} would be a
41 # null body Recipe to some verifiers and signers and a real one to others.
42 # A key that is not lowercase is invalid too (-09 §5).
43 0 ▶ if (defined(my $error = _json_lexical_error($text))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $error = _json_lexical_error($text))) 31 2256 2287
if (defined(my $error = _json_lexical_error($text))) 0 0 0
44 0 die $error;
45 }
46 0 return _json_octets($data);
47 }
48  
49 # A decoded JSON value with every string -- object keys included --
50 # turned into UTF-8 octets. Numbers and booleans are returned untouched:
51 # MessageInstance tells a JSON integer from a JSON string by its flags.
52 30889 ▶sub _json_octets {
Subroutine _json_octets: called CC=6 SCAR=23.5
53 0 ▶ my ($v) = @_;
Subroutine _json_octets: not called CC=6 SCAR=23.5
54 0 ▶ return { map { _json_octet_string($_) => _json_octets($v->{$_}) } keys %$v }
Branch: 2 branches
BranchTrueFalseTotal
if ref $v eq "HASH" 8692 22197 30889
if ref $v eq "HASH" 0 0 0
55 if ref $v eq 'HASH';
56 0 ▶ return [ map { _json_octets($_) } @$v ] if ref $v eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $v eq "ARRAY" 9868 12329 22197
if ref $v eq "ARRAY" 0 0 0
57 0 ▶ return $v if !defined $v || ref $v;
Branch: 2 branches
BranchTrueFalseTotal
if not defined $v or ref $v 229 12100 12329
if not defined $v or ref $v 0 0 0
Condition50%3/6
not defined $v or ref $v
l!l&&r!l&&!r
220912100
not defined $v or ref $v
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $v or ref $v
not defined $v ref $v
MC/DC0%0/2
not defined $v or ref $v
not defined $v ref $v
Truth table100%3/3
not(A) or B
A= not defined $vB= ref $v
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $vB= ref $v
ABresult
1X1
011
000
58 0 ▶ return $v unless B::svref_2object(\$v)->FLAGS & B::SVf_POK;
Branch: 2 branches
BranchTrueFalseTotal
unless B::svref_2object(\$v)->FLAGS & 1024 5380 6720 12100
unless B::svref_2object(\$v)->FLAGS & 1024 0 0 0
59 0 return _json_octet_string($v);
60 }
61  
62 17875 ▶sub _json_octet_string {
Subroutine _json_octet_string: called CC=2 SCAR=10.3
63 0 ▶ my ($s) = @_;
Subroutine _json_octet_string: not called CC=2 SCAR=10.3
64 0 ▶ die "JSON string is not UTF-8\n" if $s =~ /[\x{D800}-\x{DFFF}]/;
Branch: 2 branches
BranchTrueFalseTotal
if $s =~ /[\x{D800}-\x{DFFF}]/u 0 17875 17875
if $s =~ /[\x{D800}-\x{DFFF}]/u 0 0 0
65 0 utf8::encode($s);
66 0 return $s;
67 }
68  
69 # True if $octets is valid UTF-8 (ASCII included): strict, so no surrogate,
70 # nothing above U+10FFFF, no overlong form.
71 1847 ▶sub is_utf8_octets {
Subroutine is_utf8_octets: called CC=4 SCAR=18.7
Pod documented
72 0 ▶ my ($octets) = @_;
Subroutine is_utf8_octets: not called CC=4 SCAR=18.7
Pod documented
73 0 ▶ return 1 unless $octets =~ /[^\x00-\x7F]/;
Branch: 2 branches
BranchTrueFalseTotal
unless $octets =~ /[^\x00-\x7F]/u 1793 54 1847
unless $octets =~ /[^\x00-\x7F]/u 0 0 0
74 0 my $ok = eval { Encode::decode('UTF-8', $octets, Encode::FB_CROAK() | Encode::LEAVE_SRC()); 1 };
75 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 54 54
if ref $@ 0 0 0
76 0 ▶ return $ok ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$ok ? : 3 51 54
$ok ? : 0 0 0
77 }
78  
79 # The first problem the decoded value cannot show in $text (already known to
80 # be valid JSON), or undef: a key that is not lowercase (has an ASCII
81 # capital), a key that appears twice in one object, compared after
82 # unescaping, or a number with a fraction or
83 # an exponent. Every number in a Recipe is an integer (-09 section 5), and
84 # whether 1e0 decodes as an integer depends on the JSON backend -- JSON::XS
85 # gives an NV, JSON::PP an IV -- so it is refused here, from the text.
86 2287 ▶sub _json_lexical_error {
Subroutine _json_lexical_error: called CC=16 SCAR=39.9
87 0 ▶ my ($text) = @_;
Subroutine _json_lexical_error: not called CC=16 SCAR=39.9
88 0 my $json = JSON->new->allow_nonref;
89 0 my @stack; # one entry per open container: undef for an array, a hash of keys for an object
90 0 my $want_key = 0;
91 0 pos($text) = 0;
92 0 ▶ while (pos($text) < length $text) {
Branch: 2 branches
BranchTrueFalseTotal
if (pos $text < length $text) 83666 2256 85922
if (pos $text < length $text) 0 0 0
93 0 ▶ if ($text =~ /\G\s+/gc) { next }
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\G\s+/cgu) 0 83666 83666
if ($text =~ /\G\s+/cgu) 0 0 0
94 0 ▶ if ($text =~ /\G\{/gc) { push @stack, {}; $want_key = 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\G\{/cgu) 8752 74914 83666
if ($text =~ /\G\{/cgu) 0 0 0
95 0 ▶ if ($text =~ /\G\[/gc) { push @stack, undef; $want_key = 0; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\G\[/cgu) 9915 64999 74914
if ($text =~ /\G\[/cgu) 0 0 0
96 0 ▶ if ($text =~ /\G[\}\]]/gc) { pop @stack; $want_key = 0; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\G[\}\]]/cgu) 18572 46427 64999
if ($text =~ /\G[\}\]]/cgu) 0 0 0
97 0 ▶ if ($text =~ /\G,/gc) { $want_key = ref $stack[-1] ? 1 : 0; next }
Branch: 4 branches
BranchTrueFalseTotal
if ($text =~ /\G,/cgu) 11641 34786 46427
ref $stack[-1] ? : 2473 9168 11641
if ($text =~ /\G,/cgu) 0 0 0
ref $stack[-1] ? : 0 0 0
98 0 ▶ if ($text =~ /\G:/gc) { next }
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\G:/cgu) 11209 23577 34786
if ($text =~ /\G:/cgu) 0 0 0
99 0 ▶ if ($text =~ /\G("(?:[^"\\]|\\.)*")/gcs) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\G("(?:[^"\\]|\\.)*")/cgsu) 17943 5634 23577
if ($text =~ /\G("(?:[^"\\]|\\.)*")/cgsu) 0 0 0
100 0 ▶ if ($want_key && ref $stack[-1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want_key and ref $stack[-1]) 11221 6722 17943
if ($want_key and ref $stack[-1]) 0 0 0
Condition33%2/6
$want_key and ref $stack[-1]
!ll&&!rl&&r
6722011221
$want_key and ref $stack[-1]
!ll&&!rl&&r
000
MC/DC50%1/2
$want_key and ref $stack[-1]
$want_key ref $stack[-1]
MC/DC0%0/2
$want_key and ref $stack[-1]
$want_key ref $stack[-1]
Truth table66%2/3
A and B
A= $want_keyB= ref $stack[-1]
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $want_keyB= ref $stack[-1]
ABresult
0X0
100
111
101 0 my $k = $json->decode($1);
102 # -09 §5: keys MUST be lowercase (the -08 contract, D10).
103 0 ▶ return "JSON object key is not lowercase\n" if $k =~ /[A-Z]/;
Branch: 2 branches
BranchTrueFalseTotal
if $k =~ /[A-Z]/u 7 11214 11221
if $k =~ /[A-Z]/u 0 0 0
104 0 ▶ return "duplicate JSON object key\n" if $stack[-1]{$k}++;
Branch: 2 branches
BranchTrueFalseTotal
if $stack[-1]{$k}++ 5 11209 11214
if $stack[-1]{$k}++ 0 0 0
105 0 $want_key = 0;
106 }
107 0 next;
108 }
109 0 ▶ $text =~ /\G([^\s,:\[\]\{\}"]+)/gc or last; # number, true, false, null
Branch: 2 branches
BranchTrueFalseTotal
$text =~ /\G([^\s,:\[\]\{\}"]+)/cgu or last 5634 0 5634
$text =~ /\G([^\s,:\[\]\{\}"]+)/cgu or last 0 0 0
110 0 ▶ return "JSON number is not an integer\n" if $1 =~ /^-?[0-9].*[.eE]/;
Branch: 2 branches
BranchTrueFalseTotal
if $1 =~ /^-?[0-9].*[.eE]/u 19 5615 5634
if $1 =~ /^-?[0-9].*[.eE]/u 0 0 0
111 }
112 0 return;
113 }
114  
115 207 1;
116  
117 __END__
« lib/Mail/DKIM2/BodyPlan.pm ↑ Summary lib/Mail/DKIM2/Gate.pm »