Lightpanda: the headless browser designed for AI and automation
`cdp`: report back invalid same-site
Halil Durakd180a3cac235
diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zigindex 19657c3a0..7fa144362 100644--- a/src/browser/webapi/storage/Cookie.zig+++ b/src/browser/webapi/storage/Cookie.zig@@ -56,6 +56,15 @@ pub const SameSite = enum {strict,lax,none,++ pub fn parse(value: []const u8) error{InvalidSameSite}!?SameSite {+ if (std.ascii.eqlIgnoreCase(value, "strict")) return .strict;+ if (std.ascii.eqlIgnoreCase(value, "lax")) return .lax;+ if (std.ascii.eqlIgnoreCase(value, "none")) return .none;+ if (std.ascii.eqlIgnoreCase(value, "no_restriction")) return .none;+ if (std.ascii.eqlIgnoreCase(value, "unspecified")) return null;+ return error.InvalidSameSite;+ }};// How the request carrying the Cookie header reaches its target. Only adiff --git a/src/cookies.zig b/src/cookies.zigindex 05b0eb32e..92dcb7d9d 100644--- a/src/cookies.zig+++ b/src/cookies.zig@@ -156,11 +156,8 @@ const JsonCookie = struct {};fn parseJsonSameSite(value: ?[]const u8) Cookie.SameSite {- const same_site = value orelse return .none;- if (std.ascii.eqlIgnoreCase(same_site, "strict")) return .strict;- if (std.ascii.eqlIgnoreCase(same_site, "lax")) return .lax;- if (std.ascii.eqlIgnoreCase(same_site, "none")) return .none;- return .none;+ const raw = value orelse return .lax;+ return (Cookie.SameSite.parse(raw) catch .lax) orelse .lax;}/// Netscape cookie file format parser with `#HttpOnly_` addition from curl.diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zigindex b431d88bd..6b1a0e71c 100644--- a/src/server/cdp/domains/network.zig+++ b/src/server/cdp/domains/network.zig@@ -291,7 +291,10 @@ fn setCookie(cmd: *CDP.Command) !void {)) orelse return error.InvalidParams;const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;- try CdpStorage.setCdpCookie(&bc.session.cookie_jar, params);+ CdpStorage.setCdpCookie(&bc.session.cookie_jar, params) catch |err| switch (err) {+ error.InvalidSameSite => return CdpStorage.invalidSameSiteError(cmd, params.sameSite),+ else => return err,+ };try cmd.sendResult(.{ .success = true }, .{});}@@ -303,7 +306,10 @@ fn setCookies(cmd: *CDP.Command) !void {const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;for (params.cookies) |param| {- try CdpStorage.setCdpCookie(&bc.session.cookie_jar, param);+ CdpStorage.setCdpCookie(&bc.session.cookie_jar, param) catch |err| switch (err) {+ error.InvalidSameSite => return CdpStorage.invalidSameSiteError(cmd, param.sameSite),+ else => return err,+ };}try cmd.sendResult(null, .{});@@ -974,6 +980,57 @@ test "cdp.Network: cookies" {try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{} }, .{ .id = 10 });}+test "cdp.Network: setCookie accepts the sameSite spellings drivers send" {+ // Issue #3453: cookies bridged from chrome.cookies / other tooling come+ // with lowercase or `no_restriction` sameSite values. Accept them on+ // input; getCookies keeps reporting the canonical CDP spelling.+ const ResCookie = CdpStorage.ResCookie;++ var ctx = try testing.context();+ defer ctx.deinit();+ _ = try ctx.loadBrowserContext(.{ .id = "BID-SS" });++ try ctx.processMessage(+ \\{"id":1,"method":"Network.setCookie","params":{"name":"a","value":"1","url":"https://example.com/","sameSite":"lax"}}+ );+ try ctx.expectSentResult(.{ .success = true }, .{ .id = 1 });++ try ctx.processMessage(+ \\{"id":2,"method":"Network.setCookies","params":{"cookies":[+ \\ {"name":"b","value":"2","url":"https://example.com/","sameSite":"no_restriction"},+ \\ {"name":"c","value":"3","url":"https://example.com/","sameSite":"STRICT"},+ \\ {"name":"d","value":"4","url":"https://example.com/","sameSite":"unspecified"}+ \\]}}+ );+ try ctx.expectSentResult(null, .{ .id = 2 });++ try ctx.processMessage(.{+ .id = 3,+ .method = "Network.getAllCookies",+ });+ try ctx.expectSentResult(.{+ .cookies = &[_]ResCookie{+ .{ .name = "a", .value = "1", .domain = "example.com", .size = 2, .secure = true, .sameSite = "Lax" },+ // `no_restriction` is the chrome.cookies spelling of None.+ .{ .name = "b", .value = "2", .domain = "example.com", .size = 2, .secure = true, .sameSite = "None" },+ .{ .name = "c", .value = "3", .domain = "example.com", .size = 2, .secure = true, .sameSite = "Strict" },+ // `unspecified` means no SameSite was set, so it takes the default.+ .{ .name = "d", .value = "4", .domain = "example.com", .size = 2, .secure = true, .sameSite = "Lax" },+ },+ }, .{ .id = 3 });+}++test "cdp.Network: setCookie rejects an unknown sameSite by name" {+ var ctx = try testing.context();+ defer ctx.deinit();+ _ = try ctx.loadBrowserContext(.{ .id = "BID-SE" });++ try ctx.processMessage(+ \\{"id":1,"method":"Network.setCookie","params":{"name":"a","value":"1","url":"https://example.com/","sameSite":"sometimes"}}+ );+ try ctx.expectSentError(-31998, "Invalid value 'sometimes' for 'sameSite'. Accepted (case-insensitive): Strict, Lax, None, no_restriction, unspecified", .{ .id = 1 });+}+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 fd807ecb0..5e6395730 100644--- a/src/server/cdp/domains/storage.zig+++ b/src/server/cdp/domains/storage.zig@@ -86,17 +86,23 @@ fn setCookies(cmd: *CDP.Command) !void {}for (params.cookies) |param| {- try setCdpCookie(&bc.session.cookie_jar, param);+ setCdpCookie(&bc.session.cookie_jar, param) catch |err| switch (err) {+ error.InvalidSameSite => return invalidSameSiteError(cmd, param.sameSite),+ else => return err,+ };}try cmd.sendResult(null, .{});}-pub const SameSite = enum {- Strict,- Lax,- None,-};+pub fn invalidSameSiteError(cmd: *CDP.Command, value: []const u8) !void {+ const message = try std.fmt.allocPrint(+ cmd.arena,+ "Invalid value '{s}' for 'sameSite'. Accepted (case-insensitive): Strict, Lax, None, no_restriction, unspecified",+ .{value},+ );+ return cmd.sendError(-31998, message, .{});+}const CookiePriority = enum {Low,Medium,@@ -121,7 +127,7 @@ pub const CdpCookie = struct {path: ?[:0]const u8 = null,secure: ?bool = null, // default: https://www.rfc-editor.org/rfc/rfc6265#section-5.3httpOnly: bool = false, // default: https://www.rfc-editor.org/rfc/rfc6265#section-5.3- sameSite: SameSite = .None, // default: https://datatracker.ietf.org/doc/html/draft-west-first-party-cookies+ sameSite: []const u8 = "None", // default: https://datatracker.ietf.org/doc/html/draft-west-first-party-cookiesexpires: ?f64 = null, // -1? says googlepriority: CookiePriority = .Medium, // default: https://datatracker.ietf.org/doc/html/draft-west-cookie-priority-00sameParty: ?bool = null,@@ -142,6 +148,8 @@ pub fn setCdpCookie(cookie_jar: *CookieJar, param: CdpCookie) !void {return error.NotImplemented;}+ const same_site = (try Cookie.SameSite.parse(param.sameSite)) orelse .lax;+// The errdefer only protects construction failures. Once we `break :blk`// with the Cookie value, `Jar.add` owns its lifetime.const cookie = blk: {@@ -164,11 +172,7 @@ pub fn setCdpCookie(cookie_jar: *CookieJar, param: CdpCookie) !void {.expires = param.expires,.secure = secure,.http_only = param.httpOnly,- .same_site = switch (param.sameSite) {- .Strict => .strict,- .Lax => .lax,- .None => .none,- },+ .same_site = same_site,};};try cookie_jar.add(cookie, lp.datetime.timestamp(.real), true);