Lightpanda: the headless browser designed for AI and automation
cookies: a non-secure origin can't set or overwrite a Secure cookie
Lucien Coffe15fa09ec221e
diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex 7a7dc0dca..ea68b66d1 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -316,7 +316,7 @@ pub fn setCookie(self: *Document, cookie_str: []const u8, frame: *Frame) ![]consc.deinit();return ""; // SameSite cookies cannot be set from a cross-site context.}- try frame._session.cookie_jar.add(c, lp.datetime.timestamp(.real), false);+ try frame._session.cookie_jar.addFromUrl(frame.url, c, lp.datetime.timestamp(.real), false);return cookie_str;}@@ -1945,6 +1945,22 @@ test "Document: cookie access from a cross-site frame" {try testing.expectEqual("none=4; set_none=8", try doc.getCookie(frame));}+test "Document: a non-secure document can't set or overwrite a Secure cookie" {+ defer testing.test_session.closeAllPages();+ const frame = try testing.createFrame();+ const doc = frame.document;+ const jar = &frame._session.cookie_jar;+ defer jar.clearRetainingCapacity();++ frame.url = "http://victim.example/";+ _ = try doc.setCookie("s=1; Secure", frame);+ try testing.expectEqual(0, jar.cookies.items.len);++ try jar.populateFromResponse("https://victim.example/", "s=1; Secure");+ _ = try doc.setCookie("s=2", frame);+ try testing.expectEqual("1", jar.cookies.items[0].value);+}+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 3ccfd3a61..abda627b7 100644--- a/src/browser/webapi/storage/Cookie.zig+++ b/src/browser/webapi/storage/Cookie.zig@@ -24,6 +24,7 @@ const DateTime = @import("../../../datetime.zig").DateTime;const Notification = @import("../../../Notification.zig");const public_suffix_list = @import("../../../data/public_suffix_list.zig").lookup;const repeat = @import("../../../string.zig").repeat;+const isSubdomainOf = @import("../../../network/adblock/domain.zig").isSubdomainOf;const log = lp.log;const Allocator = std.mem.Allocator;@@ -606,27 +607,19 @@ pub fn appliesTo(self: *const Cookie, url: *const PreparedUri, opts: MatchOpts)if (!self.matchesHost(url.host)) return false;- {- if (self.path[self.path.len - 1] == '/') {- // If our cookie has a trailing slash, we can only match is- // the target path is a prefix. I.e., if our path is- // /doc/ we can only match /doc/*- if (std.mem.startsWith(u8, url.path, self.path) == false) {- return false;- }- } else {- // Our cookie path is something like /hello- if (std.mem.startsWith(u8, url.path, self.path) == false) {- // The target path has to either be /hello (it isn't)- return false;- } else if (url.path.len < self.path.len or (url.path.len > self.path.len and url.path[self.path.len] != '/')) {- // Or it has to be something like /hello/* (it isn't)- // it isn't!- return false;- }- }+ return pathMatches(url.path, self.path);+}++// RFC 6265bis 5.1.4 path-match.+fn pathMatches(request_path: []const u8, cookie_path: []const u8) bool {+ if (std.mem.startsWith(u8, request_path, cookie_path) == false) {+ return false;}- return true;+ // The paths are equal, or the cookie path is a directory of the+ // request path: /doc/ matches /doc/*, /hello matches /hello/*.+ return request_path.len == cookie_path.len or+ cookie_path[cookie_path.len - 1] == '/' or+ request_path[cookie_path.len] == '/';}// RFC 6265bis "site for cookies" of a request's initiator.@@ -736,6 +729,41 @@ pub const Jar = struct {}}+ /// Stores a cookie set by a response from `url`, or by a script of a+ /// document at `url`. RFC 6265bis 5.7: a non-secure origin can't set a+ /// Secure cookie, nor shadow or overwrite one.+ pub fn addFromUrl(+ self: *Jar,+ url: [:0]const u8,+ cookie: Cookie,+ request_time: u64,+ comptime is_http: bool,+ ) !void {+ if (!URL.isPotentiallyTrustworthy(url) and (cookie.secure or self.shadowsSecureCookie(&cookie))) {+ cookie.deinit();+ return;+ }+ return self.add(cookie, request_time, is_http);+ }++ // A Secure cookie with the same name, a domain that domain-matches+ // cookie's or the other way around (a leading `.` isn't compared), and+ // a path that cookie's path path-matches.+ fn shadowsSecureCookie(self: *const Jar, cookie: *const Cookie) bool {+ const domain = std.mem.trimStart(u8, cookie.domain, ".");+ for (self.cookies.items) |*c| {+ const c_domain = std.mem.trimStart(u8, c.domain, ".");+ if (c.secure and+ std.mem.eql(u8, c.name, cookie.name) and+ (isSubdomainOf(domain, c_domain) or isSubdomainOf(c_domain, domain)) and+ pathMatches(cookie.path, c.path))+ {+ return true;+ }+ }+ return false;+ }+fn dispatchChange(self: *Jar,kind: Notification.CookieChanged.Kind,@@ -822,7 +850,7 @@ pub const Jar = struct {};const now = lp.datetime.timestamp(.real);- try self.add(c, now, true);+ try self.addFromUrl(url, c, now, true);}fn writeCookie(cookie: *const Cookie, writer: anytype) !void {@@ -1101,6 +1129,36 @@ test "Jar: add limit" {}, now, true));}+test "Jar: a non-secure origin can't set or overwrite a Secure cookie" {+ var jar = Jar.init(testing.allocator, null);+ defer jar.deinit();++ try jar.populateFromResponse("http://lightpanda.io/", "s=1; Secure");+ try testing.expectEqual(0, jar.cookies.items.len);+ try jar.populateFromResponse("https://lightpanda.io/", "s=1; Secure");++ // No overwrite, no shadowing on a matching domain or a longer path, no+ // deletion.+ try jar.populateFromResponse("http://lightpanda.io/", "s=2");+ try jar.populateFromResponse("http://lightpanda.io/", "s=3; Path=/sub");+ try jar.populateFromResponse("http://lightpanda.io/", "s=4; Domain=lightpanda.io");+ try jar.populateFromResponse("http://www.lightpanda.io/", "s=5");+ try jar.populateFromResponse("http://lightpanda.io/", "s=; Max-Age=0");+ try testing.expectEqual(1, jar.cookies.items.len);+ try testing.expectEqual("1", jar.cookies.items[0].value);++ // Another name, domain, or a path the Secure cookie's path doesn't cover.+ try jar.populateFromResponse("http://lightpanda.io/", "t=1");+ try jar.populateFromResponse("http://lightpanda.dev/", "s=6");+ try jar.populateFromResponse("https://lightpanda.io/", "u=1; Secure; Path=/sub");+ try jar.populateFromResponse("http://lightpanda.io/", "u=2");+ try testing.expectEqual(5, jar.cookies.items.len);++ // Like Chrome, loopback origins are secure ones.+ try jar.populateFromResponse("http://localhost:3000/", "s=7; Secure");+ try testing.expectEqual(6, jar.cookies.items.len);+}+test "Jar: forRequest" {const expectCookies = struct {fn expect(expected: []const u8, jar: *Jar, target_url: [:0]const u8, opts: Jar.LookupOpts) !void {diff --git a/src/browser/webapi/storage/CookieStore.zig b/src/browser/webapi/storage/CookieStore.zigindex 158e7c3c1..748846ba2 100644--- a/src/browser/webapi/storage/CookieStore.zig+++ b/src/browser/webapi/storage/CookieStore.zig@@ -520,7 +520,7 @@ fn storeCookie(exec: *const Execution, init_: CookieInit, is_delete: bool) !void};// CookieStore is a script API, so is_http = false.- try session.cookie_jar.add(cookie, lp.datetime.timestamp(.real), false);+ try session.cookie_jar.addFromUrl(url, cookie, lp.datetime.timestamp(.real), false);}// Control characters (U+0000–U+001F and U+007F DEL) and `;` cannot appear indiff --git a/src/network/adblock/domain.zig b/src/network/adblock/domain.zigindex 5bec78b17..46b05de87 100644--- a/src/network/adblock/domain.zig+++ b/src/network/adblock/domain.zig@@ -74,7 +74,7 @@ fn matchEntry(e: Entry, hostname: []const u8, ent: []const u8) bool {}/// Whether `hostname` is `suffix` or one of its subdomains.-fn isSubdomainOf(hostname: []const u8, suffix: []const u8) bool {+pub fn isSubdomainOf(hostname: []const u8, suffix: []const u8) bool {if (hostname.len == suffix.len) return std.mem.eql(u8, hostname, suffix);if (hostname.len < suffix.len + 1) return false;const at = hostname.len - suffix.len;