Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: add --remote-allow-origins flag

Karl Seguin7ee8843519fa
diff --git a/src/Config.zig b/src/Config.zig
index 8c0f2b9ee..eab3dd4a4 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -415,6 +415,7 @@ pub const Commands = cli.Builder(.{
.{ .name = "cdp_max_message_size", .type = u32, .default = 1024 * 1024 },
.{ .name = "cdp_max_http_message_size", .type = u32, .default = 1024 * 1024 },
.{ .name = "http_session_timeout", .type = u32, .default = 60 },
+ .{ .name = "remote_allow_origins", .type = []const u8, .multiple = true },
.{ .name = "disable_metrics", .type = bool },
},
.shared_options = CommonOptions,
@@ -869,6 +870,15 @@ pub fn explicitAdvertiseHost(self: *const Config) ?[]const u8 {
};
}
+// The origins (or `*`) a WebSocket upgrade may carry, as Chrome's flag of
+// the same name. Empty, any Origin is rejected.
+pub fn remoteAllowOrigins(self: *const Config) []const []const u8 {
+ return switch (self.mode) {
+ .serve => |opts| opts.remote_allow_origins.items,
+ else => &.{},
+ };
+}
+
// Wildcard bind addresses (0.0.0.0, ::) are not routable for clients
// resolving /json/version. Fall back to a loopback address so the
// advertised webSocketDebuggerUrl is at least connectable from the same
diff --git a/src/help.zon b/src/help.zon
index d3b48fbd2..fa7cf23d1 100644
--- a/src/help.zon
+++ b/src/help.zon
@@ -68,6 +68,12 @@
\\ cdp Chrome DevTools Protocol (WebSocket on /).
\\ webdriver WebDriver BiDi (WebSocket on /session) plus the
\\ classic WebDriver session endpoints.
+ \\ --remote-allow-origins <ORIGIN>
+ \\ Origin (e.g. http://localhost:3000) allowed in the Origin header
+ \\ of CDP and BiDi WebSocket upgrades, or * to allow any. Can be
+ \\ passed multiple times. Upgrades without an Origin header are
+ \\ always accepted.
+ \\ Defaults to none, rejecting any upgrade that sends an Origin.
,
.fetch =
\\usage: {0s} fetch <url>... [OPTIONS] [COMMON_OPTIONS]
diff --git a/src/server/Server.zig b/src/server/Server.zig
index 833d803f3..0107d9d16 100644
--- a/src/server/Server.zig
+++ b/src/server/Server.zig
@@ -70,6 +70,7 @@ listener_paused: bool,
max_connections: usize,
advertise_host: ?[]const u8,
+remote_allow_origins: []const []const u8,
// the protocols (cdp/bidi) we support
protocols: Protocols,
@@ -224,6 +225,7 @@ pub fn init(app: *App, address: sys_net.IpAddress) !*Server {
.shutdown_begun = false,
.worker_wg = .{},
.advertise_host = config.explicitAdvertiseHost(),
+ .remote_allow_origins = config.remoteAllowOrigins(),
};
return self;
}
diff --git a/src/server/http.zig b/src/server/http.zig
index b54851b68..fd5302559 100644
--- a/src/server/http.zig
+++ b/src/server/http.zig
@@ -1069,7 +1069,10 @@ const Upgrade = union(enum) {
// hand the fd to its worker (spawning one for a new connection).
fn upgrade(server: *Server, conn: *Connection, req: *Connection.Request, target: Upgrade) !Served {
var accept_buf: [28]u8 = undefined;
- const accept_key = webSocketAccept(req.head, &accept_buf, server.advertise_host) catch |err| {
+ const accept_key = webSocketAccept(req.head, &accept_buf, .{
+ .host = server.advertise_host,
+ .origins = server.remote_allow_origins,
+ }) catch |err| {
const response: []const u8 = switch (err) {
error.ForbiddenOrigin => forbidden_origin_response,
error.ForbiddenHost => forbidden_host_response,
@@ -1101,9 +1104,14 @@ fn upgrade(server: *Server, conn: *Connection, req: *Connection.Request, target:
// Validate an incoming WebSocket upgrade request head and, on success, write
// the Sec-WebSocket-Accept value into `out`. Mirrors the origin/host defenses
-// from the old Handshake path. `advertise_host` is the explicit
-// --advertise-host, if any.
-fn webSocketAccept(head: []const u8, out: *[28]u8, advertise_host: ?[]const u8) ![]const u8 {
+// from the old Handshake path. `allow` holds the explicit --advertise-host and
+// --remote-allow-origins, if any.
+const Allow = struct {
+ host: ?[]const u8 = null,
+ origins: []const []const u8 = &.{},
+};
+
+fn webSocketAccept(head: []const u8, out: *[28]u8, allow: Allow) ![]const u8 {
const FOUND_UPGRADE: u8 = 1 << 0;
const FOUND_VERSION: u8 = 1 << 1;
const FOUND_CONNECTION: u8 = 1 << 2;
@@ -1131,12 +1139,15 @@ fn webSocketAccept(head: []const u8, out: *[28]u8, advertise_host: ?[]const u8)
key = h.value;
found |= FOUND_KEY;
} else if (std.ascii.eqlIgnoreCase(h.key, "origin")) {
- // Only a browser sends Origin, and a browser has no business
- // driving CDP/BiDi: it's cross-origin to us by definition.
- log.warn(.serve, "rejected websocket origin", .{ .origin = h.value[0..@min(h.value.len, 64)] });
- return error.ForbiddenOrigin;
+ // A browser has no business driving CDP/BiDi: it's cross-origin
+ // to us by definition. But non-browser clients (e.g. Python's
+ // websocket-client) send one too, so the operator can allow it.
+ if (isAllowedOrigin(h.value, allow.origins) == false) {
+ log.warn(.serve, "rejected websocket origin", .{ .origin = h.value[0..@min(h.value.len, 64)], .note = "See `--remote-allow-origins <origins>` if this is a legitimate origin" });
+ return error.ForbiddenOrigin;
+ }
} else if (std.ascii.eqlIgnoreCase(h.key, "host")) {
- if (isAllowedHost(h.value, advertise_host) == false) {
+ if (isAllowedHost(h.value, allow.host) == false) {
log.warn(.serve, "rejected websocket host", .{ .host = h.value[0..@min(h.value.len, 64)], .note = "See `--advertise-host <host>` if this is a legitimate host" });
return error.ForbiddenHost;
}
@@ -1155,6 +1166,17 @@ fn webSocketAccept(head: []const u8, out: *[28]u8, advertise_host: ?[]const u8)
return out;
}
+// Exact (case-insensitive) match against a --remote-allow-origins value, or
+// anything if one is `*`, as Chrome does.
+fn isAllowedOrigin(value: []const u8, allowed: []const []const u8) bool {
+ for (allowed) |origin| {
+ if (std.mem.eql(u8, origin, "*") or std.ascii.eqlIgnoreCase(origin, value)) {
+ return true;
+ }
+ }
+ return false;
+}
+
// Defense in depth against DNS rebinding: only an IP literal can legitimately
// reach us (no name resolution involved). Two names are accepted:
// `localhost:<port>`, which browsers hardwire to loopback without a lookup,
@@ -1205,8 +1227,35 @@ test "http: webSocketAccept honours the advertised host" {
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" ++
"Sec-WebSocket-Version: 13\r\n\r\n";
var out: [28]u8 = undefined;
- try testing.expectError(error.ForbiddenHost, webSocketAccept(head, &out, null));
- try testing.expectString("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", try webSocketAccept(head, &out, "lightpanda"));
+ try testing.expectError(error.ForbiddenHost, webSocketAccept(head, &out, .{}));
+ try testing.expectString("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", try webSocketAccept(head, &out, .{ .host = "lightpanda" }));
+}
+
+test "http: isAllowedOrigin" {
+ try testing.expectEqual(false, isAllowedOrigin("http://localhost:3000", &.{}));
+ try testing.expectEqual(true, isAllowedOrigin("http://localhost:3000", &.{"http://localhost:3000"}));
+ try testing.expectEqual(true, isAllowedOrigin("http://LOCALHOST:3000", &.{"http://localhost:3000"}));
+ try testing.expectEqual(true, isAllowedOrigin("http://b.test", &.{ "http://a.test", "http://b.test" }));
+ try testing.expectEqual(true, isAllowedOrigin("null", &.{"*"}));
+ try testing.expectEqual(true, isAllowedOrigin("https://evil.com", &.{ "http://a.test", "*" }));
+ try testing.expectEqual(false, isAllowedOrigin("http://localhost:3001", &.{"http://localhost:3000"}));
+ try testing.expectEqual(false, isAllowedOrigin("http://a.test.evil.com", &.{"http://a.test"}));
+}
+
+test "http: webSocketAccept honours the allowed origins" {
+ testing.expectLog(&.{ .serve, .serve });
+ const head = "GET / HTTP/1.1\r\n" ++
+ "Host: 127.0.0.1:9222\r\n" ++
+ "Origin: http://127.0.0.1:9222\r\n" ++
+ "Upgrade: websocket\r\n" ++
+ "Connection: Upgrade\r\n" ++
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" ++
+ "Sec-WebSocket-Version: 13\r\n\r\n";
+ var out: [28]u8 = undefined;
+ try testing.expectError(error.ForbiddenOrigin, webSocketAccept(head, &out, .{}));
+ try testing.expectError(error.ForbiddenOrigin, webSocketAccept(head, &out, .{ .origins = &.{"http://localhost:9222"} }));
+ try testing.expectString("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", try webSocketAccept(head, &out, .{ .origins = &.{"http://127.0.0.1:9222"} }));
+ try testing.expectString("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", try webSocketAccept(head, &out, .{ .origins = &.{"*"} }));
}
test "http: the read buffer grows with the request and gives the space back" {