Skip to content

Lightpanda: the headless browser designed for AI and automation

cookies: send longer paths first, then older cookies, in the Cookie header

Jay Hemnanib5f055b86b86
diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zig
index 62c040234..77564480d 100644
--- a/src/browser/webapi/storage/Cookie.zig
+++ b/src/browser/webapi/storage/Cookie.zig
@@ -705,11 +705,12 @@ pub const Jar = struct {
if (is_expired) {
// Dispatch while c still points at the live old cookie,
// then free its arena, then remove from the array. After
- // swapRemove, items[i] holds a different (still-live)
- // entry, so deinit must happen before that.
+ // orderedRemove, items[i] holds a different (still-live)
+ // entry, so deinit must happen before that. orderedRemove
+ // keeps the jar in creation order for forRequest.
self.dispatchChange(.deleted, c);
c.deinit();
- _ = self.cookies.swapRemove(i);
+ _ = self.cookies.orderedRemove(i);
} else {
// RFC 6265bis 5.7 step 23: the replacement keeps the
// creation time of the cookie it replaces.
@@ -757,7 +758,7 @@ pub const Jar = struct {
i -= 1;
const cookie = &self.cookies.items[i];
if (isCookieExpired(cookie, time)) {
- self.cookies.swapRemove(i).deinit();
+ self.cookies.orderedRemove(i).deinit();
}
}
}
@@ -778,7 +779,8 @@ pub const Jar = struct {
removeExpired(self, now);
- var first = true;
+ var matches: std.ArrayList(*const Cookie) = .empty;
+ defer matches.deinit(self.allocator);
for (self.cookies.items) |*cookie| {
const applies = cookie.appliesTo(&target, .{
.same_site = same_site,
@@ -786,16 +788,21 @@ pub const Jar = struct {
.kind = opts.kind,
.now = now,
});
- if (!applies) {
- continue;
+ if (applies) {
+ try matches.append(self.allocator, cookie);
}
+ }
+
+ // RFC 6265bis 5.8.3: longer paths first, then earlier creation times.
+ // The sort is stable, so cookies created in the same second keep the
+ // order they were added in.
+ std.sort.insertion(*const Cookie, matches.items, {}, sendsBefore);
- // we have a match!
- if (first) {
+ for (matches.items, 0..) |cookie, i| {
+ if (i == 0) {
if (opts.prefix) |prefix| {
try writer.writeAll(prefix);
}
- first = false;
} else {
try writer.writeAll("; ");
}
@@ -803,6 +810,13 @@ pub const Jar = struct {
}
}
+ fn sendsBefore(_: void, a: *const Cookie, b: *const Cookie) bool {
+ if (a.path.len != b.path.len) {
+ return a.path.len > b.path.len;
+ }
+ return a.creation_time < b.creation_time;
+ }
+
pub fn populateFromResponse(self: *Jar, url: [:0]const u8, set_cookie: []const u8) !void {
const c = Cookie.parse(self.allocator, url, set_cookie) catch |err| {
log.debug(.frame, "cookie parse failed", .{ .raw = set_cookie, .err = err });
@@ -1134,7 +1148,7 @@ test "Jar: forRequest" {
});
// matching path without trailing /
- try expectCookies("global1=1; global2=2; path1=3", &jar, "http://lightpanda.io/about", .{
+ try expectCookies("path1=3; global1=1; global2=2", &jar, "http://lightpanda.io/about", .{
.origin_url = .{ .url = test_url },
.is_http = true,
});
@@ -1158,13 +1172,13 @@ test "Jar: forRequest" {
});
// exact directory match
- try expectCookies("global1=1; global2=2; path2=4", &jar, "http://lightpanda.io/docs/", .{
+ try expectCookies("path2=4; global1=1; global2=2", &jar, "http://lightpanda.io/docs/", .{
.origin_url = .{ .url = test_url },
.is_http = true,
});
// sub directory match
- try expectCookies("global1=1; global2=2; path2=4", &jar, "http://lightpanda.io/docs/more", .{
+ try expectCookies("path2=4; global1=1; global2=2", &jar, "http://lightpanda.io/docs/more", .{
.origin_url = .{ .url = test_url },
.is_http = true,
});
@@ -1176,14 +1190,14 @@ test "Jar: forRequest" {
});
// navigational cross domain, secure
- try expectCookies("global1=1; global2=2; secure=5; sitenone=6; sitelax=7", &jar, "https://lightpanda.io/x/", .{
+ try expectCookies("sitenone=6; sitelax=7; global1=1; global2=2; secure=5", &jar, "https://lightpanda.io/x/", .{
.origin_url = .{ .url = "https://example.com/" },
.is_http = true,
.kind = .navigation,
});
// navigational cross domain, insecure
- try expectCookies("global1=1; global2=2; sitelax=7", &jar, "http://lightpanda.io/x/", .{
+ try expectCookies("sitelax=7; global1=1; global2=2", &jar, "http://lightpanda.io/x/", .{
.origin_url = .{ .url = "https://example.com/" },
.is_http = true,
.kind = .navigation,
@@ -1204,7 +1218,7 @@ test "Jar: forRequest" {
});
// non-navigational same origin
- try expectCookies("global1=1; global2=2; sitelax=7; sitestrict=8", &jar, "http://lightpanda.io/x/", .{
+ try expectCookies("sitelax=7; sitestrict=8; global1=1; global2=2", &jar, "http://lightpanda.io/x/", .{
.origin_url = .{ .url = "https://lightpanda.io/" },
.is_http = true,
.kind = .subresource,
@@ -1240,6 +1254,29 @@ test "Jar: forRequest" {
// the 'global2' cookie
}
+test "Jar: forRequest orders cookies by path length, then creation time" {
+ const now = lp.datetime.timestamp(.real);
+
+ var jar = Jar.init(testing.allocator, null);
+ defer jar.deinit();
+
+ try jar.add(try Cookie.parse(testing.allocator, test_url, "session=root"), now, true);
+ try jar.add(try Cookie.parse(testing.allocator, test_url, "old=1;Max-Age=10"), now, true);
+ try jar.add(try Cookie.parse(testing.allocator, test_url, "session=app;Path=/app"), now + 1, true);
+ try jar.add(try Cookie.parse(testing.allocator, test_url, "a=2"), now + 2, true);
+ try jar.add(try Cookie.parse(testing.allocator, test_url, "b=3"), now + 2, true);
+
+ // `old` expires before the request; removing it must not reorder `a` and `b`.
+ var aw: std.Io.Writer.Allocating = .init(testing.allocator);
+ defer aw.deinit();
+ try jar.forRequest("http://lightpanda.io/app/x", &aw.writer, .{
+ .origin_url = .{ .url = test_url },
+ .is_http = true,
+ .request_time = now + 20,
+ });
+ try testing.expectEqual("session=app; session=root; a=2; b=3", aw.written());
+}
+
test "Jar: forRequest Secure cookies on loopback origins" {
const expectCookies = struct {
fn expect(expected: []const u8, jar: *Jar, target_url: [:0]const u8, opts: Jar.LookupOpts) !void {