Lightpanda: the headless browser designed for AI and automation
cdp: take a cookie's host from its domain, as Chrome does
Adrià Arrufat8c50537cdcec
diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zigindex 2ab23d975..1c56d75b8 100644--- a/src/server/cdp/domains/network.zig+++ b/src/server/cdp/domains/network.zig@@ -294,6 +294,7 @@ fn setCookie(cmd: *CDP.Command) !void {const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;const stored = CdpStorage.setCdpCookies(&bc.session.cookie_jar, &.{params}) catch |err| switch (err) {error.InvalidCookie => return cmd.sendError(-32602, "Sanitizing cookie failed", .{}),+ error.MissingUrlOrDomain => return cmd.sendError(-32602, "At least one of the url or domain needs to be specified", .{}),else => return err,};@@ -307,7 +308,7 @@ fn setCookies(cmd: *CDP.Command) !void {const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;_ = CdpStorage.setCdpCookies(&bc.session.cookie_jar, params.cookies) catch |err| switch (err) {- error.InvalidCookie => return cmd.sendError(-32602, "Invalid cookie fields", .{}),+ error.InvalidCookie, error.MissingUrlOrDomain => return cmd.sendError(-32602, "Invalid cookie fields", .{}),else => return err,};@@ -975,7 +976,7 @@ test "cdp.Network: cookies" {.params = .{ .browserContextId = "BID-S" },});// Just the untouched test4 should be in the result- try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{.{ .name = "test4", .value = "value4", .domain = ".example.com", .path = "/mango", .size = 11 }} }, .{ .id = 8 });+ try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{.{ .name = "test4", .value = "value4", .domain = "example.com", .path = "/mango", .size = 11 }} }, .{ .id = 8 });// Empty after clearBrowserCookiestry ctx.processMessage(.{@@ -1019,7 +1020,7 @@ test "cdp.Network: invalid cookie fields are refused as Chrome does" {try ctx.processMessage(.{.id = 3,.method = "Storage.setCookies",- .params = .{ .cookies = &[_]CdpCookie{.{ .name = "a", .value = "v", .domain = "other.com", .url = "https://example.com/" }} },+ .params = .{ .cookies = &[_]CdpCookie{.{ .name = "a", .value = "v" }} },});try ctx.expectSentError(-32602, "Invalid cookie fields", .{ .id = 3 });@@ -1034,6 +1035,35 @@ test "cdp.Network: invalid cookie fields are refused as Chrome does" {try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{.{ .name = "a", .value = "v", .domain = "example.com", .path = "/", .size = 2, .secure = true }} }, .{ .id = 5 });}+test "cdp.Network: a cookie domain overrides the url's host" {+ const CdpCookie = CdpStorage.CdpCookie;+ const ResCookie = CdpStorage.ResCookie;++ var ctx = try testing.context();+ defer ctx.deinit();+ _ = try ctx.loadBrowserContext(.{ .id = "BID-DOM" });++ // As in Chrome, the url only makes the cookie Secure; without a leading+ // dot the domain is host-only.+ try ctx.processMessage(.{+ .id = 1,+ .method = "Network.setCookies",+ .params = .{ .cookies = &[_]CdpCookie{+ .{ .name = "a", .value = "v", .url = "https://example.com/", .domain = "other.com" },+ .{ .name = "b", .value = "v", .url = "https://example.com/", .domain = ".other.com" },+ } },+ });+ try ctx.expectSentResult(null, .{ .id = 1 });+ try ctx.processMessage(.{ .id = 2, .method = "Storage.getCookies", .params = .{ .browserContextId = "BID-DOM" } });+ try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{+ .{ .name = "a", .value = "v", .domain = "other.com", .path = "/", .size = 2, .secure = true },+ .{ .name = "b", .value = "v", .domain = ".other.com", .path = "/", .size = 2, .secure = true },+ } }, .{ .id = 2 });++ try ctx.processMessage(.{ .id = 3, .method = "Network.setCookie", .params = CdpCookie{ .name = "c", .value = "v" } });+ try ctx.expectSentError(-32602, "At least one of the url or domain needs to be specified", .{ .id = 3 });+}+test "cdp.Network: clearBrowserCookies accepts empty params object" {const CdpCookie = CdpStorage.CdpCookie;const ResCookie = CdpStorage.ResCookie;diff --git a/src/server/cdp/domains/storage.zig b/src/server/cdp/domains/storage.zigindex 9118798d3..19c7ca482 100644--- a/src/server/cdp/domains/storage.zig+++ b/src/server/cdp/domains/storage.zig@@ -87,7 +87,7 @@ fn setCookies(cmd: *CDP.Command) !void {}_ = setCdpCookies(&bc.session.cookie_jar, params.cookies) catch |err| switch (err) {- error.InvalidCookie => return cmd.sendError(-32602, "Invalid cookie fields", .{}),+ error.InvalidCookie, error.MissingUrlOrDomain => return cmd.sendError(-32602, "Invalid cookie fields", .{}),else => return err,};@@ -174,13 +174,26 @@ fn buildCdpCookie(allocator: Allocator, param: CdpCookie) !Cookie {}// Chrome's MakeCookieFromProtocolValues: an https url makes the cookie- // Secure whatever `secure` says.- const secure = (param.secure orelse false) or (if (param.url) |url| URL.isSecure(url) else false);-- return Cookie.fromFields(allocator, param.url, .{+ // Secure whatever `secure` says, and a `domain` overrides the url's host.+ // Only a domain with a leading dot is a Domain attribute; without one,+ // the cookie is host-only on that host.+ const url = param.url orelse "";+ const domain = param.domain orelse "";+ const secure = (param.secure orelse false) or URL.isSecure(url);+ const dotted = std.mem.startsWith(u8, domain, ".");+ const host = if (domain.len > 0)+ domain[@intFromBool(dotted)..]+ else if (url.len > 0)+ URL.getHostname(url)+ else+ return error.MissingUrlOrDomain;+ const cookie_url = try std.fmt.allocPrintSentinel(allocator, "{s}://{s}/", .{ if (secure) "https" else "http", host }, 0);+ defer allocator.free(cookie_url);++ return Cookie.fromFields(allocator, cookie_url, .{.name = param.name,.value = param.value,- .domain = param.domain,+ .domain = if (dotted) domain else null,.path = param.path,.expires = param.expires,.secure = secure,@@ -320,7 +333,7 @@ test "cdp.Storage: cookies" {});try ctx.expectSentResult(.{.cookies = &[_]ResCookie{- .{ .name = "test", .value = "value", .domain = ".example.com", .path = "/mango", .size = 9 },+ .{ .name = "test", .value = "value", .domain = "example.com", .path = "/mango", .size = 9 },.{ .name = "test2", .value = "value2", .domain = "car.example.com", .path = "/", .size = 11, .secure = true }, // No Pancakes!.{ .name = "test3", .value = "value3", .domain = "gov.uk", .path = "/", .size = 11 },},