Lightpanda: the headless browser designed for AI and automation
`Cookie`: getter/setter changes for site-for-cookies
Halil Durakadd8a8c00355
diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex a3fe037cf..290177129 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -294,7 +294,8 @@ pub fn getCookie(self: *Document, frame: *Frame) ![]const u8 {var aw: std.Io.Writer.Allocating = .init(frame.local_arena);try frame._session.cookie_jar.forRequest(frame.url, &aw.writer, .{.is_http = false,- .is_navigation = true,+ .is_navigation = false,+ .origin_url = frame.siteForCookies(),});return aw.written();}@@ -314,6 +315,10 @@ pub fn setCookie(self: *Document, cookie_str: []const u8, frame: *Frame) ![]consc.deinit();return ""; // HttpOnly cookies cannot be set from JS}+ if (c.same_site != .none and frame.siteForCookies() == .none) {+ c.deinit();+ return ""; // SameSite cookies cannot be set from a cross-site context.+ }try frame._session.cookie_jar.add(c, lp.datetime.timestamp(.real), false);return cookie_str;}@@ -1641,6 +1646,53 @@ test "WebApi: Document.evaluate" {try testing.htmlRunner("xpath/document_evaluate.html", .{});}+test "Document: cookie access from a cross-site frame" {+ defer testing.test_session.closeAllPages();+ const frame = try testing.createFrame();+ const doc = frame.document;+ const jar = &frame._session.cookie_jar;+ defer jar.clearRetainingCapacity();++ // victim.example embedded by attacker.example: the ancestor chain is+ // cross-site, so the frame has no site for cookies.+ var top: Frame = undefined;+ top.parent = null;+ top.url = "https://attacker.example/";+ frame.url = "https://victim.example/inner";+ frame.parent = ⊤+ defer frame.parent = null;++ try jar.populateFromResponse("https://victim.example/", "strict=1; SameSite=Strict");+ try jar.populateFromResponse("https://victim.example/", "lax=2; SameSite=Lax");+ try jar.populateFromResponse("https://victim.example/", "default=3");+ try jar.populateFromResponse("https://victim.example/", "none=4; SameSite=None; Secure");++ // Reads: only SameSite=None is visible from a cross-site context. Lax+ // gets no navigation exception for script access.+ try testing.expectEqual("none=4", try doc.getCookie(frame));++ // Writes: SameSite=None is stored, everything else (including the Lax+ // default for an unspecified attribute) is silently dropped.+ _ = try doc.setCookie("set_strict=5; SameSite=Strict", frame);+ _ = try doc.setCookie("set_lax=6; SameSite=Lax", frame);+ _ = try doc.setCookie("set_default=7", frame);+ _ = try doc.setCookie("set_none=8; SameSite=None; Secure", frame);+ try testing.expectEqual("none=4; set_none=8", try doc.getCookie(frame));++ // The same jar seen from a same-site chain: everything applies, and the+ // dropped writes really were dropped rather than hidden.+ top.url = "https://victim.example/";+ try testing.expectEqual("strict=1; lax=2; default=3; none=4; set_none=8", try doc.getCookie(frame));+ _ = try doc.setCookie("set_strict=5; SameSite=Strict", frame);+ _ = try doc.setCookie("set_default=7", frame);+ try testing.expectEqual("strict=1; lax=2; default=3; none=4; set_none=8; set_strict=5; set_default=7", try doc.getCookie(frame));++ // Back in the cross-site context, the newly written cookies obey the+ // same visibility rules.+ top.url = "https://attacker.example/";+ try testing.expectEqual("none=4; set_none=8", try doc.getCookie(frame));+}+test "Document: isRelaxableTo" {// Pure opt-in (relax to self) is always allowed, including IP hosts.try testing.expectEqual(true, isRelaxableTo("a.example.com", "a.example.com"));diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zigindex a98468703..e6e17c8e0 100644--- a/src/browser/webapi/storage/Cookie.zig+++ b/src/browser/webapi/storage/Cookie.zig@@ -652,7 +652,7 @@ fn areCookiesEqual(a: *const Cookie, b: *const Cookie) bool {return true;}-fn areSameSite(maybe_origin_url: ?SiteForCookies, target_host: []const u8) bool {+pub fn areSameSite(maybe_origin_url: ?SiteForCookies, target_host: []const u8) bool {// No initiating document (browser-initiated request).const origin_url = switch (maybe_origin_url orelse return true) {.none => return false,diff --git a/src/browser/webapi/storage/CookieStore.zig b/src/browser/webapi/storage/CookieStore.zigindex 4a8e3ac3e..a1c9b4ea6 100644--- a/src/browser/webapi/storage/CookieStore.zig+++ b/src/browser/webapi/storage/CookieStore.zig@@ -23,6 +23,7 @@ const js = @import("../../js/js.zig");const URL = @import("../../URL.zig");const Notification = @import("../../../Notification.zig");+const Frame = @import("../../Frame.zig");const Cookie = @import("Cookie.zig");const EventTarget = @import("../EventTarget.zig");const CookieChangeEvent = @import("../event/CookieChangeEvent.zig");@@ -66,7 +67,7 @@ fn onCookieChanged(ctx: *anyopaque, data: *const Notification.CookieChanged) !vo// CookieStore exposes only cookies that script would see for the// current document — same filter as `match` (HttpOnly hidden,- // same-site treated as first-party against the document URL).+ // SameSite judged against the global's site-for-cookies).const doc_url = exec.url.*;const target = Cookie.PreparedUri.init(doc_url);if (target.host.len == 0) {@@ -84,7 +85,8 @@ fn onCookieChanged(ctx: *anyopaque, data: *const Notification.CookieChanged) !vo.http_only = data.http_only,.same_site = data.same_site,};- if (!probe.appliesTo(&target, true, true, false)) {+ const same_site = Cookie.areSameSite(exec.siteForCookies(), target.host);+ if (!probe.appliesTo(&target, same_site, false, false)) {return;}@@ -377,11 +379,12 @@ fn matchCookies(// we're matchingconst normalized_name: ?[]const u8 = if (name) |n| std.mem.trim(u8, n, " \t") else null;+ const same_site = Cookie.areSameSite(exec.siteForCookies(), target.host);var items: std.ArrayList(CookieListItem) = .empty;for (session.cookie_jar.cookies.items) |*cookie| {// CookieStore exposes only cookies that script would see for the// current document. HttpOnly cookies stay hidden.- if (cookie.appliesTo(&target, true, true, false) == false) {+ if (cookie.appliesTo(&target, same_site, false, false) == false) {continue;}if (normalized_name) |n| {@@ -488,6 +491,10 @@ fn storeCookie(exec: *const Execution, init_: CookieInit, is_delete: bool) !void}}+ if (init.sameSite != .none and Cookie.areSameSite(exec.siteForCookies(), URL.getHostname(url)) == false) {+ return error.SameSiteBlocked;+ }+const is_https = URL.isSecure(url);// Per spec, SameSite=None requires Secure. CookieStore additionally// marks any cookie written from an HTTPS document as Secure.@@ -612,3 +619,78 @@ const testing = @import("../../../testing.zig");test "WebApi: CookieStore" {try testing.htmlRunner("cookie_store.html", .{});}++test "CookieStore: cross-site frame" {+ defer testing.test_session.closeAllPages();+ const frame = try testing.createFrame();+ const exec = &frame.js.execution;+ const jar = &frame._session.cookie_jar;+ defer jar.clearRetainingCapacity();++ // victim.example embedded by attacker.example: the ancestor chain is+ // cross-site, so the frame has no site for cookies.+ var top: Frame = undefined;+ top.parent = null;+ top.url = "https://attacker.example/";+ frame.url = "https://victim.example/inner";+ frame.parent = ⊤+ defer frame.parent = null;++ try jar.populateFromResponse("https://victim.example/", "strict=1; SameSite=Strict");+ try jar.populateFromResponse("https://victim.example/", "lax=2; SameSite=Lax");+ try jar.populateFromResponse("https://victim.example/", "none=3; SameSite=None; Secure");++ // getAll(): only SameSite=None is visible from a cross-site context.+ {+ const items = try matchCookies(exec, null, null, false);+ try testing.expectEqual(1, items.len);+ try testing.expectEqual("none", items[0].name.str());+ }++ // set(): the default (Strict) and Lax are rejected, None is stored.+ // delete() is an expiring Strict set, so it is rejected too.+ try std.testing.expectError(error.SameSiteBlocked, storeCookie(exec, .{ .name = "set_strict", .value = "4" }, false));+ try std.testing.expectError(error.SameSiteBlocked, storeCookie(exec, .{ .name = "set_lax", .value = "5", .sameSite = .lax }, false));+ try storeCookie(exec, .{ .name = "set_none", .value = "6", .sameSite = .none }, false);+ try std.testing.expectError(error.SameSiteBlocked, storeCookie(exec, .{ .name = "none", .value = "", .expires = 0 }, true));+ {+ const items = try matchCookies(exec, null, null, false);+ try testing.expectEqual(2, items.len);+ try testing.expectEqual("none", items[0].name.str());+ try testing.expectEqual("set_none", items[1].name.str());+ }++ // The same jar seen from a same-site chain: everything applies, and the+ // rejected writes really were rejected rather than hidden.+ top.url = "https://victim.example/";+ {+ const items = try matchCookies(exec, null, null, false);+ try testing.expectEqual(4, items.len);+ try testing.expectEqual("strict", items[0].name.str());+ try testing.expectEqual("lax", items[1].name.str());+ try testing.expectEqual("none", items[2].name.str());+ try testing.expectEqual("set_none", items[3].name.str());+ }+ try storeCookie(exec, .{ .name = "set_strict", .value = "4" }, false);+ try storeCookie(exec, .{ .name = "none", .value = "", .expires = 0 }, true);+ {+ // The jar swap-removes on delete, so only check membership here.+ const items = try matchCookies(exec, null, null, false);+ try testing.expectEqual(4, items.len);+ var has_set_strict = false;+ for (items) |item| {+ try testing.expectEqual(false, std.mem.eql(u8, "none", item.name.str()));+ if (std.mem.eql(u8, "set_strict", item.name.str())) has_set_strict = true;+ }+ try testing.expectEqual(true, has_set_strict);+ }++ // Back in the cross-site context, the Strict cookie written same-site is+ // hidden again.+ top.url = "https://attacker.example/";+ {+ const items = try matchCookies(exec, null, null, false);+ try testing.expectEqual(1, items.len);+ try testing.expectEqual("set_none", items[0].name.str());+ }+}