Skip to content

Lightpanda: the headless browser designed for AI and automation

complete merge

Karl Seguined88bd763f16
diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zig
index 0bcc72ed5..d102646d2 100644
--- a/src/browser/webapi/storage/Cookie.zig
+++ b/src/browser/webapi/storage/Cookie.zig
@@ -451,8 +451,7 @@ pub fn appliesTo(self: *const Cookie, url: *const PreparedUri, same_site: bool,
return true;
}
-// Kept distinct from a plain URL so "the initiator has no site for cookies"
-// (.none) can't be confused with "there is no initiating document".
+// RFC 6265bis "site for cookies" of a request's initiator.
pub const SiteForCookies = union(enum) {
// Matches no site, used for a frame whose ancestor chain contains a cross-site document.
none,
@@ -584,7 +583,7 @@ pub const Jar = struct {
request_time: ?u64 = null,
is_navigation: bool = true,
prefix: ?[]const u8 = null,
- origin_url: ?SiteForCookies = null,
+ origin_url: SiteForCookies,
};
pub fn forRequest(self: *Jar, target_url: [:0]const u8, writer: anytype, opts: LookupOpts) !void {
const target = PreparedUri.init(target_url);
@@ -650,12 +649,11 @@ fn areCookiesEqual(a: *const Cookie, b: *const Cookie) bool {
return true;
}
-pub fn areSameSite(maybe_origin_url: ?SiteForCookies, target_host: []const u8) bool {
- const origin_url = switch (maybe_origin_url orelse return true) {
- .none => return false,
- .url => |url| url,
+pub fn areSameSite(origin_url: SiteForCookies, target_host: []const u8) bool {
+ return switch (origin_url) {
+ .none => false,
+ .url => |url| areHostsSameSite(URL.getHostname(url), target_host),
};
- return areHostsSameSite(URL.getHostname(origin_url), target_host);
}
pub fn areHostsSameSite(target_host: []const u8, origin_host: []const u8) bool {
@@ -867,7 +865,7 @@ test "Jar: forRequest" {
{
// test with no cookies
- try expectCookies("", &jar, test_url, .{ .is_http = true });
+ try expectCookies("", &jar, test_url, .{ .origin_url = .{ .url = test_url }, .is_http = true });
}
try jar.add(try Cookie.parse(testing.allocator, test_url, "global1=1"), now, true);
@@ -881,7 +879,7 @@ test "Jar: forRequest" {
try jar.add(try Cookie.parse(testing.allocator, url2, "domain1=9;domain=test.lightpanda.io"), now, true);
// nothing fancy here
- try expectCookies("global1=1; global2=2", &jar, test_url, .{ .is_http = true });
+ try expectCookies("global1=1; global2=2", &jar, test_url, .{ .origin_url = .{ .url = test_url }, .is_http = true });
try expectCookies("global1=1; global2=2", &jar, test_url, .{ .origin_url = .{ .url = test_url }, .is_navigation = false, .is_http = true });
// We have a cookie where Domain=lightpanda.io
@@ -1024,13 +1022,14 @@ test "Jar: forRequest SameSite=Strict on cross-site navigation" {
.is_http = true,
});
- // Browser-initiated navigation (origin_url=null) is treated as same-site.
+ // Browser-initiated navigation: the initiator is the destination itself.
try expectCookies("sid=STRICT_COOKIE", &jar, "http://victim.example/transfer", .{
+ .origin_url = .{ .url = "http://victim.example/transfer" },
.is_http = true,
});
}
-test "Jar: forRequest with a null site-for-cookies" {
+test "Jar: forRequest with .none site-for-cookies" {
const expectCookies = struct {
fn expect(expected: []const u8, jar: *Jar, target_url: [:0]const u8, opts: Jar.LookupOpts) !void {
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index 71cd2fd84..de2a20005 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -613,15 +613,15 @@ pub fn newRequest(self: *Client, req: Request, owner: ?*Owner) anyerror!*Transfe
if (owned.loader_id == 0) owned.loader_id = o.loader_id;
if (owned.document_frame_id == null) owned.document_frame_id = o.document_frame_id;
if (owned.notification == null) owned.notification = o.notification;
- if (owned.cookie_origin == null) owned.cookie_origin = o.siteForCookies();
if (req.cookies) cookie_jar = o.cookie_jar;
}
- if (owned.cookie_origin) |cookie_origin| {
- owned.cookie_origin = switch (cookie_origin) {
- .none => .none,
- .url => |url| .{ .url = try arena.dupeZ(u8, url) },
- };
- }
+ // Resolved onto the transfer; the request's copy is left null so
+ // nothing reads the caller's (possibly short-lived) url through it.
+ const cookie_origin: Cookie.SiteForCookies = switch (req.cookie_origin orelse if (owner) |o| o.siteForCookies() else .none) {
+ .none => .none,
+ .url => |url| .{ .url = try arena.dupeZ(u8, url) },
+ };
+ owned.cookie_origin = null;
if (req.credentials) |c| {
owned.credentials = try arena.dupeZ(u8, c);
}
@@ -639,6 +639,7 @@ pub fn newRequest(self: *Client, req: Request, owner: ?*Owner) anyerror!*Transfe
t.* = .{
.req = owned,
.cookie_jar = cookie_jar,
+ .cookie_origin = cookie_origin,
.client = self,
.arena = arena,
.id = self.incrReqId(),
@@ -2096,6 +2097,8 @@ pub const Transfer = struct {
// The owner's jar, unless the request opted out of cookies.
cookie_jar: ?*CookieJar = null,
+ // The site for SameSite checks: Request.cookie_origin, else the owner's.
+ cookie_origin: Cookie.SiteForCookies = .none,
req_headers: std.ArrayList(RequestHeader) = .empty,
@@ -2490,7 +2493,7 @@ pub const Transfer = struct {
var aw: std.Io.Writer.Allocating = .init(arena);
try jar.forRequest(req.url, &aw.writer, .{
.is_http = true,
- .origin_url = req.cookie_origin,
+ .origin_url = self.cookie_origin,
.is_navigation = req.resource_type == .document,
});
if (aw.written().len == 0) {
diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zig
index 80e2a22ad..e257e84cd 100644
--- a/src/server/cdp/domains/network.zig
+++ b/src/server/cdp/domains/network.zig
@@ -1296,8 +1296,6 @@ const EchoDriver = struct {
.method = .POST,
.url = "http://127.0.0.1:9582/echo_body",
.body = body,
- .cookie_jar = null,
- .cookie_origin = .{ .url = "http://127.0.0.1:9582/" },
.resource_type = .fetch,
.notification = bc.session.notification,
.ctx = &driver,