Skip to content

Lightpanda: the headless browser designed for AI and automation

cookie: IP addresses are, as a whole, the 'second level domain'

Karl Seguinc5f2f1545430
diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zig
index 3f57456e6..bd6f443c3 100644
--- a/src/browser/webapi/storage/Cookie.zig
+++ b/src/browser/webapi/storage/Cookie.zig
@@ -944,6 +944,8 @@ pub fn areHostsSameSite(target_host: []const u8, origin_host: []const u8) bool {
}
fn findSecondLevelDomain(host: []const u8) []const u8 {
+ // An IP address is its own site, not one per last two octets.
+ if (isIpLiteral(host)) return host;
var i = std.mem.findScalarLast(u8, host, '.') orelse return host;
while (true) {
i = std.mem.findScalarLast(u8, host[0..i], '.') orelse return host;
@@ -1769,6 +1771,14 @@ test "Cookie: parse HttpOnly" {
try expectAttribute(.{ .http_only = true }, null, "b; HttpOnly=Off ");
}
+test "Cookie: an IP address is its own site" {
+ try testing.expectEqual(true, areHostsSameSite("127.0.0.1", "127.0.0.1"));
+ try testing.expectEqual(false, areHostsSameSite("127.0.0.1", "10.0.0.1"));
+ try testing.expectEqual(false, areHostsSameSite("192.168.0.1", "10.0.0.1"));
+ try testing.expectEqual(false, areHostsSameSite("10.0.0.1", "a.0.1"));
+ try testing.expectEqual(true, areHostsSameSite("a.lightpanda.io", "b.lightpanda.io"));
+}
+
test "Cookie: parse SameSite" {
try expectAttribute(.{ .same_site = .lax }, null, "b;samesite");
try expectAttribute(.{ .same_site = .lax }, null, "b;samesite=lax");