| 1 | package Mail::DKIM2::RecipeJSON; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 223 | ▶ | use 5.20.0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 223 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 223 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 223 | our $VERSION = '0.35'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 223 | ▶ | use B (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 223 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 223 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 223 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 223 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 223 | 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 | 295 | ▶ | 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 | 2320 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | die "JSON text is not UTF-8\n" unless defined $text; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 31177 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | if ref $v eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return [ map { _json_octets($_) } @$v ] if ref $v eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return $v if !defined $v || ref $v; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
not defined $v or ref $v
not defined $v or ref $v
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
Truth table0%0/3
not(A) or B
A= not defined $vB= ref $v
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return $v unless B::svref_2object(\$v)->FLAGS & B::SVf_POK; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return _json_octet_string($v); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 18038 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1851 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | return $ok ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 2312 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($text =~ /\G\s+/gc) { next } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($text =~ /\G\{/gc) { push @stack, {}; $want_key = 1; next } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($text =~ /\G\[/gc) { push @stack, undef; $want_key = 0; next } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($text =~ /\G[\}\]]/gc) { pop @stack; $want_key = 0; next } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($text =~ /\G,/gc) { $want_key = ref $stack[-1] ? 1 : 0; next } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($text =~ /\G:/gc) { next } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ($text =~ /\G("(?:[^"\\]|\\.)*")/gcs) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($want_key && ref $stack[-1]) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$want_key and ref $stack[-1]
$want_key and ref $stack[-1]
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]
Truth table0%0/3
A and B
A= $want_keyB= ref $stack[-1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | return "duplicate JSON object key\n" if $stack[-1]{$k}++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return "JSON number is not an integer\n" if $1 =~ /^-?[0-9].*[.eE]/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 223 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||