Skip to content

Lightpanda: the headless browser designed for AI and automation

move isSubdomainOf to URL, allow 'shadowing' an expired cookie

Karl Seguin44bc87cc9b00
diff --git a/src/browser/URL.zig b/src/browser/URL.zig
index 8ffe302fc..ae25a5c8a 100644
--- a/src/browser/URL.zig
+++ b/src/browser/URL.zig
@@ -271,6 +271,14 @@ pub fn isLoopbackHost(hostname: []const u8) bool {
};
}
+/// Whether `hostname` is `suffix` or one of its subdomains.
+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;
+ return hostname[at - 1] == '.' and std.mem.eql(u8, hostname[at..], suffix);
+}
+
pub fn getHostname(raw: []const u8) []const u8 {
const host = getHost(raw);
const port_sep = findPortSeparator(host) orelse return host;
diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zig
index abda627b7..0db05ae4d 100644
--- a/src/browser/webapi/storage/Cookie.zig
+++ b/src/browser/webapi/storage/Cookie.zig
@@ -24,7 +24,6 @@ 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;
@@ -739,7 +738,7 @@ pub const Jar = struct {
request_time: u64,
comptime is_http: bool,
) !void {
- if (!URL.isPotentiallyTrustworthy(url) and (cookie.secure or self.shadowsSecureCookie(&cookie))) {
+ if (!URL.isPotentiallyTrustworthy(url) and (cookie.secure or self.shadowsSecureCookie(&cookie, request_time))) {
cookie.deinit();
return;
}
@@ -748,14 +747,18 @@ pub const Jar = struct {
// 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 {
+ // a path that cookie's path path-matches. An expired cookie is already
+ // gone as far as the RFC is concerned, even if the jar still holds it.
+ fn shadowsSecureCookie(self: *const Jar, cookie: *const Cookie, now: u64) bool {
const domain = std.mem.trimStart(u8, cookie.domain, ".");
for (self.cookies.items) |*c| {
+ if (isCookieExpired(c, now)) {
+ continue;
+ }
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
+ (URL.isSubdomainOf(domain, c_domain) or URL.isSubdomainOf(c_domain, domain)) and
pathMatches(cookie.path, c.path))
{
return true;
@@ -1159,6 +1162,21 @@ test "Jar: a non-secure origin can't set or overwrite a Secure cookie" {
try testing.expectEqual(6, jar.cookies.items.len);
}
+test "Jar: an expired Secure cookie doesn't block a non-secure origin" {
+ var jar = Jar.init(testing.allocator, null);
+ defer jar.deinit();
+
+ const now = lp.datetime.timestamp(.real);
+ try jar.add(try Cookie.parse(testing.allocator, "https://lightpanda.io/", "s=1; Secure; Max-Age=10"), now, true);
+
+ // Still in the jar (nothing purged it), but expired by `later`.
+ const later = now + 20;
+ try jar.addFromUrl("http://lightpanda.io/", try Cookie.parse(testing.allocator, "http://lightpanda.io/", "s=2"), later, true);
+ try testing.expectEqual(1, jar.cookies.items.len);
+ try testing.expectEqual("2", jar.cookies.items[0].value);
+ try testing.expectEqual(false, jar.cookies.items[0].secure);
+}
+
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/network/adblock/domain.zig b/src/network/adblock/domain.zig
index 46b05de87..1ad941204 100644
--- a/src/network/adblock/domain.zig
+++ b/src/network/adblock/domain.zig
@@ -18,6 +18,7 @@
const std = @import("std");
+const URL = @import("../../browser/URL.zig");
const public_suffix_list = @import("../../data/public_suffix_list.zig");
const Allocator = std.mem.Allocator;
@@ -70,15 +71,7 @@ fn hasEntity(entries: []const Entry) bool {
fn matchEntry(e: Entry, hostname: []const u8, ent: []const u8) bool {
if (e.entity) return std.mem.eql(u8, e.name, ent);
- return isSubdomainOf(hostname, e.name);
-}
-
-/// Whether `hostname` is `suffix` or one of its subdomains.
-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;
- return hostname[at - 1] == '.' and std.mem.eql(u8, hostname[at..], suffix);
+ return URL.isSubdomainOf(hostname, e.name);
}
/// Parses a `$domain=` value into its inclusions and exclusions. Entries