Lightpanda: the headless browser designed for AI and automation
cookies: build CDP and BiDi cookies through one Chrome-checked constructor
Adrià Arrufat57ba8042be17
diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zigindex 24727092c..d7f321226 100644--- a/src/browser/webapi/storage/Cookie.zig+++ b/src/browser/webapi/storage/Cookie.zig@@ -253,6 +253,84 @@ pub fn parse(allocator: Allocator, url: [:0]const u8, str: []const u8) !Cookie {};}+pub const Fields = struct {+ name: []const u8,+ value: []const u8,+ domain: ?[]const u8 = null,+ path: ?[]const u8 = null,+ expires: ?f64 = null,+ secure: bool = false,+ http_only: bool = false,+ same_site: ?SameSite = null,+};++/// A cookie a protocol (CDP, BiDi, WebDriver) sets field by field rather than+/// through a Set-Cookie line, with the character and size checks of Chrome's+/// CanonicalCookie::CreateSanitizedCookie, which both its CDP and its BiDi go+/// through.+pub fn fromFields(allocator: Allocator, url: ?[:0]const u8, fields: Fields) error{ OutOfMemory, InvalidCookie, InvalidDomain }!Cookie {+ if ((fields.name.len == 0 and fields.value.len == 0) or+ fields.name.len + fields.value.len > max_cookie_size or+ !isValidField(fields.name, ";=") or+ !isValidField(fields.value, ";"))+ {+ return error.InvalidCookie;+ }+ if (fields.path) |path| {+ // Chrome URL-encodes any other control character in a path.+ if (std.mem.findAny(u8, path, ";\r\n\x00") != null or hasOuterWhitespace(path)) {+ return error.InvalidCookie;+ }+ }++ var arena = std.heap.ArenaAllocator.init(allocator);+ errdefer arena.deinit();+ const a = arena.allocator();++ // Allocate before the struct literal copies `arena` into the result.+ const name = try a.dupe(u8, fields.name);+ const value = try a.dupe(u8, fields.value);+ const domain = parseDomain(a, url, fields.domain) catch |err| switch (err) {+ error.OutOfMemory => return error.OutOfMemory,+ else => return error.InvalidDomain,+ };+ // Without a url there's nothing to encode: only the dupe can fail.+ const path = parsePath(a, null, fields.path) catch return error.OutOfMemory;++ return .{+ .arena = arena,+ .name = name,+ .value = value,+ .domain = domain,+ .path = path,+ .expires = fields.expires,+ .secure = fields.secure,+ .http_only = fields.http_only,+ .same_site = fields.same_site orelse .lax,+ .same_site_default = fields.same_site == null,+ };+}++// Chrome's IsValidCookieName/Value: no control character (tab included) and+// none of `separators`, but bytes above 0x7F are fine.+fn isValidField(field: []const u8, comptime separators: []const u8) bool {+ if (hasOuterWhitespace(field)) {+ return false;+ }+ for (field) |c| {+ if (std.ascii.isControl(c) or std.mem.findScalar(u8, separators, c) != null) {+ return false;+ }+ }+ return true;+}++// Chrome's ParseTokenString/ParseValueString trim it, and refuse a field+// that doesn't survive the parse unchanged.+fn hasOuterWhitespace(field: []const u8) bool {+ return std.mem.trim(u8, field, " \t").len != field.len;+}+const ValidateCookieError = error{ Empty, InvalidByteSequence };/// Returns an error if cookie str length is 0@@ -799,6 +877,38 @@ fn toLower(str: []u8) []u8 {const testing = @import("../../../testing.zig");const test_url = "http://lightpanda.io/";+test "cookie: fromFields refuses what Chrome's CreateSanitizedCookie does" {+ const url = "https://example.com/";+ const too_long: [max_cookie_size]u8 = @splat('v');+ const refused = [_]Fields{+ .{ .name = "a;b", .value = "v" },+ .{ .name = "a=b", .value = "v" },+ .{ .name = " a", .value = "v" },+ .{ .name = "a", .value = "x;y" },+ .{ .name = "a", .value = "x\ty" },+ .{ .name = "a", .value = "x\x7fy" },+ .{ .name = "a", .value = "v " },+ .{ .name = "", .value = "" },+ .{ .name = "a", .value = &too_long },+ .{ .name = "a", .value = "v", .path = "/a\nb" },+ };+ for (refused) |fields| {+ try testing.expectError(error.InvalidCookie, fromFields(testing.allocator, url, fields));+ }+ try testing.expectError(error.InvalidDomain, fromFields(testing.allocator, url, .{ .name = "a", .value = "v", .domain = "other.com" }));++ // Wider than RFC 6265bis's grammar, as in Chrome.+ const accepted = [_]Fields{+ .{ .name = "a b", .value = "caf\xc3\xa9" },+ .{ .name = "a", .value = "" },+ .{ .name = "a", .value = "v", .path = "/a\x01b" },+ };+ for (accepted) |fields| {+ const cookie = try fromFields(testing.allocator, url, fields);+ cookie.deinit();+ }+}+test "cookie: findSecondLevelDomain" {const cases = [_]struct { []const u8, []const u8 }{.{ "", "" },diff --git a/src/server/bidi/http_command.zig b/src/server/bidi/http_command.zigindex 3786ed884..81e4c04c5 100644--- a/src/server/bidi/http_command.zig+++ b/src/server/bidi/http_command.zig@@ -878,21 +878,20 @@ fn addCookie(cmd: *BiDi.Command, p: AddCookie) !void {}const c = p.cookie;- const spec: storage.Spec = .{+ storage.add(&cmd.bidi.user_context.session.cookie_jar, .{.name = c.name,.value = c.value,.domain = c.domain,.path = c.path,.secure = c.secure,.http_only = c.httpOnly,- .expiry = c.expiry,+ .expires = if (c.expiry) |expiry| @floatFromInt(expiry) else null,.same_site = if (c.sameSite) |same_site| switch (same_site) {.Strict => .strict,.Lax => .lax,.None => .none,} else null,- };- storage.add(&cmd.bidi.user_context.session.cookie_jar, spec, frame.url) catch |err| switch (err) {+ }, frame.url) catch |err| switch (err) {error.OutOfMemory => return err,error.InvalidDomain => return cmd.sendError("invalid cookie domain", "the domain doesn't match the current document"),error.UnableToSetCookie => return cmd.sendError("unable to set cookie", "the cookie was rejected"),diff --git a/src/server/bidi/storage.zig b/src/server/bidi/storage.zigindex 46478b255..76f55d484 100644--- a/src/server/bidi/storage.zig+++ b/src/server/bidi/storage.zig@@ -44,53 +44,14 @@ pub fn processMessage(cmd: *BiDi.Command, action: []const u8) !void {}}-// A cookie as a driver hands it over.-pub const Spec = struct {- name: []const u8,- value: []const u8,- domain: ?[]const u8 = null,- path: ?[]const u8 = null,- secure: bool = false,- http_only: bool = false,- expiry: ?u64 = null, // seconds since the epoch, null for a session cookie- same_site: ?Cookie.SameSite = null, // null: unspecified, Lax by default-};--pub fn add(jar: *Cookie.Jar, spec: Spec, url: ?[:0]const u8) !void {- const cookie: Cookie = blk: {- if (isValidPart(spec.name, "=;") == false or isValidPart(spec.value, ";") == false) {- return error.UnableToSetCookie;- }- if (spec.same_site == .none and spec.secure == false) {- // the store refuses SameSite=None without Secure, as Set-Cookie does- return error.UnableToSetCookie;- }-- var arena = std.heap.ArenaAllocator.init(jar.allocator);- errdefer arena.deinit();- const a = arena.allocator();-- // Allocate before the struct literal copies `arena` into the result.- const name = try a.dupe(u8, spec.name);- const value = try a.dupe(u8, spec.value);- const domain = Cookie.parseDomain(a, url, spec.domain) catch |err| switch (err) {- error.OutOfMemory => return error.OutOfMemory,- else => return error.InvalidDomain,- };- const path = Cookie.parsePath(a, null, spec.path) catch return error.OutOfMemory;-- break :blk .{- .arena = arena,- .name = name,- .value = value,- .domain = domain,- .path = path,- .expires = if (spec.expiry) |expiry| @floatFromInt(expiry) else null,- .secure = spec.secure,- .http_only = spec.http_only,- .same_site = spec.same_site orelse .lax,- .same_site_default = spec.same_site == null,- };+pub fn add(jar: *Cookie.Jar, fields: Cookie.Fields, url: ?[:0]const u8) !void {+ if (fields.same_site == .none and fields.secure == false) {+ // the store refuses SameSite=None without Secure, as Set-Cookie does+ return error.UnableToSetCookie;+ }+ const cookie = Cookie.fromFields(jar.allocator, url, fields) catch |err| switch (err) {+ error.OutOfMemory, error.InvalidDomain => |e| return e,+ error.InvalidCookie => return error.UnableToSetCookie,};jar.add(cookie, lp.datetime.timestamp(.real), true) catch |err| switch (err) {@@ -99,20 +60,6 @@ pub fn add(jar: *Cookie.Jar, spec: Spec, url: ?[:0]const u8) !void {};}-// What Cookie.parse accepts, minus the separators a Cookie header would-// read differently.-fn isValidPart(part: []const u8, comptime separators: []const u8) bool {- for (part) |c| {- if ((c < 32 and c != '\t') or c > 126) {- return false;- }- if (std.mem.findScalar(u8, separators, c) != null) {- return false;- }- }- return true;-}-// The cookies a navigation to `url` would carry, HttpOnly ones included:// what the HTTP session's cookie commands see and delete. Optionally, only// those called `name`.@@ -359,21 +306,21 @@ fn setCookie(cmd: *BiDi.Command) !void {const c = p.cookie;const value = c.value.decode(cmd.arena) catch return cmd.sendError("invalid argument", "invalid cookie.value");- const spec: Spec = .{+ const fields: Cookie.Fields = .{.name = c.name,.value = value,.domain = c.domain,.path = c.path,.secure = c.secure,.http_only = c.httpOnly,- .expiry = c.expiry,+ .expires = if (c.expiry) |expiry| @floatFromInt(expiry) else null,.same_site = if (c.sameSite) |same_site| switch (same_site) {.default => null,inline else => |tag| @field(Cookie.SameSite, @tagName(tag)),} else null,};- add(&cmd.bidi.user_context.session.cookie_jar, spec, null) catch |err| switch (err) {+ add(&cmd.bidi.user_context.session.cookie_jar, fields, null) catch |err| switch (err) {error.OutOfMemory => return err,error.InvalidDomain, error.UnableToSetCookie => return cmd.sendError("unable to set cookie", @errorName(err)),};diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zigindex 1e5dd0e05..2ab23d975 100644--- a/src/server/cdp/domains/network.zig+++ b/src/server/cdp/domains/network.zig@@ -292,7 +292,10 @@ fn setCookie(cmd: *CDP.Command) !void {)) orelse return error.InvalidParams;const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;- const stored = try CdpStorage.setCdpCookies(&bc.session.cookie_jar, &.{params});+ const stored = CdpStorage.setCdpCookies(&bc.session.cookie_jar, &.{params}) catch |err| switch (err) {+ error.InvalidCookie => return cmd.sendError(-32602, "Sanitizing cookie failed", .{}),+ else => return err,+ };try cmd.sendResult(.{ .success = stored == 1 }, .{});}@@ -303,7 +306,10 @@ fn setCookies(cmd: *CDP.Command) !void {})) orelse return error.InvalidParams;const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;- _ = try CdpStorage.setCdpCookies(&bc.session.cookie_jar, params.cookies);+ _ = CdpStorage.setCdpCookies(&bc.session.cookie_jar, params.cookies) catch |err| switch (err) {+ error.InvalidCookie => return cmd.sendError(-32602, "Invalid cookie fields", .{}),+ else => return err,+ };try cmd.sendResult(null, .{});}@@ -985,6 +991,49 @@ test "cdp.Network: cookies" {try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{} }, .{ .id = 10 });}+test "cdp.Network: invalid cookie fields are refused as Chrome does" {+ const CdpCookie = CdpStorage.CdpCookie;+ const ResCookie = CdpStorage.ResCookie;++ var ctx = try testing.context();+ defer ctx.deinit();+ _ = try ctx.loadBrowserContext(.{ .id = "BID-INV" });++ try ctx.processMessage(.{+ .id = 1,+ .method = "Network.setCookie",+ .params = CdpCookie{ .name = "a;b", .value = "v", .url = "https://example.com/" },+ });+ try ctx.expectSentError(-32602, "Sanitizing cookie failed", .{ .id = 1 });++ // One bad cookie fails the whole batch, and nothing is stored.+ try ctx.processMessage(.{+ .id = 2,+ .method = "Network.setCookies",+ .params = .{ .cookies = &[_]CdpCookie{+ .{ .name = "ok", .value = "v", .url = "https://example.com/" },+ .{ .name = "a", .value = "x\ty", .url = "https://example.com/" },+ } },+ });+ try ctx.expectSentError(-32602, "Invalid cookie fields", .{ .id = 2 });+ try ctx.processMessage(.{+ .id = 3,+ .method = "Storage.setCookies",+ .params = .{ .cookies = &[_]CdpCookie{.{ .name = "a", .value = "v", .domain = "other.com", .url = "https://example.com/" }} },+ });+ try ctx.expectSentError(-32602, "Invalid cookie fields", .{ .id = 3 });++ // An https url makes the cookie Secure even when `secure` says otherwise.+ try ctx.processMessage(.{+ .id = 4,+ .method = "Network.setCookie",+ .params = CdpCookie{ .name = "a", .value = "v", .url = "https://example.com/", .secure = false },+ });+ try ctx.expectSentResult(.{ .success = true }, .{ .id = 4 });+ try ctx.processMessage(.{ .id = 5, .method = "Storage.getCookies", .params = .{ .browserContextId = "BID-INV" } });+ try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{.{ .name = "a", .value = "v", .domain = "example.com", .path = "/", .size = 2, .secure = true }} }, .{ .id = 5 });+}+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 e159295d4..9118798d3 100644--- a/src/server/cdp/domains/storage.zig+++ b/src/server/cdp/domains/storage.zig@@ -86,7 +86,10 @@ fn setCookies(cmd: *CDP.Command) !void {}}- _ = try setCdpCookies(&bc.session.cookie_jar, params.cookies);+ _ = setCdpCookies(&bc.session.cookie_jar, params.cookies) catch |err| switch (err) {+ error.InvalidCookie => return cmd.sendError(-32602, "Invalid cookie fields", .{}),+ else => return err,+ };try cmd.sendResult(null, .{});}@@ -170,32 +173,23 @@ fn buildCdpCookie(allocator: Allocator, param: CdpCookie) !Cookie {return error.NotImplemented;}- // NOTE: The param.url can affect the default domain, (NOT path), secure, source port, and source scheme.- const secure = if (param.secure) |s| s else if (param.url) |url| URL.isSecure(url) else false;-- const same_site = parseSameSite(param.sameSite);-- var arena = std.heap.ArenaAllocator.init(allocator);- errdefer arena.deinit();- const a = arena.allocator();-- // Allocate before the struct literal copies `arena` into the result.- const name = try a.dupe(u8, param.name);- const value = try a.dupe(u8, param.value);- const domain = try Cookie.parseDomain(a, param.url, param.domain);- const path = if (param.path == null) "/" else try Cookie.parsePath(a, null, param.path);+ // 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 .{- .arena = arena,- .name = name,- .value = value,- .path = path,- .domain = domain,+ return Cookie.fromFields(allocator, param.url, .{+ .name = param.name,+ .value = param.value,+ .domain = param.domain,+ .path = param.path,.expires = param.expires,.secure = secure,.http_only = param.httpOnly,- .same_site = same_site orelse .lax,- .same_site_default = same_site == null,+ .same_site = parseSameSite(param.sameSite),+ }) catch |err| switch (err) {+ // Chrome refuses a cookie for a bad domain like any other bad field.+ error.InvalidDomain => error.InvalidCookie,+ else => |e| e,};}