State.pm

filter: Statementstmt 88.6% 274 / 309 Branchbran 65.0% 82 / 126 Conditioncond 38.9% 65 / 167 MC/DCmcdc 17.7% 16 / 90 Subroutinesub 81.4% 22 / 27 Podpod 83.3% 10 / 12 totaltotal 64.1% 469 / 731 CC 128 SCAR 49.9
CC
128
Cov
70%
store_cookies
209.7
__ANON__
172.2
load_cookies
9.7
CRAP 146.7
↑ Summary State/Cookie.pm »
1 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package HTTP::State;
3  
4  
5 6 our $VERSION="v0.1.1";
6  
7 # Logging
8 #
9 6 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Log::OK;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Object system. Will use feature class asap
13 #
14 6 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use HTTP::State::Cookie ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19  
20 # Fast Binary Search subroutines
21 #
22 6 ▶use List::Insertion {type=>"string", duplicate=>"left", accessor=>"->[".COOKIE_KEY."]"};
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 # Public suffix list loaded when needed
26 #
27 #use Mozilla::PublicSuffix qw<public_suffix>;
28  
29 # Date
30 #use Time::Piece;
31 #my $tz_offset=Time::Piece->localtime->tzoffset->seconds;
32  
33 6 my $tz_offset=HTTP::State::Cookie::TZ_OFFSET;
34  
35 # Constant flags foor User agent context
36 #
37 6 ▶use constant::more FLAG_SAME_SITE=>0x01; # Indicate request is same-site/cross-site
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use constant::more FLAG_TYPE_HTTP=>0x02; # Indicate request is HTTP/non-HTTP
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use constant::more FLAG_SAFE_METH=>0x04; # Indicate request is safe method
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use constant::more FLAG_TOP_LEVEL=>0x04; # Indicate top level navigation
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43  
44 6 ▶use Export::These flags=>[qw<FLAG_SAME_SITE FLAG_TYPE_HTTP FLAG_SAFE_METH FLAG_TOP_LEVEL>];
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46  
47 class HTTP::State;
48  
49 6 ▶no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
50 0 ▶field $_get_cookies_sub :reader; # Main cookie retrieval routine, reference.
Subroutine get_cookies_sub: not called CC=1 SCAR=2.6
Pod undocumented
51 6 field @_cookies; # An array of cookie 'structs', sorted by the COOKIE_KEY field
52 6 field %_partitions;
53 6 field $_suffix_cache;# :param=undef; #Hash ref used as cache
54 field $_public_suffix_sub :param=undef; # Sub used for public suffix lookup.
55 6 field $_second_level_domain_sub;
56 #field $_default_same_site :param="None";
57  
58 field $_lax_allowing_unsafe :param=undef;
59 6 field $_lax_allowing_unsafe_timeout :param=120;
60 6 field $_retrieve_sort :param=undef;
61  
62 6 field $_max_expiry :param=400*24*3600;
63  
64 6 field $_default_flags :param=FLAG_SAME_SITE|FLAG_TYPE_HTTP|FLAG_SAFE_METH;
65  
66  
67 6 field %_sld_cache;
68  
69 BUILD{
70 # Create the main lookup sub
71 $self->_make_get_cookies;
72 unless($_public_suffix_sub){
73 require Mozilla::PublicSuffix;
74 $_public_suffix_sub=\&Mozilla::PublicSuffix::public_suffix;
75 }
76 $_suffix_cache//={};
77 $_second_level_domain_sub=sub {
78 my $domain=$_[0];
79 my $highest="";
80 my $suffix=$_suffix_cache->{$domain}//=&$_public_suffix_sub;
81  
82  
83 if($suffix){
84 substr($domain, -(length($suffix)+1))="";
85  
86 if($domain){
87 my @labels=split /\./, $domain;
88 $highest=pop(@labels).".$suffix";
89 }
90 }
91 $highest;
92  
93 };
94 }
95
96 6 sub _path_match {
97 7 ▶ my($path, $cookie)=@_;
Subroutine _path_match: called CC=5 SCAR=18.0
98  
99 # Process path matching as per section 5.1.4 in RFC 6265
100 #
101 7 ▶ $path||="/"; #TODO find a reference to a standard or rfc for this
Condition50%1/2
$path ||= "/"
l!l
70
MC/DC0%0/1
$path ||= "/"
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
102 7 Log::OK::TRACE and log_trace "PATH: $path";
103 7 Log::OK::TRACE and log_trace "Cookie PATH: $_->[COOKIE_PATH]";
104 7 my $path_ok;
105 7 ▶ if($path eq $cookie->[COOKIE_PATH]){
Branch: 3 branches
BranchTrueFalseTotal
if ($path eq $cookie->[6]) { } 0 7 7
elsif (substr($cookie->[6], -1, 1) eq '/') { } 0 7 7
elsif (substr($path, length $cookie->[6], 1) eq '/') { } 7 0 7
106 0 $path_ok=1;
107 }
108  
109 elsif (substr($cookie->[COOKIE_PATH], -1, 1) eq "/"){
110 # Cookie path ends in a slash?
111 0 $path_ok=index($path, $cookie->[COOKIE_PATH])==0 # Yes, check if cookie path is a prefix
112 }
113 elsif(substr($path,length($cookie->[COOKIE_PATH]), 1) eq "/"){
114 7 $path_ok= 0==index $path, $cookie->[COOKIE_PATH];
115 }
116 else {
117 # Not a path match
118 0 $path_ok=undef;
119 }
120 7 Log::OK::TRACE and log_trace "Path ok: $path_ok";
121 7 $path_ok;
122 }
123  
124 #returns self for chaining
125  
126  
127 method store_cookies{
128 34 ▶ my ($request_uri, $partition_key, $flags, @cookies)=@_;
Subroutine store_cookies: called CC=79 SCAR=53.5
Pod documented
129 #TODO: fix this
130 34 ▶ $flags//=$_default_flags;
Condition66%2/3
$flags //= $_default_flags
l!l&&r!l&&!r
3040
MC/DC0%0/2
$flags //= $_default_flags
$flags $_default_flags
Truth table66%2/3
A //= B
A= $flagsB= $_default_flags
ABresult
1X1
011
000
131 34 Log::OK::TRACE and log_trace __PACKAGE__. " store_cookies";
132 34 Log::OK::TRACE and log_trace __PACKAGE__. " ".join ", ", caller;
133 34 Log::OK::TRACE and log_trace __PACKAGE__. " $request_uri, $flags, @cookies";
134  
135 34 ▶ return $self unless @cookies;
Branch: 1 branch
BranchTrueFalseTotal
unless @cookies 0 34 34
136 # Parse the request_uri
137 #
138 #
139
140 34 my $index;
141 my $host;
142 34 my $path;
143 34 my $authority;
144 34 my $scheme;
145  
146 34 $index=index $request_uri, "://";
147 34 $scheme=substr $request_uri, 0, $index, "";
148 34 substr($request_uri,0, 3)="";
149
150 34 $index=index $request_uri, "/", $index;
151 34 ▶ if($index>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($index > 0) { } 33 1 34
152 #have path
153 33 $authority=substr $request_uri, 0, $index, "";
154 33 $path=$request_uri;
155 }
156 else {
157 #no path
158 1 $authority=$request_uri;
159 1 $path="";
160 }
161  
162 # Parse out authority if username/password is provided
163 34 $index=index($authority, "@");
164 34 ▶ $authority= $index>0
Branch: 1 branch
BranchTrueFalseTotal
$index > 0 ? : 0 34 34
165 ?substr $authority, $index+1
166 :$authority;
167  
168 # Find the host
169 34 $index=index $authority, ":";
170 34 ▶ $host=$index>0
Branch: 1 branch
BranchTrueFalseTotal
$index > 0 ? : 0 34 34
171 ? substr $authority, 0, $index
172 : $authority;
173  
174 34 ▶ die "URI format error" unless $scheme and $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme and $host 0 34 34
Condition33%1/3
$scheme and $host
!ll&&!rl&&r
0034
MC/DC0%0/2
$scheme and $host
$scheme $host
Truth table33%1/3
A and B
A= $schemeB= $host
ABresult
0X0
100
111
175  
176  
177  
178 34 my $time=time-$tz_offset; #Cache time. Translate to GMT
179  
180 # Iterate over the cookies supplied
181 SET_COOKIE_LOOP:
182 34 for my $c_ (@cookies){
183 # Parse or copy the input
184 34 my $c;
185 34 my $ref=ref $c_;
186 34 ▶ if($ref eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 21 13 34
187 # Assume a struct
188 21 $c=[@$c_]; #Copy
189 }
190 else {
191 # Assume a string
192 13 $c=decode_set_cookie($c_);
193 }
194 34 ▶ next unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 34 34
195  
196  
197  
198  
199 #1.
200 # A user agent MAY ignore a received cookie in its entirety. See Section 5.3.
201  
202 #2.
203 # If cookie-name is empty and cookie-value is empty, abort these steps
204 # and ignore the cookie entirely.
205
206 #3.
207 # If the cookie-name or the cookie-value contains a %x00-08 / %x0A-1F /
208 # %x7F character (CTL characters excluding HTAB), abort these steps and
209 # ignore the cookie entirely.
210  
211  
212 #4. If the sum of the lengths of cookie-name and cookie-value is more than
213 #4096 octets, abort these steps and ignore the cookie entirely
214 34 ▶ next if (length($c->[COOKIE_NAME])+ length($c->[COOKIE_VALUE]))>4096;
Branch: 1 branch
BranchTrueFalseTotal
if length($c->[1]) + length($c->[2]) > 4096 0 34 34
215 34 Log::OK::TRACE and log_trace __PACKAGE__. " Step 1, 2, 3, 4 OK";
216  
217 # 5. Create a new cookie with name cookie-name, value cookie-value. Set the
218 #creation-time and the last-access-time to the current date and time.
219 34 $c->[COOKIE_LAST_ACCESS_TIME]=$c->[COOKIE_CREATION_TIME]=$time;
220 34 Log::OK::TRACE and log_trace __PACKAGE__. " Step 5 OK";
221  
222 # 6.
223 # If the cookie-attribute-list contains an attribute with an attribute-name
224 # of "Max-Age":
225 # 1.
226 # Set the cookie's persistent-flag to true
227 #
228 # 2.
229 # Set the cookie's expiry-time
230 # to attribute-value of the last attribute in the cookie-attribute-list
231 # with an attribute-name of "Max-Age".
232 #
233 # Otherwise, if the cookie-attribute-list contains an attribute with an
234 # attribute-name of "Expires" (and does not contain an attribute with an
235 # attribute-name of "Max-Age"):
236 # 1.
237 # Set the cookie's persistent-flag to true.
238 #
239 # 2.
240 # Set the cookie's expiry-time to attribute-value of the last attribute
241 # in the cookie-attribute-list with an attribute-name of "Expires".
242 #
243 #Otherwise:
244 # 1.
245 # Set the cookie's persistent-flag to false.
246 #
247 # 2.
248 # Set the cookie's expiry-time to the latest representable date.
249  
250 34 ▶ if(defined $c->[COOKIE_MAX_AGE]){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $c->[4]) { } 4 30 34
elsif (defined $c->[3]) { } 6 24 30
251 4 $c->[COOKIE_PERSISTENT]=1;
252 4 $c->[COOKIE_EXPIRES]=$time+$c->[COOKIE_MAX_AGE];
253 4 Log::OK::TRACE and log_trace "max age set: $c->[COOKIE_MAX_AGE]";
254 }
255 elsif(defined $c->[COOKIE_EXPIRES]){
256 6 $c->[COOKIE_PERSISTENT]=1;
257 # expires already in required format
258 }
259 else{
260 24 $c->[COOKIE_PERSISTENT]=undef;
261 24 $c->[COOKIE_EXPIRES]=$time+$_max_expiry;
262 #400*24*3600; #Mimic chrome for maximum date
263  
264 }
265  
266 34 Log::OK::TRACE and log_trace "Expiry set to: $c->[COOKIE_EXPIRES]";
267 34 Log::OK::TRACE and log_trace __PACKAGE__. " Step 6 OK";
268  
269 #7.
270 #If the cookie-attribute-list contains an attribute with an attribute-name of "Domain":
271 #1.
272 #Let the domain-attribute be the attribute-value of the last attribute in
273 #the cookie-attribute-list with both an attribute-name of "Domain" and an
274 #attribute-value whose length is no more than 1024 octets. (Note that a
275 #leading %x2E ("."), if present, is ignored even though that character is
276 #not permitted.)
277 #
278 #Otherwise:
279 #1.
280 #Let the domain-attribute be the empty string.
281  
282 #8.
283 #If the domain-attribute contains a character that is not in the range of
284 #[USASCII] characters, abort these steps and ignore the cookie entirely.
285 #
286  
287 #9.
288 #If the user agent is configured to reject "public suffixes" and the
289 #domain-attribute is a public suffix:
290 #1.
291 #If the domain-attribute is identical to the canonicalized request-host:
292 #1.
293 #Let the domain-attribute be the empty string.
294 #Otherwise:
295 #1.
296 #Abort these steps and ignore the cookie entirely.
297  
298 #NOTE: This step prevents attacker.example from disrupting the integrity
299 #of site.example by setting a cookie with a Domain attribute of
300 #"example".
301  
302  
303  
304  
305  
306  
307  
308  
309 # Use the host as domain if none specified
310  
311 # Process the domain of the cookie. set to default if no explicitly set
312 #
313 34 my $rhost=scalar reverse $host;
314 34 my $sld;
315 my $suffix;
316 # DO a public suffix check on cookies. Need to ensure the domain for the cookie is NOT a suffix.
317 # This means we want a 'second level domain'
318 #
319 34 ▶ if($c->[COOKIE_DOMAIN]){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[5]) 18 16 34
320 18 ▶ $suffix=$_suffix_cache->{$c->[COOKIE_DOMAIN]}//=scalar reverse
Condition100%2/2
$$_suffix_cache{$c->[5]} //= scalar reverse(&$_public_suffix_sub(scalar reverse($c->[5])))
l!l
711
MC/DC100%1/1
$$_suffix_cache{$c->[5]} //= scalar reverse(&$_public_suffix_sub(scalar reverse($c->[5])))
$$_suffix_cache{$c->[5]}
Truth table100%2/2
A
A= $$_suffix_cache{$c->[5]}
Aresult
11
00
321 $_public_suffix_sub->(scalar reverse $c->[COOKIE_DOMAIN]);
322  
323 18 Log::OK::TRACE and log_trace "Looking up $c->[COOKIE_DOMAIN]=>$suffix";
324 18 ▶ if($suffix and $suffix eq $c->[COOKIE_DOMAIN]){
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix and $suffix eq $c->[5]) 1 17 18
Condition66%2/3
$suffix and $suffix eq $c->[5]
!ll&&!rl&&r
0171
MC/DC50%1/2
$suffix and $suffix eq $c->[5]
$suffix $suffix eq $c->[5]
Truth table66%2/3
A and B
A= $suffixB= $suffix eq $c->[5]
ABresult
0X0
100
111
325 1 ▶ if($rhost eq $c->[COOKIE_DOMAIN]){
Branch: 1 branch
BranchTrueFalseTotal
if ($rhost eq $c->[5]) { } 0 1 1
326  
327 0 Log::OK::TRACE and log_trace " Domain is equal to host, which is a suffix";
328 0 $c->[COOKIE_DOMAIN]="";
329 }
330 else {
331 1 Log::OK::TRACE and log_trace "Domain is public suffix. reject";
332 1 next;
333 }
334 }
335 }
336 33 Log::OK::TRACE and log_trace __PACKAGE__. " Step 7, 8, 9 OK";
337  
338 #10
339 #If the domain-attribute is non-empty:
340 #If the canonicalized request-host does not domain-match the domain-attribute:
341 #1.
342 #Abort these steps and ignore the cookie entirely.
343 #Otherwise:
344 #1
345 #Set the cookie's host-only-flag to false.
346 #2
347 #Set the cookie's domain to the domain-attribute.
348 #Otherwise:
349 #1
350 #Set the cookie's host-only-flag to true.
351 #2
352 #Set the cookie's domain to the canonicalized request-host.
353  
354  
355 33 ▶ if($c->[COOKIE_DOMAIN]){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[5]) { } 17 16 33
356 17 ▶ if(0==index($rhost, $c->[COOKIE_DOMAIN])){
Branch: 1 branch
BranchTrueFalseTotal
if (0 == index($rhost, $c->[5])) { } 14 3 17
357 # Domain must be at least substring (parent domain).
358 14 $c->[COOKIE_HOSTONLY]=0;
359 }
360 else{
361 # Reject. no domain match
362 3 Log::OK::TRACE and log_trace __PACKAGE__."::store_cookie domain invalid";
363 3 next;
364 }
365 }
366 else{
367 16 Log::OK::TRACE and log_trace __PACKAGE__. " No domain set for cookie";
368 16 $c->[COOKIE_HOSTONLY]=1;
369 16 $c->[COOKIE_DOMAIN]=$rhost;
370 }
371  
372 30 Log::OK::TRACE and log_trace __PACKAGE__. " Step 10 OK";
373  
374 #11.
375 #If the cookie-attribute-list contains an attribute with an attribute-name
376 #of "Path", set the cookie's path to attribute-value of the last attribute
377 #in the cookie-attribute-list with both an attribute-name of "Path" and an
378 #attribute-value whose length is no more than 1024 octets. Otherwise, set
379 #the cookie's path to the default-path of the request-uri.
380  
381 30 ▶ $c->[COOKIE_PATH]//="";
Condition50%1/2
$c->[6] //= ""
l!l
030
MC/DC0%0/1
$c->[6] //= ""
$c->[6]
Truth table50%1/2
A
A= $c->[6]
Aresult
11
00
382 30 ▶ next if length($c->[COOKIE_PATH])>1024;
Branch: 1 branch
BranchTrueFalseTotal
if length $c->[6] > 1024 0 30 30
383  
384 30 ▶ if( length($c->[COOKIE_PATH])==0 or substr($c->[COOKIE_PATH], 0, 1) ne "/"){
Branch: 1 branch
BranchTrueFalseTotal
if (length $c->[6] == 0 or substr($c->[6], 0, 1) ne "/") 30 0 30
Condition33%1/3
length $c->[6] == 0 or substr($c->[6], 0, 1) ne "/"
l!l&&r!l&&!r
3000
MC/DC0%0/2
length $c->[6] == 0 or substr($c->[6], 0, 1) ne "/"
length $c->[6] == 0 substr($c->[6], 0, 1) ne "/"
Truth table33%1/3
A or B
A= length $c->[6] == 0B= substr($c->[6], 0, 1) ne "/"
ABresult
1X1
011
000
385 # Calculate default
386 30 ▶ if(length($path)==0 or substr($path, 0, 1 ) ne "/"){
Branch: 1 branch
BranchTrueFalseTotal
if (length $path == 0 or substr($path, 0, 1) ne "/") 1 29 30
Condition66%2/3
length $path == 0 or substr($path, 0, 1) ne "/"
l!l&&r!l&&!r
1029
MC/DC50%1/2
length $path == 0 or substr($path, 0, 1) ne "/"
length $path == 0 substr($path, 0, 1) ne "/"
Truth table66%2/3
A or B
A= length $path == 0B= substr($path, 0, 1) ne "/"
ABresult
1X1
011
000
387 1 $path="/";
388 }
389
390 # Remove right / if present
391 30 ▶ if(length($path) >1){
Branch: 1 branch
BranchTrueFalseTotal
if (length $path > 1) { } 16 14 30
392 16 my @parts=split "/", $path;
393 16 pop @parts;
394 16 $c->[COOKIE_PATH]=join "/", @parts;
395 }
396 else {
397 14 $c->[COOKIE_PATH]=$path;
398 }
399 }
400 30 Log::OK::TRACE and log_trace __PACKAGE__. " Step 11 OK";
401 #12
402 #If the cookie-attribute-list contains an attribute with an attribute-name
403 #of "Secure", set the cookie's secure-only-flag to true. Otherwise, set the
404 #cookie's secure-only-flag to false.
405  
406 #13
407 #If the scheme component of the request-uri does not denote a "secure"
408 #protocol (as defined by the user agent), and the cookie's secure-only-flag
409 #is true, then abort these steps and ignore the cookie entirely.
410  
411  
412 30 ▶ next if $c->[COOKIE_SECURE] and ($scheme ne "https");
Branch: 1 branch
BranchTrueFalseTotal
if $c->[7] and $scheme ne "https" 1 29 30
Condition100%3/3
$c->[7] and $scheme ne "https"
!ll&&!rl&&r
9201
MC/DC100%2/2
$c->[7] and $scheme ne "https"
$c->[7] $scheme ne "https"
Truth table100%3/3
A and B
A= $c->[7]B= $scheme ne "https"
ABresult
0X0
100
111
413 29 Log::OK::TRACE and log_trace __PACKAGE__. " Step 12, 13 OK";
414  
415 #14
416 #If the cookie-attribute-list contains an attribute with an attribute-name
417 #of "HttpOnly", set the cookie's http-only-flag to true. Otherwise, set the
418 #cookie's http-only-flag to false.
419  
420 #15
421 #If the cookie was received from a "non-HTTP" API and the cookie's
422 #http-only-flag is true, abort these steps and ignore the cookie entirely.
423  
424 29 ▶ next if ($c->[COOKIE_HTTPONLY] and !($flags & FLAG_TYPE_HTTP));
Branch: 1 branch
BranchTrueFalseTotal
if $c->[8] and not $flags & 2 0 29 29
Condition33%1/3
$c->[8] and not $flags & 2
!ll&&!rl&&r
2900
MC/DC0%0/2
$c->[8] and not $flags & 2
$c->[8] not $flags & 2
Truth table33%1/3
A and not(B)
A= $c->[8]B= not $flags & 2
ABresult
0X0
100
111
425  
426  
427 29 Log::OK::TRACE and log_trace __PACKAGE__. " Step 14, 15 OK";
428  
429 #16
430 #If the cookie's secure-only-flag is false, and the scheme component of
431 #request-uri does not denote a "secure" protocol, then abort these steps
432 #and ignore the cookie entirely if the cookie store contains one or more
433 #cookies that meet all of the following criteria:
434 #
435 #1
436 #Their name matches the name of the newly-created cookie.
437 #2
438 #Their secure-only-flag is true.
439 #3
440 #Their domain domain-matches the domain of the newly-created cookie, or vice-versa.
441 #4
442 #The path of the newly-created cookie path-matches the path of the existing cookie.
443 #
444 #Note: The path comparison is not symmetric, ensuring only that a
445 #newly-created, non-secure cookie does not overlay an existing secure
446 #cookie, providing some mitigation against cookie-fixing attacks. That is,
447 #given an existing secure cookie named 'a' with a path of '/login', a
448 #non-secure cookie named 'a' could be set for a path of '/' or '/foo', but
449 #not for a path of '/login' or '/login/en'.
450  
451  
452 29 my $part;
453 29 ▶ if(!$c->[COOKIE_SECURE] and $scheme ne "https"){
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->[7] and $scheme ne "https") 9 20 29
Condition66%2/3
not $c->[7] and $scheme ne "https"
!ll&&!rl&&r
2009
MC/DC50%1/2
not $c->[7] and $scheme ne "https"
not $c->[7] $scheme ne "https"
Truth table66%2/3
not(A) and B
A= not $c->[7]B= $scheme ne "https"
ABresult
0X0
100
111
454
455 # get the second level domain to act as base to start search
456 9 ▶ $sld//=$_sld_cache{$c->[COOKIE_DOMAIN]}//=scalar reverse $_second_level_domain_sub->(scalar reverse $c->[COOKIE_DOMAIN]);
Condition60%3/5
$sld //= ($_sld_cache{$c->[5]} //= scalar reverse(&$_second_level_domain_sub(scalar reverse($c->[5]))))
l!l&&r!l&&!r
090
$_sld_cache{$c->[5]} //= scalar reverse(&$_second_level_domain_sub(scalar reverse($c->[5])))
l!l
63
MC/DC0%0/2
$sld //= ($_sld_cache{$c->[5]} //= scalar reverse(&$_second_level_domain_sub(scalar reverse($c->[5]))))
$sld ($_sld_cache{$c->[5]} //= scalar reverse(&$_second_level_domain_sub(scalar reverse($c->[5]))))
MC/DC100%1/1
$_sld_cache{$c->[5]} //= scalar reverse(&$_second_level_domain_sub(scalar reverse($c->[5])))
$_sld_cache{$c->[5]}
Truth table33%1/3
A //= B
A= $sldB= ($_sld_cache{$c->[5]} //= scalar reverse(&$_second_level_domain_sub(scalar reverse($c->[5]))))
ABresult
1X1
011
000
Truth table100%2/2
A
A= $_sld_cache{$c->[5]}
Aresult
11
00
457 9 ▶ next unless defined $sld;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sld 0 9 9
458  
459  
460 # IF partitions are enabled and the cookie is partitioned then lookup partition
461 # otherwise use normal cookies array
462 #
463 9 ▶ my @parts=(($partition_key and $c->[COOKIE_PARTITIONED])?$_partitions{$partition_key}//=[]: \@_cookies);
Branch: 1 branch
BranchTrueFalseTotal
$partition_key && $c->[10] ? : 3 6 9
Condition60%3/5
$partition_key and $c->[10]
!ll&&!rl&&r
603
$_partitions{$partition_key} //= []
l!l
03
MC/DC50%1/2
$partition_key and $c->[10]
$partition_key $c->[10]
MC/DC0%0/1
$_partitions{$partition_key} //= []
$_partitions{$partition_key}
Truth table66%2/3
A and B
A= $partition_keyB= $c->[10]
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_partitions{$partition_key}
Aresult
11
00
464 9 for my $part (@parts){
465 9 my $index=search_string_left $sld, $part;
466  
467 9 ▶ $index=@$part if $index<@$part and (index($part->[$index][COOKIE_KEY], $sld)==0);
Branch: 1 branch
BranchTrueFalseTotal
if $index < @$part and index($part->[$index][15], $sld) == 0 3 6 9
Condition66%2/3
$index < @$part and index($part->[$index][15], $sld) == 0
!ll&&!rl&&r
603
MC/DC50%1/2
$index < @$part and index($part->[$index][15], $sld) == 0
$index < @$part index($part->[$index][15], $sld) == 0
Truth table66%2/3
A and B
A= $index < @$partB= index($part->[$index][15], $sld) == 0
ABresult
0X0
100
111
468 9 my $found;
469 9 local $_;
470 9 ▶ while(!$found and $index<@$part){
Branch: 1 branch
BranchTrueFalseTotal
if (not $found and $index < @$part) 0 9 9
Condition33%1/3
not $found and $index < @$part
!ll&&!rl&&r
090
MC/DC0%0/2
not $found and $index < @$part
not $found $index < @$part
Truth table33%1/3
not(A) and B
A= not $foundB= $index < @$part
ABresult
0X0
100
111
471 0 $_=$part->[$index];
472 #exit the inner loop if the SLD is not a prefix of the current cookie key
473 0 ▶ last if index $_->[COOKIE_KEY], $sld;
Branch: 1 branch
BranchTrueFalseTotal
if index $_->[15], $sld 0 0 0
474  
475 0 ▶ next SET_COOKIE_LOOP if $_->[COOKIE_SECURE]
Branch: 1 branch
BranchTrueFalseTotal
if $_->[7] and $_->[1] eq $c->[1] and index($_->[5], $sld) == 0 || index($sld, $_->[5]) == 0 and _path_match($c->[6], $_) 0 0 0
Condition0%0/12
$_->[7] and $_->[1] eq $c->[1] and index($_->[5], $sld) == 0 || index($sld, $_->[5]) == 0 and _path_match($c->[6], $_)
!ll&&!rl&&r
000
$_->[7] and $_->[1] eq $c->[1] and index($_->[5], $sld) == 0 || index($sld, $_->[5]) == 0
!ll&&!rl&&r
000
$_->[7] and $_->[1] eq $c->[1]
!ll&&!rl&&r
000
index($_->[5], $sld) == 0 or index($sld, $_->[5]) == 0
l!l&&r!l&&!r
000
MC/DC0%0/5
$_->[7] and $_->[1] eq $c->[1] and index($_->[5], $sld) == 0 || index($sld, $_->[5]) == 0 and _path_match($c->[6], $_)
$_->[7] $_->[1] eq $c->[1] index($_->[5], $sld) == 0 index($sld, $_->[5]) == 0 _path_match($c->[6], $_)
Truth table0%0/7
A and B and C or D and E
A= $_->[7]B= $_->[1] eq $c->[1]C= index($_->[5], $sld) == 0D= index($sld, $_->[5]) == 0E= _path_match($c->[6], $_)
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
476 and $_->[COOKIE_NAME] eq $c->[COOKIE_NAME] #name match
477 and (index($_->[COOKIE_DOMAIN], $sld)==0 or index($sld, $_->[COOKIE_DOMAIN])==0) # symmetric match
478 and _path_match $c->[COOKIE_PATH], $_; #path match
479  
480 0 $index++;
481 }
482 }
483 }
484 29 Log::OK::TRACE and log_trace __PACKAGE__. " Step 16 OK";
485  
486 #17
487 #If the cookie-attribute-list contains an attribute with an attribute-name
488 #of "SameSite", and an attribute-value of "Strict", "Lax", or "None", set
489 #the cookie's same-site-flag to the attribute-value of the last attribute
490 #in the cookie-attribute-list with an attribute-name of "SameSite".
491 #Otherwise, set the cookie's same-site-flag to "Default".
492  
493 29 ▶ $c->[COOKIE_SAMESITE]//=SAME_SITE_DEFAULT;#"Default";#$_default_same_site;
Condition100%2/2
$c->[9] //= 4
l!l
722
MC/DC100%1/1
$c->[9] //= 4
$c->[9]
Truth table100%2/2
A
A= $c->[9]
Aresult
11
00
494  
495 29 Log::OK::TRACE and log_trace __PACKAGE__. " Step 17 OK";
496  
497 #18
498 #If the cookie's same-site-flag is not "None":
499 #1
500 #If the cookie was received from a "non-HTTP" API, and the API was called
501 #from a navigable's active document whose "site for cookies" is not
502 #same-site with the top-level origin, then abort these steps and ignore
503 #the newly created cookie entirely.
504 #2
505 #If the cookie was received from a "same-site" request (as defined in
506 #Section 5.2), skip the remaining substeps and continue processing the
507 #cookie.
508 #3
509 #If the cookie was received from a request which is navigating a
510 #top-level traversable [HTML] (e.g. if the request's "reserved client" is
511 #either null or an environment whose "target browsing context"'s
512 #navigable is a top-level traversable), skip the remaining substeps and
513 #continue processing the cookie.
514 #
515 #Note: Top-level navigations can create a cookie with any SameSite value,
516 #even if the new cookie wouldn't have been sent along with the request
517 #had it already existed prior to the navigation.
518 #4
519 #Abort these steps and ignore the newly created cookie entirely.
520  
521 29 ▶ if($c->[COOKIE_SAMESITE] != SAME_SITE_NONE){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[9] != 3) 24 5 29
522 24 ▶ if (not $flags & FLAG_TYPE_HTTP and not $flags & FLAG_SAME_SITE){
Branch: 3 branches
BranchTrueFalseTotal
if (not $flags & 2 || $flags & 1) { } 0 24 24
elsif ($flags & 1) { } 24 0 24
elsif ($flags & 4) { } 0 0 0
Condition33%1/3
$flags & 2 || $flags & 1
l!l&&r!l&&!r
2400
MC/DC0%0/2
$flags & 2 || $flags & 1
$flags & 2 $flags & 1
Truth table33%1/3
A || B
A= $flags & 2B= $flags & 1
ABresult
1X1
011
000
523 0 next;
524 }
525 elsif($flags & FLAG_SAME_SITE){
526 # continue
527 }
528 elsif($flags & FLAG_TOP_LEVEL){
529 # continue
530 }
531 else {
532 0 next;
533 }
534 }
535  
536 29 Log::OK::TRACE and log_trace __PACKAGE__. " Step 18 OK";
537  
538 #19
539 #If the cookie's "same-site-flag" is "None", abort these steps and ignore
540 #the cookie entirely unless the cookie's secure-only-flag is true.
541 29 ▶ next if $c->[COOKIE_SAMESITE] == SAME_SITE_NONE and !$c->[COOKIE_SECURE];
Branch: 1 branch
BranchTrueFalseTotal
if $c->[9] == 3 and not $c->[7] 1 28 29
Condition100%3/3
$c->[9] == 3 and not $c->[7]
!ll&&!rl&&r
2441
MC/DC100%2/2
$c->[9] == 3 and not $c->[7]
$c->[9] == 3 not $c->[7]
Truth table100%3/3
A and not(B)
A= $c->[9] == 3B= not $c->[7]
ABresult
0X0
100
111
542  
543 28 Log::OK::TRACE and log_trace __PACKAGE__. " Step 19 OK";
544  
545 #20
546 #If the cookie-name begins with a case-insensitive match for the string
547 #"__Secure-", abort these steps and ignore the cookie entirely unless the
548 #cookie's secure-only-flag is true.
549 #
550 28 ▶ next if $c->[COOKIE_NAME]=~/^__Secure-/aai and !$c->[COOKIE_SECURE];
Branch: 1 branch
BranchTrueFalseTotal
if $c->[1] =~ /^__Secure-/aai and not $c->[7] 0 28 28
Condition33%1/3
$c->[1] =~ /^__Secure-/aai and not $c->[7]
!ll&&!rl&&r
2800
MC/DC0%0/2
$c->[1] =~ /^__Secure-/aai and not $c->[7]
$c->[1] =~ /^__Secure-/aai not $c->[7]
Truth table33%1/3
A and not(B)
A= $c->[1] =~ /^__Secure-/aaiB= not $c->[7]
ABresult
0X0
100
111
551  
552 28 Log::OK::TRACE and log_trace __PACKAGE__. " Step 20 OK";
553  
554 #21
555 #If the cookie-name begins with a case-insensitive match for the string
556 #"__Host-", abort these steps and ignore the cookie entirely unless the
557 #cookie meets all the following criteria:
558 #1
559 #The cookie's secure-only-flag is true.
560 #2
561 #The cookie's host-only-flag is true.
562 #3
563 #The cookie-attribute-list contains an attribute with an attribute-name
564 #of "Path", and the cookie's path is /.
565  
566 28 ▶ next if $c->[COOKIE_NAME]=~/^__Host-/aai and !($c->[COOKIE_SECURE] and
Branch: 1 branch
BranchTrueFalseTotal
if $c->[1] =~ /^__Host-/aai and not $c->[7] && $c->[6] eq "/" && $c->[14] 0 28 28
Condition11%1/9
$c->[1] =~ /^__Host-/aai and not $c->[7] && $c->[6] eq "/" && $c->[14]
!ll&&!rl&&r
2800
$c->[7] && $c->[6] eq '/' && $c->[14]
!ll&&!rl&&r
000
$c->[7] and $c->[6] eq "/"
!ll&&!rl&&r
000
MC/DC0%0/4
$c->[1] =~ /^__Host-/aai and not $c->[7] && $c->[6] eq "/" && $c->[14]
$c->[1] =~ /^__Host-/aai $c->[7] $c->[6] eq "/" $c->[14]
Truth table20%1/5
A and not(B and C && D)
A= $c->[1] =~ /^__Host-/aaiB= $c->[7]C= $c->[6] eq "/"D= $c->[14]
ABCDresult
0XXX0
11110
10XX1
110X1
11101
567 ($c->[COOKIE_PATH] eq "/") and $c->[COOKIE_HOSTONLY]);
568  
569 28 Log::OK::TRACE and log_trace __PACKAGE__. " Step 21 OK";
570  
571 #22
572 #If the cookie-name is empty and either of the following conditions are
573 #true, abort these steps and ignore the cookie:
574 #1
575 #the cookie-value begins with a case-insensitive match for the string
576 #"__Secure-"
577 #2
578 #the cookie-value begins with a case-insensitive match for the string
579 #"__Host-"
580 28 ▶ next if !$c->[COOKIE_NAME] and ($c->[COOKIE_VALUE]=~/^__Host-/i or $c->[COOKIE_VALUE]=~/^__Secure-/i);
Branch: 1 branch
BranchTrueFalseTotal
if not $c->[1] and $c->[2] =~ /^__Host-/iu || $c->[2] =~ /^__Secure-/iu 0 28 28
Condition16%1/6
not $c->[1] and $c->[2] =~ /^__Host-/iu || $c->[2] =~ /^__Secure-/iu
!ll&&!rl&&r
2800
$c->[2] =~ /^__Host-/iu or $c->[2] =~ /^__Secure-/iu
l!l&&r!l&&!r
000
MC/DC0%0/3
not $c->[1] and $c->[2] =~ /^__Host-/iu || $c->[2] =~ /^__Secure-/iu
not $c->[1] $c->[2] =~ /^__Host-/iu $c->[2] =~ /^__Secure-/iu
Truth table25%1/4
not(A) and B or C
A= not $c->[1]B= $c->[2] =~ /^__Host-/iuC= $c->[2] =~ /^__Secure-/iu
ABCresult
0XX0
1000
11X1
1011
581  
582 28 Log::OK::TRACE and log_trace __PACKAGE__. " Step 22 OK";
583
584 #23
585 #If the cookie store contains a cookie with the same name, domain,
586 #host-only-flag, and path as the newly-created cookie:
587 #1
588 #Let old-cookie be the existing cookie with the same name, domain,
589 #host-only-flag, and path as the newly-created cookie. (Notice that this
590 #algorithm maintains the invariant that there is at most one such
591 #cookie.)
592 #2
593 #If the newly-created cookie was received from a "non-HTTP" API and the
594 #old-cookie's http-only-flag is true, abort these steps and ignore the
595 #newly created cookie entirely.
596 #3
597 #Update the creation-time of the newly-created cookie to match the creation-time of the old-cookie.
598 #4
599 #Remove the old-cookie from the cookie store.
600  
601 #24
602 #Insert the newly-created cookie into the cookie store.
603 #A cookie is "expired" if the cookie has an expiry date in the past.
604  
605 #The user agent MUST evict all expired cookies from the cookie store if, at
606 #any time, an expired cookie exists in the cookie store.
607
608 #At any time, the user agent MAY "remove excess cookies" from the cookie
609 #store if the number of cookies sharing a domain field exceeds some
610 #implementation-defined upper bound (such as 50 cookies).
611  
612 #At any time, the user agent MAY "remove excess cookies" from the cookie
613 #store if the cookie store exceeds some predetermined upper bound (such as
614 #3000 cookies).
615  
616 #When the user agent removes excess cookies from the cookie store, the user
617 #agent MUST evict cookies in the following priority order:
618  
619 #1
620 #Expired cookies.
621 #2
622 #Cookies whose secure-only-flag is false, and which share a domain field
623 #with more than a predetermined number of other cookies.
624 #3
625 #Cookies that share a domain field with more than a predetermined number
626 #of other cookies.
627 #4
628 #All cookies.
629 #
630 #If two cookies have the same removal priority, the user agent MUST evict
631 #the cookie with the earliest last-access-time first.
632  
633 #When "the current session is over" (as defined by the user agent), the user
634 #agent MUST remove from the cookie store all cookies with the persistent-flag
635 #set to false.
636  
637  
638  
639  
640 # Build key to perform binary search in database. This key is unique in the database
641 #
642 28 $c->[COOKIE_KEY]="$c->[COOKIE_DOMAIN] $c->[COOKIE_PATH] $c->[COOKIE_NAME] $c->[COOKIE_HOSTONLY]";
643 28 $c->[COOKIE_MAX_AGE]=undef; # No longer need this, so
644 28 Log::OK::TRACE and log_trace __PACKAGE__."::store_cookie key: $c->[COOKIE_KEY]";
645  
646 # Locate the parition
647  
648 28 ▶ my @parts=(($partition_key and $c->[COOKIE_PARTITIONED])?$_partitions{$partition_key}//=[]: \@_cookies);
Branch: 1 branch
BranchTrueFalseTotal
$partition_key && $c->[10] ? : 3 25 28
Condition60%3/5
$partition_key and $c->[10]
!ll&&!rl&&r
2503
$_partitions{$partition_key} //= []
l!l
30
MC/DC50%1/2
$partition_key and $c->[10]
$partition_key $c->[10]
MC/DC0%0/1
$_partitions{$partition_key} //= []
$_partitions{$partition_key}
Truth table66%2/3
A and B
A= $partition_keyB= $c->[10]
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_partitions{$partition_key}
Aresult
11
00
649  
650 28 for my $part (@parts){
651 # Lookup in database
652 #Index of left side insertion
653 28 my $index=search_string_left $c->[COOKIE_KEY], $part;
654 #say "insertion index: ", $index;
655 #say "Looking for cookie key: ".$c->[COOKIE_KEY];
656 #say " against cookie key: ".$part->[$index][COOKIE_KEY];
657  
658 #Test if actually found or just insertion point
659 28 ▶ my $found=($index<@$part and ($part->[$index][COOKIE_KEY] eq $c->[COOKIE_KEY]));
Condition100%3/3
$index < @$part && $part->[$index][15] eq $c->[15]
!ll&&!rl&&r
1927
MC/DC100%2/2
$index < @$part && $part->[$index][15] eq $c->[15]
$index < @$part $part->[$index][15] eq $c->[15]
Truth table100%3/3
A && B
A= $index < @$partB= $part->[$index][15] eq $c->[15]
ABresult
0X0
100
111
660  
661 28 ▶ if($found){
Branch: 2 branches
BranchTrueFalseTotal
if ($found) { } 7 21 28
elsif ($c->[3] < $time) { } 0 21 21
662 #reject if api call http only cookie currently exists
663 7 ▶ next if $part->[$index][COOKIE_HTTPONLY] and !($flags & FLAG_TYPE_HTTP);
Branch: 1 branch
BranchTrueFalseTotal
if $part->[$index][8] and not $flags & 2 0 7 7
Condition33%1/3
$part->[$index][8] and not $flags & 2
!ll&&!rl&&r
700
MC/DC0%0/2
$part->[$index][8] and not $flags & 2
$part->[$index][8] not $flags & 2
Truth table33%1/3
A and not(B)
A= $part->[$index][8]B= not $flags & 2
ABresult
0X0
100
111
664 7 $c->[COOKIE_CREATION_TIME]=$part->[$index][COOKIE_CREATION_TIME];
665 7 ▶ if($c->[COOKIE_EXPIRES]<=$time){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[3] <= $time) { } 1 6 7
666 # Found but expired by new cookie. Delete the cookie
667 1 Log::OK::TRACE and log_trace __PACKAGE__. " found cookie and expired. purging";
668 1 splice @$part , $index, 1;
669 }
670 else {
671 # replace existing cookie
672 6 Log::OK::TRACE and log_trace __PACKAGE__. " found cookie. Updating";
673 6 $part->[$index]=$c;
674 }
675 }
676  
677 elsif($c->[COOKIE_EXPIRES]<$time){
678 0 Log::OK::TRACE and log_trace __PACKAGE__. " new cookie already expired. rejecting";
679 0 next; # new cookie already expired.
680 }
681 else {
682 # insert new cookie
683 21 Log::OK::TRACE and log_trace __PACKAGE__. " new cookie name. adding";
684 21 ▶ unless(@$part ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$part) { } 8 13 21
685 13 push @$part , $c;
686 }
687 else{
688 #Log::OK::TRACE and log_trace __PACKAGE__. " new cookie name. adding";
689 8 splice @$part , $index, 0, $c;
690 }
691 }
692 }
693 28 Log::OK::TRACE and log_trace __PACKAGE__. " Step 23, 24 OK";
694 }
695 34 return $self;
696 6 }
697  
698  
699 method _make_get_cookies{
700 $_get_cookies_sub=sub {
701 4 ▶ my ($request_uri, $partition_key, $flags)=@_;
Subroutine __ANON__: called CC=34 SCAR=51.5
702 4 ▶ $flags//=$_default_flags;
Condition33%1/3
$flags //= $_default_flags
l!l&&r!l&&!r
040
MC/DC0%0/2
$flags //= $_default_flags
$flags $_default_flags
Truth table33%1/3
A //= B
A= $flagsB= $_default_flags
ABresult
1X1
011
000
703  
704 4 my $index;
705 my $host;
706 4 my $path;
707 4 my $authority;
708 4 my $scheme;
709  
710 4 $index=index $request_uri, "://";
711 4 $scheme=substr $request_uri, 0, $index, "";
712 4 substr($request_uri,0, 3)="";
713
714 4 $index=index $request_uri, "/", $index;
715 4 ▶ if($index>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($index > 0) { } 4 0 4
716 #have path
717 4 $authority=substr $request_uri, 0, $index, "";
718 4 $path=$request_uri;
719 }
720 else {
721 #no path
722 0 $authority=$request_uri;
723 0 $path="";
724 }
725  
726 # Parse out authority if username/password is provided
727 4 $index=index($authority, "@");
728 4 ▶ $authority= $index>0
Branch: 1 branch
BranchTrueFalseTotal
$index > 0 ? : 0 4 4
729 ?substr $authority, $index+1
730 :$authority;
731  
732 # Find the host
733 4 $index=index $authority, ":";
734 4 ▶ $host=$index>0
Branch: 1 branch
BranchTrueFalseTotal
$index > 0 ? : 0 4 4
735 ? substr $authority, 0, $index
736 : $authority;
737  
738 4 ▶ die "URI format error" unless $scheme and $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme and $host 0 4 4
Condition33%1/3
$scheme and $host
!ll&&!rl&&r
004
MC/DC0%0/2
$scheme and $host
$scheme $host
Truth table33%1/3
A and B
A= $schemeB= $host
ABresult
0X0
100
111
739  
740  
741 #:($host, undef)=split ":", $authority, 2;
742  
743 # Look up the second level domain. This will be the root for our domain search
744 #
745 4 ▶ my $sld=$_sld_cache{$host}//=scalar reverse $_second_level_domain_sub->($host);
Condition100%2/2
$_sld_cache{$host} //= scalar reverse(&$_second_level_domain_sub($host))
l!l
31
MC/DC100%1/1
$_sld_cache{$host} //= scalar reverse(&$_second_level_domain_sub($host))
$_sld_cache{$host}
Truth table100%2/2
A
A= $_sld_cache{$host}
Aresult
11
00
746 4 my $rhost=scalar reverse $host;
747  
748  
749 # Iterate through all cookies until the domain no longer matches
750 #
751 4 my $time=time-$tz_offset;
752 4 my @output;
753  
754 # Search default cookies and also an existing parition. Don't create a new partition
755 4 ▶ my @parts=(\@_cookies, ($partition_key and $_partitions{$partition_key})||());
Condition66%4/6
$partition_key && $_partitions{$partition_key} || ()
l!l&&r!l&&!r
310
$partition_key and $_partitions{$partition_key}
!ll&&!rl&&r
103
MC/DC0%0/3
$partition_key && $_partitions{$partition_key} || ()
$partition_key $_partitions{$partition_key} ()
Truth table40%2/5
A and B || C
A= $partition_keyB= $_partitions{$partition_key}C= ()
ABCresult
11X1
0X11
1011
0X00
1000
756 #########################################
757 # #my $part= #
758 # $partition_key #
759 # ? $_partitions{$partition_key}//=[] #
760 # : () #
761 # ); #
762 #########################################
763 #use Data::Dumper;
764 #say Dumper \@_cookies;
765 4 for my $part (@parts){
766  
767 7 $index=search_string_left $sld, $part;
768  
769 7 Log::OK::TRACE and log_trace __PACKAGE__. " index is: $index";
770 7 Log::OK::TRACE and log_trace "looking for host: $sld";
771  
772 7 local $_;
773  
774 7 ▶ $index++ unless @$part; # Force skip the test loop if no cookies in the jar
Branch: 1 branch
BranchTrueFalseTotal
unless @$part 0 7 7
775  
776 7 ▶ while($index<@$part){
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$part) 7 7 14
777 7 $_=$part->[$index];
778  
779 # End the search when the $sld of request is no longer a prefix for the
780 # cookie domain being tested
781 7 ▶ last if index $_->[COOKIE_DOMAIN], $sld;
Branch: 1 branch
BranchTrueFalseTotal
if index $_->[5], $sld 0 7 7
782  
783 # Process expire. Do not update $index
784 7 ▶ if($_->[COOKIE_EXPIRES] <= $time){
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[3] <= $time) 0 7 7
785 0 Log::OK::TRACE and log_trace "cookie under test expired. removing";
786 0 splice @$part, $index, 1;
787 0 next;
788 }
789  
790  
791  
792  
793 ## At this point we have a domain match ##
794  
795 # Test for other restrictions...
796 7 ▶ $index++ and next if
Branch: 1 branch
BranchTrueFalseTotal
if not _path_match($path, $_) or $_->[14] and $rhost ne $_->[5] or $_->[7] and $scheme ne "https" or $_->[8] and not $flags & 2 0 7 7
Condition30%7/23
(!_path_match($path, $_) || $_->[14] && $rhost ne $_->[5] || $_->[7] && $scheme ne 'https' || $_->[8] && !($flags & 2)) && ($index++ && (next))
!ll&&!rl&&r
700
not _path_match($path, $_) or $_->[14] and $rhost ne $_->[5] or $_->[7] and $scheme ne "https" or $_->[8] and not $flags & 2
l!l&&r!l&&!r
007
not _path_match($path, $_) or $_->[14] and $rhost ne $_->[5] or $_->[7] and $scheme ne "https"
l!l&&r!l&&!r
007
not _path_match($path, $_) or $_->[14] and $rhost ne $_->[5]
l!l&&r!l&&!r
007
$_->[14] and $rhost ne $_->[5]
!ll&&!rl&&r
070
$_->[7] and $scheme ne "https"
!ll&&!rl&&r
700
$_->[8] and not $flags & 2
!ll&&!rl&&r
700
$index++ and next
!ll
00
MC/DC0%0/8
(!_path_match($path, $_) || $_->[14] && $rhost ne $_->[5] || $_->[7] && $scheme ne 'https' || $_->[8] && !($flags & 2)) && ($index++ && (next))
not _path_match($path, $_) $_->[14] $rhost ne $_->[5] $_->[7] $scheme ne "https" $_->[8] not $flags & 2 $index++
Truth table4%1/24
not(A) or B and C or D and E or F and not(G) && H
A= not _path_match($path, $_)B= $_->[14]C= $rhost ne $_->[5]D= $_->[7]E= $scheme ne "https"F= $_->[8]G= not $flags & 2H= $index++
ABCDEFGHresult
00X0X0XX0
00X0X10X0
00X100XX0
00X1010X0
0100X0XX0
0100X10X0
010100XX0
0101010X0
1XXXXXX00
011XXXX00
00X11XX00
01011XX00
00X0X1100
00X101100
0100X1100
010101100
1XXXXXX11
011XXXX11
00X11XX11
01011XX11
00X0X1111
00X101111
0100X1111
010101111
797 (!_path_match($path, $_))
798 or ($_->[COOKIE_HOSTONLY] and $rhost ne $_->[COOKIE_DOMAIN])
799 or ($_->[COOKIE_SECURE] and $scheme ne "https")
800 or ($_->[COOKIE_HTTPONLY] and not $flags & FLAG_TYPE_HTTP);
801  
802 7 ▶ if((not ($flags & FLAG_SAME_SITE)) and ($_->[COOKIE_SAMESITE] != SAME_SITE_NONE)){
Branch: 1 branch
BranchTrueFalseTotal
if (not $flags & 1 and $_->[9] != 3) 0 7 7
Condition33%1/3
not $flags & 1 and $_->[9] != 3
!ll&&!rl&&r
700
MC/DC0%0/2
not $flags & 1 and $_->[9] != 3
not $flags & 1 $_->[9] != 3
Truth table33%1/3
not(A) and B
A= not $flags & 1B= $_->[9] != 3
ABresult
0X0
100
111
803  
804  
805  
806
807 0 ▶ my $f=(($flags & FLAG_TYPE_HTTP)
Condition0%0/6
$flags & 2 && ($_->[9] == 1 || $_->[9] == 4)
!ll&&!rl&&r
000
$_->[9] == 1 or $_->[9] == 4
l!l&&r!l&&!r
000
MC/DC0%0/3
$flags & 2 && ($_->[9] == 1 || $_->[9] == 4)
$flags & 2 $_->[9] == 1 $_->[9] == 4
Truth table0%0/4
A && B or C
A= $flags & 2B= $_->[9] == 1C= $_->[9] == 4
ABCresult
0XX0
1000
11X1
1011
808 and (($_->[COOKIE_SAMESITE] == SAME_SITE_LAX)
809 or ($_->[COOKIE_SAMESITE] == SAME_SITE_DEFAULT)
810 )
811 );
812 0 ▶ $f&&=(($flags & FLAG_SAFE_METH) or (
Condition0%0/12
$f &&= $flags & 4 || $_lax_allowing_unsafe && $_->[9] == 4 && $time - $_->[11] < $_lax_allowing_unsafe_timeout
!ll&&!rl&&r
000
$flags & 4 || $_lax_allowing_unsafe && $_->[9] == 4 && $time - $_->[11] < $_lax_allowing_unsafe_timeout
l!l&&r!l&&!r
000
$_lax_allowing_unsafe and $_->[9] == 4 and $time - $_->[11] < $_lax_allowing_unsafe_timeout
!ll&&!rl&&r
000
$_lax_allowing_unsafe and $_->[9] == 4
!ll&&!rl&&r
000
MC/DC0%0/5
$f &&= $flags & 4 || $_lax_allowing_unsafe && $_->[9] == 4 && $time - $_->[11] < $_lax_allowing_unsafe_timeout
$f $flags & 4 $_lax_allowing_unsafe $_->[9] == 4 $time - $_->[11] < $_lax_allowing_unsafe_timeout
Truth table0%0/6
A &&= B || C and D and E
A= $fB= $flags & 4C= $_lax_allowing_unsafeD= $_->[9] == 4E= $time - $_->[11] < $_lax_allowing_unsafe_timeout
ABCDEresult
0XXXX0
100XX0
1010X0
101100
11XXX1
101111
813 $_lax_allowing_unsafe and $_->[COOKIE_SAMESITE] == SAME_SITE_DEFAULT
814 and ($time-$_->[COOKIE_CREATION_TIME]) < $_lax_allowing_unsafe_timeout
815 ));
816  
817 0 ▶ $f&&=($flags & FLAG_TOP_LEVEL);
Condition0%0/3
$f &&= $flags & 4
!ll&&!rl&&r
000
MC/DC0%0/2
$f &&= $flags & 4
$f $flags & 4
Truth table0%0/3
A &&= B
A= $fB= $flags & 4
ABresult
0X0
100
111
818 }
819  
820 #
821 # If we get here, cookie should be included!
822 #Update last access time
823 #
824 7 $_->[COOKIE_LAST_ACCESS_TIME]=$time;
825 7 Log::OK::TRACE and log_trace "Pushing cookie";
826 7 push @output, $_;
827 7 $index++;
828 }
829 }
830
831 # TODO:
832 # Sort the output as recommended by RFC 6525
833 # The user agent SHOULD sort the cookie-list in the following
834 # order:
835 #
836 # * Cookies with longer paths are listed before cookies with
837 # shorter paths.
838 #
839 # * Among cookies that have equal-length path fields, cookies with
840 # earlier creation-times are listed before cookies with later
841 # creation-times.
842  
843 4 ▶ if($_retrieve_sort ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_retrieve_sort) 0 4 4
844 @output= sort {
845 0 ▶ length($b->[COOKIE_PATH]) <=> length($a->[COOKIE_PATH])
Branch: 1 branch
BranchTrueFalseTotal
length $HTTP::State::b->[6] <=> length $HTTP::State::a->[6] or $HTTP::State::a->[11] <=> $HTTP::State::b->[11] 0 0 0
846 || $a->[COOKIE_CREATION_TIME] <=> $b->[COOKIE_CREATION_TIME]
847 } @output;
848 }
849 4 \@output;
850 11 ▶ };
Subroutine _make_get_cookies: called CC=1 SCAR=0.0
851 6 }
852  
853 method get_cookies{
854 # Do a copy of the matching entries
855 #
856 4 ▶ map [@$_], $_get_cookies_sub->&*->@*;
Subroutine get_cookies: called CC=1 SCAR=0.0
Pod documented
857 6 }
858  
859  
860 #TODO rename to retrieve_cookies?
861 method retrieve_cookies{
862 0 ▶ my $cookies=&$_get_cookies_sub;
Subroutine retrieve_cookies: not called CC=1 SCAR=6.9
Pod documented
863 0 join "; ", map "$_->[COOKIE_NAME]=$_->[COOKIE_VALUE]", @$cookies;
864 6 }
865 #*retrieve_cookies=\&encode_request_cookies;
866  
867 method get_kv_cookies{#
868 0 ▶ my $cookies=&$_get_cookies_sub;
Subroutine get_kv_cookies: not called CC=1 SCAR=6.9
Pod documented
869 0 map(($_->[COOKIE_NAME], $_->[COOKIE_VALUE]), @$cookies);
870 6 }
871  
872  
873 method db {
874 1 ▶ \@_cookies;
Subroutine db: called CC=1 SCAR=0.0
Pod undocumented
875 6 }
876  
877  
878  
879  
880  
881 # Compatibility matrix
882 # HTTP::CookieJar
883 # Additional api
884 # new
885 # create a new jar
886 # clear
887 # empty the jar
888 # dump_cookies
889 #
890 #
891 # Used by:
892 # HTTP::Tiny
893 # FURL
894 # Expected API
895 # $jar->add($url, $set_cookie_string)
896 # Parse set cookie string and add cookie to jar
897 #
898 # #jar->cookie_header($url)
899 # Retrieve cookies from jar and serialize for header
900 #
901 #
902 # Returns self for chaining
903 method clear{
904 7 ▶ @_cookies=(); #Clear the db
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
905 7 %_partitions=();
906 7 $self;
907 6 }
908 method add {
909 5 ▶ $self->store_cookies(shift, undef, $_default_flags, @_);
Subroutine add: called CC=1 SCAR=0.0
Pod documented
910 6 }
911  
912 method cookie_header {
913 0 ▶ splice @_, 1, 0, undef, $_default_flags;
Subroutine cookie_header: not called CC=1 SCAR=6.9
Pod documented
914 0 my $cookies=&$_get_cookies_sub;
915 6 }
916  
917 method dump_cookies {
918 19 ▶ my $all=$_[0]?!$_[0]{persistent}:1;
Subroutine dump_cookies: called CC=5 SCAR=16.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 19 19
919 19 ▶ my @out=map encode_set_cookie($_, $tz_offset) , grep $_->[COOKIE_PERSISTENT]||$all, @_cookies;
Condition66%2/3
$_->[13] || $all
l!l&&r!l&&!r
6180
MC/DC0%0/2
$_->[13] || $all
$_->[13] $all
Truth table66%2/3
A || B
A= $_->[13]B= $all
ABresult
1X1
011
000
920 19 for my ($k)(sort keys %_partitions){
921 9 my $v=$_partitions{$k};
922 9 ▶ push @out, map encode_set_cookie($_, $tz_offset, $k) , grep $_->[COOKIE_PERSISTENT]||$all, @$v;
Condition66%2/3
$_->[13] || $all
l!l&&r!l&&!r
630
MC/DC0%0/2
$_->[13] || $all
$_->[13] $all
Truth table66%2/3
A || B
A= $_->[13]B= $all
ABresult
1X1
011
000
923 }
924 19 @out;
925 6 }
926  
927 method cookies_for{
928 0 ▶ my $cookies=&$_get_cookies_sub;
Subroutine cookies_for: not called CC=1 SCAR=6.9
Pod documented
929 0 map hash_set_cookie($_,1), @$cookies;
930 6 }
931  
932 #TODO: add test for bulk adding of strings and structs
933 method load_cookies{
934 2 ▶ my $index;
Subroutine load_cookies: called CC=9 SCAR=22.7
Pod documented
935 2 my $time=time-$tz_offset;
936 2 my $c;
937 2 for my $s (@_){
938  
939 8 Log::OK::TRACE and log_trace "+++";
940 8 Log::OK::TRACE and log_trace "loading cookie from string";
941 8 Log::OK::TRACE and log_trace $s;
942 8 ▶ next unless $c=decode_set_cookie($s, $tz_offset);
Branch: 1 branch
BranchTrueFalseTotal
unless $c = decode_set_cookie($s, $tz_offset) 0 8 8
943 # Don't load if cookie is expired
944 #
945 8 ▶ next if $c->[COOKIE_EXPIRES]<=$time;
Branch: 1 branch
BranchTrueFalseTotal
if $c->[3] <= $time 0 8 8
946  
947 # Build key for search
948 8 $c->[COOKIE_KEY]="$c->[COOKIE_DOMAIN] $c->[COOKIE_PATH] $c->[COOKIE_NAME] $c->[COOKIE_HOSTONLY]";
949  
950  
951 # Adjust the partitioned flag
952 8 my $partition_key=$c->[COOKIE_PARTITIONED];
953  
954 8 my $part=\@_cookies; #default is the unparitioned jar
955  
956 8 ▶ if($partition_key){
Branch: 1 branch
BranchTrueFalseTotal
if ($partition_key) 3 5 8
957 3 $c->[COOKIE_PARTITIONED]=1;
958 3 ▶ $part=$_partitions{$partition_key}//=[];
Condition50%1/2
$_partitions{$partition_key} //= []
l!l
03
MC/DC0%0/1
$_partitions{$partition_key} //= []
$_partitions{$partition_key}
Truth table50%1/2
A
A= $_partitions{$partition_key}
Aresult
11
00
959 }
960  
961 # update the list
962 8 ▶ unless(@$part){
Branch: 1 branch
BranchTrueFalseTotal
if (@$part) { } 3 5 8
963 5 Log::OK::TRACE and log_trace "Pushing cookie in to empty jar/parition";
964 5 push @$part, $c;
965 }
966 else{
967 # Do binary search
968 #
969 3 $index=search_string_left $c->[COOKIE_KEY], $part;#\@_cookies;
970 # If the key is identical, then we prefer the latest cookie,
971 # TODO: Fix key with scheme?
972 3 ▶ if($index<@$part and ($part->[$index][COOKIE_KEY] eq $c->[COOKIE_KEY])){
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$part and $part->[$index][15] eq $c->[15]) { } 0 3 3
Condition33%1/3
$index < @$part and $part->[$index][15] eq $c->[15]
!ll&&!rl&&r
300
MC/DC0%0/2
$index < @$part and $part->[$index][15] eq $c->[15]
$index < @$part $part->[$index][15] eq $c->[15]
Truth table33%1/3
A and B
A= $index < @$partB= $part->[$index][15] eq $c->[15]
ABresult
0X0
100
111
973 0 Log::OK::TRACE and log_trace "replace cookie in jar/parition";
974 0 $part->[$index]=$c;
975 }
976 else {
977 3 Log::OK::TRACE and log_trace "splicing cookie in to jar/parition";
978 3 splice @$part, $index,1,$c;
979 }
980  
981  
982 #splice @$part, $index, $replace, $c;
983 }
984 }
985 6 }
986  
987 6 1;
↑ Summary State/Cookie.pm »