Skip to content

Commit 9fec6eb

Browse files
committed
fix typo, improve comment, add 1 test case
1 parent a00d1d0 commit 9fec6eb

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/storage/cookie.zig

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub const Jar = struct {
5757
request_time: i64,
5858
origin_uri: ?Uri,
5959
target_uri: Uri,
60-
navitation: bool,
60+
navigation: bool,
6161
) !CookieList {
6262
const target_path = target_uri.path.percent_encoded;
6363
const target_host = (target_uri.host orelse return error.InvalidURI).percent_encoded;
@@ -93,31 +93,35 @@ pub const Jar = struct {
9393
// and cookie.same_site == .lax
9494
switch (cookie.same_site) {
9595
.strict => continue,
96-
.lax => if (navitation == false) continue,
96+
.lax => if (navigation == false) continue,
9797
.none => {},
9898
}
9999
}
100100

101101
{
102102
const domain = cookie.domain;
103103
if (domain[0] == '.') {
104-
// when explicitly set, the domain
105-
// 1 - always starts with a .
106-
// 2 - always is a suffix match (or examlpe)
104+
// When a Set-Cookie header has a Domain attribute
105+
// Then we will _always_ prefix it with a dot, extending its
106+
// availability to all subdomains (yes, setting the Domain
107+
// attributes EXPANDS the domains which the cookie will be
108+
// sent to, to always include all subdomains).
107109
if (std.mem.eql(u8, target_host, domain[1..]) == false and std.mem.endsWith(u8, target_host, domain) == false) {
108110
continue;
109111
}
110112
} else if (std.mem.eql(u8, target_host, domain) == false) {
111-
// when Domain=XYX isn't specified, it's an exact match only
113+
// When the Domain attribute isn't specific, then the cookie
114+
// is only sent on an exact match.
112115
continue;
113116
}
114117
}
115118

116119
{
117120
const path = cookie.path;
118121
if (path[path.len - 1] == '/') {
119-
// If our cookie path is doc/
120-
// Then we can only match if the target path starts with doc/
122+
// If our cookie has a trailing slash, we can only match is
123+
// the target path is a perfix. I.e., if our path is
124+
// /doc/ we can only match /doc/*
121125
if (std.mem.startsWith(u8, target_path, path) == false) {
122126
continue;
123127
}
@@ -504,7 +508,7 @@ test "Jar: forRequest" {
504508
}
505509

506510
try jar.add(try Cookie.parse(testing.allocator, test_uri, "global1=1"), now);
507-
try jar.add(try Cookie.parse(testing.allocator, test_uri, "global2=2;Max-Age=30"), now);
511+
try jar.add(try Cookie.parse(testing.allocator, test_uri, "global2=2;Max-Age=30;domain=lightpanda.io"), now);
508512
try jar.add(try Cookie.parse(testing.allocator, test_uri, "path1=3;Path=/about"), now);
509513
try jar.add(try Cookie.parse(testing.allocator, test_uri, "path2=4;Path=/docs/"), now);
510514
try jar.add(try Cookie.parse(testing.allocator, test_uri, "secure=5;Secure"), now);
@@ -519,6 +523,19 @@ test "Jar: forRequest" {
519523
try expectCookies(&.{ "global1", "global2" }, &matches);
520524
}
521525

526+
{
527+
// We have a cookie where Domain=lightpanda.io
528+
// This should _not_ match xyxlightpanda.io
529+
var matches = try jar.forRequest(
530+
testing.allocator,
531+
now,
532+
test_uri,
533+
try std.Uri.parse("http://anothersitelightpanda.io/"),
534+
true,
535+
);
536+
try expectCookies(&.{}, &matches);
537+
}
538+
522539
{
523540
// matching path without trailing /
524541
var matches = try jar.forRequest(
@@ -664,27 +681,27 @@ test "Jar: forRequest" {
664681
}
665682

666683
{
667-
// exact domain match
684+
// exact domain match + suffix
668685
var matches = try jar.forRequest(
669686
testing.allocator,
670687
now,
671688
test_uri,
672689
try std.Uri.parse("http://test.lightpanda.io/"),
673690
true,
674691
);
675-
try expectCookies(&.{"domain1"}, &matches);
692+
try expectCookies(&.{ "global2", "domain1" }, &matches);
676693
}
677694

678695
{
679-
// domain suffix match
696+
// domain suffix match + suffix
680697
var matches = try jar.forRequest(
681698
testing.allocator,
682699
now,
683700
test_uri,
684701
try std.Uri.parse("http://1.test.lightpanda.io/"),
685702
true,
686703
);
687-
try expectCookies(&.{"domain1"}, &matches);
704+
try expectCookies(&.{ "global2", "domain1" }, &matches);
688705
}
689706

690707
{
@@ -696,7 +713,7 @@ test "Jar: forRequest" {
696713
try std.Uri.parse("http://other.lightpanda.io/"),
697714
true,
698715
);
699-
try expectCookies(&.{}, &matches);
716+
try expectCookies(&.{"global2"}, &matches);
700717
}
701718

702719
{

0 commit comments

Comments
 (0)