| 1 | package Mail::DKIM2::TagValueList; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 223 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 223 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 223 | our $VERSION = '0.35'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Simple tag=value list as defined in draft-ietf-dkim-dkim2-spec-09 Section 8. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Preserves insertion order for serialization. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3999 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | return bless { tags => {}, order => [] }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # The tag-list grammar shared by every DKIM2 field (-09 §8): a tag | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # name is ALPHA *(ALPHA / DIGIT / "_"), and a value is printable ASCII other | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # than ";" with FWS only between characters (x-tag-value). The known tags' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # own grammars are all narrower than that, so a field that fails it is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # malformed (§14.2) whatever its tags are. DKIM1 key records use the same | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # value characters (RFC 6376 §3.2 VALCHAR). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 223 | our $TAG_NAME = qr/[A-Za-z][A-Za-z0-9_]*/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 223 | our $TAG_VALUE = qr/(?:[\x21-\x3A\x3C-\x7E]+(?:[ \t\r\n]+[\x21-\x3A\x3C-\x7E]+)*)?/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Split a tag-list into [name, value] pairs, the whitespace around names and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # values trimmed. A trailing ";" is allowed. Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # (\@pairs, $ok): $ok is false if an empty spec comes before the end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # (";;"), any other spec is not name=value, or a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # value has a character the grammar does not allow. The pairs still hold | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # every spec that did parse, for diagnostics. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 7395 | ▶ | sub split_tag_list { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_tag_list: called CC=8 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my ($string) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_tag_list: not called CC=8 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my (@pairs, $bad); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my @specs = split /;/, $string, -1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # -09 section 8: one ";" may end the list; an empty spec anywhere else | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # (";;", a leading ";") is a syntax error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | pop @specs if @specs && $specs[-1] =~ /\A[ \t\r\n]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
MC/DC100%2/2
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
@specs
$specs[-1] =~ /\A[ \t\r\n]*\z/
MC/DC0%0/2
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
@specs
$specs[-1] =~ /\A[ \t\r\n]*\z/
Truth table100%3/3
A and B
A= @specsB= $specs[-1] =~ /\A[ \t\r\n]*\z/
Truth table0%0/3
A and B
A= @specsB= $specs[-1] =~ /\A[ \t\r\n]*\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | for my $spec (@specs) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($spec =~ /\A[ \t\r\n]*\z/) { $bad = 1; next } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # The value's trailing FWS is trimmed by a greedy match that backs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # up to the last non-WSP character, never by "(.*?)\s*\z": that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # retried every position of a run of internal FWS against the rest | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # of it, quadratic in the run (reported against 0.34, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # CVE-2026-129848). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | my ($name, $val) = $spec =~ /\A[ \t\r\n]*($TAG_NAME)[ \t\r\n]*=[ \t\r\n]*(.*)\z/s | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | or do { $bad = 1; next }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $val = $val =~ /\A(.*[^ \t\r\n])/s ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $bad = 1 unless $val =~ /\A$TAG_VALUE\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | push @pairs, [$name, $val]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | return (\@pairs, !$bad); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # A constructor: always a fresh object, even when called on an existing one | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # (parsing into one left its old tags and duplicate flag behind). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3195 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=5 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ($class, $string) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=5 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $self = (ref($class) || $class)->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
ref $class || $class
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # Tag names keep their original case and order, and values their | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # internal whitespace; as_string() writes "name=value" joined by "; ", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # so the whitespace around "=" and ";" and a trailing ";" are not kept. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # That is harmless for signing input: §12.8 deletes all WSP from these | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # fields before hashing. Tag-names are case-sensitive (-09 §8, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # -08 contract D10): "M=" is a tag of its own, which DKIM2 does not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # know, never another spelling of "m=". A spec that is not a tag is never silently | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # dropped from what is verified: it makes the whole field a syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # error (follow-up review F3). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my ($pairs, $ok) = split_tag_list($string); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $self->{_syntax_error} = 1 unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my (@order, %seen); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | for my $p (@$pairs) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ($name, $val) = @$p; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # §8: "there MUST be only one of each kind" — flag any repeat. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $self->{_duplicate} = $name if $seen{$name}++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->{tags}{$name} = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | push @order, $name; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $self->{order} = \@order; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # -09 §8: tag-names are case-sensitive, so the lookup is exact. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 50590 | ▶ | sub get_tag { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_tag: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_tag: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return $self->{tags}{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # The tag name that appeared more than once, if any (-09 §8). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 962 | ▶ | sub duplicate_tag { return $_[0]->{_duplicate} } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
duplicate_tag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # True if the parsed field did not match the tag-list grammar. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1047 | ▶ | sub syntax_error { return $_[0]->{_syntax_error} } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
syntax_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 6817 | ▶ | sub set_tag { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_tag: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 6817 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 6817 | ▶ | unless (exists $self->{tags}{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 5501 | push @{$self->{order}}, $name; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 6817 | $self->{tags}{$name} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 4 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | return join('; ', map { "$_=$self->{tags}{$_}" } @{$self->{order}}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 223 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||