Skip to content

Lightpanda: the headless browser designed for AI and automation

add obey_cors option

Muki Kiboigoec45dccacfa3
diff --git a/src/Config.zig b/src/Config.zig
index 262ab020e..c9f215b7a 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -247,6 +247,7 @@ pub const LoadResources = packed struct(u4) {
/// Common CLI args.
const CommonOptions = .{
.{ .name = "obey_robots", .type = bool },
+ .{ .name = "obey_cors", .type = bool },
.{ .name = "proxy_bearer_token", .type = ?[:0]const u8 },
.{ .name = "http_proxy", .type = ?[:0]const u8 },
.{ .name = "http_max_concurrent", .type = ?u8 },
@@ -555,6 +556,13 @@ pub fn httpVersion(self: *const Config) HttpVersion {
};
}
+pub fn obeyCors(self: *const Config) bool {
+ return switch (self.mode) {
+ inline .serve, .fetch, .mcp, .agent => |opts| opts.obey_cors,
+ else => unreachable,
+ };
+}
+
pub fn watchdogMs(self: *const Config) ?u32 {
return switch (self.mode) {
inline .serve, .fetch, .mcp, .agent => |opts| {
diff --git a/src/help.zon b/src/help.zon
index d3aec0b9a..3451600e4 100644
--- a/src/help.zon
+++ b/src/help.zon
@@ -453,6 +453,9 @@
\\ --obey-robots
\\ Fetches and obeys robots.txt of the target page.
\\ Defaults to false.
+ \\ --obey-cors
+ \\ Obeys CORS rules, executing preflights as needed.
+ \\ Defaults to false.
\\ --proxy-bearer-token <TOKEN>
\\ Token sent for bearer authentication with the proxy:
\\ Proxy-Authorization: Bearer <token>.
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index f871fc703..9a3c8c400 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -182,6 +182,7 @@ cache: *Cache,
// Cached config decisions, resolved once at init.
serve_mode: bool,
obey_robots: bool,
+obey_cors: bool,
// Applied to every transfer at configureConn, so a CDP change takes effect
// on the next request, not on in-flight ones.
@@ -231,6 +232,7 @@ pub fn init(self: *Client, app: *lp.App) !void {
.serve_mode = config.mode == .serve,
.obey_robots = config.obeyRobots(),
.http_version = config.httpVersion(),
+ .obey_cors = config.obeyCors(),
.robots = .{
.network = network,
.single_flight = .init(allocator),
@@ -1026,7 +1028,7 @@ fn pipeline(self: *Client, transfer: *Transfer, from: SubmitFrom) !void {
if (try self.cacheLookup(transfer)) {
return;
}
- if (!transfer.req.internal) {
+ if (self.obey_cors and !transfer.req.internal) {
switch (try self.cors.check(transfer)) {
.allowed => {},
.blocked => return transfer.failAsync(error.CorsBlocked),