Skip to content

Lightpanda: the headless browser designed for AI and automation

use credentials_mode instead of cookie bool

Muki Kiboigo0aeba826b49f
diff --git a/src/browser/webapi/net/EventSource.zig b/src/browser/webapi/net/EventSource.zig
index 61737d4a4..8a7424a8a 100644
--- a/src/browser/webapi/net/EventSource.zig
+++ b/src/browser/webapi/net/EventSource.zig
@@ -172,13 +172,11 @@ fn connect(self: *EventSource) !void {
try self._id_buf.appendSlice(self._arena.allocator(), self._last_event_id.items);
const same_origin = exec.isSameOrigin(self._url);
- const cookie_support = self._with_credentials or same_origin;
const transfer = try exec.newRequest(.{
.ctx = self,
.url = self._url,
.method = .GET,
- .cookies = cookie_support,
.origin = exec.origin(),
.request_mode = .cors,
.credentials_mode = if (self._with_credentials) .include else .same_origin,
diff --git a/src/browser/webapi/net/Fetch.zig b/src/browser/webapi/net/Fetch.zig
index 03c08a6f1..9de5585da 100644
--- a/src/browser/webapi/net/Fetch.zig
+++ b/src/browser/webapi/net/Fetch.zig
@@ -101,11 +101,6 @@ pub fn init(input: Input, options: ?InitOpts, exec: *const Execution) !js.Promis
.method = request._method,
.body = request._body,
.resource_type = .fetch,
- .cookies = switch (request._credentials) {
- .omit => false,
- .include => true,
- .@"same-origin" => exec.isSameOrigin(request._url),
- },
.credentials_mode = switch (request._credentials) {
.omit => .omit,
.@"same-origin" => .same_origin,
diff --git a/src/browser/webapi/net/XMLHttpRequest.zig b/src/browser/webapi/net/XMLHttpRequest.zig
index 07e2e473b..bdd616d07 100644
--- a/src/browser/webapi/net/XMLHttpRequest.zig
+++ b/src/browser/webapi/net/XMLHttpRequest.zig
@@ -303,9 +303,6 @@ pub fn send(self: *XMLHttpRequest, body_: ?BodyInit, exec_: *const Execution) !v
const exec = self._exec;
- // Only add cookies for same-origin or when withCredentials is true
- const cookie_support = self._with_credentials or exec.isSameOrigin(self._url);
-
self.acquireRef();
self._active_requests += 1;
self._send_flag = true;
@@ -315,7 +312,6 @@ pub fn send(self: *XMLHttpRequest, body_: ?BodyInit, exec_: *const Execution) !v
.url = self._url,
.method = self._method,
.body = self._request_body,
- .cookies = cookie_support,
.credentials_mode = if (self._with_credentials) .include else .same_origin,
.request_mode = .cors,
.origin = exec.origin(),
@@ -340,7 +336,8 @@ pub fn send(self: *XMLHttpRequest, body_: ?BodyInit, exec_: *const Execution) !v
self._send_flag = false;
}
try self._request_headers.populateRequestHeaders(transfer);
- if (cookie_support) {
+
+ if (transfer.req.credentialsAllowed()) {
try exec.headersForRequest(transfer);
}
}
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index 6b02e7b0a..079d6749b 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -633,7 +633,7 @@ pub fn newRequest(self: *Client, req: Request, owner: ?*Owner) anyerror!*Transfe
if (owned.loader_id == 0) owned.loader_id = o.loader_id;
if (owned.document_frame_id == null) owned.document_frame_id = o.document_frame_id;
if (owned.notification == null) owned.notification = o.notification;
- if (req.cookies) cookie_jar = o.cookie_jar;
+ cookie_jar = o.cookie_jar;
}
// Resolved onto the transfer; the request's copy is left null so
// nothing reads the caller's (possibly short-lived) url through it.
@@ -1872,10 +1872,6 @@ pub const Request = struct {
document_frame_id: ?u32 = null,
notification: ?*Notification = null,
- // Send the owner's cookies and honour Set-Cookie. Off for a credential-less
- // fetch / XHR / EventSource. Meaningless without an owner: there is no jar.
- cookies: bool = true,
-
// The site for SameSite checks. null = the owner's (Owner.siteForCookies).
// Frame.navigate is the one caller with a reason to override it: the
// initiator of a top-level navigation isn't the frame being navigated.
@@ -1919,6 +1915,17 @@ pub const Request = struct {
// every caller decides — pass `HttpClient.noopShutdown` to opt out,
// knowingly.
shutdown_callback: ShutdownCallback,
+
+ pub fn credentialsAllowed(req: *const Request) bool {
+ return switch (req.credentials_mode) {
+ .omit => false,
+ .include => true,
+ .same_origin => blk: {
+ const origin = req.origin orelse break :blk false;
+ break :blk URL.isSameOrigin(req.url, origin);
+ },
+ };
+ }
};
pub const SyncResponse = struct {
@@ -2012,10 +2019,12 @@ fn fulfillRedirect(
errdefer |err| transfer.abortPipelineError(err);
// retrieve cookies from the fulfilled response's headers.
- if (transfer.cookie_jar) |jar| {
- for (headers) |hdr| {
- if (std.ascii.eqlIgnoreCase(hdr.name, "set-cookie")) {
- try jar.populateFromResponse(transfer.req.url, hdr.value);
+ if (transfer.req.credentialsAllowed()) {
+ if (transfer.cookie_jar) |jar| {
+ for (headers) |hdr| {
+ if (std.ascii.eqlIgnoreCase(hdr.name, "set-cookie")) {
+ try jar.populateFromResponse(transfer.req.url, hdr.value);
+ }
}
}
}
@@ -2583,8 +2592,10 @@ pub const Transfer = struct {
}
pub fn getCookieString(self: *Transfer, arena: Allocator) !?[:0]const u8 {
- const jar = self.cookie_jar orelse return null;
const req = &self.req;
+ if (!req.credentialsAllowed()) return null;
+
+ const jar = self.cookie_jar orelse return null;
var aw: std.Io.Writer.Allocating = .init(arena);
try jar.forRequest(req.url, &aw.writer, .{
.is_http = true,
@@ -3055,13 +3066,15 @@ pub const Transfer = struct {
const headers = try it.collect(arena.allocator());
self.res.headers = headers.items;
- if (self.cookie_jar) |jar| {
- for (self.res.headers) |hdr| {
- if (std.ascii.eqlIgnoreCase(hdr.name, "set-cookie")) {
- jar.populateFromResponse(self.req.url, hdr.value) catch |err| {
- log.err(.http, "set cookie", .{ .err = err, .req = self });
- return err;
- };
+ if (self.req.credentialsAllowed()) {
+ if (self.cookie_jar) |jar| {
+ for (self.res.headers) |hdr| {
+ if (std.ascii.eqlIgnoreCase(hdr.name, "set-cookie")) {
+ jar.populateFromResponse(self.req.url, hdr.value) catch |err| {
+ log.err(.http, "set cookie", .{ .err = err, .req = self });
+ return err;
+ };
+ }
}
}
}