@@ -12,6 +12,7 @@ pub const LookupOpts = struct {
1212 request_time : ? i64 = null ,
1313 origin_uri : ? * const Uri = null ,
1414 navigation : bool = true ,
15+ is_http : bool ,
1516};
1617
1718pub const Jar = struct {
@@ -91,7 +92,7 @@ pub const Jar = struct {
9192
9293 var first = true ;
9394 for (self .cookies .items ) | * cookie | {
94- if (! cookie .appliesTo (& target , same_site , opts .navigation )) continue ;
95+ if (! cookie .appliesTo (& target , same_site , opts .navigation , opts . is_http )) continue ;
9596
9697 // we have a match!
9798 if (first ) {
@@ -411,7 +412,12 @@ pub const Cookie = struct {
411412 return .{ name , value , rest };
412413 }
413414
414- pub fn appliesTo (self : * const Cookie , url : * const PreparedUri , same_site : bool , navigation : bool ) bool {
415+ pub fn appliesTo (self : * const Cookie , url : * const PreparedUri , same_site : bool , navigation : bool , is_http : bool ) bool {
416+ if (self .http_only and is_http == false ) {
417+ // http only cookies can be accessed from Javascript
418+ return false ;
419+ }
420+
415421 if (url .secure == false and self .secure ) {
416422 // secure cookie can only be sent over HTTPs
417423 return false ;
@@ -581,7 +587,7 @@ test "Jar: forRequest" {
581587
582588 {
583589 // test with no cookies
584- try expectCookies ("" , & jar , test_uri , .{});
590+ try expectCookies ("" , & jar , test_uri , .{ . is_http = true });
585591 }
586592
587593 try jar .add (try Cookie .parse (testing .allocator , & test_uri , "global1=1" ), now );
@@ -595,97 +601,114 @@ test "Jar: forRequest" {
595601 try jar .add (try Cookie .parse (testing .allocator , & test_uri_2 , "domain1=9;domain=test.lightpanda.io" ), now );
596602
597603 // nothing fancy here
598- try expectCookies ("global1=1; global2=2" , & jar , test_uri , .{});
599- try expectCookies ("global1=1; global2=2" , & jar , test_uri , .{ .origin_uri = & test_uri , .navigation = false });
604+ try expectCookies ("global1=1; global2=2" , & jar , test_uri , .{ . is_http = true });
605+ try expectCookies ("global1=1; global2=2" , & jar , test_uri , .{ .origin_uri = & test_uri , .navigation = false , . is_http = true });
600606
601607 // We have a cookie where Domain=lightpanda.io
602608 // This should _not_ match xyxlightpanda.io
603609 try expectCookies ("" , & jar , try std .Uri .parse ("http://anothersitelightpanda.io/" ), .{
604610 .origin_uri = & test_uri ,
611+ .is_http = true ,
605612 });
606613
607614 // matching path without trailing /
608615 try expectCookies ("global1=1; global2=2; path1=3" , & jar , try std .Uri .parse ("http://lightpanda.io/about" ), .{
609616 .origin_uri = & test_uri ,
617+ .is_http = true ,
610618 });
611619
612620 // incomplete prefix path
613621 try expectCookies ("global1=1; global2=2" , & jar , try std .Uri .parse ("http://lightpanda.io/abou" ), .{
614622 .origin_uri = & test_uri ,
623+ .is_http = true ,
615624 });
616625
617626 // path doesn't match
618627 try expectCookies ("global1=1; global2=2" , & jar , try std .Uri .parse ("http://lightpanda.io/aboutus" ), .{
619628 .origin_uri = & test_uri ,
629+ .is_http = true ,
620630 });
621631
622632 // path doesn't match cookie directory
623633 try expectCookies ("global1=1; global2=2" , & jar , try std .Uri .parse ("http://lightpanda.io/docs" ), .{
624634 .origin_uri = & test_uri ,
635+ .is_http = true ,
625636 });
626637
627638 // exact directory match
628639 try expectCookies ("global1=1; global2=2; path2=4" , & jar , try std .Uri .parse ("http://lightpanda.io/docs/" ), .{
629640 .origin_uri = & test_uri ,
641+ .is_http = true ,
630642 });
631643
632644 // sub directory match
633645 try expectCookies ("global1=1; global2=2; path2=4" , & jar , try std .Uri .parse ("http://lightpanda.io/docs/more" ), .{
634646 .origin_uri = & test_uri ,
647+ .is_http = true ,
635648 });
636649
637650 // secure
638651 try expectCookies ("global1=1; global2=2; secure=5" , & jar , try std .Uri .parse ("https://lightpanda.io/" ), .{
639652 .origin_uri = & test_uri ,
653+ .is_http = true ,
640654 });
641655
642656 // navigational cross domain, secure
643657 try expectCookies ("global1=1; global2=2; secure=5; sitenone=6; sitelax=7" , & jar , try std .Uri .parse ("https://lightpanda.io/x/" ), .{
644658 .origin_uri = &(try std .Uri .parse ("https://example.com/" )),
659+ .is_http = true ,
645660 });
646661
647662 // navigational cross domain, insecure
648663 try expectCookies ("global1=1; global2=2; sitelax=7" , & jar , try std .Uri .parse ("http://lightpanda.io/x/" ), .{
649664 .origin_uri = &(try std .Uri .parse ("https://example.com/" )),
665+ .is_http = true ,
650666 });
651667
652668 // non-navigational cross domain, insecure
653669 try expectCookies ("" , & jar , try std .Uri .parse ("http://lightpanda.io/x/" ), .{
654670 .origin_uri = &(try std .Uri .parse ("https://example.com/" )),
655671 .navigation = false ,
672+ .is_http = true ,
656673 });
657674
658675 // non-navigational cross domain, secure
659676 try expectCookies ("sitenone=6" , & jar , try std .Uri .parse ("https://lightpanda.io/x/" ), .{
660677 .origin_uri = &(try std .Uri .parse ("https://example.com/" )),
661678 .navigation = false ,
679+ .is_http = true ,
662680 });
663681
664682 // non-navigational same origin
665683 try expectCookies ("global1=1; global2=2; sitelax=7; sitestrict=8" , & jar , try std .Uri .parse ("http://lightpanda.io/x/" ), .{
666684 .origin_uri = &(try std .Uri .parse ("https://lightpanda.io/" )),
667685 .navigation = false ,
686+ .is_http = true ,
668687 });
669688
670689 // exact domain match + suffix
671690 try expectCookies ("global2=2; domain1=9" , & jar , try std .Uri .parse ("http://test.lightpanda.io/" ), .{
672691 .origin_uri = & test_uri ,
692+ .is_http = true ,
673693 });
674694
675695 // domain suffix match + suffix
676696 try expectCookies ("global2=2; domain1=9" , & jar , try std .Uri .parse ("http://1.test.lightpanda.io/" ), .{
677697 .origin_uri = & test_uri ,
698+ .is_http = true ,
678699 });
679700
680701 // non-matching domain
681702 try expectCookies ("global2=2" , & jar , try std .Uri .parse ("http://other.lightpanda.io/" ), .{
682703 .origin_uri = & test_uri ,
704+ .is_http = true ,
683705 });
684706
685707 const l = jar .cookies .items .len ;
686708 try expectCookies ("global1=1" , & jar , test_uri , .{
687709 .request_time = now + 100 ,
688710 .origin_uri = & test_uri ,
711+ .is_http = true ,
689712 });
690713 try testing .expectEqual (l - 1 , jar .cookies .items .len );
691714
0 commit comments