blib/lib/GDPR/IAB/TCFv2/Validator.pm

filter: Statementstmt 98.8% 164 / 166 Branchbran 93.7% 90 / 96 Conditioncond 97.7% 43 / 44 MC/DCmcdc 96.3% 26 / 27 Subroutinesub 100% 23 / 23 Podpod 100% 3 / 3 totaltotal 97.2% 349 / 359 CC 69 SCAR 21.9
CC
69
Cov
97%
_run_validation
20.0
_check_li_purposes
11.0
_check_consent_purposes
8.0
CRAP 8.9
« bin/iabtcfv2 ↑ Summary blib/lib/GDPR/IAB/TCFv2/BitUtils.pm »
1 package GDPR::IAB::TCFv2::Validator 0.520;
2  
3 25 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 25 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Scalar::Util qw<blessed>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use GDPR::IAB::TCFv2;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use GDPR::IAB::TCFv2::Constants::RestrictionType qw<:all>;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use GDPR::IAB::TCFv2::Validator::Failure;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use GDPR::IAB::TCFv2::Validator::Reason qw<:all>;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use GDPR::IAB::TCFv2::Validator::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 sub new {
16 70 ▶ my ($klass, %args) = @_;
Subroutine new: called CC=6 SCAR=17.9
Pod documented
17  
18 70 ▶ my $consent = $args{consent_purpose_ids} || [];
Condition100%2/2
$args{'consent_purpose_ids'} || []
l!l
3238
MC/DC100%1/1
$args{'consent_purpose_ids'} || []
$args{'consent_purpose_ids'}
Truth table100%2/2
A
A= $args{'consent_purpose_ids'}
Aresult
11
00
19 70 ▶ my $legitimate_interest = $args{legitimate_interest_purpose_ids} || [];
Condition100%2/2
$args{'legitimate_interest_purpose_ids'} || []
l!l
1456
MC/DC100%1/1
$args{'legitimate_interest_purpose_ids'} || []
$args{'legitimate_interest_purpose_ids'}
Truth table100%2/2
A
A= $args{'legitimate_interest_purpose_ids'}
Aresult
11
00
20 70 ▶ my $flexible = $args{flexible_purpose_ids} || [];
Condition100%2/2
$args{'flexible_purpose_ids'} || []
l!l
862
MC/DC100%1/1
$args{'flexible_purpose_ids'} || []
$args{'flexible_purpose_ids'}
Truth table100%2/2
A
A= $args{'flexible_purpose_ids'}
Aresult
11
00
21  
22 70 _check_coherence($consent, $legitimate_interest, $flexible);
23  
24 # Compute cmp_validator in scalar context so a bare `return` from the
25 # coercer correctly yields undef -- a list-context call inside the
26 # anonymous-hash construction below would collapse the key/value
27 # pair instead.
28 68 my $cmp_validator = _coerce_cmp_validator($args{cmp_validator});
29  
30 my $self = {
31 vendor_id => $args{vendor_id},
32 consent_purpose_ids => $consent,
33 legitimate_interest_purpose_ids => $legitimate_interest,
34 flexible_purpose_ids => $flexible,
35 8 ▶ _flexible_set => {map { $_ => 1 } @{$flexible}},
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'strict_legal_basis'} ? : 17 50 67
Condition100%2/2
$args{"verify_disclosed_vendors"} // 0
l!l
2245
MC/DC100%1/1
$args{"verify_disclosed_vendors"} // 0
$args{"verify_disclosed_vendors"}
Truth table100%2/2
A
A= $args{"verify_disclosed_vendors"}
Aresult
11
00
36 verify_disclosed_vendors => $args{verify_disclosed_vendors} // 0,
37 min_tcf_policy_version => $args{min_tcf_policy_version},
38 cmp_validator => $cmp_validator,
39 67 strict_legal_basis => exists $args{strict_legal_basis} ? $args{strict_legal_basis} : 0,
40 };
41  
42 67 return bless $self, $klass;
43 }
44  
45 # Accept either a CMPValidator object, a hashref of constructor args
46 # (auto-instantiated lazily on the first call), or undef. Defer the
47 # `require` so callers who never opt into the CMP rule never pay for
48 # loading JSON::PP / Time::Piece.
49 sub _coerce_cmp_validator {
50 69 ▶ my ($spec) = @_;
Subroutine _coerce_cmp_validator: called CC=5 SCAR=16.1
51  
52 # Bare `return` is fine -- callers always invoke this in scalar
53 # context (see the explicit `my $cmp_validator = ...` in `new` and the
54 # `my $cmp_validator = ...` in `_run_validation`).
55 69 ▶ return unless defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 57 12 69
56 12 ▶ return $spec if blessed($spec) && $spec->isa('GDPR::IAB::TCFv2::CMPValidator');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($spec) and $spec->isa("GDPR::IAB::TCFv2::CMPValidator") 7 5 12
Condition66%2/3
builtin::blessed($spec) and $spec->isa("GDPR::IAB::TCFv2::CMPValidator")
!ll&&!rl&&r
507
MC/DC50%1/2
builtin::blessed($spec) and $spec->isa("GDPR::IAB::TCFv2::CMPValidator")
builtin::blessed($spec) $spec->isa("GDPR::IAB::TCFv2::CMPValidator")
Truth table66%2/3
A and B
A= builtin::blessed($spec)B= $spec->isa("GDPR::IAB::TCFv2::CMPValidator")
ABresult
0X0
100
111
57  
58 5 ▶ croak "cmp_validator must be a GDPR::IAB::TCFv2::CMPValidator object " . "or a hashref of constructor arguments"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 1 4 5
59 unless ref($spec) eq 'HASH';
60  
61 4 require GDPR::IAB::TCFv2::CMPValidator;
62 4 return GDPR::IAB::TCFv2::CMPValidator->new(%{$spec});
63 }
64  
65 sub _check_coherence {
66 70 ▶ my ($consent, $legitimate_interest, $flexible) = @_;
Subroutine _check_coherence: called CC=6 SCAR=17.9
67  
68 70 my %consent_set = map { $_ => 1 } @{$consent};
69 70 my %li_set = map { $_ => 1 } @{$legitimate_interest};
70  
71 70 foreach my $pid (@{$consent}) {
72 41 ▶ croak "purpose $pid cannot be in both consent_purpose_ids and legitimate_interest_purpose_ids" if $li_set{$pid};
Branch: 1 branch
BranchTrueFalseTotal
if $li_set{$pid} 1 40 41
73 }
74  
75 69 foreach my $pid (@{$flexible}) {
76 9 ▶ next if $consent_set{$pid} || $li_set{$pid};
Branch: 1 branch
BranchTrueFalseTotal
if $consent_set{$pid} or $li_set{$pid} 8 1 9
Condition100%3/3
$consent_set{$pid} or $li_set{$pid}
l!l&&r!l&&!r
531
MC/DC100%2/2
$consent_set{$pid} or $li_set{$pid}
$consent_set{$pid} $li_set{$pid}
Truth table100%3/3
A or B
A= $consent_set{$pid}B= $li_set{$pid}
ABresult
1X1
011
000
77 1 croak "flexible purpose $pid must also appear in consent_purpose_ids or legitimate_interest_purpose_ids";
78 }
79  
80 68 return;
81 }
82  
83 sub validate {
84 62 ▶ my ($self, $input, %overrides) = @_;
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
85  
86 62 return $self->_run_validation($input, 1, %overrides);
87 }
88  
89 sub validate_all {
90 9224 ▶ my ($self, $input, %overrides) = @_;
Subroutine validate_all: called CC=1 SCAR=0.0
Pod documented
91  
92 9224 return $self->_run_validation($input, 0, %overrides);
93 }
94  
95 sub _run_validation {
96 9286 ▶ my ($self, $input, $stop_on_first, %overrides) = @_;
Subroutine _run_validation: called CC=20 SCAR=30.0
97  
98 9286 ▶ my $tc = ref($input) eq 'GDPR::IAB::TCFv2' ? $input : GDPR::IAB::TCFv2->Parse($input);
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq 'GDPR::IAB::TCFv2' ? : 0 9286 9286
99  
100 9283 ▶ my $vendor_id = exists $overrides{vendor_id} ? $overrides{vendor_id} : $self->{vendor_id};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'vendor_id'} ? : 3 9280 9283
101 my $strict_legal_basis
102 9283 ▶ = exists $overrides{strict_legal_basis} ? $overrides{strict_legal_basis} : $self->{strict_legal_basis};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'strict_legal_basis'} ? : 1 9282 9283
103 my $verify_disclosed
104 = exists $overrides{verify_disclosed_vendors}
105 ? $overrides{verify_disclosed_vendors}
106 9283 ▶ : $self->{verify_disclosed_vendors};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'verify_disclosed_vendors'} ? : 0 9283 9283
107 my $min_tcf_policy_version
108 9283 ▶ = exists $overrides{min_tcf_policy_version} ? $overrides{min_tcf_policy_version} : $self->{min_tcf_policy_version};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'min_tcf_policy_version'} ? : 0 9283 9283
109 my $cmp_validator
110 9283 ▶ = exists $overrides{cmp_validator} ? _coerce_cmp_validator($overrides{cmp_validator}) : $self->{cmp_validator};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'cmp_validator'} ? : 1 9282 9283
111  
112 # Per-call list overrides. Coherence is not re-validated here:
113 # orphan flexible purposes (a pid in flexible_purpose_ids that
114 # isn't also in consent_purpose_ids or legitimate_interest_purpose_ids)
115 # are silently dropped because the rule loops only iterate over the
116 # consent/LI lists, so the flex flag for an orphan is unreachable.
117 # This keeps per-call overrides forgiving while the constructor
118 # remains strict for the static policy.
119 my $consent_ids
120 9283 ▶ = exists $overrides{consent_purpose_ids} ? $overrides{consent_purpose_ids} : $self->{consent_purpose_ids};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'consent_purpose_ids'} ? : 5 9278 9283
121 my $li_ids
122 = exists $overrides{legitimate_interest_purpose_ids}
123 ? $overrides{legitimate_interest_purpose_ids}
124 9283 ▶ : $self->{legitimate_interest_purpose_ids};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'legitimate_interest_purpose_ids'} ? : 4 9279 9283
125 my $flexible_set
126 = exists $overrides{flexible_purpose_ids}
127 3 ? {map { $_ => 1 } @{$overrides{flexible_purpose_ids}}}
128 9283 ▶ : $self->{_flexible_set};
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides{'flexible_purpose_ids'} ? : 2 9281 9283
129  
130 9283 ▶ croak "missing vendor_id" unless defined $vendor_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vendor_id 1 9282 9283
131  
132 9282 my @failures;
133  
134 9282 $self->_check_min_tcf_policy_version($tc, $min_tcf_policy_version, \@failures);
135 9282 ▶ return $self->_make_result(0, \@failures) if $stop_on_first && @failures;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first and @failures 3 9279 9282
Condition100%3/3
$stop_on_first and @failures
!ll&&!rl&&r
9224553
MC/DC100%2/2
$stop_on_first and @failures
$stop_on_first @failures
Truth table100%3/3
A and B
A= $stop_on_firstB= @failures
ABresult
0X0
100
111
136  
137 9279 $self->_check_cmp_validator($tc, $cmp_validator, \@failures);
138 9279 ▶ return $self->_make_result(0, \@failures) if $stop_on_first && @failures;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first and @failures 4 9275 9279
Condition100%3/3
$stop_on_first and @failures
!ll&&!rl&&r
9224514
MC/DC100%2/2
$stop_on_first and @failures
$stop_on_first @failures
Truth table100%3/3
A and B
A= $stop_on_firstB= @failures
ABresult
0X0
100
111
139  
140 9275 $self->_check_disclosed($tc, $vendor_id, $verify_disclosed, $min_tcf_policy_version, \@failures);
141 9275 ▶ return $self->_make_result(0, \@failures) if $stop_on_first && @failures;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first and @failures 1 9274 9275
Condition100%3/3
$stop_on_first and @failures
!ll&&!rl&&r
9224501
MC/DC100%2/2
$stop_on_first and @failures
$stop_on_first @failures
Truth table100%3/3
A and B
A= $stop_on_firstB= @failures
ABresult
0X0
100
111
142  
143 9274 $self->_check_consent_purposes($tc, $vendor_id, $strict_legal_basis, \@failures, $stop_on_first, $consent_ids,
144 $flexible_set,);
145 9273 ▶ return $self->_make_result(0, \@failures) if $stop_on_first && @failures;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first and @failures 12 9261 9273
Condition100%3/3
$stop_on_first and @failures
!ll&&!rl&&r
92243712
MC/DC100%2/2
$stop_on_first and @failures
$stop_on_first @failures
Truth table100%3/3
A and B
A= $stop_on_firstB= @failures
ABresult
0X0
100
111
146  
147 9261 $self->_check_li_purposes($tc, $vendor_id, $strict_legal_basis, \@failures, $stop_on_first, $li_ids, $flexible_set,);
148  
149 9261 ▶ if (@failures) {
Branch: 1 branch
BranchTrueFalseTotal
if (@failures) 2278 6983 9261
150 2278 return $self->_make_result(0, \@failures);
151 }
152  
153 6983 return $self->_make_result(1, []);
154 }
155  
156 sub _check_cmp_validator {
157 9279 ▶ my ($self, $tc, $cmp_validator, $failures) = @_;
Subroutine _check_cmp_validator: called CC=3 SCAR=11.0
158  
159 9279 ▶ return unless defined $cmp_validator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmp_validator 8244 1035 9279
160  
161 1035 my $cmp_id = $tc->cmp_id;
162 1035 ▶ unless ($cmp_validator->is_valid($cmp_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmp_validator->is_valid($cmp_id)) 836 199 1035
163 836 push @{$failures},
164 GDPR::IAB::TCFv2::Validator::Failure->new(
165 code => ReasonInvalidCMP,
166 message => "CMP $cmp_id is not valid/disclosed",
167 cmp_id => $cmp_id,
168 );
169 }
170 1035 return;
171 }
172  
173 sub _check_min_tcf_policy_version {
174 9282 ▶ my ($self, $tc, $min_tcf_policy_version, $failures) = @_;
Subroutine _check_min_tcf_policy_version: called CC=3 SCAR=11.0
175  
176 9282 ▶ return unless defined $min_tcf_policy_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min_tcf_policy_version 8251 1031 9282
177  
178 1031 my $actual = $tc->policy_version;
179 1031 ▶ if ($actual < $min_tcf_policy_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual < $min_tcf_policy_version) 17 1014 1031
180 17 push @{$failures},
181 GDPR::IAB::TCFv2::Validator::Failure->new(
182 code => ReasonPolicyVersionTooLow,
183 message => "TC string policy version $actual is below required minimum $min_tcf_policy_version",
184 );
185 }
186 1031 return;
187 }
188  
189 sub _check_disclosed {
190 9275 ▶ my ($self, $tc, $vendor_id, $verify_disclosed, $min_tcf_policy_version, $failures) = @_;
Subroutine _check_disclosed: called CC=6 SCAR=17.9
191  
192 9275 ▶ return unless $verify_disclosed;
Branch: 1 branch
BranchTrueFalseTotal
unless $verify_disclosed 8246 1029 9275
193  
194 1029 ▶ if ($tc->has_vendor_disclosure) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tc->has_vendor_disclosure) { } 55 974 1029
elsif (defined $min_tcf_policy_version and $min_tcf_policy_version >= 5) { } 1 1945 1946
195 55 ▶ unless ($tc->disclosed_vendor($vendor_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tc->disclosed_vendor($vendor_id)) 2 53 55
196 2 push @{$failures},
197 GDPR::IAB::TCFv2::Validator::Failure->new(
198 code => ReasonVendorNotDisclosed,
199 message => "vendor $vendor_id not disclosed",
200 vendor_id => $vendor_id,
201 );
202 }
203 }
204 ▶ elsif (defined $min_tcf_policy_version && $min_tcf_policy_version >= 5) {
Condition100%3/3
defined $min_tcf_policy_version and $min_tcf_policy_version >= 5
!ll&&!rl&&r
97211
MC/DC100%2/2
defined $min_tcf_policy_version and $min_tcf_policy_version >= 5
defined $min_tcf_policy_version $min_tcf_policy_version >= 5
Truth table100%3/3
A and B
A= defined $min_tcf_policy_versionB= $min_tcf_policy_version >= 5
ABresult
0X0
100
111
205 1 push @{$failures},
206 GDPR::IAB::TCFv2::Validator::Failure->new(
207 code => ReasonMissingDisclosedVendors,
208 message => "missing disclosed vendors segment",
209 vendor_id => $vendor_id,
210 );
211 }
212  
213 1029 return;
214 }
215  
216 sub _check_consent_purposes {
217 9274 ▶ my ($self, $tc, $vendor_id, $strict_legal_basis, $failures, $stop_on_first, $consent_ids, $flexible_set) = @_;
Subroutine _check_consent_purposes: called CC=8 SCAR=20.8
218  
219 9274 foreach my $pid (@{$consent_ids}) {
220 3109 my $is_flexible = $flexible_set->{$pid};
221  
222 # Publisher-restriction inspection runs before the parser
223 # delegate so a restriction-driven failure carries the precise
224 # Reason* code (and restriction_type) rather than the generic
225 # vendor-not-allowed code. Skipped for flexible purposes: the
226 # flex API may flip the basis based on the restriction itself,
227 # so what looks like a contradiction here can still pass.
228 3109 ▶ if (!$is_flexible) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_flexible) 2081 1028 3109
229 2081 my $pr_failure = $self->_publisher_restriction_failure($tc, $vendor_id, $pid, 0);
230 2081 ▶ if ($pr_failure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pr_failure) 2 2079 2081
231 2 push @{$failures}, $pr_failure;
232 2 ▶ return if $stop_on_first;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first 2 0 2
233 0 next;
234 }
235 }
236  
237 3107 ▶ my $is_allowed
Branch: 1 branch
BranchTrueFalseTotal
$is_flexible ? : 1028 2079 3107
238 = $is_flexible
239 ? $tc->is_vendor_allowed_for_flexible_purpose($vendor_id, $pid, 0, strict => $strict_legal_basis)
240 : $tc->is_vendor_consent_allowed($vendor_id, $pid, strict => $strict_legal_basis);
241  
242 3106 ▶ unless ($is_allowed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_allowed) 1090 2016 3106
243 1090 push @{$failures},
244 GDPR::IAB::TCFv2::Validator::Failure->new(
245 code => ReasonVendorNotAllowedConsent,
246 message => "vendor $vendor_id not allowed for purpose $pid (consent)",
247 purpose_id => $pid,
248 vendor_id => $vendor_id,
249 );
250 1090 ▶ return if $stop_on_first;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first 10 1080 1090
251 }
252 }
253 9261 return;
254 }
255  
256 sub _check_li_purposes {
257 9261 ▶ my ($self, $tc, $vendor_id, $strict_legal_basis, $failures, $stop_on_first, $li_ids, $flexible_set) = @_;
Subroutine _check_li_purposes: called CC=11 SCAR=24.0
258  
259 9261 my $policy_version = $tc->policy_version;
260  
261 9261 foreach my $pid (@{$li_ids}) {
262 2063 my $is_flexible = $flexible_set->{$pid};
263  
264 # TCF carve-out: legitimate interest is never permitted for
265 # Purpose 1, and is forbidden for Purposes 3-6 in TCF v2.2+
266 # (TcfPolicyVersion >= 4). When the configured basis is LI and
267 # the purpose is not flexible, this is a configuration the
268 # spec cannot satisfy regardless of the vendor's signals; emit
269 # the dedicated reason instead of the generic LI failure so
270 # callers can distinguish "spec forbids this" from "vendor
271 # missing the LI bit".
272 2063 ▶ if (!$is_flexible && _li_carve_out_applies($pid, $policy_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_flexible and _li_carve_out_applies($pid, $policy_version)) 4 2059 2063
Condition100%3/3
not $is_flexible and _li_carve_out_applies($pid, $policy_version)
!ll&&!rl&&r
102610334
MC/DC100%2/2
not $is_flexible and _li_carve_out_applies($pid, $policy_version)
not $is_flexible _li_carve_out_applies($pid, $policy_version)
Truth table100%3/3
not(A) and B
A= not $is_flexibleB= _li_carve_out_applies($pid, $policy_version)
ABresult
0X0
100
111
273 4 push @{$failures},
274 GDPR::IAB::TCFv2::Validator::Failure->new(
275 code => ReasonLegitimateInterestNotPermittedForPurpose,
276 message => "legitimate interest not permitted for purpose $pid",
277 purpose_id => $pid,
278 vendor_id => $vendor_id,
279 );
280 4 ▶ return if $stop_on_first;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first 4 0 4
281 0 next;
282 }
283  
284 # Publisher-restriction inspection: same rationale as on the
285 # consent path. Skipped for flexible purposes (the flex API
286 # honors the restriction by flipping bases, so what looks like
287 # a contradiction here can still pass).
288 2059 ▶ if (!$is_flexible) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_flexible) 1033 1026 2059
289 1033 my $pr_failure = $self->_publisher_restriction_failure($tc, $vendor_id, $pid, 1);
290 1033 ▶ if ($pr_failure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pr_failure) 296 737 1033
291 296 push @{$failures}, $pr_failure;
292 296 ▶ return if $stop_on_first;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first 1 295 296
293 295 next;
294 }
295 }
296  
297 1763 ▶ my $is_allowed
Branch: 1 branch
BranchTrueFalseTotal
$is_flexible ? : 1026 737 1763
298 = $is_flexible
299 ? $tc->is_vendor_allowed_for_flexible_purpose($vendor_id, $pid, 1, strict => $strict_legal_basis)
300 : $tc->is_vendor_legitimate_interest_allowed($vendor_id, $pid, strict => $strict_legal_basis);
301  
302 1763 ▶ unless ($is_allowed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_allowed) 62 1701 1763
303 62 push @{$failures},
304 GDPR::IAB::TCFv2::Validator::Failure->new(
305 code => ReasonVendorNotAllowedLegitimateInterest,
306 message => "vendor $vendor_id not allowed for purpose $pid (legitimate interest)",
307 purpose_id => $pid,
308 vendor_id => $vendor_id,
309 );
310 62 ▶ return if $stop_on_first;
Branch: 1 branch
BranchTrueFalseTotal
if $stop_on_first 0 62 62
311 }
312 }
313 9256 return;
314 }
315  
316 sub _li_carve_out_applies {
317 1037 ▶ my ($pid, $policy_version) = @_;
Subroutine _li_carve_out_applies: called CC=5 SCAR=16.1
318  
319 1037 ▶ return 1 if $pid == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $pid == 1 3 1034 1037
320 1034 ▶ return 1 if $pid >= 3 && $pid <= 6 && $policy_version >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $pid >= 3 and $pid <= 6 and $policy_version >= 4 1 1033 1034
Condition100%6/6
$pid >= 3 and $pid <= 6 and $policy_version >= 4
!ll&&!rl&&r
103211
$pid >= 3 and $pid <= 6
!ll&&!rl&&r
110312
MC/DC100%3/3
$pid >= 3 and $pid <= 6 and $policy_version >= 4
$pid >= 3 $pid <= 6 $policy_version >= 4
Truth table100%4/4
A and B and C
A= $pid >= 3B= $pid <= 6C= $policy_version >= 4
ABCresult
0XX0
10X0
1100
1111
321 1033 return 0;
322 }
323  
324 # Inspect publisher restrictions for ($vendor_id, $pid) and return a
325 # Failure object when a restriction contradicts the configured basis,
326 # or undef when no restriction-driven failure applies.
327 #
328 # $basis: 0 = consent, 1 = legitimate interest.
329 #
330 # A NotAllowed restriction always wins (it terminates the rule for
331 # either basis). Beyond that, only the restriction type that
332 # contradicts the configured basis is reported here -- the matching
333 # restriction (e.g. RequireConsent on a consent-basis purpose) is a
334 # legal coexistence and falls through to the parser delegate.
335 sub _publisher_restriction_failure {
336 3114 ▶ my ($self, $tc, $vendor_id, $pid, $basis) = @_;
Subroutine _publisher_restriction_failure: called CC=6 SCAR=17.9
337  
338 3114 ▶ if ($tc->check_publisher_restriction($pid, NotAllowed, $vendor_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tc->check_publisher_restriction($pid, 0, $vendor_id)) 2 3112 3114
339 2 return GDPR::IAB::TCFv2::Validator::Failure->new(
340 code => ReasonPublisherRestrictionNotAllowed,
341 message => "publisher restriction: purpose $pid not allowed (vendor $vendor_id)",
342 purpose_id => $pid,
343 vendor_id => $vendor_id,
344 restriction_type => NotAllowed,
345 );
346 }
347  
348 3112 ▶ if ($basis == 0 && $tc->check_publisher_restriction($pid, RequireLegitimateInterest, $vendor_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basis == 0 and $tc->check_publisher_restriction($pid, 2, $vendor_id)) 1 3111 3112
Condition100%3/3
$basis == 0 and $tc->check_publisher_restriction($pid, 2, $vendor_id)
!ll&&!rl&&r
103220791
MC/DC100%2/2
$basis == 0 and $tc->check_publisher_restriction($pid, 2, $vendor_id)
$basis == 0 $tc->check_publisher_restriction($pid, 2, $vendor_id)
Truth table100%3/3
A and B
A= $basis == 0B= $tc->check_publisher_restriction($pid, 2, $vendor_id)
ABresult
0X0
100
111
349 1 return GDPR::IAB::TCFv2::Validator::Failure->new(
350 code => ReasonPublisherRestrictionRequireLegitimateInterest,
351 message => "publisher restriction: purpose $pid requires legitimate interest (vendor $vendor_id)",
352 purpose_id => $pid,
353 vendor_id => $vendor_id,
354 restriction_type => RequireLegitimateInterest,
355 );
356 }
357  
358 3111 ▶ if ($basis == 1 && $tc->check_publisher_restriction($pid, RequireConsent, $vendor_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basis == 1 and $tc->check_publisher_restriction($pid, 1, $vendor_id)) 295 2816 3111
Condition100%3/3
$basis == 1 and $tc->check_publisher_restriction($pid, 1, $vendor_id)
!ll&&!rl&&r
2079737295
MC/DC100%2/2
$basis == 1 and $tc->check_publisher_restriction($pid, 1, $vendor_id)
$basis == 1 $tc->check_publisher_restriction($pid, 1, $vendor_id)
Truth table100%3/3
A and B
A= $basis == 1B= $tc->check_publisher_restriction($pid, 1, $vendor_id)
ABresult
0X0
100
111
359 295 return GDPR::IAB::TCFv2::Validator::Failure->new(
360 code => ReasonPublisherRestrictionRequireConsent,
361 message => "publisher restriction: purpose $pid requires consent (vendor $vendor_id)",
362 purpose_id => $pid,
363 vendor_id => $vendor_id,
364 restriction_type => RequireConsent,
365 );
366 }
367  
368 2816 return;
369 }
370  
371  
372 sub _make_result {
373 9281 ▶ my ($self, $ok, $failures) = @_;
Subroutine _make_result: called CC=1 SCAR=0.0
374  
375 9281 return GDPR::IAB::TCFv2::Validator::Result->new(ok => $ok, failures => $failures,);
376 }
377  
378 25 1;
379 __END__
380  
381 =encoding utf8
382  
383 =head1 NAME
384  
385 GDPR::IAB::TCFv2::Validator - declarative compliance checks for TC strings
386  
387 =head1 SYNOPSIS
388  
389 use GDPR::IAB::TCFv2::Validator;
390  
391 my $validator = GDPR::IAB::TCFv2::Validator->new(
392 vendor_id => 284,
393 consent_purpose_ids => [ 1, 3, 9 ],
394 legitimate_interest_purpose_ids => [ 10 ],
395 flexible_purpose_ids => [ 10 ],
396 verify_disclosed_vendors => 1,
397 min_tcf_policy_version => 5,
398 strict_legal_basis => 1,
399 );
400  
401 # Fail-fast: stops at the first failing rule.
402 my $tc_string = '...';
403 my $result = $validator->validate($tc_string);
404  
405 # Accumulate every failure for richer error reporting.
406 $result = $validator->validate_all($tc_string);
407  
408 if ($result) {
409 # All rules passed.
410 }
411 else {
412 warn "Compliance failed:\n$result\n"; # stringification = reasons
413 for my $reason ( $result->reasons ) {
414 warn "$reason";
415 }
416 }
417  
418 =head1 DESCRIPTION
419  
420 C<GDPR::IAB::TCFv2::Validator> is a small rule engine that turns a static
421 "compliance policy" — required purposes, expected vendor, optional
422 disclosed-vendors check — into a single C<validate> / C<validate_all>
423 call against a TC string (or a pre-parsed L<GDPR::IAB::TCFv2> object).
424  
425 Each rule produces a human-readable B<reason> on failure; reasons are
426 collected on a L<GDPR::IAB::TCFv2::Validator::Result> object that
427 overloads boolean and string contexts so it drops into typical
428 error-handling idioms (C<if (!$result)>, C<print "$result\n">) without
429 ceremony.
430  
431 =head1 CONSTRUCTOR
432  
433 =head2 new
434  
435 my $v = GDPR::IAB::TCFv2::Validator->new( %args );
436  
437 Recognized keys:
438  
439 =over 4
440  
441 =item *
442  
443 C<vendor_id> — the vendor whose access is being validated. Optional in
444 the constructor (can be supplied per call via C<< validate(..., vendor_id
445 => N) >>) but B<one of the two> must be set or C<validate>/C<validate_all>
446 will C<croak> with C<"missing vendor_id">.
447  
448 =item *
449  
450 C<consent_purpose_ids> — arrayref of purpose IDs that B<must> have
451 vendor consent. Validated via L<GDPR::IAB::TCFv2/is_vendor_consent_allowed>.
452  
453 =item *
454  
455 C<legitimate_interest_purpose_ids> — arrayref of purpose IDs that B<must>
456 have vendor legitimate-interest. Validated via
457 L<GDPR::IAB::TCFv2/is_vendor_legitimate_interest_allowed>. The IAB spec
458 forbids LI for Purpose 1 always, and for Purposes 3-6 in TCF v2.2+;
459 those are enforced by the underlying parser and surface here as failures.
460  
461 =item *
462  
463 C<flexible_purpose_ids> — arrayref of purpose IDs that are B<flexible> per
464 the vendor's GVL declaration (the basis can flip if a publisher restriction
465 is present in the TC string). The default basis is derived structurally
466 from the other two lists:
467  
468 =over 8
469  
470 =item *
471  
472 If the purpose ID also appears in C<consent_purpose_ids>, the default basis
473 is consent.
474  
475 =item *
476  
477 If the purpose ID also appears in C<legitimate_interest_purpose_ids>, the
478 default basis is legitimate interest.
479  
480 =back
481  
482 A purpose listed in C<flexible_purpose_ids> must also appear in exactly one
483 of the other two lists, or the constructor C<croak>s. Validated via
484 L<GDPR::IAB::TCFv2/is_vendor_allowed_for_flexible_purpose>.
485  
486 =item *
487  
488 C<verify_disclosed_vendors> — boolean. When true, the validator inspects
489 the TC string's Disclosed Vendors segment.
490  
491 If the segment is present, the vendor must appear there or the rule
492 fails with C<"vendor N not disclosed"> (ReasonVendorNotDisclosed).
493  
494 If the segment is B<absent>, the behavior depends on the
495 C<min_tcf_policy_version> floor:
496  
497 =over 8
498  
499 =item *
500  
501 When C<min_tcf_policy_version> is set to B<5 or higher> (TCF v2.3+), the
502 segment is mandatory; absence causes a failure
503 (ReasonMissingDisclosedVendors).
504  
505 =item *
506  
507 Otherwise (if C<min_tcf_policy_version> is below 5 or unset), absence is
508 B<silently ignored> (matches legacy behavior).
509  
510 =back
511  
512 =item *
513  
514 C<strict_legal_basis> — boolean. Passed through to the underlying
515 C<is_vendor_*_allowed> calls (as the C<strict> named argument) so
516 invalid purpose IDs cause C<croak> instead of a silent failure.
517 Defaults to C<0>.
518  
519 =back
520  
521 =head1 METHODS
522  
523 =head2 validate
524  
525 my $result = $validator->validate( $tc_string_or_object, %overrides );
526  
527 Runs the configured rules against C<$tc_string_or_object>. Stops at
528 the first failing rule (B<fail-fast> mode) and returns a
529 L<GDPR::IAB::TCFv2::Validator::Result> carrying that one reason.
530  
531 C<%overrides> can replace the constructor values for C<vendor_id>,
532 C<strict_legal_basis>, C<verify_disclosed_vendors>,
533 C<min_tcf_policy_version>, C<cmp_validator>,
534 C<consent_purpose_ids>, C<legitimate_interest_purpose_ids>, and
535 C<flexible_purpose_ids> for this call only.
536  
537 The list overrides (C<consent_purpose_ids>,
538 C<legitimate_interest_purpose_ids>, C<flexible_purpose_ids>) do B<not>
539 re-validate coherence — orphan entries (a flexible pid that isn't also
540 in one of the basis lists) are silently dropped at runtime rather than
541 fatal. This makes per-call overrides forgiving for callers that
542 generate their lists dynamically; the constructor remains strict for
543 the static policy.
544  
545 C<$tc_string_or_object> may be either a raw consent string or a
546 pre-parsed L<GDPR::IAB::TCFv2> object — handy when the same TC string
547 is being validated against multiple policies.
548  
549 =head2 validate_all
550  
551 Identical to L</validate> but runs B<every> rule and accumulates all
552 failures into the result. Use when you want a complete error report
553 rather than the first failure.
554  
555 =head1 SEE ALSO
556  
557 L<GDPR::IAB::TCFv2::Validator::Result> for the result-object API,
558 including the C<bool> / C<""> overloads and the C<$\>-aware
559 stringification.
560  
561 L<GDPR::IAB::TCFv2> for the underlying parser and the
562 C<is_vendor_*_allowed> family of methods this validator is built on.
563  
564 =cut
« bin/iabtcfv2 ↑ Summary blib/lib/GDPR/IAB/TCFv2/BitUtils.pm »