lib/GDPR/IAB/TCFv2/CMPValidator.pm

filter: Statementstmt 98.9% 94 / 95 Branchbran 89.6% 52 / 58 Conditioncond 75.0% 15 / 20 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 16 / 16 Podpod 85.7% 6 / 7 totaltotal 90.9% 190 / 209 CC 37 SCAR 15.7
CC
37
Cov
93%
_validate_http_options
8.0
new
7.0
state
6.1
CRAP 4.8
« blib/lib/GDPR/IAB/TCFv2/Parser.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/PublisherTC.pm »
1 package GDPR::IAB::TCFv2::CMPValidator 0.530;
2  
3 11 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Carp qw<croak carp>;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Scalar::Util qw<blessed>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # JSON::PP and Time::Piece are loaded lazily (see load_from_data and
10 # _parse_date). They are listed under META "recommends" rather than
11 # hard-required so the base parser doesn't pull them in for callers
12 # that never opt into CMPValidator. The HTTP::Tiny dependency is
13 # handled the same way in load_from_url.
14  
15 33 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
16 33 my ($klass, %args) = @_;
17  
18 33 _validate_http_options(\%args);
19  
20 my $self = {
21 cmps => {},
22 last_updated => undef,
23 now => $args{now},
24 verify_ssl => exists $args{verify_ssl} ? $args{verify_ssl} : 1,
25 timeout => exists $args{timeout} ? $args{timeout} : 30,
26 http_client => $args{http_client},
27 28 ▶ };
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'verify_ssl'} ? : 9 19 28
exists $args{'timeout'} ? : 10 18 28
28 28 bless $self, $klass;
29  
30 28 ▶ if (defined $args{file}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $args{'file'}) { } 18 10 28
elsif (defined $args{'url'}) { } 5 5 10
elsif (defined $args{'data'}) { } 5 0 5
31 18 $self->load_from_file($args{file});
32 }
33 elsif (defined $args{url}) {
34  
35 # Network fetch is opt-in. A library that silently dials out
36 # over the network when handed an arbitrary URL is a footgun
37 # (proxy traversal, blocked egress, surprise latency, supply-
38 # chain risk). Force the caller to pass network_ok => 1 so the
39 # decision is intentional.
40 croak "CMPValidator: refusing to fetch '$args{url}' because "
41 . "network_ok was not set. Pass network_ok => 1 to enable "
42 . "URL loading, or use file => '...' / data => '...' instead."
43 5 ▶ unless $args{network_ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"network_ok"} 2 3 5
44  
45 3 $self->load_from_url($args{url});
46 }
47 elsif (defined $args{data}) {
48 5 $self->load_from_data($args{data});
49 }
50  
51 20 return $self;
52 }
53  
54 33 ▶sub _validate_http_options {
Subroutine _validate_http_options: called CC=8 SCAR=20.8
55 33 my ($args) = @_;
56  
57 33 ▶ return unless exists $args->{http_client};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"http_client"} 26 7 33
58  
59 7 my $client = $args->{http_client};
60 7 ▶ croak "http_client must be a blessed object responding to ->get(\$url); got "
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($client) and $client->can("get") 3 4 7
defined $client ? : 1 0 1
Condition83%5/6
builtin::blessed($client) and $client->can("get")
!ll&&!rl&&r
214
ref $client || (defined $client ? 'a non-reference' : 'undef')
l!l&&r!l&&!r
210
MC/DC100%2/2
builtin::blessed($client) and $client->can("get")
builtin::blessed($client) $client->can("get")
MC/DC0%0/2
ref $client || (defined $client ? 'a non-reference' : 'undef')
ref $client (defined $client ? 'a non-reference' : 'undef')
Truth table100%3/3
A and B
A= builtin::blessed($client)B= $client->can("get")
ABresult
0X0
100
111
Truth table66%2/3
A || B
A= ref $clientB= (defined $client ? 'a non-reference' : 'undef')
ABresult
1X1
011
000
61 . (ref($client) || (defined $client ? "a non-reference" : "undef"))
62 unless blessed($client) && $client->can('get');
63  
64 croak "http_client and verify_ssl/timeout are mutually exclusive: configure SSL/timeout on the http_client itself"
65 4 ▶ if exists $args->{verify_ssl} || exists $args->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"verify_ssl"} or exists $args->{"timeout"} 2 2 4
Condition100%3/3
exists $args->{"verify_ssl"} or exists $args->{"timeout"}
l!l&&r!l&&!r
112
MC/DC100%2/2
exists $args->{"verify_ssl"} or exists $args->{"timeout"}
exists $args->{"verify_ssl"} exists $args->{"timeout"}
Truth table100%3/3
A or B
A= exists $args->{"verify_ssl"}B= exists $args->{"timeout"}
ABresult
1X1
011
000
66  
67 2 return;
68 }
69  
70 18 ▶sub load_from_file {
Subroutine load_from_file: called CC=2 SCAR=6.9
Pod documented
71 18 my ($self, $path) = @_;
72  
73 18 ▶ open my $fh, '<', $path or croak "Could not open CMP list file '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or croak("Could not open CMP list file '${path}': $!") 16 2 18
74 16 my $content = do { local $/ = undef; <$fh> };
75 16 close $fh;
76  
77 16 return $self->load_from_data($content);
78 }
79  
80 3 ▶sub load_from_url {
Subroutine load_from_url: called CC=4 SCAR=13.9
Pod documented
81 3 my ($self, $url) = @_;
82  
83 3 my $client = $self->{http_client};
84 3 ▶ unless (defined $client) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client) 1 2 3
85 1 ▶ eval { require HTTP::Tiny; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require HTTP::Tiny; 1 } } or croak("HTTP::Tiny is required to load CMP list from URL. Please install it or use a local file instead.") 1 0 1
86 or croak "HTTP::Tiny is required to load CMP list from URL. " . "Please install it or use a local file instead.";
87  
88 # env_proxy => 1: honor https_proxy / http_proxy / no_proxy.
89 # verify_SSL => 1 (default): pin the secure default regardless
90 # of the installed HTTP::Tiny version (older versions defaulted
91 # to 0).
92 1 $client = HTTP::Tiny->new(env_proxy => 1, verify_SSL => $self->{verify_ssl}, timeout => $self->{timeout},);
93 }
94  
95 3 my $response = $client->get($url);
96 3 ▶ croak "Failed to fetch CMP list from '$url': $response->{reason}" unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 2 1 3
97  
98 1 return $self->load_from_data($response->{content});
99 }
100  
101 22 ▶sub load_from_data {
Subroutine load_from_data: called CC=5 SCAR=16.1
Pod documented
102 22 my ($self, $json_text) = @_;
103  
104 22 ▶ eval { require JSON::PP; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require JSON::PP; 1 } } or croak("JSON::PP is required to decode the CMP list. Please install it (it is core in Perl 5.14+).") 22 0 22
105 or croak "JSON::PP is required to decode the CMP list. " . "Please install it (it is core in Perl 5.14+).";
106  
107 22 my $data = eval { JSON::PP->new->utf8->decode($json_text) };
108 22 ▶ croak "Failed to decode CMP list JSON: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 21 22
109  
110 21 ▶ croak "Invalid CMP list format: missing 'cmps' key" unless ref $data eq 'HASH' && $data->{cmps};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and $data->{"cmps"} 1 20 21
Condition66%2/3
ref $data eq "HASH" and $data->{"cmps"}
!ll&&!rl&&r
0120
MC/DC50%1/2
ref $data eq "HASH" and $data->{"cmps"}
ref $data eq "HASH" $data->{"cmps"}
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= $data->{"cmps"}
ABresult
0X0
100
111
111  
112 20 $self->{cmps} = $data->{cmps};
113 20 $self->{last_updated} = $data->{lastUpdated};
114  
115 20 $self->_check_age();
116  
117 20 return $self;
118 }
119  
120 10 ▶sub is_valid {
Subroutine is_valid: called CC=5 SCAR=16.1
Pod documented
121 10 my ($self, $cmp_id) = @_;
122  
123 10 my $cmp = $self->{cmps}->{$cmp_id};
124 10 ▶ return 0 unless $cmp;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmp 2 8 10
125  
126 8 ▶ if ($cmp->{deletedDate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp->{"deletedDate"}) 3 5 8
127 3 my $deleted = $self->_parse_date($cmp->{deletedDate});
128 3 ▶ return 0 if $deleted && $deleted <= $self->_now();
Branch: 1 branch
BranchTrueFalseTotal
if $deleted and $deleted <= $self->_now 2 1 3
Condition66%2/3
$deleted and $deleted <= $self->_now
!ll&&!rl&&r
012
MC/DC50%1/2
$deleted and $deleted <= $self->_now
$deleted $deleted <= $self->_now
Truth table66%2/3
A and B
A= $deletedB= $deleted <= $self->_now
ABresult
0X0
100
111
129 }
130  
131 6 return 1;
132 }
133  
134 1039 ▶sub state {
Subroutine state: called CC=6 SCAR=18.2
Pod undocumented
135 1039 my ($self, $cmp_id, $now) = @_;
136  
137 1039 my $cmp = $self->{cmps}->{$cmp_id};
138 1039 ▶ return 'unknown' unless $cmp;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmp 837 202 1039
139  
140 202 ▶ if ($cmp->{deletedDate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp->{"deletedDate"}) 2 200 202
141 2 my $deleted = $self->_parse_date($cmp->{deletedDate});
142 2 ▶ my $ref = defined $now ? $now : $self->_now();
Branch: 1 branch
BranchTrueFalseTotal
defined $now ? : 1 1 2
143 2 ▶ return 'deleted' if $deleted && $deleted <= $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $deleted and $deleted <= $ref 2 0 2
Condition33%1/3
$deleted and $deleted <= $ref
!ll&&!rl&&r
002
MC/DC0%0/2
$deleted and $deleted <= $ref
$deleted $deleted <= $ref
Truth table33%1/3
A and B
A= $deletedB= $deleted <= $ref
ABresult
0X0
100
111
144 }
145  
146 200 return 'active';
147 }
148  
149 20 ▶sub last_updated_epoch {
Subroutine last_updated_epoch: called CC=2 SCAR=6.9
Pod documented
150 20 my ($self) = @_;
151 20 ▶ return unless $self->{last_updated};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"last_updated"} 1 19 20
152 19 return $self->_parse_date($self->{last_updated});
153 }
154  
155 20 ▶sub _check_age {
Subroutine _check_age: called CC=3 SCAR=11.0
156 20 my ($self) = @_;
157  
158 20 my $epoch = $self->last_updated_epoch();
159 20 ▶ return unless $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless $epoch 1 19 20
160  
161 19 my $age_days = ($self->_now() - $epoch) / 86400;
162 19 ▶ if ($age_days > 28) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age_days > 28) 8 11 19
163 8 carp sprintf "CMP list is older than 28 days (last updated: %s)", $self->{last_updated};
164 }
165 19 return;
166 }
167  
168 23 ▶sub _now {
Subroutine _now: called CC=2 SCAR=6.9
169 23 my ($self) = @_;
170 23 ▶ return $self->{now} // time();
Condition100%2/2
$self->{"now"} // time
l!l
176
MC/DC100%1/1
$self->{"now"} // time
$self->{"now"}
Truth table100%2/2
A
A= $self->{"now"}
Aresult
11
00
171 }
172  
173 24 ▶sub _load_time_piece {
Subroutine _load_time_piece: called CC=2 SCAR=6.9
174 24 ▶ return 1 if $INC{'Time/Piece.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Time/Piece.pm"} 15 9 24
175 9 require Time::Piece;
176 9 return 1;
177 }
178  
179 24 ▶sub _parse_date {
Subroutine _parse_date: called CC=2 SCAR=7.1
180 24 my ($self, $date_str) = @_;
181  
182 # IAB Registry timestamps come as "2020-04-27T20:27:54Z" or
183 # "2020-04-27T20:27:54.2Z". Strip the optional fractional seconds
184 # and the timezone suffix (always Z in the IAB feed) before handing
185 # to Time::Piece, which doesn't grok %z portably.
186 24 ▶ if ($date_str =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_str =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/u) 24 0 24
187 24 $self->_load_time_piece();
188  
189 24 my $t_str = "$1-$2-$3 $4:$5:$6";
190 24 my $epoch = eval { Time::Piece->strptime($t_str, "%Y-%m-%d %H:%M:%S")->epoch; };
191 24 return $epoch;
192 }
193 0 return;
194 }
195  
196 11 1;
197  
198 __END__
« blib/lib/GDPR/IAB/TCFv2/Parser.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/PublisherTC.pm »